File size: 2,258 Bytes
b605eca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16dd818
b605eca
 
 
 
 
 
 
 
 
 
 
 
5051968
b605eca
 
 
5051968
b605eca
 
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
import gradio as gr
import yaml
from joeynmt.prediction import load_params_for_prediction,translate
from huggingface_hub import hf_hub_download

def load_config(path="configs/default.yaml") -> dict:
    """
    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
    
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)

def get_translation(source_sentence,type_=2):
    '''
    This takes a sentence and gets the translation.
    type_=2 tells joeynmt translate that it should expect a sentence. 
    '''

    pred = translate(params,source_language,2)
    return pred

  
title = "Interact with Masakhane Benchmark Models"
description = "Interact with Masakhane Benchmark Models"
iface = gr.Interface(fn=get_translation, 
  inputs=[gr.inputs.Textbox(label="input")],
  outputs='text',
  title=title,
  description=description,
  examples=[["God is great that he sent Jesus to help us"]],
  enable_queue=True)
iface.launch()