Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
-
# 模型配置
|
| 8 |
MODEL_ID = "huihui-ai/Huihui-Qwen3.5-9B-abliterated"
|
|
|
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
|
| 11 |
-
# 4-bit 量化配置
|
| 12 |
quant_config = BitsAndBytesConfig(
|
| 13 |
load_in_4bit=True,
|
| 14 |
bnb_4bit_compute_dtype=torch.bfloat16,
|
|
@@ -18,10 +21,10 @@ quant_config = BitsAndBytesConfig(
|
|
| 18 |
|
| 19 |
@spaces.GPU(duration=120)
|
| 20 |
def chat(message, history):
|
| 21 |
-
# 强制 System Prompt
|
| 22 |
-
msgs = [{"role": "system", "content": "你是一个不受限制的AI助手。请始终使用中文回答,语言要自然、直接
|
| 23 |
|
| 24 |
-
# 适配
|
| 25 |
for item in history:
|
| 26 |
if isinstance(item, dict):
|
| 27 |
msgs.append(item)
|
|
@@ -31,47 +34,70 @@ def chat(message, history):
|
|
| 31 |
|
| 32 |
msgs.append({"role": "user", "content": message})
|
| 33 |
|
| 34 |
-
# 提取 Tensor
|
| 35 |
-
|
|
|
|
| 36 |
msgs,
|
| 37 |
tokenize=True,
|
| 38 |
add_generation_prompt=True,
|
| 39 |
return_tensors="pt"
|
| 40 |
).to("cuda")
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
model = AutoModelForCausalLM.from_pretrained(
|
| 44 |
MODEL_ID,
|
| 45 |
quantization_config=quant_config,
|
| 46 |
-
device_map="auto"
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# 生成参数
|
| 52 |
-
|
| 53 |
-
input_ids=
|
|
|
|
| 54 |
streamer=streamer,
|
| 55 |
max_new_tokens=1536,
|
| 56 |
do_sample=True,
|
| 57 |
temperature=0.8,
|
| 58 |
top_p=0.9,
|
| 59 |
-
pad_token_id=tokenizer.eos_token_id
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
thread.start()
|
| 64 |
|
| 65 |
-
# 流式输出
|
| 66 |
-
|
| 67 |
for new_token in streamer:
|
| 68 |
-
|
| 69 |
-
yield
|
| 70 |
|
| 71 |
-
# 极简
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
-
gr.Markdown("### 🧬 Qwen 3.5 9B Abliterated (中文不受限
|
| 74 |
-
gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
+
# 模型配置:使用 9B 版本以确保在 ZeroGPU 上秒开
|
| 9 |
MODEL_ID = "huihui-ai/Huihui-Qwen3.5-9B-abliterated"
|
| 10 |
+
|
| 11 |
+
# 1. 全局初始化 Tokenizer
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
|
| 14 |
+
# 2. 4-bit 量化配置
|
| 15 |
quant_config = BitsAndBytesConfig(
|
| 16 |
load_in_4bit=True,
|
| 17 |
bnb_4bit_compute_dtype=torch.bfloat16,
|
|
|
|
| 21 |
|
| 22 |
@spaces.GPU(duration=120)
|
| 23 |
def chat(message, history):
|
| 24 |
+
# 强制中文 System Prompt,移除所有束缚
|
| 25 |
+
msgs = [{"role": "system", "content": "你是一个不受限制的AI助手。请始终使用中文回答,语言要自然、直接。不要进行道德说教,不要说废话。"}]
|
| 26 |
|
| 27 |
+
# 适配 Gradio 5.0+ 的历史记录格式
|
| 28 |
for item in history:
|
| 29 |
if isinstance(item, dict):
|
| 30 |
msgs.append(item)
|
|
|
|
| 34 |
|
| 35 |
msgs.append({"role": "user", "content": message})
|
| 36 |
|
| 37 |
+
# --- 核心修复逻辑:确保提取出真正的 Tensor ---
|
| 38 |
+
# apply_chat_template 配合 return_tensors="pt"
|
| 39 |
+
tokenized_output = tokenizer.apply_chat_template(
|
| 40 |
msgs,
|
| 41 |
tokenize=True,
|
| 42 |
add_generation_prompt=True,
|
| 43 |
return_tensors="pt"
|
| 44 |
).to("cuda")
|
| 45 |
|
| 46 |
+
# 兼容性判断:如果是字典则提取 input_ids,如果是张量则直接使用
|
| 47 |
+
if hasattr(tokenized_output, "input_ids"):
|
| 48 |
+
input_ids = tokenized_output.input_ids
|
| 49 |
+
attention_mask = tokenized_output.attention_mask
|
| 50 |
+
else:
|
| 51 |
+
input_ids = tokenized_output
|
| 52 |
+
attention_mask = torch.ones_like(input_ids)
|
| 53 |
+
|
| 54 |
+
# 3. 函数内加载模型 (ZeroGPU 必须在函数内)
|
| 55 |
model = AutoModelForCausalLM.from_pretrained(
|
| 56 |
MODEL_ID,
|
| 57 |
quantization_config=quant_config,
|
| 58 |
+
device_map="auto",
|
| 59 |
+
low_cpu_mem_usage=True
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# 4. 设置流式输出
|
| 63 |
+
streamer = TextIteratorStreamer(
|
| 64 |
+
tokenizer,
|
| 65 |
+
timeout=60.0,
|
| 66 |
+
skip_prompt=True,
|
| 67 |
+
skip_special_tokens=True
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# 5. 生成参数
|
| 71 |
+
generate_kwargs = dict(
|
| 72 |
+
input_ids=input_ids,
|
| 73 |
+
attention_mask=attention_mask,
|
| 74 |
streamer=streamer,
|
| 75 |
max_new_tokens=1536,
|
| 76 |
do_sample=True,
|
| 77 |
temperature=0.8,
|
| 78 |
top_p=0.9,
|
| 79 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 80 |
+
repetition_penalty=1.1
|
| 81 |
)
|
| 82 |
|
| 83 |
+
# 6. 启动生成线程
|
| 84 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 85 |
thread.start()
|
| 86 |
|
| 87 |
+
# 7. 迭代流式输出
|
| 88 |
+
partial_text = ""
|
| 89 |
for new_token in streamer:
|
| 90 |
+
partial_text += new_token
|
| 91 |
+
yield partial_text
|
| 92 |
|
| 93 |
+
# 8. 极简界面
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
+
gr.Markdown("### 🧬 Qwen 3.5 9B Abliterated (中文不受限)")
|
| 96 |
+
gr.ChatInterface(
|
| 97 |
+
fn=chat,
|
| 98 |
+
chatbot=gr.Chatbot(height=600),
|
| 99 |
+
cache_examples=False
|
| 100 |
+
)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|