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bc18e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12f8480
 
 
bc18e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66e45b5
bc18e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12f8480
bc18e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# Deployment Guide - DanceDynamics

## 🚀 Deployment Options

This guide covers multiple deployment strategies for the DanceDynamics.

---

## Option 1: Docker Deployment (Recommended)

### Prerequisites
- Docker 20.10+
- Docker Compose 1.29+
- 4GB RAM minimum
- 10GB disk space

### Quick Start

```bash
# 1. Clone repository
git clone https://github.com/Prathameshv07/DanceDynamics
cd DanceDynamics

# 2. Build Docker image
docker-compose build

# 3. Run container
docker-compose up -d

# 4. Check status
docker-compose ps
docker-compose logs -f
```

### Access Application
- **Frontend**: http://localhost:7860
- **API Docs**: http://localhost:7860/api/docs
- **Health Check**: http://localhost:7860/health

### Docker Commands

```bash
# Start services
docker-compose up -d

# Stop services
docker-compose down

# View logs
docker-compose logs -f dance-analyzer

# Restart service
docker-compose restart

# Rebuild after code changes
docker-compose build --no-cache
docker-compose up -d

# Clean up
docker-compose down -v  # Removes volumes
docker system prune -a  # Clean unused images
```

---

## Option 2: Hugging Face Spaces

### Setup

1. **Create Space**
   - Go to https://huggingface.co/spaces
   - Click "Create new Space"
   - Choose Docker SDK
   - Name: DanceDynamics

2. **Prepare Files**

Create `Dockerfile` in root:
```dockerfile
FROM python:3.10-slim

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libgl1-mesa-glx libglib2.0-0 libsm6 \
    libxext6 libxrender-dev libgomp1 ffmpeg \
    && rm -rf /var/lib/apt/lists/*

# Copy and install Python dependencies
COPY backend/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application
COPY backend/app /app/app
COPY frontend /app/frontend

# Create directories
RUN mkdir -p /app/uploads /app/outputs

EXPOSE 7860

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
```

3. **Push to Space**
```bash
git init
git remote add space https://huggingface.co/spaces/prathameshv07/DanceDynamics
git add .
git commit -m "Initial deployment"
git push --force space main
```

4. **Configure Space**
   - Set visibility (Public/Private)
   - Add README with usage instructions
   - Configure hardware (CPU/GPU)

---

## Option 3: AWS EC2

### Launch Instance

1. **Choose AMI**: Ubuntu 22.04 LTS
2. **Instance Type**: t3.medium (2 vCPU, 4GB RAM) minimum
3. **Storage**: 20GB EBS volume
4. **Security Group**: 
   - SSH (22) from your IP
   - HTTP (80) from anywhere
   - Custom TCP (7860) from anywhere

### Setup Script

SSH into instance and run:

```bash
#!/bin/bash

# Update system
sudo apt-get update && sudo apt-get upgrade -y

# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker ubuntu

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Clone repository
git clone https://github.com/Prathameshv07/DanceDynamics
cd DanceDynamics

# Start services
docker-compose up -d

# Setup nginx reverse proxy (optional)
sudo apt-get install -y nginx
sudo tee /etc/nginx/sites-available/dance-analyzer << EOF
server {
    listen 80;
    server_name _;
    
    location / {
        proxy_pass http://localhost:7860;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
    }
    
    location /ws {
        proxy_pass http://localhost:7860;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
EOF

sudo ln -s /etc/nginx/sites-available/dance-analyzer /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```

### SSL Certificate (Let's Encrypt)

```bash
sudo apt-get install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
```

---

## Option 4: Google Cloud Run

### Prerequisites
- Google Cloud account
- gcloud CLI installed

### Deployment

```bash
# 1. Set project
gcloud config set project YOUR_PROJECT_ID

# 2. Build and push image
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/dance-analyzer

# 3. Deploy to Cloud Run
gcloud run deploy dance-analyzer \
  --image gcr.io/YOUR_PROJECT_ID/dance-analyzer \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated \
  --memory 2Gi \
  --timeout 300s \
  --max-instances 10
```

