Spaces:
Build error
Build error
Commit
·
ace9aa6
1
Parent(s):
3d4b6fc
additional commits
Browse files
app.py
CHANGED
@@ -3,9 +3,19 @@ import yaml
|
|
3 |
from joeynmt.prediction import load_params_for_prediction,translate
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def load_config(path="configs/default.yaml") -> dict:
|
7 |
"""
|
8 |
-
ADAPTED FROM: https://github.com/joeynmt/joeynmt
|
9 |
Loads and parses a YAML configuration file.
|
10 |
|
11 |
:param path: path to YAML configuration file
|
@@ -15,43 +25,65 @@ def load_config(path="configs/default.yaml") -> dict:
|
|
15 |
|
16 |
cfg = yaml.safe_load(ymlfile)
|
17 |
return cfg
|
18 |
-
|
19 |
-
source_language
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
try:
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
except Exception:
|
29 |
-
|
30 |
|
31 |
|
32 |
-
parsed_yaml_file = load_config(file_yaml)
|
33 |
-
parsed_yaml_file['data']['src_vocab']=src_vocab
|
34 |
-
parsed_yaml_file['data']['trg_vocab']=trg_vocab
|
35 |
|
36 |
-
params = load_params_for_prediction(parsed_yaml_file,best_ckpt)
|
|
|
37 |
|
38 |
-
def get_translation(source_sentence,
|
39 |
'''
|
40 |
This takes a sentence and gets the translation.
|
41 |
type_=2 tells joeynmt translate that it should expect a sentence.
|
42 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
pred = translate(params,source_sentence,2)
|
45 |
-
return pred[0]
|
46 |
|
47 |
-
|
48 |
title = "Interact with Masakhane Benchmark Models"
|
49 |
-
description = "
|
|
|
50 |
iface = gr.Interface(fn=get_translation,
|
51 |
-
inputs=[gr.inputs.
|
52 |
-
|
|
|
|
|
|
|
53 |
title=title,
|
54 |
description=description,
|
55 |
-
examples=[
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
57 |
iface.launch()
|
|
|
3 |
from joeynmt.prediction import load_params_for_prediction,translate
|
4 |
from huggingface_hub import hf_hub_download
|
5 |
|
6 |
+
|
7 |
+
|
8 |
+
language_map = {'English':'en','Swahili':'sw','Fon':'fon','Igbo':'ig',
|
9 |
+
'Arabic':'ar','Shona':'sn','Ẹ̀dó':'bin','Hausa':'ha',
|
10 |
+
'Efik':'efi','Twi':'twi','Afrikaans':'af','Yoruba':'yo'}
|
11 |
+
|
12 |
+
|
13 |
+
available_languages =list(language_map.keys())
|
14 |
+
|
15 |
+
|
16 |
def load_config(path="configs/default.yaml") -> dict:
|
17 |
"""
|
18 |
+
CODE ADAPTED FROM: https://github.com/joeynmt/joeynmt
|
19 |
Loads and parses a YAML configuration file.
|
20 |
|
21 |
:param path: path to YAML configuration file
|
|
|
25 |
|
26 |
cfg = yaml.safe_load(ymlfile)
|
27 |
return cfg
|
28 |
+
|
29 |
+
def load_model(source_language,target_language):
|
30 |
+
source_language = 'en'
|
31 |
+
target_language = 'sw'
|
32 |
+
translation_dir = 'main'
|
33 |
|
34 |
+
try:
|
35 |
+
file_yaml = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/config.yaml",force_filename='config.yaml')
|
36 |
+
src_vocab = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/src_vocab.txt")
|
37 |
+
trg_vocab = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/trg_vocab.txt")
|
38 |
+
best_ckpt = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/best.ckpt")
|
39 |
+
except Exception:
|
40 |
+
raise Exception(f'It seems we do not have a working configuration yet repo for {source_language} -> {target_language}. \n You could help us by creating it here: https://huggingface.co/chrisjay/masakhane_benchmarks/tree/main')
|
41 |
|
42 |
|
43 |
+
parsed_yaml_file = load_config(file_yaml)
|
44 |
+
parsed_yaml_file['data']['src_vocab']=src_vocab
|
45 |
+
parsed_yaml_file['data']['trg_vocab']=trg_vocab
|
46 |
|
47 |
+
params = load_params_for_prediction(parsed_yaml_file,best_ckpt)
|
48 |
+
return params
|
49 |
|
50 |
+
def get_translation(source_language,target_language,source_sentence=None,source_file=None):
|
51 |
'''
|
52 |
This takes a sentence and gets the translation.
|
53 |
type_=2 tells joeynmt translate that it should expect a sentence.
|
54 |
'''
|
55 |
+
source = source_sentence
|
56 |
+
type_=2
|
57 |
+
if source_file!=None:
|
58 |
+
type_=1
|
59 |
+
source = source_file.name
|
60 |
+
try:
|
61 |
+
params = load_model(source_language,target_language)
|
62 |
+
pred = translate(params,source,type_)
|
63 |
+
except Exception:
|
64 |
+
return 'There was an issue loading the translation model for this language pair.'
|
65 |
+
|
66 |
+
return pred[0] if source_file==None else pred
|
67 |
+
|
68 |
+
|
69 |
|
|
|
|
|
70 |
|
|
|
71 |
title = "Interact with Masakhane Benchmark Models"
|
72 |
+
description = "This is a demo to enable you interact with some of the Masakhane Benchmark Models"
|
73 |
+
|
74 |
iface = gr.Interface(fn=get_translation,
|
75 |
+
inputs=[gr.inputs.Dropdown(choices = available_languages,default='English'),
|
76 |
+
gr.inputs.Dropdown(choices = available_languages,default='Swahili'),
|
77 |
+
gr.inputs.Textbox(label="Input")],
|
78 |
+
gr.inputs.File(file_count="single", type="file", label='File with sentences', optional=True)
|
79 |
+
outputs=gr.outputs.Textbox(self, type="auto", label='Translation'),
|
80 |
title=title,
|
81 |
description=description,
|
82 |
+
examples=[
|
83 |
+
['English','Swahili'],
|
84 |
+
['English','Afrikaans'],['English','Arabic'],['Efik','English'],['English','Hausa'],
|
85 |
+
['English','Igbo'],['English','Fon'],['English','Twi'],['Shona','English'],['Swahili','English'],
|
86 |
+
['Yoruba','English']],
|
87 |
+
enable_queue=True),
|
88 |
+
theme='darkdefault'
|
89 |
iface.launch()
|