ranbac commited on
Commit
46018e0
·
verified ·
1 Parent(s): b943c80

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from pypinyin import pinyin, Style
4
+
5
+ # Tải mô hình dịch (sẽ tự động lưu vào cache của Space)
6
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-vi")
7
+
8
+ def process_chinese_text(text):
9
+ if not text.strip():
10
+ return "", ""
11
+
12
+ # Bước 1: Tạo phiên âm Pinyin
13
+ # Trả về dạng có dấu (ví dụ: nǐ hǎo)
14
+ pinyin_list = pinyin(text, style=Style.TONE)
15
+
16
+ # Gộp các âm tiết lại thành câu hoàn chỉnh
17
+ pinyin_result = " ".join([item[0] for item in pinyin_list])
18
+
19
+ # Bước 2: Dịch sang tiếng Việt
20
+ translation_result = translator(text)[0]['translation_text']
21
+
22
+ return pinyin_result, translation_result
23
+
24
+ # Tạo giao diện người dùng với Gradio
25
+ demo = gr.Interface(
26
+ fn=process_chinese_text,
27
+ inputs=gr.Textbox(lines=4, placeholder="Nhập văn bản tiếng Trung vào đây..."),
28
+ outputs=[
29
+ gr.Textbox(label="Phiên âm (Pinyin)"),
30
+ gr.Textbox(label="Bản dịch Tiếng Việt")
31
+ ],
32
+ title="Dịch & Phiên âm Trung - Việt",
33
+ description="Ứng dụng nhẹ, tối ưu chạy trên CPU. Sử dụng Helsinki-NLP và pypinyin.",
34
+ allow_flagging="never"
35
+ )
36
+
37
+ # Khởi chạy ứng dụng
38
+ if __name__ == "__main__":
39
+ demo.launch()