Full-stack web application for real-time student engagement monitoring with face recognition, emotion detection, and live metrics.
- Backend: FastAPI (Python 3.10+) with async PostgreSQL and Redis
- Frontend: React + Vite with Tailwind CSS
- Database: PostgreSQL for persistent storage
- Cache/PubSub: Redis for real-time WebSocket broadcasting
- Containerization: Docker + Docker Compose
ai_sms/
βββ backend/ # FastAPI backend
β βββ app/
β β βββ api/ # API endpoints
β β βββ services/ # Background services
β β βββ migrations/ # Database migrations
β β βββ main.py # App entry point
β β βββ config.py # Configuration
β β βββ db.py # Database helpers
β β βββ models.py # Pydantic models
β βββ Dockerfile
β βββ requirements.txt
βββ web/ # React frontend
β βββ src/
β β βββ pages/ # Page components
β β βββ components/ # Reusable components
β β βββ api.js # API client
β β βββ ws.js # WebSocket client
β βββ Dockerfile
β βββ package.json
βββ src/ # Existing capture code
βββ models/ # PyTorch models
βββ data/ # Data storage
βββ docker-compose.yml
βββ README.md
- Docker and Docker Compose installed
- Your
emotion_model.ptin themodels/folder
cd ai_sms
cp .env.example .env
# Edit .env with your configurationdocker-compose up -dThis will start:
- PostgreSQL (port 5432)
- Redis (port 6379)
- Backend API (port 8001)
- Frontend Web (port 3000)
- Frontend: http://localhost:3000
- Backend API: http://localhost:8001
- API Docs: http://localhost:8001/docs
- Default Login: [email protected] / admin123
docker-compose down- Create Virtual Environment
cd backend
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install Dependencies
pip install -r requirements.txt- Setup Database
# Install PostgreSQL locally or use Docker for just the database
docker run -d \
--name aisms_postgres \
-e POSTGRES_DB=aismsdb \
-e POSTGRES_USER=aismsuser \
-e POSTGRES_PASSWORD=aismspass \
-p 5432:5432 \
postgres:15-alpine
# Run migrations
psql -h localhost -U aismsuser -d aismsdb -f app/migrations/001_create_tables.sql- Setup Redis
# Install Redis locally or use Docker
docker run -d --name aisms_redis -p 6379:6379 redis:7-alpine- Set Environment Variables
export DATABASE_URL="postgresql://aismsuser:aismspass@localhost:5432/aismsdb"
export REDIS_URL="redis://localhost:6379"
export SECRET_KEY="your-secret-key"
export INGEST_API_KEY="your-ingest-key"
export MODEL_PATH="../models/emotion_model.pt"- Run Backend
cd backend
uvicorn app.main:app --host 0.0.0.0 --port 8001 --reload- Install Dependencies
cd web
npm install- Set Environment Variables
Create
web/.env:
VITE_API_BASE=http://localhost:8001
VITE_WS_BASE=ws://localhost:8001
- Run Frontend
npm run devFrontend will be available at http://localhost:3000
# Connect to PostgreSQL
psql -h localhost -U aismsuser -d aismsdb
# Run migration file
\i backend/app/migrations/001_create_tables.sqlMigrations run automatically when starting with docker-compose.
- Email:
[email protected] - Password:
admin123
curl -X POST http://localhost:8001/auth/signup \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "secure-password",
"full_name": "John Doe"
}'curl -X POST http://localhost:8001/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "admin123"
}'POST /auth/signup- Register new userPOST /auth/login- Login and get JWT tokenGET /auth/me- Get current user info
GET /enroll- Enrollment form pagePOST /enroll- Upload student photosGET /students- List enrolled students
POST /ingest- Ingest events from edge devices (requires X-API-KEY header)
GET /students/{id}/metrics- Get student metricsGET /classes/{device_id}/overview- Get class overviewGET /dashboard/summary- Get dashboard summary
WS /ws/live?room={device_id}- Live event feed
Your edge capture script should POST events to /ingest:
import requests
import json
event_data = {
"timestamp": "2024-01-15T10:30:00Z",
"student_id": "12345",
"face_match_confidence": 0.95,
"emotion": "Happy",
"emotion_confidence": 0.87,
"probabilities": {
"Happy": 0.87,
"Neutral": 0.10,
"Sad": 0.03
},
"metrics": {
"engagement": 0.75,
"boredom": 0.12,
"frustration": 0.05
},
"ear": 0.25,
"head_pose": {
"yaw": 5.2,
"pitch": -2.1,
"roll": 0.5
},
"source_device": "classroom_1"
}
response = requests.post(
"http://localhost:8001/ingest",
headers={"X-API-KEY": "your-ingest-key"},
json=event_data
)
print(response.json())events = [event1, event2, event3]
response = requests.post(
"http://localhost:8001/ingest",
headers={"X-API-KEY": "your-ingest-key"},
json={"events": events}
)| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://aismsuser:aismspass@localhost:5432/aismsdb |
REDIS_URL |
Redis connection string | redis://localhost:6379 |
SECRET_KEY |
JWT signing key | dev-secret-key |
INGEST_API_KEY |
API key for ingest endpoint | dev-ingest-key |
MODEL_PATH |
Path to emotion model | ./models/emotion_model.pt |
VITE_API_BASE |
Frontend API base URL | http://localhost:8001 |
VITE_WS_BASE |
Frontend WebSocket base URL | ws://localhost:8001 |
Computes per-minute aggregates and checks for alerts:
cd backend
python -m app.services.aggregatorOr use Celery for production:
celery -A app.services.aggregator worker --loglevel=infocd backend
docker build -t aisms-backend .
docker run -p 8001:8001 \
-e DATABASE_URL="..." \
-e REDIS_URL="..." \
-e SECRET_KEY="..." \
aisms-backendcd web
npm run build
# Serve dist/ folder with nginx or any static server# Check if PostgreSQL is running
docker ps | grep postgres
# Test connection
psql -h localhost -U aismsuser -d aismsdb# Check if Redis is running
docker ps | grep redis
# Test connection
redis-cli ping- Ensure Redis is running
- Check CORS settings in backend
- Verify WebSocket URL in frontend
- Ensure
data/known_encodings.jsonexists - Check that students are enrolled with photos
- Verify
face_recognitionlibrary is installed
MIT License - See LICENSE file for details
Contributions welcome! Please open an issue or submit a pull request.
For issues or questions, please open a GitHub issue.