File size: 5,159 Bytes
ce699d1
7de41d7
 
 
ce699d1
 
7de41d7
ce699d1
 
7de41d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Emotion Detector API
emoji: 🎧
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
---

# 🎧 Emotion Detector API

Professional RESTful API for emotion recognition in speech using the fine-tuned HuBERT model: **abedir/emotion-detector**

## πŸš€ Quick Start

### Health Check
```bash
curl https://YOUR-SPACE-NAME.hf.space/health
```

### Predict Emotion
```bash
curl -X POST "https://YOUR-SPACE-NAME.hf.space/predict" \
  -F "file=@audio.wav"
```

### Python Example
```python
import requests

# Predict emotion
url = "https://YOUR-SPACE-NAME.hf.space/predict"
files = {"file": open("audio.wav", "rb")}
response = requests.post(url, files=files)
result = response.json()

print(f"Emotion: {result['emotion']}")
print(f"Confidence: {result['confidence']:.2%}")
```

## 🎯 Supported Emotions

1. **Angry/Fearful** - Expressions of anger or fear
2. **Happy/Laugh** - Joyful or laughing expressions
3. **Neutral/Calm** - Neutral or calm speech
4. **Sad/Cry** - Expressions of sadness or crying
5. **Surprised/Amazed** - Surprised or amazed reactions

## πŸ“‘ API Endpoints

### Core Endpoints
- `GET /` - API welcome and version info
- `GET /health` - Health check with system status
- `GET /docs` - **Interactive API documentation (Swagger UI)**
- `GET /redoc` - Alternative API documentation
- `GET /model/info` - Model configuration details
- `GET /emotions` - List of supported emotions
- `GET /stats` - API and system statistics
- `GET /version` - API version information

### Prediction Endpoints
- `POST /predict` - Basic emotion prediction
- `POST /predict/detailed` - Prediction with audio metadata
- `POST /predict/base64` - Predict from base64 encoded audio
- `POST /predict/batch` - Batch processing (max 50 files)
- `POST /predict/top-k` - Get top K predictions
- `POST /predict/threshold` - Confidence-based prediction

### Analysis Endpoints
- `POST /analyze/audio` - Get audio metadata without prediction

## πŸ“¦ Response Format

```json
{
  "emotion": "Happy/Laugh",
  "confidence": 0.8745,
  "probabilities": {
    "Angry/Fearful": 0.0234,
    "Happy/Laugh": 0.8745,
    "Neutral/Calm": 0.0521,
    "Sad/Cry": 0.0178,
    "Surprised/Amazed": 0.0322
  }
}
```

## πŸ› οΈ Integration Examples

### cURL
```bash
# Basic prediction
curl -X POST "https://YOUR-SPACE-NAME.hf.space/predict" \
  -F "file=@audio.wav"

# Detailed prediction
curl -X POST "https://YOUR-SPACE-NAME.hf.space/predict/detailed" \
  -F "file=@audio.wav"

# Top 3 predictions
curl -X POST "https://YOUR-SPACE-NAME.hf.space/predict/top-k?k=3" \
  -F "file=@audio.wav"

# Batch prediction
curl -X POST "https://YOUR-SPACE-NAME.hf.space/predict/batch" \
  -F "files=@audio1.wav" \
  -F "files=@audio2.wav" \
  -F "files=@audio3.wav"
```

### Python
```python
import requests

BASE_URL = "https://YOUR-SPACE-NAME.hf.space"

# Basic prediction
with open("audio.wav", "rb") as f:
    response = requests.post(f"{BASE_URL}/predict", files={"file": f})
    result = response.json()
    print(f"Emotion: {result['emotion']}")
    print(f"Confidence: {result['confidence']:.2%}")

# Batch prediction
files = [
    ("files", open("audio1.wav", "rb")),
    ("files", open("audio2.wav", "rb")),
    ("files", open("audio3.wav", "rb"))
]
response = requests.post(f"{BASE_URL}/predict/batch", files=files)
results = response.json()
print(f"Processed {results['total_files']} files in {results['processing_time_seconds']:.2f}s")
```

### JavaScript
```javascript
// Using Fetch API
const formData = new FormData();
formData.append('file', audioFile);

fetch('https://YOUR-SPACE-NAME.hf.space/predict', {
    method: 'POST',
    body: formData
})
.then(response => response.json())
.then(data => {
    console.log('Emotion:', data.emotion);
    console.log('Confidence:', data.confidence);
});
```

## πŸ“š Documentation

After deployment, visit:
- **Swagger UI**: `/docs` - Interactive API testing
- **ReDoc**: `/redoc` - Beautiful API documentation

## πŸ”§ Technical Details

- **Model**: HuBERT (Hidden-Unit BERT)
- **Model ID**: abedir/emotion-detector
- **Sample Rate**: 16kHz (automatic resampling)
- **Max Duration**: 3 seconds
- **Supported Formats**: WAV, MP3, FLAC, OGG, M4A, WebM
- **Framework**: FastAPI + PyTorch + Transformers

## 🎯 Use Cases

βœ… Call center sentiment analysis  
βœ… Mental health monitoring  
βœ… Voice assistant emotion detection  
βœ… Gaming and entertainment  
βœ… Media content analysis  
βœ… Research in affective computing  

## 🚨 Error Handling

All errors return a consistent format:

```json
{
  "error": "Invalid file format",
  "detail": "Supported formats: .wav, .mp3, .flac, .ogg, .m4a, .webm",
  "timestamp": "2024-02-06T10:30:00"
}
```

HTTP Status Codes:
- `200` - Success
- `400` - Bad Request (invalid input)
- `422` - Validation Error
- `500` - Internal Server Error

## πŸ”— Related Links

- **Model**: [abedir/emotion-detector](https://huggingface.co/abedir/emotion-detector)
- **HuBERT Paper**: [arXiv:2106.07447](https://arxiv.org/abs/2106.07447)
- **FastAPI**: [Documentation](https://fastapi.tiangolo.com/)

## πŸ“„ License

Apache 2.0

---

**Built with ❀️ using HuBERT, FastAPI, and Transformers**