File size: 8,391 Bytes
3e08670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Architecture et Documentation Technique

## ๐Ÿ—๏ธ Architecture de l'API

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      Client/Frontend                        โ”‚
โ”‚  (Web, Mobile, CLI, Python Client, cURL, etc.)             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ”‚ HTTP/REST
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Flask API Server                         โ”‚
โ”‚  - Health Check         GET /health                         โ”‚
โ”‚  - Documentation        GET /                               โ”‚
โ”‚  - Langues              GET /supported-languages            โ”‚
โ”‚  - ASR (Audioโ†’Text)     POST /asr                           โ”‚
โ”‚  - TTS (Textโ†’Audio)     POST /tts                           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ”‚                              โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  ASR Pipeline     โ”‚        โ”‚  TTS Pipeline      โ”‚
    โ”‚                   โ”‚        โ”‚                    โ”‚
    โ”‚ 1. Load Audio     โ”‚        โ”‚ 1. Validate Text   โ”‚
    โ”‚ 2. Process       โ”‚        โ”‚ 2. Load Model      โ”‚
    โ”‚ 3. Tokenize      โ”‚        โ”‚ 3. Tokenize        โ”‚
    โ”‚ 4. Infer w/ MMS  โ”‚        โ”‚ 4. Infer (VITS)    โ”‚
    โ”‚ 5. Decode        โ”‚        โ”‚ 5. Generate WAV    โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ”‚                              โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚         Model Cache & Management               โ”‚
    โ”‚  - facebook/mms-1b-all (ASR)                   โ”‚
    โ”‚  - facebook/mms-tts-* (8 langues)             โ”‚
    โ”‚  - Thread-safe loading                        โ”‚
    โ”‚  - Lazy initialization                        โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚   PyTorch / GPU Support                      โ”‚
    โ”‚  - Dรฉtection automatique GPU/CPU             โ”‚
    โ”‚  - Device management                        โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿ“Š Flow des requรชtes

### ASR (Automatic Speech Recognition)

```
Audio File
    โ†“
[Validation] โ†’ Error if invalid
    โ†“
[Load & Resample] โ†’ Convert to 16kHz mono
    โ†“
[Normalize] โ†’ [-1, 1] range
    โ†“
[Truncate] โ†’ Max 30 seconds
    โ†“
[Tokenize] โ†’ Convert to features
    โ†“
[Infer] โ†’ facebook/mms-1b-all (GPU/CPU)
    โ†“
[Decode] โ†’ Text output
    โ†“
JSON Response
```

### TTS (Text-to-Speech)

```
Text + Language
    โ†“
[Validation] โ†’ Error if empty/too long
    โ†“
[Load Model] โ†’ facebook/mms-tts-{lang}
    โ†“
[Tokenize] โ†’ Convert text to token IDs
    โ†“
[Infer] โ†’ VITS model (GPU/CPU)
    โ†“
[Generate WAV] โ†’ Audio synthesis (22050 Hz)
    โ†“
WAV File (audio/wav)
```

## ๐Ÿง  Modรจles utilisรฉs

### ASR: facebook/mms-1b-all

- **Architecture**: wav2vec2
- **Taille**: 964.8M parameters
- **Langues**: 100+ (ISO 639-3)
- **Input**: Audio 16kHz mono
- **Output**: Transcription texte
- **Entraรฎnement**: XLSL-R + Fine-tuning multilingual

### TTS: facebook/mms-tts-{language}

- **Architecture**: VITS (Variational Inference Text-to-Speech)
- **Taille**: ~5-10M parameters par modรจle
- **Langues**: 8 (voir supported languages)
- **Input**: Texte (max 1000 chars)
- **Output**: Waveform 22050 Hz
- **Entraรฎnement**: Multilingual dataset + data augmentation

## ๐Ÿ”ง Configuration

```python
SAMPLE_RATE = 16000           # Taux d'รฉchantillonnage ASR
MAX_AUDIO_LENGTH = 30         # Max 30 secondes d'audio
MAX_TEXT_LENGTH = 1000        # Max 1000 caractรจres
DEVICE = auto (GPU if available)
MODEL_CACHE = Thread-safe dict
```

## ๐Ÿ“ˆ Performance

