|
|
|
|
|
from fastapi import FastAPI |
|
import requests |
|
|
|
app = FastAPI() |
|
|
|
LIBRETRANSLATE_URL = "http://libretranslate:5000/translate" |
|
|
|
@app.get("/") |
|
def root(): |
|
return {"message": "Hello World"} |
|
response = requests.post(LIBRETRANSLATE_URL, json={"q": "text", "source": "auto", "target": "hi"}) |
|
print(response) |
|
@app.get("/translate") |
|
def translate_text(text: str): |
|
try: |
|
response = requests.post(LIBRETRANSLATE_URL, json={"q": text, "source": "auto", "target": "en"}) |
|
translation = response.json()["translatedText"] |
|
return {"translation": translation} |
|
except Exception as e: |
|
return {"error": str(e)} |
|
|