Jay4769 commited on
Commit
d519129
·
verified ·
1 Parent(s): 4f06877

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -1,14 +1,22 @@
1
- from transformers import pipeline
2
  import gradio as gr
3
 
4
- translator = pipeline(
5
- "translation_ru_to_vi",
6
- model="Helsinki-NLP/opus-mt-ru-vi"
7
- )
8
 
9
  def translate(text):
10
- result = translator(text)
11
- return result[0]["translation_text"]
 
 
 
 
 
 
 
 
12
 
13
  iface = gr.Interface(
14
  fn=translate,
@@ -16,9 +24,8 @@ iface = gr.Interface(
16
  lines=2,
17
  placeholder="Nhập tiếng Nga..."
18
  ),
19
- outputs=gr.Textbox(),
20
- title="🇷🇺 Nga → Việt",
21
- description="Mini translator for Notion"
22
  )
23
 
24
  iface.launch(server_name="0.0.0.0")
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import gradio as gr
3
 
4
+ model_name = "Helsinki-NLP/opus-mt-ru-vi"
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
 
9
  def translate(text):
10
+ inputs = tokenizer(text, return_tensors="pt")
11
+
12
+ outputs = model.generate(**inputs)
13
+
14
+ translated = tokenizer.decode(
15
+ outputs[0],
16
+ skip_special_tokens=True
17
+ )
18
+
19
+ return translated
20
 
21
  iface = gr.Interface(
22
  fn=translate,
 
24
  lines=2,
25
  placeholder="Nhập tiếng Nga..."
26
  ),
27
+ outputs="text",
28
+ title="🇷🇺 Nga → Việt"
 
29
  )
30
 
31
  iface.launch(server_name="0.0.0.0")