KiraKel/ai-sales-optimization
0
๐ Car Sales AI Platform - Backend API
FastAPI backend for AI-powered sales forecasting and inventory ranking system.
Quick Start
Prerequisites
- Python 3.11+
- pip
- Virtual environment (recommended)
Installation
# 1. Create virtual environment
python -m venv venv
# 2. Activate virtual environment
# Windows:
venv\Scripts\activate
# Mac/Linux:
source venv/bin/activate
# 3. Install dependencies
pip install -r requirements.txt
# 4. Copy environment file
cp .env.example .env
# 5. Edit .env with your configuration
nano .env # or use your favorite editor
# 6. Save your trained models (IMPORTANT!)
python save_models.py
# 7. Run the server
uvicorn app.main:app --reloadServer will start at: http://localhost:8000
API Documentation: http://localhost:8000/api/docs
๐ Project Structure
backend/
โโโ app/
โ โโโ main.py # Main application
โ โโโ config.py # Configuration
โ โโโ models/
โ โ โโโ schemas.py # Pydantic models
โ โโโ routers/
โ โ โโโ auth.py # Authentication endpoints
โ โ โโโ forecast.py # Sales forecast endpoints
โ โ โโโ ranking.py # Priority ranking endpoints
โ โโโ services/
โ โ โโโ ml_service.py # ML model service
โ โ โโโ auth_service.py # Authentication service
โ โโโ utils/
โ โโโ helpers.py # Helper functions
โโโ models/ # Trained ML models (*.pkl)
โโโ logs/ # Application logs
โโโ requirements.txt # Python dependencies
โโโ .env # Environment variables
โโโ Dockerfile # Docker configuration๐ API Endpoints
Authentication
# Sign Up
POST /api/auth/signup
Body: {
"full_name": "John Doe",
"email": "john@example.com",
"password": "password123",
"company_name": "ABC Dealership"}
# Sign In
POST /api/auth/signin
Body: {
"email": "john@example.com",
"password": "password123"}
Response: {
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
"token_type": "bearer"}
# Get Current User
GET /api/auth/me
Headers: Authorization: Bearer <token>Sales Forecasting
# Generate Forecast
POST /api/forecast/sales
Headers: Authorization: Bearer <token>
Body: {
"view_type": "full" # or "quick"
}
Response: {
"total_forecast": 18500000000,
"total_2024": 17500000000,
"growth_rate": 5.7,
"monthly_forecast": [
{
"month": "Jan 2025",
"forecast": 1501577,
"lower_bound": 1325000,
"upper_bound": 1678000
},
...
]
}
# Health Check
GET /api/forecast/healthPriority Ranking
# Generate Ranking
POST /api/ranking/priority
Headers: Authorization: Bearer <token>
Body: {
"cars": [
{
"make": "Mercedes",
"model": "C-Class",
"year": 2023,
"quantity": 1
},
{
"make": "Toyota",
"model": "Camry",
"year": 2023,
"quantity": 2
}
],
"target_month": "November",
"target_year": 2025,
"region": "East",
"profit_margin": 0.15
}
Response: {
"rankings": [
{
"make": "Mercedes",
"model": "C-Class",
"year": 2023,
"age": 2,
"quantity": 1,
"profit": 2008.45,
"confidence": 67.5,
"risk": "LOW"
},
...
]
}
# Health Check
GET /api/ranking/health๐ง Configuration
Environment Variables (.env)
# Application
APP_NAME=Car Sales AI Platform
DEBUG=True
ENVIRONMENT=development
# Server
HOST=0.0.0.0
PORT=8000
# CORS (Add your frontend URL)
CORS_ORIGINS=["http://localhost:3000"]
# JWT Secret (Generate with: openssl rand -hex 32)
SECRET_KEY=your-secret-key-here
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Model Paths
PROFIT_MODEL_PATH=models/priority_ranking_model.pkl
FEATURE_SCALER_PATH=models/full_preprocessor.pkl
PROPHET_MODEL_PATH=models/prophet_sales_model.pklSaving Your Trained Models
IMPORTANT: You must save your trained models before running the backend.
# save_models.py
import joblib
from your_training_script import xgb_model, feature_scaler, prophet_model
# Save models
joblib.dump(xgb_model, 'models/priority_ranking_model.pkl')
joblib.dump(feature_scaler, 'models/full_preprocessor.pkl')
joblib.dump(prophet_model, 'models/prophet_sales_model.pkl') # Optional
print(" Models saved successfully!")Then run:
python save_models.pyDocker Deployment
# Build image
docker build -t car-sales-backend .
# Run container
docker run -d \
-p 8000:8000 \
-v $(pwd)/models:/app/models \
-v $(pwd)/logs:/app/logs \
--name car-sales-backend \
car-sales-backend
# Or use docker-compose (from root directory)
docker-compose up -dTesting
# Run with test data
curl -X POST http://localhost:8000/api/forecast/sales \
-H "Authorization: Bearer <your-token>" \
-H "Content-Type: application/json" \
-d '{"view_type": "full"}'
# Check health
curl http://localhost:8000/healthLogging
Logs are saved to:
- File:
logs/app.log - Console: Standard output
Log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
Security
Production Checklist:
- [ ] Generate strong SECRET_KEY
- [ ] Set DEBUG=False
- [ ] Use HTTPS
- [ ] Configure CORS properly
- [ ] Add rate limiting
- [ ] Use PostgreSQL (not SQLite)
- [ ] Set up proper authentication
- [ ] Enable logging
- [ ] Add monitoring
Production Deployment
Option 1: Docker
docker-compose -f docker-compose.prod.yml up -dOption 2: Traditional Server
# Install dependencies
pip install -r requirements.txt
# Run with gunicorn (production server)
gunicorn app.main:app \
-w 4 \
-k uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:8000 \
--access-logfile logs/access.log \
--error-logfile logs/error.logOption 3: Cloud Platforms
AWS (Elastic Beanstalk):
eb init
eb create car-sales-api
eb deployGoogle Cloud (Cloud Run):
gcloud run deploy car-sales-api \
--source . \
--platform managed \
--region us-central1Heroku:
heroku create car-sales-api
git push heroku mainTroubleshooting
Issue: Models not loading
# Check if model files exist
ls -la models/
# Re-save models
python save_models.pyIssue: CORS errors
# Add your frontend URL to CORS_ORIGINS in .env
CORS_ORIGINS=["http://localhost:3000", "https://yourdomain.com"]Issue: Prophet installation fails
# Install Prophet dependencies first
pip install pystan
pip install prophetSupport
For issues or questions:
- Open an issue on GitHub
- Contact: kirakel924@gmail.com