---

## Option 5: DigitalOcean App Platform

### Setup

1. **Create App**
   - Go to DigitalOcean App Platform
   - Connect GitHub repository
   - Select branch

2. **Configure Build**
   - Build Command: `docker build -t dance-analyzer .`
   - Run Command: `uvicorn app.main:app --host 0.0.0.0 --port 8080`

3. **Environment Variables**
```
API_HOST=0.0.0.0
API_PORT=8080
MAX_FILE_SIZE=104857600
```

4. **Resources**
   - Basic: 1 GB RAM, 1 vCPU
   - Pro: 2 GB RAM, 2 vCPU (recommended)

---

## Environment Variables

### Production Configuration

```bash
# API Settings
API_HOST=0.0.0.0
API_PORT=7860
DEBUG=false

# Security
CORS_ORIGINS=https://yourdomain.com,https://www.yourdomain.com

# File Limits
MAX_FILE_SIZE=104857600
MAX_VIDEO_DURATION=60

# Processing
MEDIAPIPE_MODEL_COMPLEXITY=1
MEDIAPIPE_MIN_DETECTION_CONFIDENCE=0.5
MAX_WORKERS=2

# Storage
UPLOAD_DIR=/app/uploads
OUTPUT_DIR=/app/outputs
LOG_DIR=/app/logs

# Session Management
SESSION_CLEANUP_INTERVAL=3600
MAX_SESSIONS=50
```

---

## Performance Optimization

### 1. Increase Workers

```yaml
# docker-compose.yml
environment:
  - MAX_WORKERS=4  # Increase for more concurrent processing
```

### 2. Use GPU (if available)

```dockerfile
# Dockerfile
FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04

# Install TensorFlow GPU
RUN pip install tensorflow-gpu mediapipe-gpu
```

### 3. Enable Caching

```python
# app/config.py
CACHE_ENABLED = True
CACHE_DIR = "/app/cache"
CACHE_MAX_SIZE = 10737418240  # 10GB
```

### 4. CDN for Static Files

```nginx
# nginx.conf
location /static/ {
    alias /app/frontend/;
    expires 30d;
    add_header Cache-Control "public, immutable";
}
```

---

## Monitoring & Logging

### 1. Health Checks

```bash
# Check application health
curl http://localhost:7860/health

# Monitor logs
docker-compose logs -f --tail=100
```

### 2. Prometheus Metrics

Add to `main.py`:
```python
from prometheus_fastapi_instrumentator import Instrumentator

Instrumentator().instrument(app).expose(app)
```

### 3. Log Aggregation

```yaml
# docker-compose.yml
logging:
  driver: "json-file"
  options:
    max-size: "10m"
    max-file: "3"
```

---

## Backup & Recovery

### Backup Data

```bash
# Backup uploads and outputs
docker run --rm \
  -v DanceDynamics_uploads:/uploads \
  -v DanceDynamics_outputs:/outputs \
  -v $(pwd)/backup:/backup \
  alpine \
  tar czf /backup/data-$(date +%Y%m%d).tar.gz /uploads /outputs
```

### Restore Data

```bash
# Restore from backup
docker run --rm \
  -v DanceDynamics_uploads:/uploads \
  -v DanceDynamics_outputs:/outputs \
  -v $(pwd)/backup:/backup \
  alpine \
  tar xzf /backup/data-YYYYMMDD.tar.gz -C /
```

---

## Security Best Practices

### 1. Use Secrets

```yaml
# docker-compose.yml
secrets:
  api_key:
    file: ./secrets/api_key.txt

services:
  dance-analyzer:
    secrets:
      - api_key
```

### 2. Enable HTTPS

```python
# main.py
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware

app.add_middleware(HTTPSRedirectMiddleware)
```

### 3. Rate Limiting

