Skip to content

Commit

Permalink
Update main branch with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanBV committed Oct 21, 2024
1 parent c3d2efa commit 77341f3
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 17 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/update_static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Update Static Files

on:
schedule:
- cron: '0 */6 * * *' # Runs every 6 hours
workflow_dispatch: # Allows manual triggering

jobs:
update_static:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Generate static files
env:
OPENWEATHERMAP_API_KEY: ${{ secrets.OPENWEATHERMAP_API_KEY || '' }}
run: |
python generate_static.py
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ This Weather Monitoring System is a Python-based application that fetches real-t
- Web dashboard for real-time weather visualization
- Machine learning-based weather prediction for the next 24 hours

### Web Dashboard
### Web Dashboard
The web dashboard provides a user-friendly interface to view current weather data, forecasts, and predictions for all monitored cities.

### Machine Learning Weather Prediction
### Machine Learning Weather Prediction
Trained machine learning models for 24-hour weather prediction are stored in the `models/` directory.

### Hosted Dashboard

You can view a static version of the weather dashboard online:

https://aryanbv.github.io/weather_monitoring_system/

Note: This static version is updated periodically and may not reflect real-time data. For the most up-to-date information, please run the application locally as described in the Usage section.

### Weather Monitoring System

To view the real-time weather dashboard:
Expand Down
29 changes: 18 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ def dashboard():
predictor.load_model()
except FileNotFoundError:
historical_data = db_handler.get_historical_weather_data(city)
predictor.train_model(historical_data)

if historical_data:
predictor.train_model(historical_data)
else:
continue # Skip prediction if no historical data

next_day = datetime.now() + timedelta(days=1)
prediction = predictor.predict(
next_day.hour,
next_day.weekday(),
next_day.month,
latest_data[city]['humidity'],
latest_data[city]['wind_speed'],
latest_data[city]['weather_condition']
)
predictions[city] = round(prediction, 1)
try:
prediction = predictor.predict(
next_day.hour,
next_day.weekday(),
next_day.month,
latest_data[city]['humidity'],
latest_data[city]['wind_speed'],
latest_data[city]['weather_condition']
)
predictions[city] = round(prediction, 1)
except ValueError as e:
print(f"Prediction error for {city}: {str(e)}")
predictions[city] = "N/A"

return render_template('dashboard.html', latest_data=latest_data, predictions=predictions)

Expand Down
Binary file added models/Bangalore_label_encoder.joblib
Binary file not shown.
Binary file added models/Chennai_label_encoder.joblib
Binary file not shown.
Binary file added models/Delhi_label_encoder.joblib
Binary file not shown.
Binary file added models/Hyderabad_label_encoder.joblib
Binary file not shown.
Binary file added models/Kolkata_label_encoder.joblib
Binary file not shown.
Binary file added models/Mumbai_label_encoder.joblib
Binary file not shown.
Binary file modified src/database/__pycache__/db_handler.cpython-312.pyc
Binary file not shown.
Binary file modified src/ml/__pycache__/weather_predictor.cpython-312.pyc
Binary file not shown.
23 changes: 19 additions & 4 deletions src/ml/weather_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ def __init__(self, city):
self.model = None
self.le = LabelEncoder()
self.model_path = f'models/{city}_weather_model.joblib'
self.le_path = f'models/{city}_label_encoder.joblib'
self.is_fitted = False

def prepare_data(self, data):
df = pd.DataFrame(data)
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['hour'] = df['timestamp'].dt.hour
df['day_of_week'] = df['timestamp'].dt.dayofweek
df['month'] = df['timestamp'].dt.month
df['weather_condition'] = self.le.fit_transform(df['weather_condition'])
self.le.fit(df['weather_condition'])
df['weather_condition'] = self.le.transform(df['weather_condition'])
return df

def train_model(self, data):
Expand All @@ -29,21 +32,33 @@ def train_model(self, data):
self.model = RandomForestRegressor(n_estimators=100, random_state=42)
self.model.fit(X_train, y_train)
self.save_model()
self.is_fitted = True

def predict(self, hour, day_of_week, month, humidity, wind_speed, weather_condition):
if self.model is None:
if not self.is_fitted:
self.load_model()
weather_condition_encoded = self.le.transform([weather_condition])[0]
try:
weather_condition_encoded = self.le.transform([weather_condition])[0]
except ValueError:
# If the weather condition is not in the encoder, use a default value
weather_condition_encoded = -1

if self.model is None:
raise ValueError("Model not trained or loaded. Please train the model first.")

prediction = self.model.predict([[hour, day_of_week, month, humidity, wind_speed, weather_condition_encoded]])
return prediction[0]

def save_model(self):
if not os.path.exists('models'):
os.makedirs('models')
joblib.dump(self.model, self.model_path)
joblib.dump(self.le, self.le_path)

def load_model(self):
if os.path.exists(self.model_path):
if os.path.exists(self.model_path) and os.path.exists(self.le_path):
self.model = joblib.load(self.model_path)
self.le = joblib.load(self.le_path)
self.is_fitted = True
else:
raise FileNotFoundError(f"No trained model found for {self.city}")
20 changes: 20 additions & 0 deletions update_github.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Update main branch
git checkout main
git add .
git commit -m "Update main branch with latest changes"
git push origin main

# Update gh-pages branch
git checkout gh-pages
git merge main
python generate_static.py
git add .
git commit -m "Update static files for GitHub Pages"
git push origin gh-pages

# Switch back to main branch
git checkout main

echo "GitHub repository and Pages site updated successfully!"

0 comments on commit 77341f3

Please sign in to comment.