gemma / app.py
luck210's picture
Update app.py
3320fc6 verified
raw
history blame
803 Bytes
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}")