Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import os
|
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
# Load the trained model for image-based fog classification
|
| 11 |
-
learn = load_learner('
|
| 12 |
labels = learn.dls.vocab
|
| 13 |
|
| 14 |
# Use environment variables for API keys
|
|
@@ -29,14 +29,14 @@ def get_weather_data(location):
|
|
| 29 |
current_data = current_response.json()
|
| 30 |
|
| 31 |
current_weather = {
|
| 32 |
-
'temperature': current_data['main']
|
| 33 |
-
'feels_like': current_data['main']
|
| 34 |
-
'description': current_data['weather'][0]
|
| 35 |
-
'wind_speed': current_data['wind']
|
| 36 |
-
'pressure': current_data['main']
|
| 37 |
-
'humidity': current_data['main']
|
| 38 |
-
'visibility': current_data
|
| 39 |
-
'dew_point': current_data['main']
|
| 40 |
}
|
| 41 |
return current_weather
|
| 42 |
|
|
@@ -53,20 +53,20 @@ def train_fog_model():
|
|
| 53 |
# Function to predict fog based on weather data
|
| 54 |
def predict_fog(model, feature_columns, weather_data):
|
| 55 |
new_data = pd.DataFrame({
|
| 56 |
-
'Temperature': [weather_data
|
| 57 |
-
'Feels like': [weather_data
|
| 58 |
-
'Wind speed': [weather_data
|
| 59 |
-
'Pressure': [weather_data
|
| 60 |
-
'Humidity': [weather_data
|
| 61 |
-
'Dew point': [weather_data
|
| 62 |
-
'Visibility': [weather_data
|
| 63 |
})
|
| 64 |
|
| 65 |
for col in feature_columns:
|
| 66 |
if col.startswith('Description_'):
|
| 67 |
new_data[col] = 0
|
| 68 |
|
| 69 |
-
description_column = f"Description_{weather_data
|
| 70 |
if description_column in feature_columns:
|
| 71 |
new_data[description_column] = 1
|
| 72 |
|
|
@@ -83,9 +83,9 @@ def get_5day_forecast(location):
|
|
| 83 |
for entry in forecast_data['list']:
|
| 84 |
forecast_list.append({
|
| 85 |
'date': entry['dt_txt'],
|
| 86 |
-
'temperature': entry['main']
|
| 87 |
-
'humidity': entry['main']
|
| 88 |
-
'description': entry['weather'][0]
|
| 89 |
})
|
| 90 |
return forecast_list
|
| 91 |
|
|
@@ -99,14 +99,14 @@ def predict_current_weather(location):
|
|
| 99 |
fog_prediction = predict_fog(fog_model, feature_columns, current_weather)
|
| 100 |
|
| 101 |
result = f"Current weather in {location}:\n"
|
| 102 |
-
result += f"Temperature: {current_weather
|
| 103 |
-
result += f"Feels like: {current_weather
|
| 104 |
-
result += f"Description: {current_weather
|
| 105 |
-
result += f"Wind speed: {current_weather
|
| 106 |
-
result += f"Pressure: {current_weather
|
| 107 |
-
result += f"Humidity: {current_weather
|
| 108 |
-
result += f"Dew point: {current_weather
|
| 109 |
-
result += f"Visibility: {current_weather
|
| 110 |
result += f"\nFog Prediction: {fog_prediction}"
|
| 111 |
|
| 112 |
return result
|
|
@@ -171,18 +171,18 @@ def integrated_prediction(image, location):
|
|
| 171 |
formatted_forecast = format_5day_forecast(forecast_list)
|
| 172 |
|
| 173 |
# Determine transmission power based on current and future fog predictions
|
| 174 |
-
transmission_power = determine_transmission_power(image_prediction, weather_prediction, current_weather
|
| 175 |
|
| 176 |
# Check fog predictions for the next 5 days
|
| 177 |
fog_days = []
|
| 178 |
for entry in forecast_list:
|
| 179 |
date = entry['date'].split()[0]
|
| 180 |
weather_data = {
|
| 181 |
-
'temperature': entry
|
| 182 |
-
'humidity': entry
|
| 183 |
-
'description': entry
|
| 184 |
'visibility': 10, # Assuming visibility is 10 km for simplicity
|
| 185 |
-
'dew_point': entry
|
| 186 |
}
|
| 187 |
fog_prediction = predict_fog(fog_model, feature_columns, weather_data)
|
| 188 |
if fog_prediction == "Foggy weather":
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
# Load the trained model for image-based fog classification
|
| 11 |
+
learn = load_learner('afog_classifier.pkl')
|
| 12 |
labels = learn.dls.vocab
|
| 13 |
|
| 14 |
# Use environment variables for API keys
|
|
|
|
| 29 |
current_data = current_response.json()
|
| 30 |
|
| 31 |
current_weather = {
|
| 32 |
+
'temperature': current_data['main'].get('temp', 0),
|
| 33 |
+
'feels_like': current_data['main'].get('feels_like', 0),
|
| 34 |
+
'description': current_data['weather'][0].get('description', ''),
|
| 35 |
+
'wind_speed': current_data['wind'].get('speed', 0),
|
| 36 |
+
'pressure': current_data['main'].get('pressure', 0),
|
| 37 |
+
'humidity': current_data['main'].get('humidity', 0),
|
| 38 |
+
'visibility': current_data.get('visibility', 10000) / 1000,
|
| 39 |
+
'dew_point': current_data['main'].get('temp', 0) - ((100 - current_data['main'].get('humidity', 0)) / 5.0)
|
| 40 |
}
|
| 41 |
return current_weather
|
| 42 |
|
|
|
|
| 53 |
# Function to predict fog based on weather data
|
| 54 |
def predict_fog(model, feature_columns, weather_data):
|
| 55 |
new_data = pd.DataFrame({
|
| 56 |
+
'Temperature': [weather_data.get('temperature', 0)],
|
| 57 |
+
'Feels like': [weather_data.get('feels_like', 0)],
|
| 58 |
+
'Wind speed': [weather_data.get('wind_speed', 0)],
|
| 59 |
+
'Pressure': [weather_data.get('pressure', 0)],
|
| 60 |
+
'Humidity': [weather_data.get('humidity', 0)],
|
| 61 |
+
'Dew point': [weather_data.get('dew_point', 0)],
|
| 62 |
+
'Visibility': [weather_data.get('visibility', 10)]
|
| 63 |
})
|
| 64 |
|
| 65 |
for col in feature_columns:
|
| 66 |
if col.startswith('Description_'):
|
| 67 |
new_data[col] = 0
|
| 68 |
|
| 69 |
+
description_column = f"Description_{weather_data.get('description', '').replace(' ', '_')}"
|
| 70 |
if description_column in feature_columns:
|
| 71 |
new_data[description_column] = 1
|
| 72 |
|
|
|
|
| 83 |
for entry in forecast_data['list']:
|
| 84 |
forecast_list.append({
|
| 85 |
'date': entry['dt_txt'],
|
| 86 |
+
'temperature': entry['main'].get('temp', 0),
|
| 87 |
+
'humidity': entry['main'].get('humidity', 0),
|
| 88 |
+
'description': entry['weather'][0].get('description', '')
|
| 89 |
})
|
| 90 |
return forecast_list
|
| 91 |
|
|
|
|
| 99 |
fog_prediction = predict_fog(fog_model, feature_columns, current_weather)
|
| 100 |
|
| 101 |
result = f"Current weather in {location}:\n"
|
| 102 |
+
result += f"Temperature: {current_weather.get('temperature', 0)}°C\n"
|
| 103 |
+
result += f"Feels like: {current_weather.get('feels_like', 0)}°C\n"
|
| 104 |
+
result += f"Description: {current_weather.get('description', '')}\n"
|
| 105 |
+
result += f"Wind speed: {current_weather.get('wind_speed', 0)} m/s\n"
|
| 106 |
+
result += f"Pressure: {current_weather.get('pressure', 0)} hPa\n"
|
| 107 |
+
result += f"Humidity: {current_weather.get('humidity', 0)}%\n"
|
| 108 |
+
result += f"Dew point: {current_weather.get('dew_point', 0)}°C\n"
|
| 109 |
+
result += f"Visibility: {current_weather.get('visibility', 10)} km\n"
|
| 110 |
result += f"\nFog Prediction: {fog_prediction}"
|
| 111 |
|
| 112 |
return result
|
|
|
|
| 171 |
formatted_forecast = format_5day_forecast(forecast_list)
|
| 172 |
|
| 173 |
# Determine transmission power based on current and future fog predictions
|
| 174 |
+
transmission_power = determine_transmission_power(image_prediction, weather_prediction, current_weather.get('humidity', 0))
|
| 175 |
|
| 176 |
# Check fog predictions for the next 5 days
|
| 177 |
fog_days = []
|
| 178 |
for entry in forecast_list:
|
| 179 |
date = entry['date'].split()[0]
|
| 180 |
weather_data = {
|
| 181 |
+
'temperature': entry.get('temperature', 0),
|
| 182 |
+
'humidity': entry.get('humidity', 0),
|
| 183 |
+
'description': entry.get('description', ''),
|
| 184 |
'visibility': 10, # Assuming visibility is 10 km for simplicity
|
| 185 |
+
'dew_point': entry.get('temperature', 0) - ((100 - entry.get('humidity', 0)) / 5.0)
|
| 186 |
}
|
| 187 |
fog_prediction = predict_fog(fog_model, feature_columns, weather_data)
|
| 188 |
if fog_prediction == "Foggy weather":
|