DoanMinhTri commited on
Commit
77e422b
·
1 Parent(s): c358fb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -73
app.py CHANGED
@@ -1,71 +1,20 @@
1
- # coding=utf-8
2
-
3
  import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
 
6
- dict_map = {
7
- "òa": "",
8
- "Òa": "Oà",
9
- "ÒA": "OÀ",
10
- "óa": "oá",
11
- "Óa": "Oá",
12
- "ÓA": "OÁ",
13
- "ỏa": "oả",
14
- "Ỏa": "Oả",
15
- "ỎA": "OẢ",
16
- "õa": "oã",
17
- "Õa": "Oã",
18
- "ÕA": "OÃ",
19
- "ọa": "oạ",
20
- "Ọa": "Oạ",
21
- "ỌA": "OẠ",
22
- "òe": "oè",
23
- "Òe": "Oè",
24
- "ÒE": "OÈ",
25
- "óe": "oé",
26
- "Óe": "Oé",
27
- "ÓE": "OÉ",
28
- "ỏe": "oẻ",
29
- "Ỏe": "Oẻ",
30
- "ỎE": "OẺ",
31
- "õe": "oẽ",
32
- "Õe": "Oẽ",
33
- "ÕE": "OẼ",
34
- "ọe": "oẹ",
35
- "Ọe": "Oẹ",
36
- "ỌE": "OẸ",
37
- "ùy": "uỳ",
38
- "Ùy": "Uỳ",
39
- "ÙY": "UỲ",
40
- "úy": "uý",
41
- "Úy": "Uý",
42
- "ÚY": "UÝ",
43
- "ủy": "uỷ",
44
- "Ủy": "Uỷ",
45
- "ỦY": "UỶ",
46
- "ũy": "uỹ",
47
- "Ũy": "Uỹ",
48
- "ŨY": "UỸ",
49
- "ụy": "uỵ",
50
- "Ụy": "Uỵ",
51
- "ỤY": "UỴ",
52
- }
53
-
54
- tokenizer_vi2en = AutoTokenizer.from_pretrained("vinai/vinai-translate-vi2en-v2", src_lang="vi_VN")
55
- model_vi2en = AutoModelForSeq2SeqLM.from_pretrained("vinai/vinai-translate-vi2en-v2")
56
 
57
  def translate_vi2en(vi_text: str) -> str:
58
- for i, j in dict_map.items():
59
- vi_text = vi_text.replace(i, j)
60
  input_ids = tokenizer_vi2en(vi_text, return_tensors="pt").input_ids