```python
from slowapi import Limiter
from slowapi.util import get_remote_address

limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter

@app.post("/api/upload")
@limiter.limit("5/minute")
async def upload_video():
    pass
```

### 4. Input Validation

All inputs are validated. Ensure:
- File size limits enforced
- File types restricted
- Path traversal prevented
- SQL injection not applicable (no DB)

---

## Troubleshooting

### Container Won't Start

```bash
# Check logs
docker-compose logs dance-analyzer

# Common issues:
# 1. Port already in use
docker ps -a
sudo lsof -i :7860

# 2. Permission denied
sudo chown -R 1000:1000 uploads outputs logs

# 3. Out of memory
docker stats
# Increase memory limit in docker-compose.yml
```

### High CPU Usage

```bash
# Check resource usage
docker stats dance-analyzer

# Reduce model complexity
# Edit docker-compose.yml
environment:
  - MEDIAPIPE_MODEL_COMPLEXITY=0
```

### Slow Processing

```bash
# Increase workers
environment:
  - MAX_WORKERS=4

# Use GPU if available
# Requires nvidia-docker
```

---

## Scaling

### Horizontal Scaling

```yaml
# docker-compose.yml
services:
  dance-analyzer:
    deploy:
      replicas: 3
    
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
    depends_on:
      - dance-analyzer
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
```

### Load Balancer Configuration

```nginx
upstream dance_analyzer {
    least_conn;
    server dance-analyzer-1:7860;
    server dance-analyzer-2:7860;
    server dance-analyzer-3:7860;
}

server {
    listen 80;
    
    location / {
        proxy_pass http://dance_analyzer;
    }
}
```

---

## Cost Optimization

### Cloud Costs

| Platform | Cost/Month | Notes |
|----------|-----------|-------|
| Hugging Face Spaces | Free - $15 | Good for demos |
| AWS EC2 t3.medium | $30 - $35 | Pay for compute |
| Google Cloud Run | $10 - $50 | Pay per use |
| DigitalOcean App | $12 - $24 | Fixed pricing |

### Optimization Tips

1. **Use spot instances** (AWS, GCP)
2. **Auto-scaling** based on demand
3. **Session cleanup** to free resources
4. **Caching** to reduce processing
5. **CDN** for static files

---

## CI/CD Pipeline

### GitHub Actions

Create `.github/workflows/deploy.yml`:

```yaml
name: Deploy

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v3
      
      - name: Build and push Docker image
        run: |
          docker build -t dance-analyzer .
          echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
          docker tag dance-analyzer ${{ secrets.DOCKER_USERNAME }}/dance-analyzer:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/dance-analyzer:latest
      
      - name: Deploy to server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            cd /app/DanceDynamics
            docker-compose pull
            docker-compose up -d
```

---

## Maintenance

### Regular Tasks

```bash
# Weekly: Clean old sessions
docker exec dance-analyzer python -c "
from app.utils import cleanup_old_sessions
cleanup_old_sessions(max_age_hours=168)
"

# Monthly: Update dependencies
docker-compose build --no-cache
docker-compose up -d

# As needed: Backup data
./scripts/backup.sh
```

---

## Support & Monitoring

### Set Up Alerts

```yaml
# docker-compose.yml
services:
  dance-analyzer:
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7860/health"]
      interval: 30s
      timeout: 10s
      retries: 3
```

### Monitor Metrics

- Response time
- Error rate
- Active sessions
- Memory usage
- Disk space

---

## Success Checklist

- [ ] Application builds successfully
- [ ] Docker container runs
- [ ] Health check passes
- [ ] Can upload video
- [ ] Processing works
- [ ] Can download result
- [ ] HTTPS configured (production)
- [ ] Monitoring set up
- [ ] Backups configured
- [ ] Documentation updated

---

## Next Steps

1. **Test deployment** thoroughly
2. **Set up monitoring**
3. **Configure backups**
4. **Optimize performance**
5. **Scale as needed**

---