jing-ju commited on
Commit
b3b7ed6
·
verified ·
1 Parent(s): 12ada15

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+
6
+ # Bạn có thể đổi MODEL_NAME tại "Variables" của Space thay vì sửa code
7
+ MODEL_NAME = os.getenv("MODEL_NAME", "tencent/Hunyuan-MT-Chimera-7B-fp8")
8
+
9
+ # Tham số sinh khuyến nghị theo model card
10
+ GEN_KW = dict(
11
+ max_new_tokens=256, # giữ thấp để bản thử CPU tránh quá chậm/thiếu RAM
12
+ top_k=20,
13
+ top_p=0.6,
14
+ repetition_penalty=1.05,
15
+ temperature=0.7,
16
+ do_sample=True
17
+ )
18
+
19
+ # Load model/tokenizer (trust_remote_code cho phép dùng chat template do model cung cấp)
20
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
21
+ # Trên CPU miễn phí, không có GPU → bỏ device_map="auto".
22
+ # Nếu bạn đổi Space sang GPU về sau, có thể thêm device_map="auto".
23
+ model = AutoModelForCausalLM.from_pretrained(
24
+ MODEL_NAME,
25
+ torch_dtype="auto",
26
+ trust_remote_code=True
27
+ )
28
+
29
+ def _chat_translate(prompt: str) -> str:
30
+ messages = [{"role": "user", "content": prompt}]
31
+ inputs = tokenizer.apply_chat_template(
32
+ messages, tokenize=True, add_generation_prompt=False, return_tensors="pt"
33
+ )
34
+ device = getattr(model, "device", torch.device("cpu"))
35
+ outputs = model.generate(inputs.to(device), **GEN_KW)
36
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
37
+
38
+ def zh_to_vi(text: str) -> str:
39
+ return _chat_translate(
40
+ f"Translate the following segment into Vietnamese, without additional explanation.\n\n{text}"
41
+ )
42
+
43
+ def vi_to_zh(text: str) -> str:
44
+ return _chat_translate(
45
+ f"Translate the following segment into Chinese, without additional explanation.\n\n{text}"
46
+ )
47
+
48
+ # ===== Gradio UI (2 tab) + API endpoints =====
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("## Hunyuan-MT Chimera-7B-fp8 (Trial on CPU) — ZH ⇄ VI\n⚠️ Bản thử nghiệm CPU: tốc độ chậm / có thể giới hạn độ dài.")
51
+
52
+ with gr.Tab("ZH → VI"):
53
+ inp_zh = gr.Textbox(label="Nhập tiếng Trung", lines=6, placeholder="Nhập đoạn tiếng Trung...")
54
+ out_vi = gr.Textbox(label="Bản dịch tiếng Việt", lines=6)
55
+ btn_zh = gr.Button("Dịch ZH→VI")
56
+ # api_name để client có thể gọi như một API
57
+ btn_zh.click(fn=zh_to_vi, inputs=inp_zh, outputs=out_vi, api_name="zh_vi")
58
+
59
+ with gr.Tab("VI → ZH"):
60
+ inp_vi = gr.Textbox(label="Nhập tiếng Việt", lines=6, placeholder="Nhập đoạn tiếng Việt...")
61
+ out_zh = gr.Textbox(label="Bản dịch tiếng Trung", lines=6)
62
+ btn_vi = gr.Button("Dịch VI→ZH")
63
+ btn_vi.click(fn=vi_to_zh, inputs=inp_vi, outputs=out_zh, api_name="vi_zh")
64
+
65
+ # Giới hạn hàng đợi để demo ít user (tránh quá tải)
66
+ demo.queue(concurrency_count=1, max_size=2).launch()