A machine learning-powered REST API that predicts whether it will rain based on weather parameters. Built with FastAPI and a trained ML model, this API can be easily integrated with any frontend application (Streamlit, React, Vue, mobile apps, etc.).
- ๐ฎ Intelligent Rain Prediction โ Binary classification (Rain / No Rain)
- โก FastAPI Backend โ High-performance async API with automatic Swagger UI
- ๐ง Server-Side Feature Engineering โ Automatically computes derived features (e.g.,
temp_range) - ๐งช Interactive Testing โ Built-in Swagger documentation at
/docs - ๐ Cloud-Ready โ Deploy to Render, Railway, AWS, or any cloud platform
- ๐ Production-Ready โ Optimized model loading and efficient inference
The API expects the following weather parameters:
| Parameter | Type | Description | Example |
|---|---|---|---|
pressure |
float | Atmospheric pressure (hPa) | 1012.5 |
temperature |
float | Average temperature (ยฐC) | 26.0 |
humidity |
float | Relative humidity (%) | 70.0 |
cloud |
float | Cloud cover (%) | 60.0 |
sunshine |
float | Hours of sunshine | 5.0 |
winddirection |
float | Wind direction (0โ360ยฐ, 0 = North) | 180.0 |
windspeed |
float | Wind speed (km/h) | 10.0 |
max_temp |
float | Maximum temperature of the day (ยฐC) | 30.0 |
min_temp |
float | Minimum temperature of the day (ยฐC) | 22.0 |
โน๏ธ Note:
temp_range = max_temp - min_tempis computed automatically by the API during inference.
rainfall-api/
โ
โโโ main.py # FastAPI application
โโโ rainfall_model.pkl # Trained ML model
โโโ requirements.txt # Python dependencies
โโโ README.md # Project documentation
โโโ .gitignore # Git ignore rules
โโโ Dockerfile # (Optional) Docker configuration
git clone https://github.com/your-username/rainfall-api.git
cd rainfall-api# On Windows
python -m venv venv
venv\Scripts\activate
# On macOS/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtuvicorn main:app --reloadThe API will start at http://127.0.0.1:8000
Visit the interactive API documentation:
๐ http://127.0.0.1:8000/docs
Predicts whether it will rain based on input weather parameters.
{
"pressure": 1012.5,
"temperature": 26.0,
"humidity": 70.0,
"cloud": 60.0,
"sunshine": 5.0,
"winddirection": 180.0,
"windspeed": 10.0,
"max_temp": 30.0,
"min_temp": 22.0
}{
"prediction": "Rain"
}or
{
"prediction": "No Rain"
}Health check endpoint.
{
"message": "Rainfall Prediction API is running!"
}curl -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{
"pressure": 1012.5,
"temperature": 26.0,
"humidity": 70.0,
"cloud": 60.0,
"sunshine": 5.0,
"winddirection": 180.0,
"windspeed": 10.0,
"max_temp": 30.0,
"min_temp": 22.0
}'import requests
url = "http://127.0.0.1:8000/predict"
data = {
"pressure": 1012.5,
"temperature": 26.0,
"humidity": 70.0,
"cloud": 60.0,
"sunshine": 5.0,
"winddirection": 180.0,
"windspeed": 10.0,
"max_temp": 30.0,
"min_temp": 22.0
}
response = requests.post(url, json=data)
print(response.json())- Push your code to GitHub
- Create a new Web Service on Render
- Connect your GitHub repository
- Set the following:
- Build Command:
pip install -r requirements.txt - Start Command:
uvicorn main:app --host 0.0.0.0 --port $PORT
- Build Command:
- Deploy! ๐
- Install the Railway CLI
- Run:
railway login railway init railway up
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Build and run:
docker build -t rainfall-api .
docker run -p 8000:8000 rainfall-apiThe rainfall prediction model was trained using historical weather data with the following approach:
- Data Collection: Weather observations including temperature, humidity, pressure, wind metrics, and rainfall records
- Feature Engineering:
- Created
temp_rangefeature (max_temp - min_temp) - Normalized numerical features
- Handled missing values and outliers
- Created
- Model Selection: Tested multiple algorithms (Logistic Regression, Random Forest, XGBoost)
- Training: Used scikit-learn with cross-validation for robust performance
- Evaluation: Achieved strong performance on validation set using metrics like accuracy, precision, and recall
- Serialization: Saved the trained model using
joblibfor efficient loading
๐ก Future Improvements: Plans to integrate time-series features and ensemble methods for better accuracy.
- FastAPI โ Modern, fast web framework for building APIs
- scikit-learn โ Machine learning library for model training
- pandas โ Data manipulation and preprocessing
- joblib โ Model serialization
- uvicorn โ ASGI server for production deployment
fastapi==0.104.1
uvicorn==0.24.0
scikit-learn==1.3.2
pandas==2.1.3
joblib==1.3.2
pydantic==2.5.0- โ Ensure feature names and order match the training data exactly
- โ The model is loaded once at startup for optimal performance
- โ API is designed to be consumed by any frontend or mobile application
- โ CORS is enabled for cross-origin requests (configure as needed)
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Aman Rawat
- ๐ผ LinkedIn
- ๐ GitHub
- ๐ง Email: amansinghrawat992752@gmail.com
If you found this project helpful, please give it a โญ๏ธ!
Built with โค๏ธ using FastAPI and Machine Learning