Spaces:
Build error
Build error
edaiofficial
commited on
Commit
•
6c20b5b
1
Parent(s):
97eac69
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import yaml
|
3 |
+
import torch
|
4 |
+
from mmtafrica import load_params, translate
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
language_map = {'English':'en','Swahili':'sw','Fon':'fon','Igbo':'ig',
|
8 |
+
'Kinyarwanda':'rw','Xhosa':'xh','Yoruba':'yo','French':'fr'}
|
9 |
+
|
10 |
+
available_languages = list(language_map.keys())
|
11 |
+
|
12 |
+
# Load parameters and model from checkpoint
|
13 |
+
checkpoint = hf_hub_download(repo_id="chrisjay/mmtafrica", filename="mmt_translation.pt")
|
14 |
+
device = 'gpu' if torch.cuda.is_available() else 'cpu'
|
15 |
+
params = load_params({'checkpoint':checkpoint,'device':device})
|
16 |
+
|
17 |
+
|
18 |
+
def get_translation(source_language,target_language,source_sentence=None):
|
19 |
+
'''
|
20 |
+
This takes a sentence and gets the translation.
|
21 |
+
'''
|
22 |
+
|
23 |
+
source_language_ = language_map[source_language]
|
24 |
+
target_language_ = language_map[target_language]
|
25 |
+
|
26 |
+
|
27 |
+
try:
|
28 |
+
pred = translate(params,source_sentence,source_lang=source_language_,target_lang=target_language_)
|
29 |
+
if pred=='':
|
30 |
+
return f"Could not find translation"
|
31 |
+
else:
|
32 |
+
return pred
|
33 |
+
except Exception as error:
|
34 |
+
return f"Issue with translation: \n {error}"
|
35 |
+
|
36 |
+
title = "MMTAfrica: Multilingual Machine Translation"
|
37 |
+
description = "Enjoy our MMT model that allows you to translate among 6 African languages, English and French!"
|
38 |
+
|
39 |
+
iface = gr.Interface(fn=get_translation,
|
40 |
+
inputs=[gr.inputs.Dropdown(choices = available_languages,default='Igbo'),
|
41 |
+
gr.inputs.Dropdown(choices = available_languages,default='Fon'),
|
42 |
+
gr.inputs.Textbox(label="Input")],
|
43 |
+
outputs=gr.outputs.Textbox(type="auto", label='Translation'),
|
44 |
+
title=title,
|
45 |
+
description=description,
|
46 |
+
enable_queue=True,
|
47 |
+
theme='huggingface')
|
48 |
+
iface.launch()
|