Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,49 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 6 |
|
| 7 |
+
# 1️⃣ Load & prepare data (runs once at startup)
|
| 8 |
+
ds = load_dataset("notadib/NASA-Power-Daily-Weather", split="train")
|
| 9 |
+
df = pd.DataFrame(ds)[["RH2M", "PRECTOTCORR", "ALLSKY_SFC_SW_DWN", "T2M"]].dropna()
|
| 10 |
|
| 11 |
+
X = df[["RH2M", "PRECTOTCORR", "ALLSKY_SFC_SW_DWN"]]
|
| 12 |
+
y = df["T2M"]
|
| 13 |
+
|
| 14 |
+
# use a small subset so startup stays fast
|
| 15 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 16 |
+
X, y, test_size=0.2, random_state=42
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
model = RandomForestRegressor(n_estimators=50, random_state=42)
|
| 20 |
+
model.fit(X_train, y_train)
|
| 21 |
+
|
| 22 |
+
# 2️⃣ Define your prediction function
|
| 23 |
+
def predict_temperature(rh2m, prectotcorr, solar):
|
| 24 |
+
"""Given humidity, precipitation, and solar radiation, predict temperature."""
|
| 25 |
+
val = model.predict([[rh2m, prectotcorr, solar]])[0]
|
| 26 |
+
return round(float(val), 2)
|
| 27 |
+
|
| 28 |
+
# 3️⃣ Build the Gradio interface
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=predict_temperature,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Number(label="Relative Humidity (%)", value=50, precision=1),
|
| 33 |
+
gr.Number(label="Precipitation (mm)", value=1.0, precision=2),
|
| 34 |
+
gr.Number(label="Solar Radiation (W/m²)", value=200.0, precision=1),
|
| 35 |
+
],
|
| 36 |
+
outputs=gr.Number(label="Predicted Temp (°C)"),
|
| 37 |
+
title="🌍 ClimatePredict: Daily Temperature Forecast",
|
| 38 |
+
description=(
|
| 39 |
+
"This demo uses a Random Forest model trained on NASA POWER daily weather data. "
|
| 40 |
+
"Adjust the inputs and click **Submit** to see the forecasted temperature."
|
| 41 |
+
),
|
| 42 |
+
examples=[
|
| 43 |
+
[60, 0.5, 180],
|
| 44 |
+
[30, 2.0, 300],
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|