An intelligent stock management system for supermarkets that uses Google Gemini Vision API to analyze produce shelf images and provide real-time inventory insights.
- Overview
- Key Features
- Technology Stack
- Prerequisites
- Quick Start
- Project Structure
- API Documentation
- Database Schema
- Frontend Architecture
- Configuration
- Usage Guide
- Development
- Testing
- Troubleshooting
- Future Roadmap
- License
AISLEAI is an AI-powered stock level estimation system designed for supermarkets to automate inventory monitoring of fresh produce. The system analyzes shelf images using Google's Gemini Vision API to detect stock levels, identify products, and generate restocking alerts. Phase 3 introduces machine learning forecasting capabilities with adaptive models, intelligent alert systems, and comprehensive data export functionality.
Traditional inventory management in supermarkets is:
- Time-consuming: Manual stock checks require significant staff time
- Inconsistent: Human error leads to inaccurate counts
- Reactive: Stock-outs discovered too late
- Labor-intensive: Requires constant monitoring
AISLEAI provides:
- Automated Analysis: Upload shelf images for instant AI-powered analysis
- Real-time Monitoring: Live dashboard with stock level trends
- Intelligent Alerts: Priority-based restocking recommendations with real-time notifications
- ML Forecasting: 7-day stock predictions with adaptive machine learning models
- Historical Tracking: Complete audit trail of all analyses
- Multi-store Support: Manage inventory across multiple locations
- Data Export: CSV exports for external analysis and reporting
- Predictive Insights: Proactive restocking alerts based on future predictions
- Gemini Vision API Integration: Leverages Google's latest AI model for produce detection
- 15 Product Categories: Detects banana, apple, orange, tomato, lettuce, carrot, broccoli, cucumber, avocado, strawberry, cabbage, capsicum, pear, potato, sweet potato
- Image Enhancement Pipeline: Automatic contrast, sharpness, and noise reduction
- Comprehensive Analysis: Stock percentage, abundance, shelf coverage, confidence scores
- Priority Classification: Intelligent restocking priority (high/medium/low)
- Real-time Stock Summary: Live view of all product stock levels
- Trend Analysis: Detects increasing, decreasing, or stable trends
- Smart Alerts: Highlights products needing immediate attention
- Visual Analytics: Chart.js powered stock level visualizations
- Status Indicators: Color-coded status (critical/low/moderate/good/abundant)
- Toast Notifications: Real-time alert notifications via react-toastify
- Auto-refresh Dashboard: Polls for new alerts every 30 seconds
- Historical Tracking: Complete history of all analyses with timestamps
- Advanced Filtering: Search by product, date range, or store
- Processing Metrics: Track analysis performance and processing times
- Detailed Results: Per-product observations and recommendations
- Store Hierarchy: Manage multiple store locations
- Independent Catalogs: Each store maintains its own product catalog
- Store-specific Analysis: Filter and view data by store location
- Intelligent Rate Limiting: Sliding window algorithm prevents API quota violations
- Rate Limit Monitoring: Real-time status of API call quotas
- Automatic Throttling: Waits for quota availability when limits approached
- Configurable Limits: Customizable RPM (15) and RPD (1500) limits
- PostgreSQL Database: Robust relational database for all data persistence
- Database Migrations: Alembic-powered schema versioning
- Data Integrity: Foreign key constraints and cascade deletes
- Performance Indexes: Optimized queries for fast data retrieval
- Adaptive ML Models: Automatically selects best model based on data availability
- Exponential Smoothing (7+ data points): Advanced time series forecasting with damped trend
- Linear Trend Analysis (4-6 data points): Regression-based predictions
- Moving Average (<4 data points): Simple average-based forecasting
- 7-Day Predictions: Generate stock level forecasts up to 30 days ahead
- Confidence Intervals: 95% confidence bounds for all predictions
- Batch Forecasting: Generate forecasts for all products simultaneously
- Interactive Charts: Visualize historical data and future predictions
- Model Accuracy Tracking: Real-time accuracy metrics and performance monitoring
- Forecast Caching: Optimized 24-hour cache to reduce computation
- Multi-Level Alerts:
- Critical (≤20%): Immediate restocking required
- Low (≤40%): Restocking recommended soon
- Forecast Warning (≤30% predicted): Proactive advance warnings
- Smart Alert Rules: Configurable per-store threshold rules
- Deduplication: Prevents duplicate alerts within time windows
- Alert Management:
- Acknowledge individual alerts
- Bulk acknowledge all store alerts
- Track acknowledgment timestamps
- Alert Statistics: Comprehensive metrics and counts by type
- Real-time Notifications: Frontend toast notifications for new alerts
- Auto-checking: Background alert checking on every analysis
- CSV Export: Download stock history and forecast data
- Flexible Filtering: Export by date range, store, or specific products
- Forecast Reports: Export ML predictions with confidence intervals
- Analysis History: Full historical data export for external analysis
- Auto-formatted Files: Timestamped filenames for easy organization
- Forecast Accuracy Analysis: Compare predictions vs actual stock levels
- Mean Absolute Error (MAE): Quantitative accuracy metrics
- Trend Detection: Identify long-term patterns in stock movements
- Predictive Insights: Forecast-based restocking recommendations
- Coverage Metrics: Track forecast generation coverage across products
- Framework: FastAPI (Python 3.11+)
- Database: PostgreSQL 15 with SQLAlchemy ORM
- AI/ML:
- Google Generative AI (Gemini Vision) for image analysis
- Statsmodels for time series forecasting (Exponential Smoothing)
- SciPy for statistical analysis and linear regression
- NumPy for numerical computations
- Image Processing:
- Pillow (PIL) for image manipulation
- OpenCV (headless) for advanced image processing
- Migrations: Alembic
- Validation: Pydantic v2
- Authentication: Python-JOSE, Passlib (infrastructure ready)
- Framework: React 18 with TypeScript
- Routing: React Router DOM v6
- Styling: Tailwind CSS
- HTTP Client: Axios
- Charts: Chart.js with react-chartjs-2
- File Upload: react-dropzone
- Notifications: react-toastify
- Date Handling: date-fns
- Build Tool: Create React App
- Containerization: Docker & Docker Compose
- Web Server: Uvicorn (ASGI)
- Reverse Proxy: Nginx (production)
- Version Control: Git
- Testing: pytest (Backend), React Testing Library (Frontend)
- Code Quality: ESLint, TypeScript strict mode
- Hot Reload: Enabled for both frontend and backend
Before you begin, ensure you have the following installed:
- Docker Desktop (v20.10+) - Download
- Git - For cloning the repository
- Google Gemini API Key - Get API Key
- OS: Windows 10/11, macOS 10.15+, or Linux
- RAM: 4GB minimum, 8GB recommended
- Storage: 2GB free space
- Ports: 3000, 8000, 5432 must be available
git clone <repository-url>
cd AISLEAICreate or edit the .env file in the root directory:
# Database Configuration
DB_PASSWORD=your_secure_password
DATABASE_URL=postgresql://aisleai:${DB_PASSWORD}@db:5432/aisleai
# Google Gemini API (REQUIRED)
GEMINI_API_KEY=your_actual_gemini_api_key_here
# Backend Configuration
SECRET_KEY=your_secret_key_here
ENVIRONMENT=development
# Rate Limiting (Optional)
GEMINI_MAX_RPM=15
GEMINI_MAX_RPD=1500Important: Replace
your_actual_gemini_api_key_herewith your real Gemini API key
# Build all Docker containers
docker-compose build
# Start all services in detached mode
docker-compose up -dWait for services to start (approximately 30-60 seconds).
# Run database migrations
docker-compose exec backend alembic upgrade head
# Seed initial data (creates stores and products)
docker-compose exec backend python seed_data.py- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Alternative API Docs: http://localhost:8000/redoc
# Check service status
docker-compose ps
# View logs
docker-compose logs -f
# Test API health
curl http://localhost:8000/api/v1/healthAISLEAI/
├── backend/ # FastAPI Backend
│ ├── app/
│ │ ├── api/ # API Endpoints
│ │ │ ├── __init__.py # Router aggregator
│ │ │ └── endpoints/
│ │ │ ├── analyze.py # Image analysis endpoints
│ │ │ ├── products.py # Product CRUD endpoints
│ │ │ ├── dashboard.py # Dashboard data endpoints
│ │ │ ├── analyses.py # Analysis history endpoints
│ │ │ ├── stores.py # Store management endpoints
│ │ │ ├── alerts.py # 🆕 Alert management endpoints
│ │ │ ├── forecasting.py# 🆕 ML forecasting endpoints
│ │ │ └── export.py # 🆕 CSV export endpoints
│ │ ├── core/ # Core Configuration
│ │ │ ├── config.py # Application settings (with ML configs)
│ │ │ ├── database.py # Database connection
│ │ │ ├── exceptions.py # Custom exceptions
│ │ │ └── rate_limiter.py # Rate limiting logic
│ │ ├── db/ # Database Setup
│ │ │ ├── base.py # Database base class
│ │ │ ├── session.py # Database session management
│ │ │ └── init_db.py # Database initialization
│ │ ├── models/ # SQLAlchemy Models
│ │ │ ├── product.py # Product model
│ │ │ ├── analysis.py # Stock analysis model
│ │ │ ├── stock_level.py # Stock level model
│ │ │ ├── store.py # Store model
│ │ │ ├── forecast.py # 🆕 ML forecast model
│ │ │ ├── alert.py # 🆕 Alert model
│ │ │ └── alert_rule.py # 🆕 Alert rule model
│ │ ├── schemas/ # Pydantic Schemas
│ │ │ ├── product.py # Product schemas
│ │ │ ├── analysis.py # Analysis schemas
│ │ │ ├── dashboard.py # Dashboard schemas
│ │ │ ├── store.py # Store schemas
│ │ │ ├── stock_level.py # Stock level schemas
│ │ │ ├── forecast.py # 🆕 Forecast schemas
│ │ │ ├── alert.py # 🆕 Alert schemas
│ │ │ └── alert_rule.py # 🆕 Alert rule schemas
│ │ ├── services/ # Business Logic
│ │ │ ├── gemini_service.py # Gemini API integration
│ │ │ ├── image_service.py # Image processing
│ │ │ ├── ml/ # 🆕 Machine Learning Services
│ │ │ │ ├── __init__.py
│ │ │ │ └── forecasting_service.py # Time series forecasting
│ │ │ ├── analytics/ # 🆕 Analytics Services
│ │ │ │ ├── __init__.py
│ │ │ │ └── alert_service.py # Alert generation & management
│ │ │ └── export/ # 🆕 Export Services
│ │ │ ├── __init__.py
│ │ │ └── csv_exporter.py # CSV export functionality
│ │ └── main.py # FastAPI application entry point
│ ├── alembic/ # Database Migrations
│ │ ├── versions/ # Migration files
│ │ │ ├── 19aa56c4c4bb_initial_migration.py
│ │ │ ├── 57c76a05aca9_remove_authentication_keep_stores.py
│ │ │ ├── d965d4ae57ac_add_phase_3_tables_forecasts_alerts_and_.py
│ │ │ └── dcb86029a681_add_performance_indexes_for_queries.py
│ │ └── env.py # Alembic configuration
│ ├── tests/ # Backend Tests
│ ├── seed_data.py # Database seed script
│ ├── requirements.txt # Python dependencies
│ └── Dockerfile # Backend Docker image
│
├── frontend/ # React Frontend
│ ├── public/ # Static Assets
│ ├── src/
│ │ ├── components/ # React Components
│ │ │ ├── Common/ # Shared Components
│ │ │ │ ├── Header.tsx # Navigation header
│ │ │ │ ├── LoadingSpinner.tsx
│ │ │ │ └── ErrorBoundary.tsx # Error handling
│ │ │ ├── Layout/ # Layout Components
│ │ │ │ ├── Layout.tsx # Page layout wrapper
│ │ │ │ └── Header.tsx # Navigation header
│ │ │ ├── Dashboard/ # Dashboard Components
│ │ │ │ ├── DashboardSummary.tsx
│ │ │ │ ├── StatsCard.tsx
│ │ │ │ ├── ProductCard.tsx
│ │ │ │ ├── StockLevelChart.tsx
│ │ │ │ ├── ForecastChart.tsx # 🆕 ML forecast visualization
│ │ │ │ └── ForecastsSection.tsx # 🆕 Forecast management UI
│ │ │ ├── Upload/ # Upload Components
│ │ │ │ └── ImageUploader.tsx
│ │ │ └── Results/ # Results Components
│ │ │ ├── AnalysisResults.tsx
│ │ │ └── ProductAnalysisCard.tsx
│ │ ├── contexts/ # 🆕 React Contexts
│ │ │ └── NotificationContext.tsx # Real-time alert notifications
│ │ ├── hooks/ # Custom React Hooks
│ │ │ ├── useImageUpload.ts # Image upload logic
│ │ │ └── useStockData.ts # Dashboard data fetching
│ │ ├── pages/ # Page Components
│ │ │ ├── HomePage.tsx # Landing page
│ │ │ ├── UploadPage.tsx # Image upload & analysis
│ │ │ ├── DashboardPage.tsx # Real-time monitoring (with forecasts)
│ │ │ └── HistoryPage.tsx # Analysis history
│ │ ├── services/ # API Services
│ │ │ └── api.ts # Axios API client (with Phase 3 endpoints)
│ │ ├── types/ # TypeScript Types
│ │ │ └── index.ts # All type definitions
│ │ ├── App.tsx # Main React component (with NotificationProvider)
│ │ ├── index.tsx # React entry point
│ │ └── index.css # Global styles
│ ├── package.json # Node dependencies
│ ├── tsconfig.json # TypeScript configuration
│ ├── tailwind.config.js # Tailwind CSS config
│ └── Dockerfile # Frontend Docker image
│
├── uploads/ # Uploaded Images (Docker volume)
├── logs/ # Application Logs (Docker volume)
├── .env # Environment variables
├── docker-compose.yml # Docker services configuration
└── README.md # This file
http://localhost:8000/api/v1
All endpoints are currently public (no authentication required).
GET /health- Check API status
GET /stores/- List all storesGET /stores/{store_id}- Get store detailsPOST /stores/- Create new storePUT /stores/{store_id}- Update storeDELETE /stores/{store_id}- Deactivate store
GET /products/- List all products (with optionalstore_idfilter)GET /products/{product_id}- Get product detailsPOST /products/?store_id={store_id}- Create new productPUT /products/{product_id}- Update productGET /products/{product_id}/stock-levels- Get stock history
POST /analyze/- Upload and analyze shelf imageGET /analyze/{analysis_id}- Get analysis resultsGET /analyze/rate-limit-status- Check API rate limits
GET /dashboard/summary- Get comprehensive dashboard dataGET /dashboard/alerts- Get restocking alertsGET /dashboard/stock-summary- Get current stock levelsGET /dashboard/stock-trends/{product_id}- Get trend dataGET /dashboard/statistics- Get overall statistics
GET /analyses/- List historical analyses (with filters)GET /analyses/{analysis_id}- Get specific analysisGET /analyses/stats- Get analysis statistics
POST /forecast/generate- Generate forecast for specific productGET /forecast/product/{product_id}- Get existing forecast dataPOST /forecast/batch- Generate forecasts for all products (background task)GET /forecast/batch/status- Check forecast generation statusGET /forecast/accuracy- Get forecast accuracy metricsDELETE /forecast/product/{product_id}- Delete product forecasts
GET /alerts/- Get all alerts with filtersGET /alerts/unacknowledged- Get unacknowledged alerts (for notifications)POST /alerts/{alert_id}/acknowledge- Mark alert as acknowledgedPOST /alerts/acknowledge-all- Acknowledge all alerts for a storePOST /alerts/check- Manually trigger alert checkingGET /alerts/statistics- Get alert statisticsGET /alerts/rules- Get all alert rulesPOST /alerts/rules- Create new alert rulePUT /alerts/rules/{rule_id}- Update alert ruleDELETE /alerts/rules/{rule_id}- Delete alert rule
GET /export/stock-history- Export stock history to CSVGET /export/forecast/{product_id}- Export forecast data to CSV
Request:
curl -X POST "http://localhost:8000/api/v1/analyze/" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@shelf_image.jpg"Response:
{
"analysis_id": 1,
"timestamp": "2025-10-30T10:30:00Z",
"overall_assessment": "Overall stock levels are moderate...",
"processing_time_ms": 3245,
"products": [
{
"product_name": "banana",
"stock_level_percentage": 45,
"abundance": "moderate",
"visible_quantity": 12,
"shelf_coverage": 60,
"confidence_score": 92,
"restocking_priority": "medium",
"priority_reasoning": "Stock level approaching low threshold",
"observations": "Bananas are displayed in the center..."
}
]
}The API implements rate limiting for Gemini API calls:
- 15 requests per minute (RPM)
- 1500 requests per day (RPD)
If limits are exceeded, the API will automatically wait and retry. Check rate limit status:
curl http://localhost:8000/api/v1/analyze/rate-limit-statusResponse:
{
"calls_last_minute": 5,
"calls_last_day": 234,
"max_rpm": 15,
"max_rpd": 1500,
"remaining_minute": 10,
"remaining_day": 1266,
"next_minute_reset": "2025-10-30T10:31:00Z",
"next_day_reset": "2025-10-31T00:00:00Z"
}┌─────────────────┐
│ Store │
├─────────────────┤
│ id (PK) │
│ store_code │◄───────────────┐
│ store_name │ │
│ location │ │ 1:N
│ is_active │ │
└─────────────────┘ │
│
┌─────────────────────────┐ │
│ Product │◄───────┤
├─────────────────────────┤ │
│ id (PK) │ │
│ store_id (FK) │────────┘
│ name │
│ optimal_stock_level │
└─────────────────────────┘
▲ ▲
│ N:1 │ 1:N
│ │
┌────────────────────────────┐ ┌──────────────────────┐
│ ProductStockLevel │ │ 🆕 StockForecast │
├────────────────────────────┤ ├──────────────────────┤
│ id (PK) │ │ id (PK) │
│ product_id (FK) │────│ product_id (FK) │
│ analysis_id (FK) │ │ store_id (FK) │
│ stock_level_percentage │ │ forecast_date │
│ abundance │ │ predicted_level │
│ restocking_priority │ │ confidence_lower │
│ timestamp │ │ confidence_upper │
└────────────────────────────┘ │ model_type │
│ └──────────────────────┘
│ N:1
▼ ┌──────────────────────┐
┌─────────────────────┐ │ 🆕 Alert │
│ StockAnalysis │ ├──────────────────────┤
├─────────────────────┤ │ id (PK) │
│ id (PK) │ │ product_id (FK) │────┐
│ image_path │ │ store_id (FK) │ │
│ overall_assessment │ │ alert_type │ │ N:1
│ processing_time_ms │ │ alert_level │ │
│ raw_response (JSON) │ │ message │ │
└─────────────────────┘ │ stock_level │ │
│ is_acknowledged │ │
└──────────────────────┘ │
│
┌──────────────────────┐ │
│ 🆕 AlertRule │◄───┘
├──────────────────────┤
│ id (PK) │
│ store_id (FK) │
│ alert_type │
│ threshold_percentage │
│ forecast_days_ahead │
│ is_active │
└──────────────────────┘
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| store_code | VARCHAR(50) | UNIQUE, NOT NULL |
| store_name | VARCHAR(100) | NOT NULL |
| location | VARCHAR(200) | |
| address | TEXT | |
| phone | VARCHAR(20) | |
| is_active | INTEGER | DEFAULT 1 |
| created_at | TIMESTAMP | DEFAULT NOW() |
| updated_at | TIMESTAMP | DEFAULT NOW() |
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| store_id | INTEGER | FOREIGN KEY → stores.id |
| name | VARCHAR(50) | NOT NULL |
| display_name | VARCHAR(100) | |
| description | TEXT | |
| optimal_stock_level | INTEGER | DEFAULT 80 |
| created_at | TIMESTAMP | DEFAULT NOW() |
Indexes: idx_store_name (store_id, name)
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| image_path | VARCHAR(500) | NOT NULL |
| analysis_timestamp | TIMESTAMP | NOT NULL, INDEXED |
| overall_assessment | TEXT | |
| raw_response | JSON | |
| processing_time_ms | INTEGER | |
| created_at | TIMESTAMP | DEFAULT NOW() |
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| analysis_id | INTEGER | FK → stock_analyses.id (CASCADE) |
| product_id | INTEGER | FK → products.id |
| stock_level_percentage | INTEGER | CHECK (0-100) |
| abundance | VARCHAR(20) | |
| visible_quantity | INTEGER | |
| shelf_coverage | INTEGER | CHECK (0-100) |
| confidence_score | INTEGER | CHECK (0-100) |
| restocking_priority | VARCHAR(20) | |
| observations | TEXT | |
| timestamp | TIMESTAMP | INDEXED |
Constraints: UNIQUE(analysis_id, product_id)
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| product_id | INTEGER | FK → products.id, INDEXED |
| store_id | INTEGER | FK → stores.id, INDEXED |
| forecast_date | TIMESTAMP | NOT NULL, INDEXED |
| predicted_stock_level | FLOAT | NOT NULL |
| confidence_lower | FLOAT | 95% confidence interval lower bound |
| confidence_upper | FLOAT | 95% confidence interval upper bound |
| model_type | VARCHAR(50) | NOT NULL (exponential_smoothing, linear_trend, moving_average) |
| created_at | TIMESTAMP | DEFAULT NOW() |
Indexes:
idx_product_forecast_date(product_id, forecast_date)idx_store_forecast_date(store_id, forecast_date)
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| product_id | INTEGER | FK → products.id, INDEXED |
| store_id | INTEGER | FK → stores.id, INDEXED |
| alert_type | VARCHAR(50) | NOT NULL, INDEXED (critical, low, forecast_warning, trend_negative) |
| alert_level | VARCHAR(20) | NOT NULL (critical, warning, info) |
| message | TEXT | NOT NULL |
| stock_level | FLOAT | Current/predicted stock level |
| threshold_crossed | FLOAT | Threshold that triggered alert |
| is_acknowledged | BOOLEAN | DEFAULT FALSE, INDEXED |
| created_at | TIMESTAMP | DEFAULT NOW(), INDEXED |
| acknowledged_at | TIMESTAMP | NULL until acknowledged |
Indexes:
idx_unacknowledged(is_acknowledged, created_at)idx_store_alerts(store_id, is_acknowledged)
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY |
| store_id | INTEGER | FK → stores.id, INDEXED |
| alert_type | VARCHAR(50) | NOT NULL (critical, low, forecast_warning) |
| threshold_percentage | FLOAT | NOT NULL |
| forecast_days_ahead | INTEGER | For forecast alerts only |
| is_active | BOOLEAN | DEFAULT TRUE |
| created_at | TIMESTAMP | DEFAULT NOW() |
| updated_at | TIMESTAMP | DEFAULT NOW(), AUTO-UPDATE |
App
├── NotificationProvider
│ └── ToastContainer
├── Layout
│ ├── Header
│ └── Router
│ ├── HomePage
│ ├── UploadPage
│ │ ├── ImageUploader
│ │ └── AnalysisResults
│ │ └── ProductAnalysisCard[]
│ ├── DashboardPage
│ │ ├── DashboardSummary
│ │ │ ├── StatsCard[]
│ │ │ ├── ProductCard[]
│ │ │ └── StockLevelChart
│ │ └── ForecastsSection (🆕 Phase 3 - ML Forecasting)
│ │ ├── Product Selector Sidebar
│ │ └── ForecastChart (🆕 Phase 3)
│ │ ├── Historical Data Line
│ │ ├── Forecast Predictions Line
│ │ └── Confidence Interval Bands
│ └── HistoryPage
useImageUpload.ts
- Manages image upload process
- Tracks upload progress (0-100%)
- Handles Gemini API responses
- Error handling and reset functionality
useStockData.ts
- Fetches dashboard data on mount
- Manages loading and error states
- Provides refetch capability
- Parallel data fetching for performance
NotificationContext.tsx
- Global notification state management
- Polls for new alerts every 30 seconds
- Tracks unacknowledged alert count
- Displays toast notifications for new alerts
- Prevents duplicate notifications
- Color-coded alerts by severity (critical/warning/info)
- Tailwind CSS: Utility-first CSS framework
- Custom Colors: Primary color
#4F46E5(indigo) - Responsive Design: Mobile-first approach
- Dark Mode: Ready for future implementation
const routes = [
{ path: '/', component: HomePage },
{ path: '/upload', component: UploadPage },
{ path: '/dashboard', component: DashboardPage },
{ path: '/history', component: HistoryPage }
];File: backend/app/core/config.py
class Settings(BaseSettings):
PROJECT_NAME: str = "AISLEAI"
VERSION: str = "1.0.0"
ENVIRONMENT: str = "development"
# Database
DATABASE_URL: str
# API Keys
GEMINI_API_KEY: str
SECRET_KEY: str
# Gemini API
GEMINI_MODEL: str = "gemini-flash-latest"
GEMINI_MAX_RPM: int = 15
GEMINI_MAX_RPD: int = 1500
# File Upload
UPLOAD_DIR: str = "/app/uploads"
MAX_UPLOAD_SIZE: int = 10 * 1024 * 1024 # 10MB
ALLOWED_EXTENSIONS: set = {".jpg", ".jpeg", ".png"}
# CORS
BACKEND_CORS_ORIGINS: list = [
"http://localhost:3000",
"http://localhost:8000"
]
# 🆕 Phase 3: Alert Configuration
ALERT_CRITICAL_THRESHOLD: int = 20 # % stock level
ALERT_LOW_THRESHOLD: int = 40 # % stock level
ALERT_FORECAST_THRESHOLD: int = 30 # % predicted level
ALERT_FORECAST_DAYS: int = 3 # Days ahead to check
ALERT_CHECK_ENABLED: bool = True
# 🆕 Phase 3: ML Forecasting Configuration
FORECAST_DEFAULT_DAYS: int = 7 # Default forecast horizon
FORECAST_MIN_DATA_POINTS: int = 4 # Minimum data required
FORECAST_CACHE_HOURS: int = 24 # Cache duration
ML_MODEL_AUTO_SELECT: bool = True # Auto-choose best modelEnvironment Variables (.env or .env.local):
REACT_APP_API_URL=http://localhost:8000Tailwind Config (tailwind.config.js):
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {
colors: {
primary: {
DEFAULT: '#4F46E5',
dark: '#4338CA'
}
}
}
}
}- Navigate to Upload page (
/upload) - Click "Click to upload" or drag and drop an image
- Supported formats: JPG, JPEG, PNG (max 10MB)
- Click "Analyze Image"
- View results showing:
- Overall assessment
- Per-product stock levels
- Restocking priorities
- Detailed observations
curl -X POST "http://localhost:8000/api/v1/analyze/" \
-F "file=@path/to/shelf_image.jpg"- Navigate to Dashboard page (
/dashboard) - View real-time data:
- Alerts: Products needing attention (red indicators)
- Statistics: Total products, analyses, alerts
- Stock Chart: Visual representation of stock levels
- Product Grid: All products with status indicators
- Navigate to History page (
/history) - Use search filters:
- Product Name: Filter by specific product
- Analysis ID: Find specific analysis
- Assessment: Search in overall assessment text
- Sort by newest/oldest
- Click analysis cards to view details
curl -X POST "http://localhost:8000/api/v1/products/?store_id=1" \
-H "Content-Type: application/json" \
-d '{
"name": "mango",
"display_name": "Fresh Mangoes",
"description": "Sweet tropical mangoes",
"optimal_stock_level": 75
}'curl -X PUT "http://localhost:8000/api/v1/products/1" \
-H "Content-Type: application/json" \
-d '{
"optimal_stock_level": 85
}'curl -X POST "http://localhost:8000/api/v1/stores/" \
-H "Content-Type: application/json" \
-d '{
"store_code": "STORE001",
"store_name": "Downtown Market",
"location": "Downtown",
"address": "123 Main St",
"phone": "555-0100"
}'curl -X POST "http://localhost:8000/api/v1/forecast/generate?product_id=1&days_ahead=7"Response:
{
"status": "success",
"product_id": 1,
"product_name": "banana",
"forecast_data": [
{
"date": "2025-11-04T00:00:00",
"predicted_level": 42.5,
"confidence_lower": 35.2,
"confidence_upper": 49.8
}
],
"historical_data": [...],
"model_used": "exponential_smoothing",
"accuracy_estimate": "±10-15%",
"data_points_used": 12
}curl -X POST "http://localhost:8000/api/v1/forecast/batch?days_ahead=7"This runs in the background and generates forecasts for all products.
curl "http://localhost:8000/api/v1/forecast/product/1?include_history=true"- Navigate to Dashboard page
- Scroll to ML Stock Forecasts section
- Select a product from the sidebar
- View interactive forecast chart with:
- Historical stock levels (blue line)
- 7-day predictions (green dashed line)
- 95% confidence intervals (shaded area)
- Click Regenerate to update forecast
- Click Generate All Forecasts to batch-generate for all products
curl "http://localhost:8000/api/v1/alerts/unacknowledged"Response:
[
{
"id": 15,
"product_id": 3,
"product_name": "banana",
"product_display_name": "Fresh Bananas",
"alert_type": "critical",
"alert_level": "critical",
"message": "Fresh Bananas is critically low at 18%. Immediate restocking required.",
"stock_level": 18.0,
"is_acknowledged": false,
"created_at": "2025-11-03T14:30:00Z"
}
]curl -X POST "http://localhost:8000/api/v1/alerts/15/acknowledge"curl -X POST "http://localhost:8000/api/v1/alerts/check?check_forecasts=true"curl "http://localhost:8000/api/v1/alerts/statistics"Response:
{
"total_alerts": 45,
"unacknowledged": 8,
"critical": 3,
"low": 4,
"forecast_warning": 1,
"recent_24h": 12
}- Automatic: Toast notifications appear automatically for new alerts
- Dashboard: Alert counts and status shown in dashboard stats
- Alerts are automatically checked after each image analysis
curl "http://localhost:8000/api/v1/export/stock-history?start_date=2025-10-01&end_date=2025-11-03&store_id=1" -o stock_history.csvcurl "http://localhost:8000/api/v1/export/forecast/1" -o banana_forecast.csvDownloaded CSV files include:
- Stock History: timestamp, product details, stock levels, confidence scores, observations
- Forecast Data: forecast dates, predicted levels, confidence intervals, model type
AISLEAI uses an adaptive model selection approach based on available historical data:
def select_model(data_points):
if data_points >= 7:
return "Exponential Smoothing" # Best accuracy
elif data_points >= 4:
return "Linear Trend" # Medium accuracy
else:
return "Moving Average" # Basic fallbackBest For: Products with consistent historical data
Algorithm:
- Uses Holt's method with damped trend
- Automatically adjusts to recent trends
- Dampening prevents over-extrapolation
- Implemented via
statsmodels.tsa.holtwinters.ExponentialSmoothing
Accuracy: ±10-15% (14+ data points) or ±15-20% (7-13 data points)
Configuration:
model = ExponentialSmoothing(
data,
trend='add', # Additive trend component
seasonal=None, # No seasonal component (not enough data)
damped_trend=True, # Prevent over-extrapolation
initialization_method='estimated'
)Best For: Products with limited data showing clear trends
Algorithm:
- Simple linear regression:
y = mx + b - Calculates slope and intercept from historical data
- Predictions clamped to [0, 100] range
- Implemented via
scipy.stats.linregress
Accuracy: ±15-20% (7+ data points) or ±20-25% (4-6 data points)
Confidence Intervals: Based on residual standard deviation
Best For: New products with minimal history
Algorithm:
- Average of last 3 data points (or all if <3)
- Flat forecast (no trend)
- Wide confidence intervals (±2σ) due to uncertainty
Accuracy: ±25-30%
Use Case: Provides baseline predictions until more data is collected
All models provide 95% confidence intervals calculated as:
Upper Bound = Prediction + (1.96 × σ)
Lower Bound = Prediction - (1.96 × σ)
Where σ is the standard deviation of:
- Historical data (Exponential Smoothing)
- Regression residuals (Linear Trend)
- Data variance (Moving Average)
- Generation: Triggered manually or via batch processing
- Storage: Saved to
stock_forecaststable - Caching: 24-hour cache to reduce computation
- Validation: Compared with actual data for accuracy tracking
- Regeneration: Updated when new analysis data arrives
- Batch Processing: Background tasks for generating multiple forecasts
- Lazy Loading: Forecasts generated on-demand
- Index Optimization: Database indexes on
product_id+forecast_date - Model Fallback: Automatic fallback to simpler models on errors
cd backend
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export DATABASE_URL="postgresql://aisleai:password@localhost:5432/aisleai"
export GEMINI_API_KEY="your_api_key"
# Run migrations
alembic upgrade head
# Start server
uvicorn app.main:app --reload --port 8000- Create schema in
app/schemas/ - Add endpoint in
app/api/v1/ - Register router in
app/main.py
Example:
# app/api/v1/my_endpoint.py
from fastapi import APIRouter
router = APIRouter()
@router.get("/my-endpoint")
async def my_endpoint():
return {"message": "Hello"}
# app/main.py
from app.api.v1 import my_endpoint
app.include_router(my_endpoint.router, prefix="/api/v1")# Auto-generate migration
docker-compose exec backend alembic revision --autogenerate -m "Add new column"
# Apply migration
docker-compose exec backend alembic upgrade head
# Rollback
docker-compose exec backend alembic downgrade -1cd frontend
# Install dependencies
npm install
# Set environment
echo "REACT_APP_API_URL=http://localhost:8000" > .env.local
# Start development server
npm start- Create component in
src/pages/ - Add route in
App.tsx - Update navigation in
Header.tsx
Example:
// src/pages/NewPage.tsx
export const NewPage = () => {
return <div>New Page Content</div>;
};
// App.tsx
import { NewPage } from './pages/NewPage';
<Route path="/new-page" element={<NewPage />} />// src/hooks/useMyHook.ts
import { useState, useEffect } from 'react';
export const useMyHook = () => {
const [data, setData] = useState(null);
useEffect(() => {
// Fetch data
}, []);
return { data };
};Both frontend and backend support hot reload:
- Backend: Uvicorn
--reloadflag watches.pyfiles - Frontend: React Fast Refresh watches
.tsx,.ts,.cssfiles
Changes are reflected automatically without container restart.
# Run all tests
docker-compose exec backend pytest
# Run with coverage
docker-compose exec backend pytest --cov=app
# Run specific test file
docker-compose exec backend pytest tests/test_gemini.py
# Run with verbose output
docker-compose exec backend pytest -v# Run all tests
docker-compose exec frontend npm test
# Run with coverage
docker-compose exec frontend npm test -- --coverage
# Run in watch mode
docker-compose exec frontend npm test -- --watchdocker-compose exec backend python test_gemini.pydocker-compose exec backend python list_models.pyProblem: Docker containers fail to start
Solutions:
# Check if ports are in use
netstat -an | grep "3000\|8000\|5432"
# Stop all containers and remove volumes
docker-compose down -v
# Rebuild and restart
docker-compose build --no-cache
docker-compose up -dProblem: Backend can't connect to PostgreSQL
Solutions:
# Check database is running
docker-compose ps db
# Check database logs
docker-compose logs db
# Recreate database
docker-compose down -v
docker-compose up -d db
docker-compose exec backend alembic upgrade headProblem: "Invalid API key" or "403 Forbidden"
Solutions:
- Verify API key in
.envfile - Check API key is enabled at Google AI Studio
- Ensure no extra spaces in API key
- Test API key:
docker-compose exec backend python test_gemini.py
Problem: "Network Error" or "CORS Error"
Solutions:
# Check backend is running
curl http://localhost:8000/api/v1/health
# Verify environment variable
docker-compose exec frontend printenv | grep REACT_APP_API_URL
# Check CORS settings in backend/app/core/config.pyProblem: "Rate limit exceeded" error
Solutions:
- Wait for rate limit window to reset (check
/analyze/rate-limit-status) - Adjust limits in
.env:GEMINI_MAX_RPM=15 GEMINI_MAX_RPD=1500
- The system will automatically wait and retry
Problem: "File too large" or "Invalid format"
Solutions:
- Check file size (max 10MB)
- Verify file format (JPG, JPEG, PNG only)
- Ensure
uploads/directory has write permissions - Check logs:
docker-compose logs backend | grep -i error
Enable debug logging:
Backend:
# In backend/app/main.py
import logging
logging.basicConfig(level=logging.DEBUG)Frontend:
// In frontend/src/services/api.ts
// Uncomment console.log statements in interceptors# All services
docker-compose logs -f
# Specific service
docker-compose logs -f backend
docker-compose logs -f frontend
docker-compose logs -f db
# Last 100 lines
docker-compose logs --tail=100 backend
# With timestamps
docker-compose logs -t backend- PDF and Excel export formats
- Email notifications for critical alerts
- SMS notifications integration
- Scheduled reports (daily/weekly/monthly)
- Custom dashboard layouts per store
- Advanced filtering and data visualization
- Support for 50+ product categories
- Shelf arrangement optimization suggestions
- Spoilage detection (bruising, overripe produce)
- Price tag verification
- Competitor price monitoring
- POS system integration
- Automated reordering based on stock levels
- Supplier integration API
- Mobile app (iOS/Android)
- Barcode/QR code scanning
- Role-based access control (RBAC)
- Multi-tenant architecture
- SSO integration (OAuth, SAML)
- Audit logging and compliance
- Custom branding/white-labeling
- Redis caching for frequently accessed data
- Image CDN integration
- Database query optimization
- WebSocket for real-time updates
- Progressive Web App (PWA)
- CI/CD pipeline (GitHub Actions)
- Kubernetes deployment manifests
- Automated testing in pipeline
- Performance monitoring (Prometheus/Grafana)
- Log aggregation (ELK stack)
# Start all services
docker-compose up -d
# Stop all services
docker-compose down
# Restart specific service
docker-compose restart backend
# Rebuild service
docker-compose build backend --no-cache
# View running containers
docker-compose ps
# Remove all containers and volumes
docker-compose down -v# View logs (all services)
docker-compose logs -f
# View logs (specific service)
docker-compose logs -f backend
# Last N lines
docker-compose logs --tail=50 backend
# Access container shell
docker-compose exec backend bash
docker-compose exec frontend sh
docker-compose exec db psql -U aisleai -d aisleai# Connect to PostgreSQL
docker-compose exec db psql -U aisleai -d aisleai
# Backup database
docker-compose exec db pg_dump -U aisleai aisleai > backup.sql
# Restore database
docker-compose exec -T db psql -U aisleai aisleai < backup.sql
# Reset database
docker-compose down -v
docker-compose up -d
docker-compose exec backend alembic upgrade head
docker-compose exec backend python seed_data.pyThis is a capstone project for CSE3CAP, Semester 6.
- Create feature branch:
git checkout -b feature/your-feature - Make changes and test locally
- Commit with descriptive messages
- Push and create pull request
- Code review and merge
Capstone Project - CSE3CAP Semester 6
This project is developed as part of the Computer Science Engineering capstone course.
Project: AISLEAI - AI-Powered Stock Level Estimation Course: CSE3CAP - Capstone Project Semester: 6
- FastAPI Documentation
- React Documentation
- Google Gemini API Docs
- Docker Documentation
- PostgreSQL Documentation
- Tailwind CSS Documentation
For issues, questions, or contributions:
- Check Troubleshooting section
- Review API documentation at
http://localhost:8000/docs - Check Docker logs:
docker-compose logs -f
Version 2.0.0 introduces comprehensive ML-powered analytics and intelligent alerting capabilities:
- 3 Adaptive ML Models: Automatically selects best model based on data availability
- 7-Day Predictions: Forecast stock levels up to 30 days ahead
- 95% Confidence Intervals: Quantified prediction uncertainty
- Batch Processing: Generate forecasts for all products simultaneously
- Real-time Accuracy Tracking: Compare predictions vs actual stock levels
- Interactive Visualizations: Chart.js powered forecast charts with historical comparison
- Multi-Level Alerts: Critical (≤20%), Low (≤40%), Forecast Warning (≤30% predicted)
- Configurable Rules: Per-store customizable threshold rules
- Smart Deduplication: Prevents alert spam with time-window based filtering
- Real-time Notifications: Toast notifications via react-toastify integration
- Background Checking: Automatic alert generation after each analysis
- Comprehensive Management: Acknowledge individual or bulk alerts
- CSV Export: Download stock history with flexible date/product filtering
- Forecast Reports: Export ML predictions with confidence intervals
- Timestamped Files: Auto-formatted filenames for easy organization
- Analysis History: Complete historical data export capability
- New Database Tables:
stock_forecasts,alerts,alert_ruleswith optimized indexes - New API Endpoints: 20+ new endpoints for forecasting, alerts, and exports
- New Services:
ForecastingService,AlertService,CSVExporter - Frontend Components:
ForecastChart,ForecastsSection,NotificationContext - Dependencies Added: statsmodels, scipy, numpy, react-toastify, date-fns
- Forecast Accuracy: ±10-30% depending on data availability and model
- Alert Response Time: <100ms for real-time notification delivery
- Batch Processing: Generates forecasts for 50+ products in <30 seconds
- Database Performance: Optimized with 8+ new indexes for query speed
- Cache Optimization: 24-hour forecast caching reduces computation by 95%
- Proactive Insights: Forecast warnings alert 3 days before predicted stockouts
- Visual Clarity: Color-coded alerts and confidence interval visualization
- Reduced Manual Work: Automated batch forecasting eliminates repetitive tasks
- Export Flexibility: CSV downloads for Excel analysis and external reporting
- Real-time Updates: 30-second polling keeps dashboard always current
Last Updated: November 3, 2025 Version: 2.0.0 Status: ✅ Phase 3 Complete - ML-Enhanced Analytics Fully Operational
Contributors: CSE3CAP Team, Semester 6 Technologies: Python, TypeScript, React, FastAPI, PostgreSQL, Machine Learning (statsmodels, scipy)