File size: 1,267 Bytes
476e166
 
 
 
 
 
 
 
 
ceaa373
 
476e166
ceaa373
476e166
 
ceaa373
476e166
ceaa373
c757bee
ceaa373
 
 
c757bee
ceaa373
 
 
 
 
476e166
ceaa373
 
 
 
 
 
476e166
 
21c3383
97e52ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests
import os
import gradio as gr

title = "Translate Text"
description = """"""
article = "Check out [the original repo](https://huggingface.co/language-tools/language-translation) that this demo is based off of."


TRANSLATION_API_URL = "https://api-inference.huggingface.co/models/t5-base"
LANG_ID_API_URL = "https://noe30ht5sav83xm1.us-east-1.aws.endpoints.huggingface.cloud"
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
# ACCESS_TOKEN = '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'
headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}


def query(payload):
  translation_response = requests.post(TRANSLATION_API_URL, headers=headers, json={
      "inputs": payload, "wait_for_model": True, "use_cache": True})
  translation = translation_response.json()[0]['translation_text']

  lang_id_response = requests.post(LANG_ID_API_URL, headers=headers, json={
      "inputs": payload, "wait_for_model": True, "use_cache": True})
  lang_id = lang_id_response.json()[0][0]

  return [lang_id, translation]


gr.Interface(
    query,
    gr.Textbox(lines=2),
    outputs=[
        gr.Textbox(lines=3, label="Detected Language"),
        gr.Textbox(lines=3, label="Translation")
    ],
    title=title,
    description=description,
    article=article
).launch()