File size: 3,046 Bytes
3170343 ddfc712 3170343 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 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() |