Spaces:
Sleeping
Sleeping
metadata
title: Weather Simulation & Traffic Risk
emoji: π¦οΈ
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
π¦οΈ Weather-Traffic Risk Prediction Pipeline
This project implements a full E2E pipeline that fetches weather data, cleans it, performs feature engineering, and uses a Machine Learning model to predict traffic risk.
π Components
Weather Pipeline (
weather_pipeline.py):- Fetches real-time data from WeatherAPI.com.
- Handles missing values and normalizes conditions (Rainy, Cloudy, etc.).
- Calculates a "Risk Score" based on heuristics.
- Implements caching (10 min) to optimize API usage.
ML Model (
model.py):- Uses a Random Forest Classifier trained on synthetic data.
- Predicts
Low,Medium, orHightraffic risk based on temperature, rainfall, and weather condition. - Includes a training script to generate baseline patterns.
FastAPI Server (
app.py):- Exposes a REST API endpoint:
/weather/{city}. - Returns structured JSON with weather details and ML risk prediction.
- Visual alerts (Green/Yellow/Red) included in responses.
- Exposes a REST API endpoint:
π οΈ Setup
Install dependencies:
pip install -r requirements.txtConfigure API Key:
- Open
.envand replaceyour_api_key_herewith your OpenWeather API key.
- Open
Run the Server:
python app.pyTest the endpoint:
- Open
http://localhost:8000/weather/Dehradunin your browser.
- Open
π Output Format
{
"city": "Dehradun",
"weather": {
"temperature": 28,
"rainfall_1h": 12,
"condition": "Rainy",
"base_risk_score": 3
},
"ml_prediction": {
"traffic_risk_level": "High",
"confidence": 0.89,
"status_color": "π΄ Red"
}
}
π€ Deploying to Hugging Face Spaces
This project is configured for easy deployment on Hugging Face using Docker.
Create a New Space:
- Go to huggingface.co/new-space.
- Select Docker as the SDK.
- Choose the Blank template or upload your files directly.
Add Secret (API Key):
- In your Space settings, go to Variables and secrets.
- Add a new secret named
WEATHER_API_KEYand paste your key.
Upload Files:
- Upload
app.py,weather_pipeline.py,model.py,requirements.txt,Dockerfile, and the.pklfiles. - Hugging Face will automatically build and deploy your container on port 7860.
- Upload
Test:
- Access your API at
https://huggingface.co/spaces/[YOUR_USERNAME]/[YOUR_SPACE_NAME]/weather/Dehradun.
- Access your API at