PRAli22 commited on
Commit
04272f7
1 Parent(s): f0752b9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
+ import gradio as gr
3
+
4
+ tokenizer = AutoTokenizer.from_pretrained("PRAli22/arat5-base-arabic-dialects-translation" )
5
+ model = AutoModelForSeq2SeqLM.from_pretrained("PRAli22/arat5-base-arabic-dialects-translation")
6
+
7
+ class Translator:
8
+ def __init__(self, model:AutoModelForSeq2SeqLM, tokenizer:AutoTokenizer):
9
+ self.model = model
10
+ self.tokenizer = tokenizer
11
+
12
+ def translate(self, source:str) -> str:
13
+
14
+ encoding = self.tokenizer.encode_plus(source, pad_to_max_length=True, return_tensors="pt")
15
+ input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
16
+ outputs = self.model.generate(
17
+ input_ids=input_ids, attention_mask=attention_masks,
18
+ max_length=256,
19
+ do_sample=True,
20
+ top_k=120,
21
+ top_p=0.95,
22
+ early_stopping=True,
23
+ num_return_sequences=1
24
+ )
25
+ translation = self.tokenizer.decode(outputs[0], skip_special_tokens=True,clean_up_tokenization_spaces=True)
26
+ return translation
27
+
28
+
29
+ translator = Translator(model, tokenizer)
30
+
31
+ translation = translator.translate()
32
+
33
+ css_code='body{background-image:url("https://media.istockphoto.com/id/1256252051/vector/people-using-online-translation-app.jpg?s=612x612&w=0&k=20&c=aa6ykHXnSwqKu31fFR6r6Y1bYMS5FMAU9yHqwwylA94=");}'
34
+
35
+ demo = gr.Interface(
36
+ fn=translation,
37
+ inputs=
38
+ gr.Textbox(label="text", placeholder="Enter the text "),
39
+
40
+ outputs=gr.Textbox(label="summary"),
41
+ title="Text Summarizer",
42
+ description= "This is Text Summarizer System, it takes a text in English as inputs and returns it's summary",
43
+ css = css_code
44
+ )
45
+
46
+ demo.launch()
47
+