Bgk Injector SqLi commited on
Commit
b2297ec
·
1 Parent(s): 7afeaac

Add bidirectional translation (FR ↔ DYU)

Browse files

- New endpoint /api/translate/fr-dyu for French → Dioula
- Renamed /api/translate to /api/translate/dyu-fr for clarity
- Standardized JSON response format for both endpoints
- Updated documentation with workflow examples
- Version 1.1.0

Files changed (1) hide show
  1. app.py +72 -9
app.py CHANGED
@@ -143,14 +143,23 @@ def home():
143
  </div>
144
 
145
  <div class="endpoint">
146
- <p><span class="method post">POST</span> <code>/api/translate</code></p>
147
- <p><strong>Traduction</strong> - Traduit du Dioula vers le Français</p>
148
  <p><strong>Entrée:</strong> Texte en Dioula (paramètre <code>text</code>)</p>
149
- <p><strong>Sortie:</strong> <code>{"texte_dioula": "...", "traduction_francais": "..."}</code></p>
150
- <pre>curl -X POST https://votre-space.hf.space/api/translate \\
151
  -F "text=Sanji bɛna kɛ bi"</pre>
152
  </div>
153
 
 
 
 
 
 
 
 
 
 
154
  <h2>🔗 Documentation interactive</h2>
155
  <p>
156
  <a href="/docs">Swagger UI</a> |
@@ -161,8 +170,16 @@ def home():
161
  <ul>
162
  <li><strong>STT:</strong> facebook/mms-1b-all (adapter Dioula)</li>
163
  <li><strong>TTS:</strong> facebook/mms-tts-dyu</li>
164
- <li><strong>Traduction:</strong> facebook/nllb-200-distilled-600M (Dioula Français)</li>
165
  </ul>
 
 
 
 
 
 
 
 
166
  </body>
167
  </html>
168
  """
@@ -296,7 +313,7 @@ async def text_to_speech(text: str = Form(...)):
296
  print(f"Erreur TTS: {e}")
297
  raise HTTPException(status_code=500, detail=f"Erreur lors de la génération audio: {str(e)}")
298
 
299
- @app.post("/api/translate")
300
  async def translate_dioula_to_french(text: str = Form(...)):
301
  """
302
  Traduit du texte du Dioula vers le Français
@@ -328,14 +345,60 @@ async def translate_dioula_to_french(text: str = Form(...)):
328
  traduction = translate_tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
329
 
330
  return {
331
- "texte_dioula": text,
332
- "traduction_francais": traduction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  }
334
 
335
  except HTTPException:
336
  raise
337
  except Exception as e:
338
- print(f"Erreur Traduction: {e}")
339
  raise HTTPException(status_code=500, detail=f"Erreur lors de la traduction: {str(e)}")
340
 
341
  if __name__ == "__main__":
 
143
  </div>
144
 
145
  <div class="endpoint">
146
+ <p><span class="method post">POST</span> <code>/api/translate/dyu-fr</code></p>
147
+ <p><strong>Traduction Dioula Français</strong></p>
148
  <p><strong>Entrée:</strong> Texte en Dioula (paramètre <code>text</code>)</p>
149
+ <p><strong>Sortie:</strong> JSON avec traduction française</p>
150
+ <pre>curl -X POST https://votre-space.hf.space/api/translate/dyu-fr \\
151
  -F "text=Sanji bɛna kɛ bi"</pre>
152
  </div>
153
 
154
+ <div class="endpoint">
155
+ <p><span class="method post">POST</span> <code>/api/translate/fr-dyu</code></p>
156
+ <p><strong>Traduction Français → Dioula</strong></p>
157
+ <p><strong>Entrée:</strong> Texte en Français (paramètre <code>text</code>)</p>
158
+ <p><strong>Sortie:</strong> JSON avec traduction dioula</p>
159
+ <pre>curl -X POST https://votre-space.hf.space/api/translate/fr-dyu \\
160
+ -F "text=Il va pleuvoir aujourd'hui"</pre>
161
+ </div>
162
+
163
  <h2>🔗 Documentation interactive</h2>
164
  <p>
165
  <a href="/docs">Swagger UI</a> |
 
170
  <ul>
171
  <li><strong>STT:</strong> facebook/mms-1b-all (adapter Dioula)</li>
172
  <li><strong>TTS:</strong> facebook/mms-tts-dyu</li>
173
+ <li><strong>Traduction:</strong> facebook/nllb-200-distilled-600M (Dioula Français)</li>
174
  </ul>
175
+
176
+ <h2>🔄 Flux de travail complet</h2>
177
+ <p><strong>Exemple :</strong> Audio Dioula → Texte Dioula → Traduction Français → Audio Français</p>
178
+ <ol>
179
+ <li><code>/api/stt</code> : Convertit audio en texte dioula</li>
180
+ <li><code>/api/translate/dyu-fr</code> : Traduit en français</li>
181
+ <li>(Optionnel) Utiliser un TTS français pour générer l'audio</li>
182
+ </ol>
183
  </body>
184
  </html>
185
  """
 
313
  print(f"Erreur TTS: {e}")
314
  raise HTTPException(status_code=500, detail=f"Erreur lors de la génération audio: {str(e)}")
315
 
316
+ @app.post("/api/translate/dyu-fr")
317
  async def translate_dioula_to_french(text: str = Form(...)):
318
  """
319
  Traduit du texte du Dioula vers le Français
 
345
  traduction = translate_tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
346
 
347
  return {
348
+ "texte_source": text,
349
+ "langue_source": "dioula",
350
+ "texte_traduit": traduction,
351
+ "langue_cible": "français"
352
+ }
353
+
354
+ except HTTPException:
355
+ raise
356
+ except Exception as e:
357
+ print(f"Erreur Traduction DYU→FR: {e}")
358
+ raise HTTPException(status_code=500, detail=f"Erreur lors de la traduction: {str(e)}")
359
+
360
+ @app.post("/api/translate/fr-dyu")
361
+ async def translate_french_to_dioula(text: str = Form(...)):
362
+ """
363
+ Traduit du texte du Français vers le Dioula
364
+
365
+ - **text**: Texte en Français à traduire
366
+ """
367
+ try:
368
+ if not text.strip():
369
+ raise HTTPException(status_code=400, detail="Le texte ne peut pas être vide")
370
+
371
+ # Définir la langue source : Français
372
+ translate_tokenizer.src_lang = "fra_Latn"
373
+
374
+ # Préparation des tokens
375
+ inputs = translate_tokenizer(text, return_tensors="pt").to(device)
376
+
377
+ # Récupérer l'ID de la langue cible : Dioula
378
+ target_lang_id = translate_tokenizer.convert_tokens_to_ids("dyu_Latn")
379
+
380
+ # Génération de la traduction
381
+ with torch.no_grad():
382
+ translated_tokens = translate_model.generate(
383
+ **inputs,
384
+ forced_bos_token_id=target_lang_id,
385
+ max_length=200
386
+ )
387
+
388
+ # Décodage
389
+ traduction = translate_tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
390
+
391
+ return {
392
+ "texte_source": text,
393
+ "langue_source": "français",
394
+ "texte_traduit": traduction,
395
+ "langue_cible": "dioula"
396
  }
397
 
398
  except HTTPException:
399
  raise
400
  except Exception as e:
401
+ print(f"Erreur Traduction FR→DYU: {e}")
402
  raise HTTPException(status_code=500, detail=f"Erreur lors de la traduction: {str(e)}")
403
 
404
  if __name__ == "__main__":