Spaces:
Sleeping
Sleeping
File size: 5,726 Bytes
b2c7817 2e45a15 b2c7817 | 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | ---
title: AgriPredict Analysis Service
emoji: πΎ
colorFrom: green
colorTo: blue
sdk: docker
app_file: main.py
pinned: false
---
# AgriPredict Analysis Service
A production-ready FastAPI service for agricultural demand forecasting with multiple ML models including CatBoost ensemble methods.
## Features
- **Multi-Model Forecasting**: SMA, WMA, Exponential Smoothing, ARIMA, and CatBoost
- **RESTful API**: Clean endpoints for health checks, model listing, and forecasting
- **Docker Ready**: Containerized for easy deployment
- **Hugging Face Spaces**: Configured for cloud deployment
- **Comprehensive Testing**: Built-in test suite for validation
- **Confidence Intervals**: Uncertainty quantification for predictions
- **Real-time Processing**: Asynchronous processing for high performance
## Quick Start
### 1. Install Dependencies
```bash
python run.py install
```
### 2. Run the Service
```bash
python run.py run
```
The API will be available at:
- **Local Service**: http://localhost:7860
- **Local Documentation**: http://localhost:7860/docs
- **Local Health Check**: http://localhost:7860/health
### Production (Hugging Face Spaces)
- **Live Service**: https://adsurkasur-agripredict-analysis.hf.space
- **Live Documentation**: https://adsurkasur-agripredict-analysis.hf.space/docs
- **Live Health Check**: https://adsurkasur-agripredict-analysis.hf.space/health
### 3. Test the Service
```bash
python run.py test
```
### 4. Train CatBoost Model (Optional)
```bash
python run.py train
```
## API Endpoints
### GET /health
Health check endpoint
```json
{
"status": "healthy",
"service": "AgriPredict Analysis Service",
"version": "1.0.0"
}
```
### GET /models
List available forecasting models
```json
{
"models": ["SMA", "WMA", "ES", "ARIMA", "CatBoost"]
}
```
### POST /forecast
Generate demand forecasts
**Request Body:**
```json
{
"historical_data": [
{
"date": "2023-01-01",
"demand": 100,
"price": 50.0,
"weather_temp": 25.0
}
],
"forecast_horizon": 7,
"models": ["SMA", "WMA", "ES"],
"confidence_level": 0.95
}
```
**Response:**
```json
{
"forecast_horizon": 7,
"models_used": ["SMA", "WMA", "ES"],
"forecast_dates": ["2023-01-08", "2023-01-09", ...],
"forecasts": {
"SMA": [105.2, 107.8, ...],
"WMA": [108.5, 110.2, ...],
"ES": [106.1, 108.9, ...]
}
}
```
"models": ["ensemble"],
"include_confidence": true,
"scenario": "realistic"
}
```
### List Models
```
GET /models
```
Returns list of available forecasting models.
## Models Available
1. **SMA** - Simple Moving Average (basic trend analysis)
2. **WMA** - Weighted Moving Average (recent data weighted more)
3. **ES** - Exponential Smoothing (seasonal trend analysis)
4. **ARIMA** - Statistical time series model
5. **CatBoost** - Machine learning model (gradient boosting)
## Usage
### Local Development
1. Install dependencies:
```bash
python run.py install
```
2. Run the service:
```bash
python run.py run
```
The API will be available at `http://localhost:7860`
### API Documentation
Once running, visit `http://localhost:7860/docs` for interactive API documentation.
### Testing
Test the service with the built-in test suite:
```bash
python run.py test
```
## Deployment
This service is designed to run on Hugging Face Spaces with the following configuration:
- **Runtime**: Python 3.10+
- **Framework**: FastAPI with Uvicorn
- **Container**: Docker-based deployment
- **Port**: 7860
- **GPU**: Not required (CPU-only ML models)
- **Memory**: 2GB minimum recommended
## Training the CatBoost Model
The CatBoost model includes a training script for artificial data:
```bash
python run.py train
```
For production use with real data:
1. Prepare your training dataset with features like:
- Historical demand and prices
- Date-based features (day of week, month, season)
- Lag features (previous days' data)
- Rolling statistics
- Weather data
2. Modify `train_catboost.py` to use your real dataset
3. Train the model and update the implementation in `models/forecast_models.py`
## Project Structure
```
analysis-service/
βββ main.py # FastAPI application
βββ models/
β βββ forecast_models.py # Forecasting algorithms
β βββ data_processor.py # Data validation & processing
βββ utils/
β βββ config.py # Configuration management
β βββ logger.py # Logging setup
βββ train_catboost.py # Model training script
βββ test_service.py # API testing script
βββ run.py # Development runner
βββ requirements.txt # Python dependencies
βββ Dockerfile # Container configuration
βββ README.md # This file
```
## Development Commands
- `python run.py install` - Install dependencies
- `python run.py run` - Start the service
- `python run.py test` - Test the running service
- `python run.py train` - Train CatBoost model
## Error Handling
The API includes comprehensive error handling:
- Input validation with Pydantic models
- Graceful error responses with appropriate HTTP status codes
- Detailed error messages for debugging
- Logging for monitoring and troubleshooting
## Contributing
1. Test locally before committing: `python run.py test`
2. Ensure all tests pass
3. Update documentation as needed
4. Follow the existing code style and structure
## License
MIT License - see LICENSE file for details.
|