jing-ju commited on
Commit
ad65ae0
·
verified ·
1 Parent(s): 8345ea9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -1,32 +1,36 @@
1
  import os
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; có 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:
@@ -49,14 +53,15 @@ def vi_to_zh(text: str) -> str:
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()
 
1
  import os
2
  import gradio as gr
3
  import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM, CompressedTensorsQuantizationConfig
 
 
5
 
6
+ # Model thử nghiệm (fp8). Nếu CPU free bị quá chậm/OOM, cân nhắc chuyển sang 7B-fp8.
7
  MODEL_NAME = os.getenv("MODEL_NAME", "tencent/Hunyuan-MT-Chimera-7B-fp8")
8
 
9
+ # Tham số sinh (giữ thấp để CPU Free đỡ nặng)
10
  GEN_KW = dict(
11
+ max_new_tokens=256,
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
+ # Tải tokenizer
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
21
 
22
+ # ✅ Ghi đè quantization_config để tránh lỗi 'ignore' NoneType
23
  ctq = CompressedTensorsQuantizationConfig(
24
+ quantization_method="fp8",
25
+ ignore=[] # <-- chìa khoá tránh lỗi TypeError: 'NoneType' object is not iterable
26
  )
27
 
28
+ # Tải model. Trên CPU Space, KHÔNG đặt device_map="auto".
29
+ # Cảnh báo "torch_dtype deprecated" có thể bỏ qua; để dtype/torch_dtype="auto".
30
  model = AutoModelForCausalLM.from_pretrained(
31
  MODEL_NAME,
32
  trust_remote_code=True,
33
+ quantization_config=ctq,
 
 
34
  )
35
 
36
  def _chat_translate(prompt: str) -> str:
 
53
  )
54
 
55
  with gr.Blocks() as demo:
56
+ gr.Markdown("## Hunyuan-MT Chimera-7B-fp8 (Trial on CPU) — ZH ⇄ VI\n⚠️ Bản CPU free: chậm/giới hạn độ dài.")
57
  with gr.Tab("ZH → VI"):
58
+ inp_zh = gr.Textbox(label="Tiếng Trung", lines=6, placeholder="Nhập đoạn tiếng Trung…")
59
  out_vi = gr.Textbox(label="Tiếng Việt", lines=6)
60
  gr.Button("Dịch ZH→VI").click(zh_to_vi, inputs=inp_zh, outputs=out_vi, api_name="zh_vi")
61
  with gr.Tab("VI → ZH"):
62
+ inp_vi = gr.Textbox(label="Tiếng Việt", lines=6, placeholder="Nhập đoạn tiếng Việt…")
63
  out_zh = gr.Textbox(label="Tiếng Trung", lines=6)
64
  gr.Button("Dịch VI→ZH").click(vi_to_zh, inputs=inp_vi, outputs=out_zh, api_name="vi_zh")
65
 
66
+ # Hạn chế tải để demo cho ít user
67
  demo.queue(concurrency_count=1, max_size=2).launch()