| Mรฉtrique | Valeur |
|----------|--------|
| Premiรจre requรชte ASR | 2-5 min (chargement modรจle) |
| Requรชtes suivantes ASR | 1-10 sec (audio 10sec) |
| Premiรจre requรชte TTS | 30-60 sec (chargement modรจle) |
| Requรชtes suivantes TTS | 1-5 sec (100 chars) |
| Mรฉmoire GPU | ~2GB (ASR) + 1GB (TTS) |
| Mรฉmoire RAM | ~1GB cache |

## ๐Ÿ” Sรฉcuritรฉ

### Input Validation
- โœ… Vรฉrification type fichier audio
- โœ… Limitation taille audio (30s)
- โœ… Limitation taille texte (1000 chars)
- โœ… Vรฉrification contenu non-vide

### Rate Limiting (ร€ ajouter)
```python
from flask_limiter import Limiter
limiter = Limiter(app, key_func=lambda: request.remote_addr)

@app.route('/tts')
@limiter.limit("10/minute")
def tts():
    ...
```

### Authentication (ร€ ajouter)
```python
from functools import wraps
def require_token(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        token = request.headers.get('Authorization')
        if not validate_token(token):
            return {'error': 'Unauthorized'}, 401
        return f(*args, **kwargs)
    return decorated
```

## ๐Ÿš€ Optimisations

### Cache des modรจles
- Modรจles chargรฉs une seule fois
- Partage entre toutes les requรชtes
- Thread-safe avec locks

### GPU Acceleration
- Dรฉtection automatique GPU
- Inference sur GPU si disponible
- Fallback CPU automatique

### Memory Management
- Gradients dรฉsactivรฉs pour infรฉrence
- Modรจles en eval mode
- Audio / texte tronquรฉs

## ๐Ÿ“ฆ Dรฉploiement

### Local Development
```bash
python app_v2.py
# Runs on http://localhost:7860
```

### Docker
```bash
docker build -t mms-api .
docker run -p 7860:7860 mms-api
```

### Docker Compose (avec GPU)
```bash
docker-compose up
```

### Hugging Face Spaces
- Crรฉe un Space Docker
- Push code vers HF
- Auto-build et dรฉploiement
- URL: https://huggingface.co/spaces/{user}/{space}

## ๐Ÿ“ก API Endpoints

### GET /
Documentation et mรฉtadonnรฉes

### GET /health
ร‰tat du service et device info

### GET /supported-languages
Langues supportรฉes ASR/TTS

### GET /models-info
Infos dรฉtaillรฉes sur les modรจles

### POST /asr
Transcription audio
- **Input**: multipart/form-data (audio + language)
- **Output**: JSON (transcription + mรฉtadonnรฉes)

### POST /tts
Synthรจse vocale
- **Input**: JSON (text + language)
- **Output**: WAV audio file

## ๐Ÿ› Debugging

### Logs
```bash
# Local
python app_v2.py
# Voir les logs en stdout

# Docker
docker logs <container_id>

# HF Spaces
# Voir onglet "Logs" dans le Space
```

### Common Issues

**Issue**: Model not found
**Solution**: Attendre le tรฉlรฉchargement des modรจles (5-10 min)

**Issue**: CUDA out of memory
**Solution**: Rรฉduire MAX_AUDIO_LENGTH ou utiliser CPU

**Issue**: Port already in use
**Solution**: `PORT=8080 python app_v2.py`

## ๐Ÿ”ฎ Roadmap

- [ ] Streaming ASR/TTS
- [ ] Batch processing
- [ ] WebSockets pour streaming
- [ ] Caching Redis
- [ ] Database logging
- [ ] Rate limiting
- [ ] Authentication/API keys
- [ ] Metrics (Prometheus)
- [ ] Web UI (Gradio/Streamlit)
- [ ] More languages
- [ ] Emotion synthesis
- [ ] Custom voices

## ๐Ÿ“š Rรฉfรฉrences

- [Meta MMS Paper](https://arxiv.org/abs/2305.13516)
- [facebook/mms-1b-all](https://huggingface.co/facebook/mms-1b-all)
- [facebook/mms-tts](https://huggingface.co/facebook/mms-tts)
- [Transformers Documentation](https://huggingface.co/docs/transformers)
- [Flask Documentation](https://flask.palletsprojects.com/)