Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,16 +4,11 @@ import gradio as gr
|
|
4 |
tokenizer = AutoTokenizer.from_pretrained("PRAli22/arat5-base-arabic-dialects-translation" )
|
5 |
model = AutoModelForSeq2SeqLM.from_pretrained("PRAli22/arat5-base-arabic-dialects-translation")
|
6 |
|
7 |
-
|
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 =
|
15 |
input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
|
16 |
-
outputs =
|
17 |
input_ids=input_ids, attention_mask=attention_masks,
|
18 |
max_length=256,
|
19 |
do_sample=True,
|
@@ -22,17 +17,16 @@ class Translator:
|
|
22 |
early_stopping=True,
|
23 |
num_return_sequences=1
|
24 |
)
|
25 |
-
translation =
|
26 |
return translation
|
27 |
|
28 |
|
29 |
-
translator = Translator(model, tokenizer)
|
30 |
|
31 |
|
32 |
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=");}'
|
33 |
|
34 |
demo = gr.Interface(
|
35 |
-
fn=
|
36 |
inputs=
|
37 |
gr.Textbox(label="text", placeholder="Enter the sentence "),
|
38 |
|
|
|
4 |
tokenizer = AutoTokenizer.from_pretrained("PRAli22/arat5-base-arabic-dialects-translation" )
|
5 |
model = AutoModelForSeq2SeqLM.from_pretrained("PRAli22/arat5-base-arabic-dialects-translation")
|
6 |
|
7 |
+
def translate(source):
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
encoding = tokenizer.encode_plus(source, pad_to_max_length=True, return_tensors="pt")
|
10 |
input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
|
11 |
+
outputs = model.generate(
|
12 |
input_ids=input_ids, attention_mask=attention_masks,
|
13 |
max_length=256,
|
14 |
do_sample=True,
|
|
|
17 |
early_stopping=True,
|
18 |
num_return_sequences=1
|
19 |
)
|
20 |
+
translation = tokenizer.decode(outputs[0], skip_special_tokens=True,clean_up_tokenization_spaces=True)
|
21 |
return translation
|
22 |
|
23 |
|
|
|
24 |
|
25 |
|
26 |
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=");}'
|
27 |
|
28 |
demo = gr.Interface(
|
29 |
+
fn=translate,
|
30 |
inputs=
|
31 |
gr.Textbox(label="text", placeholder="Enter the sentence "),
|
32 |
|