Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def stream_response(message, history):
|
| 17 |
messages = [{"role": "system", "content": "你是一个乐于助人的 AI 助手。"}]
|
| 18 |
for h in history:
|
|
@@ -25,21 +29,35 @@ def stream_response(message, history):
|
|
| 25 |
messages.append({"role": "assistant", "content": h[1]})
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
partial_text = ""
|
| 30 |
-
for
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
):
|
| 36 |
-
delta = chunk["choices"][0]["delta"]
|
| 37 |
-
if "content" in delta:
|
| 38 |
-
partial_text += delta["content"]
|
| 39 |
-
|
| 40 |
-
# --- 实时符号过滤(保留你原有的逻辑) ---
|
| 41 |
-
clean_display_text = re.sub(r'[\n\-\*\"""\"#]', '', partial_text)
|
| 42 |
-
yield clean_display_text
|
| 43 |
|
| 44 |
# 4. 界面设置
|
| 45 |
demo = gr.ChatInterface(
|
|
@@ -50,4 +68,4 @@ demo = gr.ChatInterface(
|
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
demo.launch(
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import re
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
+
from threading import Thread
|
| 6 |
+
|
| 7 |
+
# 1. 设置 CPU 线程数
|
| 8 |
+
torch.set_num_threads(2)
|
| 9 |
+
|
| 10 |
+
# 2. 加载模型(float32 兼容 CPU,0.5B 体积小速度快)
|
| 11 |
+
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
torch_dtype=torch.float32,
|
| 16 |
+
device_map="cpu",
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# 3. 流式生成函数
|
| 20 |
def stream_response(message, history):
|
| 21 |
messages = [{"role": "system", "content": "你是一个乐于助人的 AI 助手。"}]
|
| 22 |
for h in history:
|
|
|
|
| 29 |
messages.append({"role": "assistant", "content": h[1]})
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
+
model_inputs = tokenizer.apply_chat_template(
|
| 33 |
+
messages,
|
| 34 |
+
add_generation_prompt=True,
|
| 35 |
+
return_tensors="pt",
|
| 36 |
+
return_dict=True,
|
| 37 |
+
).to("cpu")
|
| 38 |
+
|
| 39 |
+
streamer = TextIteratorStreamer(
|
| 40 |
+
tokenizer, skip_prompt=True, skip_special_tokens=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
generate_kwargs = {
|
| 44 |
+
**model_inputs,
|
| 45 |
+
"streamer": streamer,
|
| 46 |
+
"max_new_tokens": 512,
|
| 47 |
+
"do_sample": True,
|
| 48 |
+
"temperature": 0.7,
|
| 49 |
+
"pad_token_id": tokenizer.eos_token_id,
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 53 |
+
thread.start()
|
| 54 |
+
|
| 55 |
partial_text = ""
|
| 56 |
+
for new_text in streamer:
|
| 57 |
+
partial_text += new_text
|
| 58 |
+
clean_display_text = re.sub(r'[
|
| 59 |
+
\-\*\"""""#]', '', partial_text)
|
| 60 |
+
yield clean_display_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# 4. 界面设置
|
| 63 |
demo = gr.ChatInterface(
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|