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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -2,28 +2,31 @@ 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:
@@ -37,30 +40,23 @@ def _chat_translate(prompt: str) -> str:
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()
 
2
  import gradio as gr
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ # 👇 import config lượng tử hoá cho compressed-tensors
6
+ from transformers import CompressedTensorsQuantizationConfig
7
 
 
8
  MODEL_NAME = os.getenv("MODEL_NAME", "tencent/Hunyuan-MT-Chimera-7B-fp8")
9
 
10
+ # Tham số sinh gợi ý
11
  GEN_KW = dict(
12
+ max_new_tokens=256, # hạ thấp cho CPU free; thể tăng nếu ổn
13
+ top_k=20, top_p=0.6, repetition_penalty=1.05, temperature=0.7, do_sample=True
 
 
 
 
14
  )
15
 
 
16
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
17
+
18
+ # Truyền quantization_config ép ignore=[] để tránh NoneType
19
+ ctq = CompressedTensorsQuantizationConfig(
20
+ quantization_method="fp8", # để transformers chọn đúng quantizer
21
+ ignore=[] # <-- chìa khoá sửa lỗi
22
+ )
23
+
24
  model = AutoModelForCausalLM.from_pretrained(
25
  MODEL_NAME,
26
+ trust_remote_code=True,
27
+ quantization_config=ctq, # <-- dùng ctq thay vì mặc định trong config
28
+ # Không đặt device_map="auto" nếu bạn đang ở CPU Space
29
+ # dtype có thể để "auto"; cảnh báo "torch_dtype deprecated" có thể bỏ qua
30
  )
31
 
32
  def _chat_translate(prompt: str) -> str:
 
40
 
41
  def zh_to_vi(text: str) -> str:
42
  return _chat_translate(
43
+ "Translate the following segment into Vietnamese, without additional explanation.\n\n" + text
44
  )
45
 
46
  def vi_to_zh(text: str) -> str:
47
  return _chat_translate(
48
+ "Translate the following segment into Chinese, without additional explanation.\n\n" + text
49
  )
50
 
 
51
  with gr.Blocks() as demo:
52
+ gr.Markdown("## Hunyuan-MT Chimera-7B-fp8 (Trial on CPU) — ZH ⇄ VI")
 
53
  with gr.Tab("ZH → VI"):
54
+ inp_zh = gr.Textbox(label="Tiếng Trung", lines=6)
55
+ out_vi = gr.Textbox(label="Tiếng Việt", lines=6)
56
+ gr.Button("Dịch ZH→VI").click(zh_to_vi, inputs=inp_zh, outputs=out_vi, api_name="zh_vi")
 
 
 
57
  with gr.Tab("VI → ZH"):
58
+ inp_vi = gr.Textbox(label="Tiếng Việt", lines=6)
59
+ out_zh = gr.Textbox(label="Tiếng Trung", lines=6)
60
+ gr.Button("Dịch VI→ZH").click(vi_to_zh, inputs=inp_vi, outputs=out_zh, api_name="vi_zh")
 
61
 
 
62
  demo.queue(concurrency_count=1, max_size=2).launch()