File size: 2,698 Bytes
1f15b4e
 
 
 
 
 
 
 
 
513c1a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---
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

1.  **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.

2.  **ML Model (`model.py`)**:
    *   Uses a **Random Forest Classifier** trained on synthetic data.
    *   Predicts `Low`, `Medium`, or `High` traffic risk based on temperature, rainfall, and weather condition.
    *   Includes a training script to generate baseline patterns.

3.  **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.

## 🛠️ Setup

1.  **Install dependencies**:
    ```bash
    pip install -r requirements.txt
    ```

2.  **Configure API Key**:
    *   Open `.env` and replace `your_api_key_here` with your OpenWeather API key.

3.  **Run the Server**:
    ```bash
    python app.py
    ```

4.  **Test the endpoint**:
    *   Open `http://localhost:8000/weather/Dehradun` in your browser.

## 📊 Output Format
```json
{
  "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.

1.  **Create a New Space**:
    *   Go to [huggingface.co/new-space](https://huggingface.co/new-space).
    *   Select **Docker** as the SDK.
    *   Choose the **Blank** template or upload your files directly.

2.  **Add Secret (API Key)**:
    *   In your Space settings, go to **Variables and secrets**.
    *   Add a new secret named `WEATHER_API_KEY` and paste your key.

3.  **Upload Files**:
    *   Upload `app.py`, `weather_pipeline.py`, `model.py`, `requirements.txt`, `Dockerfile`, and the `.pkl` files.
    *   Hugging Face will automatically build and deploy your container on port 7860.

4.  **Test**:
    *   Access your API at `https://huggingface.co/spaces/[YOUR_USERNAME]/[YOUR_SPACE_NAME]/weather/Dehradun`.