from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from transformers import pipeline
# Création de l'application FastAPI
app = FastAPI(title="Traduction Anglais → Français")
# Chargement du modèle de traduction
translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
@app.get("/", response_class=HTMLResponse)
def serve_html():
""" Sert l'interface utilisateur en HTML """
return """
Traduction Anglais → Français
Traduction Anglais → Français
"""
@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:
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}")