Spaces:
Runtime error
Runtime error
Update app.py
Browse filesLanguage translation model without pipeline API.
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
2 |
import gradio as grad
|
3 |
import ast
|
4 |
|
@@ -21,11 +21,27 @@ import ast
|
|
21 |
|
22 |
# 3. Different task: language translation.
|
23 |
# First model translates English to German.
|
24 |
-
mdl_name = "Helsinki-NLP/opus-mt-en-de"
|
25 |
-
opus_translator = pipeline("translation", model=mdl_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def translate(text):
|
28 |
-
|
|
|
|
|
29 |
return response
|
30 |
|
31 |
-
grad.
|
|
|
|
|
|
1 |
+
from transformers import AutoModel, AutoModelForSeq2SeqLM, AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
2 |
import gradio as grad
|
3 |
import ast
|
4 |
|
|
|
21 |
|
22 |
# 3. Different task: language translation.
|
23 |
# First model translates English to German.
|
24 |
+
# mdl_name = "Helsinki-NLP/opus-mt-en-de"
|
25 |
+
# opus_translator = pipeline("translation", model=mdl_name)
|
26 |
+
|
27 |
+
# def translate(text):
|
28 |
+
# response = opus_translator(text)
|
29 |
+
# return response
|
30 |
+
|
31 |
+
# grad.Interface(translate, inputs=["text",], outputs="text").launch()
|
32 |
+
|
33 |
+
# 4. Language translation without pipeline API.
|
34 |
+
# Second model translates English to French.
|
35 |
+
mdl_name = "Helsinki-NLP/opus-mt-en-fr"
|
36 |
+
mdl = AutoModelForSeq2SeqLM.from_pretrained(mdl_name)
|
37 |
+
my_tkn = AutoTokenizer.from_pretrained(mdl_name)
|
38 |
|
39 |
def translate(text):
|
40 |
+
inputs = my_tkn(text, return_tensors="pt")
|
41 |
+
trans_output = mdl.generate(**inputs)
|
42 |
+
response = my_tkn.decode(trans_output[0], skip_special_tokens=True)
|
43 |
return response
|
44 |
|
45 |
+
txt=grad.Textbox(lines=1, label="English", placeholder="English Text here")
|
46 |
+
out=grad.Textbox(lines=1, label="French")
|
47 |
+
grad.Interface(translate, inputs=txt, outputs=out).launch()
|