Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,22 @@
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
import requests
|
3 |
-
import json
|
4 |
-
|
5 |
-
url = "http://127.0.0.1:5000/translate"
|
6 |
-
data = {
|
7 |
-
"q": "Hello i m here",
|
8 |
-
"source": "auto",
|
9 |
-
"target": "en",
|
10 |
-
"format": "text",
|
11 |
-
"alternatives": 3,
|
12 |
-
"api_key": ""
|
13 |
-
}
|
14 |
-
headers = {
|
15 |
-
"Content-Type": "application/json"
|
16 |
-
}
|
17 |
-
|
18 |
|
19 |
app = FastAPI()
|
20 |
-
# Making the POST request
|
21 |
-
response = requests.post(url, json=data, headers=headers)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
print(response.json())
|
26 |
-
else:
|
27 |
-
print(f"Error: {response.status_code} - {response.reason}")
|
28 |
@app.get("/")
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
from fastapi import FastAPI
|
4 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
app = FastAPI()
|
|
|
|
|
7 |
|
8 |
+
LIBRETRANSLATE_URL = "http://libretranslate:5000/translate"
|
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 |
+
response = requests.post(LIBRETRANSLATE_URL, json={"q": text, "source": "auto", "target": "en"})
|
19 |
+
translation = response.json()["translatedText"]
|
20 |
+
return {"translation": translation}
|
21 |
+
except Exception as e:
|
22 |
+
return {"error": str(e)}
|