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))