ahmedoumar commited on
Commit
e1faa37
1 Parent(s): 35d3e92

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import our modules
2
+ import gradio as gr
3
+ from turjuman import turjuman
4
+ import logging
5
+ import os
6
+ from transformers import AutoTokenizer
7
+
8
+
9
+ logging.basicConfig(
10
+ format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
11
+ datefmt="%Y-%m-%d %H:%M:%S",
12
+ level=os.environ.get("LOGLEVEL", "INFO").upper(),
13
+ )
14
+ logger = logging.getLogger("turjuman.translate")
15
+ cache_dir="/content/mycache"
16
+
17
+
18
+ # Get the turjuman object and its tokenizer
19
+ turj = turjuman.turjuman(logger, cache_dir)
20
+ tokenizer = AutoTokenizer.from_pretrained('UBC-NLP/AraT5-base-title-generation')
21
+
22
+
23
+ # The translate function
24
+ def translate(sent):
25
+ beam_options = {"search_method":"beam", "seq_length": 300, "num_beams":5, "no_repeat_ngram_size":2, "max_outputs":1}
26
+ targets = turj.translate(sent,**beam_options)
27
+ #print(targets)
28
+ ans = ""
29
+ for target in targets:
30
+ target = tokenizer.decode(target, skip_special_tokens=True, clean_up_tokenization_spaces=True)
31
+ ans += target
32
+ return ans
33
+
34
+ #print(translate('Здравствуй, друг'))
35
+
36
+ interface = gr.Interface(fn=translate, inputs=['text'], outputs=['text'],
37
+ allow_flagging=False,
38
+ title='Turjuman Multi-lingual Translation',
39
+ description='Please write the sentence you are trying to translate',
40
+ css="""
41
+ .chatbox{display:flex; flex-direction-column}
42
+ .user_msg, resp_msg {padding: 4px;margin-bottom:4px;border-radius:4px; width:80%}
43
+ .user_msg {background-color:cornflowerblue; color:white; align-self:start}
44
+ .resp_msg {background-color:lightgray; align-self: self-end}
45
+ """)
46
+
47
+ interface.launch()