edaiofficial commited on
Commit
b605eca
·
1 Parent(s): bb5c326

initial commits

Browse files
Files changed (2) hide show
  1. app.py +57 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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
12
+ :return: configuration dictionary
13
+ """
14
+ with open(path, 'r', encoding="utf-8") as ymlfile:
15
+
16
+ cfg = yaml.safe_load(ymlfile)
17
+ return cfg
18
+
19
+ source_language = 'en'
20
+ target_language = 'sw'
21
+ translation_dir = 'main'
22
+
23
+ try:
24
+ file_yaml = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/config.yaml",force_filename='config.yaml')
25
+ src_vocab = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/src_vocab.txt")
26
+ trg_vocab = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/trg_vocab.txt")
27
+ best_ckpt = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/best.ckpt")
28
+ except Exception:
29
+ 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')
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,type_=2)
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_language,2)
45
+ return pred
46
+
47
+
48
+ title = "Interact with Masakhane Benchmark Models"
49
+ description = "Interact with Masakhane Benchmark Models"
50
+ iface = gr.Interface(fn=get_translation,
51
+ inputs=[gr.inputs.Textbox(label="sentence"), gr.inputs.Textbox(label="translation")],
52
+ outputs='text',
53
+ title=title,
54
+ description=description,
55
+ examples=[["God is great that he sent Jesus to help us", "Mungu ni mkuu kwamba alimtuma Yesu atusaidie"]],
56
+ enable_queue=True)
57
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/chrisemezue/joeynmt.git
2
+ huggingface-hub