ranbac commited on
Commit
fbde2f0
·
verified ·
1 Parent(s): 7591c26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -55
app.py CHANGED
@@ -1,64 +1,45 @@
 
1
  import gradio as gr
2
- from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
 
3
 
4
- # 1. Tải mô hình và Tokenizer của mBART-50
5
- model_name = "facebook/mbart-large-50-many-to-many-mmt"
 
 
6
 
7
- print("Đang tải mô hình mBART-50, vui lòng đợi...")
8
- model = MBartForConditionalGeneration.from_pretrained(model_name)
9
- tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
10
- print("Tải mô hình thành công!")
11
-
12
- def translate_mbart(text, src_lang, tgt_lang):
13
  if not text.strip():
14
- return ""
15
-
16
- # 2. Bộ từ điển map ngôn ngữ giao diện sang mã chuẩn của mBART
17
- lang_map = {
18
- "Tiếng Trung": "zh_CN",
19
- "Tiếng Anh": "en_XX",
20
- "Tiếng Việt": "vi_VN"
21
- }
22
-
23
- # Lấy mã ngôn ngữ
24
- source_code = lang_map.get(src_lang, "zh_CN")
25
- target_code = lang_map.get(tgt_lang, "vi_VN")
26
 
27
- # 3. Ép Tokenizer hiểu ngôn ngữ đầu vào
28
- tokenizer.src_lang = source_code
29
- encoded_text = tokenizer(text, return_tensors="pt")
30
 
31
- # 4. Dịch sang ngôn ngữ đích
32
- generated_tokens = model.generate(
33
- **encoded_text,
34
- forced_bos_token_id=tokenizer.lang_code_to_id[target_code],
35
- max_length=512 # Tránh lỗi bị cắt đuôi câu
36
- )
37
 
38
- # 5. Giải mã kết quả
39
- result = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
40
- return result
41
 
42
- # ================= GIAO DIỆN GRADIO =================
43
- with gr.Blocks(theme=gr.themes.Soft()) as interface:
44
- gr.Markdown("## 🌍 Công cụ Dịch thuật Đa Ngôn Ngữ")
45
- gr.Markdown("Xử lý bởi **Meta mBART-50 Many-to-Many** - Chạy trên Hugging Face CPU.")
46
-
47
- with gr.Row():
48
- with gr.Column():
49
- src_lang = gr.Dropdown(choices=["Tiếng Trung", "Tiếng Anh", "Tiếng Việt"], value="Tiếng Trung", label="Ngôn ngữ gốc")
50
- input_text = gr.Textbox(lines=5, label="Văn bản cần dịch", placeholder="Ví dụ: 电台要选出一对最恩爱的夫妻。对比后,有三对夫妻入围。")
51
- with gr.Column():
52
- tgt_lang = gr.Dropdown(choices=["Tiếng Trung", "Tiếng Anh", "Tiếng Việt"], value="Tiếng Việt", label="Ngôn ngữ đích")
53
- output_text = gr.Textbox(lines=5, label="Kết quả dịch")
54
-
55
- translate_btn = gr.Button("🚀 Dịch ngay với mBART-50", variant="primary")
56
-
57
- translate_btn.click(
58
- fn=translate_mbart,
59
- inputs=[input_text, src_lang, tgt_lang],
60
- outputs=output_text
61
- )
62
 
63
- if __name__ == "__main__":
64
- interface.launch()
 
 
 
 
 
 
1
+ # app.py
2
  import gradio as gr
3
+ from transformers import MarianMTModel, MarianTokenizer
4
+ from pypinyin import lazy_pinyin, Style
5
 
6
+ # Load model
7
+ model_name = "Helsinki-NLP/opus-mt-zh-vi"
8
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
9
+ model = MarianMTModel.from_pretrained(model_name)
10
 
11
+ def translate_zh_vi(text):
 
 
 
 
 
12
  if not text.strip():
13
+ return "", ""
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Phiên âm Pinyin
16
+ pinyin = " ".join(lazy_pinyin(text, style=Style.TONE))
 
17
 
18
+ # Dịch sang tiếng Việt
19
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
20
+ translated = model.generate(**inputs)
21
+ vi_text = tokenizer.decode(translated[0], skip_special_tokens=True)
 
 
22
 
23
+ return pinyin, vi_text
 
 
24
 
25
+ demo = gr.Interface(
26
+ fn=translate_zh_vi,
27
+ inputs=gr.Textbox(label="Tiếng Trung (中文)", placeholder="Nhập văn bản tiếng Trung..."),
28
+ outputs=[
29
+ gr.Textbox(label="Phiên âm Pinyin"),
30
+ gr.Textbox(label="Dịch tiếng Việt")
31
+ ],
32
+ title="🈺 Dịch Trung - Việt + Pinyin",
33
+ examples=[["你好,世界!"], ["我爱学习中文"], ["今天天气很好"]]
34
+ )
35
+
36
+ demo.launch()
37
+ ```
 
 
 
 
 
 
 
38
 
39
+ **`requirements.txt`:**
40
+ ```
41
+ transformers>=4.30.0
42
+ sentencepiece
43
+ pypinyin
44
+ gradio
45
+ torch