Spaces:
Running
Running
File size: 4,240 Bytes
d00a823 a18dceb 76e73a2 d00a823 76e73a2 d89b0ed 76e73a2 bf37873 76e73a2 d89b0ed a18dceb d89b0ed d00a823 55e3a25 2d1ca3d d89b0ed 55e3a25 2d1ca3d d00a823 55e3a25 2d1ca3d 55e3a25 d00a823 2d1ca3d d00a823 2d1ca3d d00a823 2d1ca3d d00a823 a18dceb 55e3a25 a18dceb |
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 |
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}")
|