swara-api / README.md
Zakha123-cyber
Initial deployment: SWARA API with eye tracking, facial expression, and gesture detection
8e73bed
|
raw
history blame
9.57 kB
metadata
title: SWARA - AI Public Speaking Evaluation
emoji: 🎀
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860

🎀 SWARA API - AI-Powered Public Speaking Evaluation

API backend untuk sistem evaluasi public speaking berbasis AI.

πŸ“‹ Fitur

  • βœ… Async Video Processing - Non-blocking video analysis dengan RQ (Redis Queue)
  • βœ… Multi-Model AI - Eye tracking, facial expression, gesture detection
  • βœ… Level-based Evaluation - 5 level kesulitan dengan indikator berbeda
  • βœ… RESTful API - FastAPI dengan OpenAPI documentation
  • βœ… Cloud Redis - Upstash Redis untuk production
  • βœ… Progress Tracking - Real-time progress updates untuk analysis

πŸ—οΈ Arsitektur

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Docker Container                β”‚
β”‚                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   FastAPI    │───▢│    Redis     β”‚  β”‚
β”‚  β”‚ (Port 7860)  β”‚    β”‚ (Queue & KV) β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚         β”‚                    β”‚          β”‚
β”‚         β”‚ POST /analyze      β”‚          β”‚
β”‚         β”‚ return task_id     β”‚          β”‚
β”‚         β”‚                    β–Ό          β”‚
β”‚         β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚         β”‚           β”‚  RQ Worker   β”‚   β”‚
β”‚         β”‚           β”‚ (Background) β”‚   β”‚
β”‚         β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚         β”‚                    β”‚          β”‚
β”‚         β”‚ GET /task/{id}     β”‚          β”‚
β”‚         β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Docker & Docker Compose
  • Python 3.10+ (untuk development tanpa Docker)

1. Clone & Setup

# Clone repository (if applicable)
cd API-MODEL

# Copy environment file
cp .env.example .env

# Edit .env jika perlu (optional untuk local development)

2. Run dengan Docker Compose

# Build dan start semua services
docker-compose up --build

# Atau run di background
docker-compose up -d --build

# Lihat logs
docker-compose logs -f

# Stop services
docker-compose down

API akan tersedia di: http://localhost:7860

3. Akses Documentation

πŸ“– API Endpoints

1. Health Check

GET /health

Response:

{
  "status": "healthy",
  "version": "1.0.0",
  "redis_connected": true,
  "timestamp": "2025-11-10T10:00:00"
}

2. Upload Video untuk Analysis

POST /api/v1/analyze
Content-Type: multipart/form-data

Parameters:

  • video (file): Video file (max 50MB, max 1 minute)
  • level (int): Level 1-5
  • user_id (string, optional): User identifier

Response:

{
  "task_id": "abc123def456",
  "status": "pending",
  "message": "Video uploaded successfully. Processing has been queued.",
  "created_at": "2025-11-10T10:00:00"
}

3. Get Task Status

GET /api/v1/task/{task_id}

Response (Processing):

{
  "task_id": "abc123def456",
  "status": "processing",
  "progress": {
    "current_step": "processing",
    "percentage": 45.5,
    "message": "Analyzing facial expressions..."
  },
  "created_at": "2025-11-10T10:00:00"
}

Response (Completed):

{
  "task_id": "abc123def456",
  "status": "completed",
  "result": {
    "level": 2,
    "video_metadata": {
      "duration": 58.5,
      "fps": 30,
      "resolution": "1920x1080",
      "file_size": 15728640
    },
    "main_indicators": {
      "kontak_mata": {
        "score": 4,
        "raw_data": {...}
      }
    },
    "bonus_indicators": {
      "first_impression": {
        "detected": true,
        "raw_data": {...}
      },
      "face_expression": {...},
      "gesture": {...}
    },
    "processing_time": 42.3
  },
  "created_at": "2025-11-10T10:00:00",
  "completed_at": "2025-11-10T10:01:00"
}

4. Delete Task

DELETE /api/v1/task/{task_id}

πŸ§ͺ Testing dengan cURL

Upload Video

