File size: 803 Bytes
3320fc6 fa0996d ae6c842 3320fc6 ae6c842 3320fc6 ae6c842 3320fc6 fa0996d 3320fc6 fa0996d 3320fc6 e059b03 |
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 |
from fastapi import FastAPI, HTTPException
from transformers import pipeline
# Création de l'application FastAPI
app = FastAPI(title="Traduction Anglais → Français")
@app.get("/")
def home():
return {"message": "Bienvenue sur l'API de traduction !"}
@app.post("/translate/")
def translate(text: str):
"""
Traduit un texte de l'anglais vers le français.
"""
if not text:
raise HTTPException(status_code=400, detail="Le texte est vide.")
try:
translator = pipeline("translation", model="facebook/wmt19-en-fr")
translated_text = translator(text)[0]['translation_text']
return {"original": text, "translated": translated_text}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Erreur de traduction : {e}")
|