File size: 2,301 Bytes
bd4cbe5
 
 
 
b1e71be
f561e39
bd4cbe5
 
 
 
 
f561e39
bd4cbe5
 
 
 
 
 
 
 
 
 
653ebf6
 
bd4cbe5
 
 
 
 
 
 
 
 
 
 
 
 
 
6d84266
 
 
 
 
bd4cbe5
 
 
 
0c0cd92
b1e71be
 
bd4cbe5
 
bb8176e
0c0cd92
 
bd4cbe5
 
 
 
 
 
 
 
b1e71be
0c0cd92
 
6d84266
bd4cbe5
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
59
60
61
62
63
64
65
66
67
68
69
import os
import gradio as gr
from simpletransformers.seq2seq import Seq2SeqModel, Seq2SeqArgs


# os.environ["TOKENIZERS_PARALLELISM"] = "false"


def load_translator(model_name='Enutrof/marian-mt-en-pcm'):
    '''
    This method loads the sequence to sequence model for translation.
    :return: model
    '''
    pmodel_args = Seq2SeqArgs()
    pmodel_args.max_length = 1024
    pmodel_args.length_penalty = 1
    pmodel_args.num_beams = 20
    pmodel_args.num_return_sequences = 3

    pmodel = Seq2SeqModel(
        encoder_decoder_type="marian",
        encoder_decoder_name=model_name,
        args=pmodel_args,
        use_cuda=False
    )
    return pmodel


en_pcm_model = load_translator()


def predict(input):
    if isinstance(input, str):
        input = [input]
    predictions = en_pcm_model.predict(input)
    return [i.replace('▁', ' ') for i in predictions[0]]


# HF_TOKEN = os.getenv('english-pidgin-flagging')
# hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN,
#                                        dataset_name="English-NigerianPidgin-Result-Validation",
#                                        organization="Enutrof",
#                                        )
gr.Interface(
    fn=predict,
    inputs=gr.inputs.Textbox(lines=1, label="Input Text in English"),
    outputs=[
        gr.outputs.Textbox(label="Translated texts in πŸ‡³πŸ‡¬ Pidgin"),
        gr.outputs.Textbox(label=''),
        gr.outputs.Textbox(label=''),
    ],
    # theme="peach",
    title='English to πŸ‡³πŸ‡¬ Pidgin Automatic Translation',
    description='Type your English text in the left text box to get πŸ‡³πŸ‡¬ Pidgin translations on the right. '
                'Tell us the best translation by clicking one of the buttons below.',
    examples=[
        'Who are you?',
        'You shall not pervert justice due the stranger or the fatherless, nor take a widow’s garment as a pledge.',
        'I know every song by that artiste.',
        'They should not be permitted here.',
        'What are you looking for?',
        'I am lost please help me find my way to the market.',
    ],
    allow_flagging="manual",
    flagging_options=["translation 1 βœ…", "translation 2 βœ…",
                      "translation 3 βœ…"],
    #flagging_callback=hf_writer,
).launch(enable_queue=True)