lyimo commited on
Commit
2e8b17e
1 Parent(s): 6a71c0f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import pandas as pd
4
+ from sklearn.linear_model import LogisticRegression
5
+ import os
6
+
7
+ # Use environment variables for API keys
8
+ API_KEY = os.environ.get("OPENWEATHER_API_KEY")
9
+ BASE_URL = 'https://api.openweathermap.org/data/2.5/'
10
+
11
+ def get_weather_data(location):
12
+ current_weather_url = f'{BASE_URL}weather?q={location}&appid={API_KEY}&units=metric'
13
+ current_response = requests.get(current_weather_url)
14
+ current_data = current_response.json()
15
+
16
+ current_weather = {
17
+ 'temperature': current_data['main']['temp'],
18
+ 'feels_like': current_data['main']['feels_like'],
19
+ 'description': current_data['weather'][0]['description'],
20
+ 'wind_speed': current_data['wind']['speed'],
21
+ 'pressure': current_data['main']['pressure'],
22
+ 'humidity': current_data['main']['humidity'],
23
+ 'visibility': current_data['visibility'] / 1000,
24
+ 'dew_point': current_data['main']['temp'] - ((100 - current_data['main']['humidity']) / 5.0)
25
+ }
26
+
27
+ return current_weather
28
+
29
+ def train_fog_model():
30
+ df = pd.read_csv('fog_weather_data.csv')
31
+ df = pd.get_dummies(df, columns=['Description'], drop_first=True)
32
+ X = df.drop('Fog', axis=1)
33
+ y = df['Fog']
34
+ model = LogisticRegression()
35
+ model.fit(X, y)
36
+ return model, X.columns
37
+
38
+ def predict_fog(model, feature_columns, weather_data):
39
+ new_data = pd.DataFrame({
40
+ 'Temperature': [weather_data['temperature']],
41
+ 'Feels like': [weather_data['feels_like']],
42
+ 'Wind speed': [weather_data['wind_speed']],
43
+ 'Pressure': [weather_data['pressure']],
44
+ 'Humidity': [weather_data['humidity']],
45
+ 'Dew point': [weather_data['dew_point']],
46
+ 'Visibility': [weather_data['visibility']]
47
+ })
48
+
49
+ for col in feature_columns:
50
+ if col.startswith('Description_'):
51
+ new_data[col] = 0
52
+
53
+ description_column = f"Description_{weather_data['description'].replace(' ', '_')}"
54
+ if description_column in feature_columns:
55
+ new_data[description_column] = 1
56
+
57
+ prediction = model.predict(new_data)
58
+ return "Foggy weather" if prediction[0] == 1 else "Clear weather"
59
+
60
+ # Load the model once when the app starts
61
+ fog_model, feature_columns = train_fog_model()
62
+
63
+ def predict_current_weather(location):
64
+ try:
65
+ current_weather = get_weather_data(location)
66
+ fog_prediction = predict_fog(fog_model, feature_columns, current_weather)
67
+
68
+ result = f"Current weather in {location}:\n"
69
+ result += f"Temperature: {current_weather['temperature']}°C\n"
70
+ result += f"Feels like: {current_weather['feels_like']}°C\n"
71
+ result += f"Description: {current_weather['description']}\n"
72
+ result += f"Wind speed: {current_weather['wind_speed']} m/s\n"
73
+ result += f"Pressure: {current_weather['pressure']} hPa\n"
74
+ result += f"Humidity: {current_weather['humidity']}%\n"
75
+ result += f"Dew point: {current_weather['dew_point']}°C\n"
76
+ result += f"Visibility: {current_weather['visibility']} km\n"
77
+ result += f"\nFog Prediction: {fog_prediction}"
78
+
79
+ return result
80
+ except Exception as e:
81
+ return f"Error: {str(e)}"
82
+
83
+ def predict_custom_weather(temperature, feels_like, wind_speed, pressure, humidity, visibility, description):
84
+ try:
85
+ weather_data = {
86
+ 'temperature': temperature,
87
+ 'feels_like': feels_like,
88
+ 'wind_speed': wind_speed,
89
+ 'pressure': pressure,
90
+ 'humidity': humidity,
91
+ 'visibility': visibility,
92
+ 'description': description,
93
+ 'dew_point': temperature - ((100 - humidity) / 5.0)
94
+ }
95
+
96
+ fog_prediction = predict_fog(fog_model, feature_columns, weather_data)
97
+
98
+ result = "Custom weather prediction:\n"
99
+ result += f"Fog Prediction: {fog_prediction}"
100
+
101
+ return result
102
+ except Exception as e:
103
+ return f"Error: {str(e)}"
104
+
105
+ # Gradio interface
106
+ with gr.Blocks() as demo:
107
+ gr.Markdown("# Weather and Fog Prediction")
108
+
109
+ with gr.Tab("Current Weather Prediction"):
110
+ location_input = gr.Textbox(label="Enter Location")
111
+ predict_button = gr.Button("Predict Weather")
112
+ output = gr.Textbox(label="Prediction Result")
113
+
114
+ predict_button.click(predict_current_weather, inputs=location_input, outputs=output)
115
+
116
+ with gr.Tab("Custom Weather Prediction"):
117
+ with gr.Row():
118
+ temperature = gr.Number(label="Temperature (°C)")
119
+ feels_like = gr.Number(label="Feels Like (°C)")
120
+ wind_speed = gr.Number(label="Wind Speed (m/s)")
121
+ pressure = gr.Number(label="Pressure (hPa)")
122
+ with gr.Row():
123
+ humidity = gr.Number(label="Humidity (%)")
124
+ visibility = gr.Number(label="Visibility (km)")
125
+ description = gr.Dropdown(label="Weather Description", choices=["clear sky", "few clouds", "scattered clouds", "broken clouds", "shower rain", "rain", "thunderstorm", "snow", "mist"])
126
+
127
+ custom_predict_button = gr.Button("Predict Fog")
128
+ custom_output = gr.Textbox(label="Prediction Result")
129
+
130
+ custom_predict_button.click(
131
+ predict_custom_weather,
132
+ inputs=[temperature, feels_like, wind_speed, pressure, humidity, visibility, description],
133
+ outputs=custom_output
134
+ )
135
+
136
+ demo.launch()