Spaces:
Running
Running
import streamlit as st | |
import pandas as pd | |
import pickle | |
import requests | |
import os | |
# Function to download files from Hugging Face | |
def download_file(url, output_path): | |
if not os.path.exists(output_path): | |
with open(output_path, "wb") as f: | |
response = requests.get(url) | |
if response.status_code == 200: | |
f.write(response.content) | |
else: | |
st.error(f"Failed to download {url}. HTTP status code: {response.status_code}") | |
st.stop() | |
# Updated URLs to the model and label encoder files | |
model_url = "https://huggingface.co/spaces/amornpan/weather-prediction-app/raw/main/random_forest_weather_model.pkl" | |
encoder_url = "https://huggingface.co/spaces/amornpan/weather-prediction-app/raw/main/label_encoders.pkl" | |
# Local paths for the downloaded files | |
model_path = "random_forest_weather_model.pkl" | |
encoder_path = "label_encoders.pkl" | |
# Download files if not already present | |
download_file(model_url, model_path) | |
download_file(encoder_url, encoder_path) | |
# Load the model and label encoders | |
try: | |
with open(model_path, "rb") as f: | |
model = pickle.load(f) | |
except Exception as e: | |
st.error(f"Error loading model: {e}") | |
st.stop() | |
try: | |
with open(encoder_path, "rb") as f: | |
label_encoders = pickle.load(f) | |
except Exception as e: | |
st.error(f"Error loading label encoders: {e}") | |
st.stop() | |
# Debug: Check the structure of label_encoders | |
if not isinstance(label_encoders, dict): | |
st.error("Label encoders file is not in the expected format.") | |
st.stop() | |
st.write("Loaded Label Encoders:", label_encoders.keys()) | |
# Define UI | |
st.title("Weather Prediction App") | |
st.write("กรอกข้อมูลเพื่อพยากรณ์ประเภทของสภาพอากาศ") | |
# User inputs | |
temperature = st.number_input("Temperature (°C)", min_value=-50, max_value=50, value=25) | |
humidity = st.number_input("Humidity (%)", min_value=0, max_value=100, value=50) | |
wind_speed = st.number_input("Wind Speed (km/h)", min_value=0.0, max_value=200.0, value=10.0) | |
precipitation = st.number_input("Precipitation (%)", min_value=0.0, max_value=100.0, value=10.0) | |
# Handle 'Cloud Cover' selection | |
cloud_cover_options = label_encoders['Cloud Cover'].classes_ if 'Cloud Cover' in label_encoders else ["Clear", "Partly Cloudy", "Overcast"] | |
cloud_cover = st.selectbox("Cloud Cover", cloud_cover_options) | |
# Other inputs | |
atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa)", min_value=900.0, max_value=1100.0, value=1010.0) | |
uv_index = st.number_input("UV Index", min_value=0, max_value=10, value=5) | |
season_options = label_encoders['Season'].classes_ if 'Season' in label_encoders else ["Winter", "Spring", "Summer", "Autumn"] | |
season = st.selectbox("Season", season_options) | |
visibility = st.number_input("Visibility (km)", min_value=0.0, max_value=50.0, value=10.0) | |
location_options = label_encoders['Location'].classes_ if 'Location' in label_encoders else ["Inland", "Coastal", "Mountain"] | |
location = st.selectbox("Location", location_options) | |
# Encode categorical inputs | |
try: | |
cloud_cover_encoded = label_encoders['Cloud Cover'].transform([cloud_cover])[0] | |
season_encoded = label_encoders['Season'].transform([season])[0] | |
location_encoded = label_encoders['Location'].transform([location])[0] | |
except Exception as e: | |
st.error(f"Error encoding categorical inputs: {e}") | |
st.stop() | |
# Prepare input data | |
input_data = pd.DataFrame({ | |
"Temperature": [temperature], | |
"Humidity": [humidity], | |
"Wind Speed": [wind_speed], | |
"Precipitation (%)": [precipitation], | |
"Cloud Cover": [cloud_cover_encoded], | |
"Atmospheric Pressure": [atmospheric_pressure], | |
"UV Index": [uv_index], | |
"Season": [season_encoded], | |
"Visibility (km)": [visibility], | |
"Location": [location_encoded] | |
}) | |
# Predict weather type | |
if st.button("Predict Weather Type"): | |
try: | |
prediction = model.predict(input_data) | |
weather_type = label_encoders['Weather Type'].inverse_transform(prediction)[0] if 'Weather Type' in label_encoders else "Unknown" | |
st.success(f"Predicted Weather Type: {weather_type}") | |
except Exception as e: | |
st.error(f"Error in prediction: {e}") | |