File size: 485 Bytes
02272f3
 
03d9cf2
 
02272f3
03d9cf2
02272f3
 
4143a13
02272f3
4143a13
 
02272f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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))