sheonhan commited on
Commit
ceaa373
1 Parent(s): 97e52ab

add both translation and langid endpoints

Browse files
Files changed (2) hide show
  1. __pycache__/app.cpython-37.pyc +0 -0
  2. app.py +21 -7
__pycache__/app.cpython-37.pyc ADDED
Binary file (913 Bytes). View file
 
app.py CHANGED
@@ -7,18 +7,32 @@ description = """"""
7
  article = "Check out [the original repo](https://huggingface.co/language-tools/language-translation) that this demo is based off of."
8
 
9
 
10
- API_URL = "https://api-inference.huggingface.co/models/t5-base"
 
11
  ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
 
12
  headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
13
 
 
14
  def query(payload):
15
- response = requests.post(API_URL, headers=headers, json={"inputs": payload})
16
- return response.json()
17
-
 
 
 
 
 
 
 
 
18
  gr.Interface(
19
- fn=query,
20
- inputs="textbox",
21
- outputs="text",
 
 
 
22
  title=title,
23
  description=description,
24
  article=article
 
7
  article = "Check out [the original repo](https://huggingface.co/language-tools/language-translation) that this demo is based off of."
8
 
9
 
10
+ TRANSLATION_API_URL = "https://api-inference.huggingface.co/models/t5-base"
11
+ LANG_ID_API_URL = "https://noe30ht5sav83xm1.us-east-1.aws.endpoints.huggingface.cloud"
12
  ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
13
+ # ACCESS_TOKEN = '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'
14
  headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
15
 
16
+
17
  def query(payload):
18
+ translation_response = requests.post(TRANSLATION_API_URL, headers=headers, json={
19
+ "inputs": payload, "wait_for_model": True})
20
+ translation = translation_response.json()[0]['translation_text']
21
+
22
+ lang_id_response = requests.post(LANG_ID_API_URL, headers=headers, json={
23
+ "inputs": payload, "wait_for_model": True})
24
+ lang_id = lang_id_response.json()[0][0]
25
+
26
+ return [lang_id, translation]
27
+
28
+
29
  gr.Interface(
30
+ query,
31
+ gr.Textbox(lines=2),
32
+ outputs=[
33
+ gr.Textbox(lines=3, label="Detected Language"),
34
+ gr.Textbox(lines=3, label="Translation")
35
+ ],
36
  title=title,
37
  description=description,
38
  article=article