61
  output_ids = model_vi2en.generate(
62
  input_ids,
63
  decoder_start_token_id=tokenizer_vi2en.lang_code_to_id["en_XX"],
64
  num_return_sequences=1,
65
  # # With sampling
66
- # do_sample=True,
67
- # top_k=100,
68
- # top_p=0.8,
69
  # With beam search
70
  num_beams=5,
71
  early_stopping=True
@@ -74,8 +23,9 @@ def translate_vi2en(vi_text: str) -> str:
74
  en_text = " ".join(en_text)
75
  return en_text
76
 
77
- tokenizer_en2vi = AutoTokenizer.from_pretrained("vinai/vinai-translate-en2vi-v2", src_lang="en_XX")
78
- model_en2vi = AutoModelForSeq2SeqLM.from_pretrained("vinai/vinai-translate-en2vi-v2")
 
79
 
80
  def translate_en2vi(en_text: str) -> str:
81
  input_ids = tokenizer_en2vi(en_text, return_tensors="pt").input_ids
@@ -83,10 +33,10 @@ def translate_en2vi(en_text: str) -> str:
83
  input_ids,
84
  decoder_start_token_id=tokenizer_en2vi.lang_code_to_id["vi_VN"],
85
  num_return_sequences=1,
86
- # # With sampling
87
- # do_sample=True,
88
- # top_k=100,
89
- # top_p=0.8,
90
  # With beam search
91
  num_beams=5,
92
  early_stopping=True
@@ -95,35 +45,46 @@ def translate_en2vi(en_text: str) -> str:
95
  vi_text = " ".join(vi_text)
96
  return vi_text
97
 
98
- vi_example_text = [" cho biết: trước giờ tôi không đến phòng tập công cộng, tập cùng giáo viên Yoga riêng hoặc tự tập ở nhà. Khi tập thể dục trong không gian riêng tư, tôi thoải mái dễ chịu hơn.",
99
- " cho biết trước giờ tôi không đến phòng tập công cộng mà tập cùng giáo viên yoga riêng hoặc tự tập ở nhà khi tập thể dục trong không gian riêng tư tôi thoải mái dễ chịu hơn"]
 
 
 
100
 
101
- en_example_text = ["I haven't been to a public gym before. When I exercise in a private space, I feel more comfortable.",
102
- "i haven't been to a public gym before when i exercise in a private space i feel more comfortable"]
 
 
 
103
 
104
- with gr.Blocks() as demo:
 
 
 
105
  with gr.Tabs():
106
- with gr.TabItem("Vietnamese to English"):
107
  with gr.Row():
108
  with gr.Column():
109
  vietnamese = gr.Textbox(label="Vietnamese Text")
110
- translate_to_english = gr.Button(value="Translate To English")
111
  with gr.Column():
112
  english = gr.Textbox(label="English Text")
 
113
  translate_to_english.click(lambda text: translate_vi2en(text), inputs=vietnamese, outputs=english)
114
  gr.Examples(examples=vi_example_text,
115
  inputs=[vietnamese])
116
 
117
- with gr.TabItem("English to Vietnamese"):
118
  with gr.Row():
119
  with gr.Column():
120
  english = gr.Textbox(label="English Text")
121
- translate_to_vietnamese = gr.Button(value="Translate To Vietnamese")
122
  with gr.Column():
123
  vietnamese = gr.Textbox(label="Vietnamese Text")
 
124
  translate_to_vietnamese.click(lambda text: translate_en2vi(text), inputs=english, outputs=vietnamese)
125
  gr.Examples(examples=en_example_text,
126
  inputs=[english])
127
 
128
  if __name__ == "__main__":
129
- demo.launch()
 
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
+ md_name1 = "vinai/vinai-translate-vi2en-v2"
5
+ tokenizer_vi2en = AutoTokenizer.from_pretrained(md_name1, src_lang="vi_VN")
6
+ model_vi2en = AutoModelForSeq2SeqLM.from_pretrained(md_name1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def translate_vi2en(vi_text: str) -> str:
 
 
9
  input_ids = tokenizer_vi2en(vi_text, return_tensors="pt").input_ids
10
  output_ids = model_vi2en.generate(
11
  input_ids,
12
  decoder_start_token_id=tokenizer_vi2en.lang_code_to_id["en_XX"],
13
  num_return_sequences=1,
14
  # # With sampling
15
+ do_sample=True,
16
+ top_k=100,
17
+ top_p=0.8,
18
  # With beam search
19
  num_beams=5,
20
  early_stopping=True
 
23
  en_text = " ".join(en_text)
24
  return en_text
25
 
26
+ md_name2 = "vinai/vinai-translate-en2vi-v2"
27
+ tokenizer_en2vi = AutoTokenizer.from_pretrained(md_name2, src_lang="en_XX")
28
+ model_en2vi = AutoModelForSeq2SeqLM.from_pretrained(md_name2)
29
 
30
  def translate_en2vi(en_text: str) -> str:
31
  input_ids = tokenizer_en2vi(en_text, return_tensors="pt").input_ids
 
33
  input_ids,
34
  decoder_start_token_id=tokenizer_en2vi.lang_code_to_id["vi_VN"],
35
  num_return_sequences=1,
36
+ # With sampling
37
+ do_sample=True,
38
+ top_k=100,
39
+ top_p=0.8,
40
  # With beam search
41
  num_beams=5,
42
  early_stopping=True
 
45
  vi_text = " ".join(vi_text)
46
  return vi_text
47
 
48
+ vi_example_text = ["Xin chào, chúng tôi nhóm 01, bao gồm 3 thành viên: Minh Trí, Kim Thanh Hồng Ngọc",
49
+ "Chúng ta đang từng bước học cách trở nên tốt đẹp hơn!",
50
+ "Bạn có phải là người chăm chỉ?",
51
+ "Luận văn thạc sĩ Khoa học Máy tính",
52
+ "Hãy sống như những đoá hoa toả ngát hương thơm"]
53
 
54
+ en_example_text = ["Life is countless days of trying.",
55
+ "Always remember, what doesn't kill you makes you stronger",
56
+ "What's up man?",
57
+ "How could you...?",
58
+ "Could you do me a favor?"]
59
 
60
+ # GIAO DIỆN WEB MACHINE TRANSLATION
61
+ with gr.Blocks(theme=gr.themes.Soft(), title="Charmed's One MT") as demo:
62
+ with gr.Row():
63
+ test = gr.Text(label="MACHINE TRANSLATION", value="The Application of English-Vietnamese automatic translation was created by The Power of Three: Doan Minh Tri, Che Thi Kim Thanh and Nguyen Thi Hong Ngoc",)
64
  with gr.Tabs():
65
+ with gr.TabItem("VIETNAMESE TO ENGLISH"):
66
  with gr.Row():
67
  with gr.Column():
68
  vietnamese = gr.Textbox(label="Vietnamese Text")
69
+ gr.ClearButton(vietnamese)
70
  with gr.Column():
71
  english = gr.Textbox(label="English Text")
72
+ translate_to_english = gr.Button(value="Translate To English")
73
  translate_to_english.click(lambda text: translate_vi2en(text), inputs=vietnamese, outputs=english)
74
  gr.Examples(examples=vi_example_text,
75
  inputs=[vietnamese])
76
 
77
+ with gr.TabItem("ENGLISH TO VIETNAMESE"):
78
  with gr.Row():
79
  with gr.Column():
80
  english = gr.Textbox(label="English Text")
81
+ gr.ClearButton(english)
82
  with gr.Column():
83
  vietnamese = gr.Textbox(label="Vietnamese Text")
84
+ translate_to_vietnamese = gr.Button(value="Translate To Vietnamese")
85
  translate_to_vietnamese.click(lambda text: translate_en2vi(text), inputs=english, outputs=vietnamese)
86
  gr.Examples(examples=en_example_text,
87
  inputs=[english])
88
 
89
  if __name__ == "__main__":
90
+ demo.launch(share=True) #share=True NẾU MUỐN ONLINE