-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_UI.py
94 lines (82 loc) · 3.37 KB
/
gradio_UI.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import configparser
import urllib.request, json
# specify languages
lesan_LANGS = ["Amharic", "Tigrinya"]
lesan_LANGS_codes = ["am", "ti"]
ghananlp_LANGS = ["Twi","Ga","Ewe","Yoruba","Dagbani","Kikuyu","Gurene","Luo","Kimeru"]
ghananlp_LANGS_codes = ["tw","gaa","ee","yo","dag","ki","gur","luo","mer"]
LANGS = ["English"]+lesan_LANGS+ghananlp_LANGS
LANGS_codes = ["en"]+lesan_LANGS_codes+ghananlp_LANGS_codes
LANGS_dict = {}
for el,code in zip(LANGS,LANGS_codes):
LANGS_dict[el]=code
# get user configs
config = configparser.RawConfigParser()
config.read('config.cfg')
URLS_dict = dict(config.items('API_URLS'))
KEYS_dict = dict(config.items('API_KEYS'))
def translate(text, src_lang, tgt_lang):
"""
Translate the text from source lang to target lang
"""
if src_lang in lesan_LANGS or tgt_lang in lesan_LANGS:
try:
hdr ={
# Request headers
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
}
# Request body - # 'tgt_lang': am, ti, or auto
data = {'key': KEYS_dict["lesan"], 'text': text,
'src_lang': LANGS_dict[src_lang], 'tgt_lang': LANGS_dict[tgt_lang]}
data = json.dumps(data)
req = urllib.request.Request(URLS_dict["lesan"], headers=hdr, data = bytes(data.encode("utf-8")))
req.get_method = lambda: 'POST'
response = urllib.request.urlopen(req)
#print(response.getcode())
response_text = response.read()
result = json.loads(response_text.decode())["tgt_text"]
except Exception as e:
print("EXCEPTION::LESAN::")
print(e)
if src_lang in ghananlp_LANGS or tgt_lang in ghananlp_LANGS:
try:
hdr ={
# Request headers
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': KEYS_dict["ghananlp"],
}
# Request body
data = {
"in": text,
"lang": LANGS_dict[src_lang] + "-" + LANGS_dict[tgt_lang]
}
data = json.dumps(data)
req = urllib.request.Request(URLS_dict["ghananlp"], headers=hdr, data = bytes(data.encode("utf-8")))
req.get_method = lambda: 'POST'
response = urllib.request.urlopen(req)
#print(response.getcode())
response_text = response.read()
result = response_text.decode('utf-8').strip('"')
except Exception as e:
print("EXCEPTION::GHANANLP::")
print(e)
print(result)
return result
demo = gr.Interface(
fn=translate,
inputs=[
gr.components.Textbox(label="Text"),
gr.components.Dropdown(label="Source Language", choices=LANGS),
gr.components.Dropdown(label="Target Language", choices=LANGS),
],
outputs=["text"],
examples=[["United, we will preserve our culture!", "English", "Twi"],
["United, we will preserve our culture!", "English", "Amharic"]],
cache_examples=False,
title="Unified African Language Translator",
description="Joint work by GhanaNLP, Algorine, Lesan, DAIR, 2023"
)
demo.launch(share=True,server_port=8080)