Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 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 |
-
# 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 |
-
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
MODEL_NAME,
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 41 |
)
|
| 42 |
|
| 43 |
def vi_to_zh(text: str) -> str:
|
| 44 |
return _chat_translate(
|
| 45 |
-
|
| 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
|
| 51 |
-
|
| 52 |
with gr.Tab("ZH → VI"):
|
| 53 |
-
inp_zh = gr.Textbox(label="
|
| 54 |
-
out_vi = gr.Textbox(label="
|
| 55 |
-
|
| 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="
|
| 61 |
-
out_zh = gr.Textbox(label="
|
| 62 |
-
|
| 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; 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 và é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()
|