Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
import numpy as np
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import zipfile
|
| 7 |
-
from sklearn.model_selection import train_test_split
|
| 8 |
-
from sklearn.preprocessing import LabelEncoder
|
| 9 |
-
from sklearn.ensemble import RandomForestRegressor
|
| 10 |
-
|
| 11 |
-
# Ensure required packages are installed
|
| 12 |
-
try:
|
| 13 |
-
import gradio as gr
|
| 14 |
-
except ImportError:
|
| 15 |
-
subprocess.run(["pip", "install", "gradio"], check=True)
|
| 16 |
-
import gradio as gr
|
| 17 |
-
|
| 18 |
-
# Define file paths
|
| 19 |
-
zip_file_path = "AI-powered Weather Forecasting.zip"
|
| 20 |
-
extract_folder = "weather_forecasting_dataset"
|
| 21 |
-
|
| 22 |
-
# Extract the ZIP file
|
| 23 |
-
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
|
| 24 |
-
zip_ref.extractall(extract_folder)
|
| 25 |
-
|
| 26 |
-
# Locate CSV file
|
| 27 |
-
csv_file_path = os.path.join(extract_folder, "weatherHistory.csv")
|
| 28 |
-
|
| 29 |
-
# Load the dataset
|
| 30 |
-
df = pd.read_csv(csv_file_path)
|
| 31 |
-
|
| 32 |
-
# Convert 'Formatted Date' to datetime format
|
| 33 |
-
df['Formatted Date'] = pd.to_datetime(df['Formatted Date'], utc=True)
|
| 34 |
-
df.set_index('Formatted Date', inplace=True)
|
| 35 |
-
|
| 36 |
-
# Drop unnecessary columns
|
| 37 |
-
df.drop(columns=['Summary', 'Daily Summary'], inplace=True)
|
| 38 |
-
|
| 39 |
-
# Handle missing values
|
| 40 |
-
df.dropna(inplace=True)
|
| 41 |
-
|
| 42 |
-
# Encode categorical variable 'Precip Type'
|
| 43 |
-
le = LabelEncoder()
|
| 44 |
-
df['Precip Type'] = le.fit_transform(df['Precip Type'])
|
| 45 |
-
|
| 46 |
-
# Feature engineering: Extract time-based features
|
| 47 |
-
df['Year'] = df.index.year
|
| 48 |
-
df['Month'] = df.index.month
|
| 49 |
-
df['Day'] = df.index.day
|
| 50 |
-
df['Hour'] = df.index.hour
|
| 51 |
-
|
| 52 |
-
# Define target variable (temperature prediction)
|
| 53 |
-
X = df.drop(columns=['Temperature (C)'])
|
| 54 |
-
y = df['Temperature (C)']
|
| 55 |
-
|
| 56 |
-
# Store feature names for debugging
|
| 57 |
-
feature_names = X.columns.tolist()
|
| 58 |
-
|
| 59 |
-
# Split data into training and testing sets
|
| 60 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 61 |
-
|
| 62 |
-
# 🚀 **No Scaling - Using Raw Data Directly**
|
| 63 |
-
model = RandomForestRegressor(n_estimators=500, random_state=42)
|
| 64 |
-
model.fit(X_train, y_train)
|
| 65 |
-
|
| 66 |
-
# 🚀 Debug: Print feature importance to ensure the model is learning
|
| 67 |
-
feature_importance = model.feature_importances_
|
| 68 |
-
print("Feature Importance:", dict(zip(feature_names, feature_importance)))
|
| 69 |
-
|
| 70 |
-
# Define Prediction Function
|
| 71 |
-
def predict_temperature(precip_type, humidity, wind_speed, wind_bearing, visibility, pressure, loud_cover, year, month, day, hour):
|
| 72 |
-
try:
|
| 73 |
-
# Encode categorical variable safely
|
| 74 |
-
if precip_type not in ["rain", "snow"]:
|
| 75 |
-
return "Error: Invalid Precip Type (Choose 'rain' or 'snow')"
|
| 76 |
-
|
| 77 |
-
precip_type_encoded = le.transform([precip_type])[0]
|
| 78 |
-
|
| 79 |
-
# Create sample data with correct features
|
| 80 |
-
sample_data = np.array([[precip_type_encoded, humidity, wind_speed, wind_bearing, visibility, pressure, loud_cover, year, month, day, hour]])
|
| 81 |
-
|
| 82 |
-
# 🚀 Debug: Print input sample before prediction
|
| 83 |
-
print("🔹 Input Sample:", sample_data)
|
| 84 |
-
|
| 85 |
-
# Predict temperature
|
| 86 |
-
prediction = model.predict(sample_data)[0]
|
| 87 |
-
|
| 88 |
-
return f"Predicted Temperature: {prediction:.2f}°C"
|
| 89 |
-
except Exception as e:
|
| 90 |
-
return f"Error: {e}"
|
| 91 |
-
|
| 92 |
-
# Gradio UI
|
| 93 |
-
inputs = [
|
| 94 |
-
gr.Radio(["rain", "snow"], label="Precip Type"),
|
| 95 |
-
gr.Number(label="Humidity"),
|
| 96 |
-
gr.Number(label="Wind Speed (km/h)"),
|
| 97 |
-
gr.Number(label="Wind Bearing (degrees)"),
|
| 98 |
-
gr.Number(label="Visibility (km)"),
|
| 99 |
-
gr.Number(label="Pressure (millibars)"),
|
| 100 |
-
gr.Number(label="Loud Cover", value=0.0), # Default to 0 if always 0
|
| 101 |
-
gr.Number(label="Year"),
|
| 102 |
-
gr.Number(label="Month"),
|
| 103 |
-
gr.Number(label="Day"),
|
| 104 |
-
gr.Number(label="Hour"),
|
| 105 |
-
]
|
| 106 |
-
|
| 107 |
-
demo = gr.Interface(fn=predict_temperature, inputs=inputs, outputs="text", title="AI-Powered Weather Forecasting")
|
| 108 |
-
|
| 109 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|