Spaces:
Build error
Build error
import gradio as gr | |
import yaml | |
from joeynmt.prediction import load_params_for_prediction,translate | |
from huggingface_hub import hf_hub_download | |
language_map = {'English':'en','Swahili':'sw','Fon':'fon','Igbo':'ig', | |
'Arabic':'ar','Shona':'sn','Ẹ̀dó':'bin','Hausa':'ha', | |
'Efik':'efi','Twi':'twi','Afrikaans':'af','Yoruba':'yo'} | |
available_languages =list(language_map.keys()) | |
def load_config(path="configs/default.yaml") -> dict: | |
""" | |
CODE ADAPTED FROM: https://github.com/joeynmt/joeynmt | |
Loads and parses a YAML configuration file. | |
:param path: path to YAML configuration file | |
:return: configuration dictionary | |
""" | |
with open(path, 'r', encoding="utf-8") as ymlfile: | |
cfg = yaml.safe_load(ymlfile) | |
return cfg | |
def load_model(source_language,target_language): | |
source_language = 'en' | |
target_language = 'sw' | |
translation_dir = 'main' | |
try: | |
file_yaml = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/config.yaml",force_filename='config.yaml') | |
src_vocab = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/src_vocab.txt") | |
trg_vocab = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/trg_vocab.txt") | |
best_ckpt = hf_hub_download("chrisjay/masakhane_benchmarks", filename=f"{source_language}-{target_language}/{translation_dir}/best.ckpt") | |
except Exception: | |
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') | |
parsed_yaml_file = load_config(file_yaml) | |
parsed_yaml_file['data']['src_vocab']=src_vocab | |
parsed_yaml_file['data']['trg_vocab']=trg_vocab | |
params = load_params_for_prediction(parsed_yaml_file,best_ckpt) | |
return params | |
def get_translation(source_language,target_language,source_sentence=None,source_file=None): | |
''' | |
This takes a sentence and gets the translation. | |
type_=2 tells joeynmt translate that it should expect a sentence. | |
''' | |
source = source_sentence | |
type_=2 | |
if source_file!=None: | |
type_=1 | |
source = source_file.name | |
try: | |
params = load_model(source_language,target_language) | |
pred = translate(params,source,type_) | |
except Exception: | |
return 'There was an issue loading the translation model for this language pair.' | |
return pred[0] if source_file==None else pred | |
title = "Interact with Masakhane Benchmark Models" | |
description = "This is a demo to enable you interact with some of the Masakhane Benchmark Models" | |
iface = gr.Interface(fn=get_translation, | |
inputs=[gr.inputs.Dropdown(choices = available_languages,default='English'), | |
gr.inputs.Dropdown(choices = available_languages,default='Swahili'), | |
gr.inputs.Textbox(label="Input")], | |
gr.inputs.File(file_count="single", type="file", label='File with sentences', optional=True) | |
outputs=gr.outputs.Textbox(self, type="auto", label='Translation'), | |
title=title, | |
description=description, | |
examples=[ | |
['English','Swahili'], | |
['English','Afrikaans'],['English','Arabic'],['Efik','English'],['English','Hausa'], | |
['English','Igbo'],['English','Fon'],['English','Twi'],['Shona','English'],['Swahili','English'], | |
['Yoruba','English']], | |
enable_queue=True), | |
theme='darkdefault' | |
iface.launch() | |