tran / app.py
asv7j's picture
Update app.py
02272f3 verified
raw
history blame
485 Bytes
from fastapi import FastAPI, HTTPException
from libretranslate import LibreTranslate
app = FastAPI()
translator = LibreTranslate()
@app.get("/translate/")
async def translate_text(text: str, source_lang: str = "auto", target_lang: str = "en"):
try:
translation = translator.translate(text, source_lang=source_lang, target_lang=target_lang)
return {"translation": translation}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))