Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,13 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from fastapi import FastAPI
|
4 |
-
import requests
|
5 |
|
6 |
app = FastAPI()
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
@app.get("/")
|
11 |
-
def root():
|
12 |
-
return {"message": "Hello World"}
|
13 |
-
response = requests.post(LIBRETRANSLATE_URL, json={"q": "text", "source": "auto", "target": "hi"})
|
14 |
-
print(response)
|
15 |
-
@app.get("/translate")
|
16 |
-
def translate_text(text: str):
|
17 |
try:
|
18 |
-
|
19 |
-
translation = response.json()["translatedText"]
|
20 |
return {"translation": translation}
|
21 |
except Exception as e:
|
22 |
-
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from libretranslate import LibreTranslate
|
|
|
|
|
3 |
|
4 |
app = FastAPI()
|
5 |
+
translator = LibreTranslate()
|
6 |
|
7 |
+
@app.get("/translate/")
|
8 |
+
async def translate_text(text: str, source_lang: str = "auto", target_lang: str = "en"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
10 |
+
translation = translator.translate(text, source_lang=source_lang, target_lang=target_lang)
|
|
|
11 |
return {"translation": translation}
|
12 |
except Exception as e:
|
13 |
+
raise HTTPException(status_code=500, detail=str(e))
|