File size: 1,665 Bytes
e1faa37
 
 
 
 
 
 
 
 
 
 
 
 
 
23e6ea7
e1faa37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d5bbba
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
# Import our modules 
import gradio as gr
from turjuman import turjuman 
import logging
import os
from transformers import AutoTokenizer


logging.basicConfig(
    format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=os.environ.get("LOGLEVEL", "INFO").upper(),
)
logger = logging.getLogger("turjuman.translate")
cache_dir="./mycache"


# Get the turjuman object and its tokenizer
turj = turjuman.turjuman(logger, cache_dir)
tokenizer = AutoTokenizer.from_pretrained('UBC-NLP/AraT5-base-title-generation')


# The translate function
def translate(sent):
    beam_options = {"search_method":"beam", "seq_length": 300, "num_beams":5, "no_repeat_ngram_size":2, "max_outputs":1}
    targets = turj.translate(sent,**beam_options)
    #print(targets)
    ans = ""
    for target in targets:
        target = tokenizer.decode(target, skip_special_tokens=True, clean_up_tokenization_spaces=True)
        ans += target
    return ans

#print(translate('Здравствуй, друг'))

interface = gr.Interface(fn=translate, inputs=['text'], outputs=['text'],
             allow_flagging=False,
             title='Turjuman Multi-lingual Translation',
             description='Please write the sentence you are trying to translate',
             css="""
             .chatbox{display:flex; flex-direction-column}
             .user_msg, resp_msg {padding: 4px;margin-bottom:4px;border-radius:4px; width:80%}
             .user_msg {background-color:cornflowerblue; color:white; align-self:start}
             .resp_msg {background-color:lightgray; align-self: self-end}
             """)

interface.launch(inline=False)