amornpan commited on
Commit
2d1ca3d
·
verified ·
1 Parent(s): 54e1991

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -15,10 +15,6 @@ def download_file(url, output_path):
15
  st.error(f"Failed to download {url}. HTTP status code: {response.status_code}")
16
  st.stop()
17
 
18
- # Updated URLs to the model and label encoder files
19
- model_url = "https://huggingface.co/spaces/amornpan/weather-prediction-app/raw/main/random_forest_weather_model.pkl"
20
- encoder_url = "https://huggingface.co/spaces/amornpan/weather-prediction-app/raw/main/label_encoders.pkl"
21
-
22
  # Local paths for the downloaded files
23
  model_path = "random_forest_weather_model.pkl"
24
  encoder_path = "label_encoders.pkl"
@@ -43,12 +39,12 @@ except Exception as e:
43
  st.stop()
44
 
45
  # Debug: Check the structure of label_encoders
46
- if isinstance(label_encoders, dict):
47
- st.write("Loaded Label Encoders:", label_encoders.keys())
48
- else:
49
  st.error("Label encoders file is not in the expected format.")
50
  st.stop()
51
 
 
 
52
  # Define UI
53
  st.title("Weather Prediction App")
54
  st.write("กรอกข้อมูลเพื่อพยากรณ์ประเภทของสภาพอากาศ")
@@ -60,24 +56,26 @@ wind_speed = st.number_input("Wind Speed (km/h)", min_value=0.0, max_value=200.0
60
  precipitation = st.number_input("Precipitation (%)", min_value=0.0, max_value=100.0, value=10.0)
61
 
62
  # Handle 'Cloud Cover' selection
63
- if 'Cloud Cover' in label_encoders:
64
- cloud_cover_options = label_encoders['Cloud Cover'].classes_ if hasattr(label_encoders['Cloud Cover'], 'classes_') else ["Clear", "Partly Cloudy", "Overcast"]
65
- else:
66
- cloud_cover_options = ["Clear", "Partly Cloudy", "Overcast"]
67
-
68
  cloud_cover = st.selectbox("Cloud Cover", cloud_cover_options)
69
 
70
  # Other inputs
71
  atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa)", min_value=900.0, max_value=1100.0, value=1010.0)
72
  uv_index = st.number_input("UV Index", min_value=0, max_value=10, value=5)
73
- season = st.selectbox("Season", label_encoders['Season'].classes_ if 'Season' in label_encoders else ["Winter", "Spring", "Summer", "Autumn"])
 
74
  visibility = st.number_input("Visibility (km)", min_value=0.0, max_value=50.0, value=10.0)
75
- location = st.selectbox("Location", label_encoders['Location'].classes_ if 'Location' in label_encoders else ["Inland", "Coastal", "Mountain"])
 
76
 
77
  # Encode categorical inputs
78
- cloud_cover_encoded = label_encoders['Cloud Cover'].transform([cloud_cover])[0] if 'Cloud Cover' in label_encoders else 0
79
- season_encoded = label_encoders['Season'].transform([season])[0] if 'Season' in label_encoders else 0
80
- location_encoded = label_encoders['Location'].transform([location])[0] if 'Location' in label_encoders else 0
 
 
 
 
81
 
82
  # Prepare input data
83
  input_data = pd.DataFrame({
 
15
  st.error(f"Failed to download {url}. HTTP status code: {response.status_code}")
16
  st.stop()
17
 
 
 
 
 
18
  # Local paths for the downloaded files
19
  model_path = "random_forest_weather_model.pkl"
20
  encoder_path = "label_encoders.pkl"
 
39
  st.stop()
40
 
41
  # Debug: Check the structure of label_encoders
42
+ if not isinstance(label_encoders, dict):
 
 
43
  st.error("Label encoders file is not in the expected format.")
44
  st.stop()
45
 
46
+ st.write("Loaded Label Encoders:", label_encoders.keys())
47
+
48
  # Define UI
49
  st.title("Weather Prediction App")
50
  st.write("กรอกข้อมูลเพื่อพยากรณ์ประเภทของสภาพอากาศ")
 
56
  precipitation = st.number_input("Precipitation (%)", min_value=0.0, max_value=100.0, value=10.0)
57
 
58
  # Handle 'Cloud Cover' selection
59
+ cloud_cover_options = label_encoders['Cloud Cover'].classes_ if 'Cloud Cover' in label_encoders else ["Clear", "Partly Cloudy", "Overcast"]
 
 
 
 
60
  cloud_cover = st.selectbox("Cloud Cover", cloud_cover_options)
61
 
62
  # Other inputs
63
  atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa)", min_value=900.0, max_value=1100.0, value=1010.0)
64
  uv_index = st.number_input("UV Index", min_value=0, max_value=10, value=5)
65
+ season_options = label_encoders['Season'].classes_ if 'Season' in label_encoders else ["Winter", "Spring", "Summer", "Autumn"]
66
+ season = st.selectbox("Season", season_options)
67
  visibility = st.number_input("Visibility (km)", min_value=0.0, max_value=50.0, value=10.0)
68
+ location_options = label_encoders['Location'].classes_ if 'Location' in label_encoders else ["Inland", "Coastal", "Mountain"]
69
+ location = st.selectbox("Location", location_options)
70
 
71
  # Encode categorical inputs
72
+ try:
73
+ cloud_cover_encoded = label_encoders['Cloud Cover'].transform([cloud_cover])[0]
74
+ season_encoded = label_encoders['Season'].transform([season])[0]
75
+ location_encoded = label_encoders['Location'].transform([location])[0]
76
+ except Exception as e:
77
+ st.error(f"Error encoding categorical inputs: {e}")
78
+ st.stop()
79
 
80
  # Prepare input data
81
  input_data = pd.DataFrame({