DoanMinhTri commited on
Commit
c358fb8
1 Parent(s): 0b6407f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ dict_map = {
7
+ "òa": "oà",
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
72
+ )
73
+ en_text = tokenizer_vi2en.batch_decode(output_ids, skip_special_tokens=True)
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
82
+ output_ids = model_en2vi.generate(
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
93
+ )
94
+ vi_text = tokenizer_en2vi.batch_decode(output_ids, skip_special_tokens=True)
95
+ vi_text = " ".join(vi_text)
96
+ return vi_text
97
+
98
+ vi_example_text = ["Cô 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.",
99
+ "cô 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()