asv7j commited on
Commit
02272f3
1 Parent(s): 4143a13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -16
app.py CHANGED
@@ -1,22 +1,13 @@
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)}
 
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))