rahmanmaqbool286's picture
Update app.py
ddfc712 verified
Raw
History Blame Contribute Delete
3.05 kB
import gradio as gr
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import google.generativeai as genai
# -----------------------------
# Physics Models
# -----------------------------
def solar_power(capacity_kw):
irradiance = np.sin(np.linspace(0, np.pi, 24))
irradiance[irradiance < 0] = 0
return capacity_kw * irradiance
def wind_power(capacity_kw):
wind_speed = np.random.uniform(3, 10, 24)
power = capacity_kw * (wind_speed / 12) ** 3
return power
def load_profile(homes):
night_load = homes * 0.3
day_load = homes * 0.1
load = []
for h in range(24):
if 18 <= h or h <= 6:
load.append(night_load)
else:
load.append(day_load)
return np.array(load)
# -----------------------------
# AI + Simulation
# -----------------------------
def generate_microgrid(desc, api_key):
homes = 50
solar_kw = 60
wind_kw = 20
solar = solar_power(solar_kw)
wind = wind_power(wind_kw)
load = load_profile(homes)
total_generation = solar + wind
df = pd.DataFrame({
"Hour": range(24),
"Solar": solar,
"Wind": wind,
"Load": load,
"Generation": total_generation
})
# Chart
plt.figure()
plt.plot(df["Hour"], df["Solar"])
plt.plot(df["Hour"], df["Wind"])
plt.plot(df["Hour"], df["Load"])
plt.plot(df["Hour"], df["Generation"])
plt.xlabel("Hour")
plt.ylabel("Power (kW)")
plt.title("24 Hour Microgrid Simulation")
chart_path = "simulation.png"
plt.savefig(chart_path)
# Gemini AI Analysis
ai_text = "Demo Mode"
if api_key:
genai.configure(api_key=api_key)
model = genai.GenerativeModel("gemini-pro")
prompt = f"""
Analyze this rural energy system and give professional
engineering recommendations:
{desc}
Include optimization suggestions.
"""
response = model.generate_content(prompt)
ai_text = response.text
report = f"""
✅ AI Microgrid Design Completed
Solar Capacity: {solar_kw} kW
Wind Capacity: {wind_kw} kW
Battery Recommendation: 150 kWh
Total Daily Energy Generated:
{round(total_generation.sum(),2)} kWh
AI Engineering Insight:
{ai_text}
"""
return report, chart_path
# -----------------------------
# UI
# -----------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# 🌱 GreenSolar AI Microgrid Designer
### AI-Powered Solar + Wind Hybrid System for Rural Pakistan
""")
description = gr.Textbox(
label="Village Energy Description",
lines=6
)
api = gr.Textbox(
label="Google Gemini API Key",
type="password"
)
output = gr.Textbox(label="Engineering Report", lines=12)
chart = gr.Image(label="24 Hour Simulation Dashboard")
btn = gr.Button("Generate AI Microgrid")
btn.click(
generate_microgrid,
inputs=[description, api],
outputs=[output, chart]
)
demo.launch()