muhammadayman commited on
Commit
837d615
1 Parent(s): 045380f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer
4
+ import torch
5
+ import json, re
6
+ tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
7
+ model = torch.load("helsinki_fineTuned.pt", map_location=torch.device('cpu'))
8
+ model.eval()
9
+
10
+ # Open Keywords
11
+ with open('merged.json', encoding='utf8') as merged:
12
+ data = json.load(merged)
13
+ keyword_map = {}
14
+ for en in data:
15
+ keyword_map[en.lower()] = data[en]
16
+ merged.close()
17
+
18
+ # Getting keywords from the file
19
+ def getKeywords(word):
20
+ words = word.lower().rstrip()
21
+ if keyword_map.get(words):
22
+ return keyword_map[words], True
23
+ return word, False
24
+
25
+ # Replace keywords with the translation
26
+ def final_output(text):
27
+ reg = re.compile('[a-zA-Z][ \-()a-zA-Z]+')
28
+ keywords = re.findall(reg, text)
29
+ text_split = re.split(reg, text)
30
+ n=len(keywords)
31
+ for i in range(n):
32
+ word, found = getKeywords(keywords[i])
33
+ if found:
34
+ text_split[i]+=(word + " (" + keywords[i].rstrip() + ")")
35
+ else:
36
+ text_split[i]+=word
37
+ return ' '.join(text_split)
38
+
39
+
40
+
41
+ def translate(input):
42
+ translation = []
43
+ text_list = input.split('.')
44
+ for i in range(len(text_list)):
45
+ encode = model.generate(**tokenizer.prepare_seq2seq_batch(text_list[i],return_tensors='pt'))
46
+ text_ar = tokenizer.batch_decode(encode,skip_special_tokens=True)[0]
47
+ text_post = final_output(text_ar)
48
+ translation.append(text_post)
49
+ article_ar = ".".join(translation)
50
+ return article_ar
51
+ # Wrap up function
52
+ translate_interface = gr.Interface(fn = translate,
53
+ allow_flagging = True,
54
+ flagging_dir = 'flagged/logs',
55
+ title = 'Translating "English Data Science" content into Arabic',
56
+ inputs=gr.inputs.Textbox(lines = 7, label = 'English content'),
57
+ outputs="text",
58
+ examples = [
59
+ ['In the last few years the RNN-based architectures have shown the best performance in machine translation problems, but still they have some problems that had to be solved. First, they have a difficulty to cope with long-range dependencies (also LSTM when it has to deal with really long sentences). Secondly, each hidden state depends on the previous one which impossible to parallelize and makes it inefficient on GPUs.']
60
+ ,
61
+ ['Data science is an interdisciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from noisy, structured and unstructured data,[1][2] and apply knowledge and actionable insights from data across a broad range of application domains. Data science is related to data mining, machine learning and big data.']
62
+ ]
63
+ )
64
+ translate_interface.launch()