curl -X POST "http://localhost:7860/api/v1/analyze" `
  -F "video=@test_video.mp4" `
  -F "level=2" `
  -F "user_id=user123"

Check Status

curl "http://localhost:7860/api/v1/task/abc123def456"

πŸ› οΈ Development Setup (Tanpa Docker)

1. Install Dependencies

# Create virtual environment
python -m venv venv
.\venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

2. Setup Redis (Local atau Upstash)

Option A: Local Redis (dengan Docker)

docker run -d -p 6379:6379 redis:7-alpine

Option B: Upstash Redis (Gratis)

  1. Daftar di https://upstash.com
  2. Create Redis database
  3. Copy connection string ke .env:
REDIS_URL=redis://default:YOUR_PASSWORD@YOUR_ENDPOINT:6379

3. Run API Server

python -m app.main

4. Run Worker (Terminal terpisah)

python -m app.worker

πŸ“ Project Structure

API-MODEL/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py              # FastAPI app
β”‚   β”œβ”€β”€ config.py            # Configuration
β”‚   β”œβ”€β”€ models.py            # Pydantic models
β”‚   β”œβ”€β”€ tasks.py             # Background tasks
β”‚   β”œβ”€β”€ worker.py            # RQ worker
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ routes.py        # API endpoints
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ redis_client.py  # Redis connection
β”‚   β”‚   └── storage.py       # File storage
β”‚   └── services/
β”‚       β”œβ”€β”€ video_processor.py    # Main orchestrator
β”‚       β”œβ”€β”€ eye_tracking.py       # Eye tracking service
β”‚       β”œβ”€β”€ facial_expression.py  # Facial expression service
β”‚       └── gesture_detection.py  # Gesture detection service
β”œβ”€β”€ models/                  # AI model files
β”œβ”€β”€ temp/                    # Temporary video storage
β”œβ”€β”€ logs/                    # Application logs
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ requirements.txt
└── README.md

βš™οΈ Configuration

Edit .env file untuk konfigurasi:

# Environment
ENV=development

# Redis (Upstash atau local)
REDIS_URL=redis://localhost:6379

# API
API_HOST=0.0.0.0
API_PORT=7860

# Processing
MAX_VIDEO_SIZE_MB=50
MAX_VIDEO_DURATION_SECONDS=60
TASK_TIMEOUT_SECONDS=300

# Logging
LOG_LEVEL=INFO

πŸ“Š Monitoring

View Logs

# API logs
docker-compose logs -f api

# Worker logs
docker-compose logs -f worker

# Redis logs
docker-compose logs -f redis

# All logs
docker-compose logs -f

Check Redis Queue

# Connect to Redis container
docker exec -it swara-redis redis-cli

# Check queue length
LLEN swara:tasks

# View all keys
KEYS *

# Get task data
GET task:abc123def456

πŸ”§ Troubleshooting

Problem: Redis connection failed

Solution:

# Check Redis is running
docker-compose ps

# Restart Redis
docker-compose restart redis

# Check Redis logs
docker-compose logs redis

Problem: Worker not processing tasks

Solution:

# Check worker logs
docker-compose logs worker

# Restart worker
docker-compose restart worker

# Check if worker is running
docker-compose ps worker

Problem: Video file too large

Solution:

  • Compress video atau gunakan format yang lebih efisien
  • Increase MAX_VIDEO_SIZE_MB di .env

πŸ“ Next Steps (TODO)

Untuk complete implementation, Anda perlu:

  1. Refactor existing code ke services:

    • app/services/eye_tracking.py - dari eye_tracking_production.py
    • app/services/facial_expression.py - dari facial_expression.py
    • app/services/gesture_detection.py - dari gesture_detection.py
  2. Add audio processing:

    • app/services/audio_processor.py - untuk tempo, artikulasi, jeda
    • Speech-to-text integration
    • Kata pengisi & kata tidak senonoh detection
  3. Add NLP processing:

    • app/services/nlp_processor.py - untuk kesesuaian topik, struktur kalimat
    • Topic matching
    • Sentence structure analysis
  4. Optimization:

    • Implement frame sampling (5 fps instead of 30 fps)
    • Model loading optimization
    • Memory management
  5. Testing:

    • Unit tests
    • Integration tests
    • Load testing

🚒 Deployment ke HuggingFace Spaces

1. Create Space

  1. Go to https://huggingface.co/spaces
  2. Create new Space (Docker type)
  3. Clone repository

2. Prepare Files

# Add Dockerfile for HF Spaces
# (sudah ada di repository)

3. Setup Secrets

Di HuggingFace Space settings, add secrets:

  • REDIS_URL - Upstash Redis URL
  • ENV - production

4. Push & Deploy

git push origin main

HuggingFace akan auto-deploy!

πŸ“ž Support

Untuk pertanyaan atau issues, contact SWARA team.


Built with ❀️ by SWARA Team for LIDM Competition 2025