File size: 640 Bytes
4143a13
 
03d9cf2
4105b8b
03d9cf2
 
 
4143a13
 
03d9cf2
4143a13
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# app.py

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