asv7j commited on
Commit
4143a13
1 Parent(s): 3c90a4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -32
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
- # Handling the response
24
- if response.status_code == 200:
25
- print(response.json())
26
- else:
27
- print(f"Error: {response.status_code} - {response.reason}")
28
  @app.get("/")
29
- def greet_json():
30
- # Making the POST request
31
- response = requests.post(url, json=data, headers=headers)
32
-
33
- # Handling the response
34
- if response.status_code == 200:
35
- print(response.json())
36
- else:
37
- print(f"Error: {response.status_code} - {response.reason}")
38
- return {"Hello": "World!"}
 
 
 
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)}