|
from fastapi import FastAPI, HTTPException |
|
from transformers import pipeline |
|
|
|
|
|
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}") |
|
|
|
|