Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
# 加载模型和分词器(这会在Space启动时执行)
|
6 |
-
print("正在加载模型和分词器...")
|
7 |
-
try:
|
8 |
-
model_name = "ZhangQiao123/medical-model-grpo-16bit"
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
11 |
-
model_name,
|
12 |
-
torch_dtype=torch.float16,
|
13 |
-
device_map="auto"
|
14 |
-
)
|
15 |
-
print("模型加载成功!")
|
16 |
-
except Exception as e:
|
17 |
-
print(f"模型加载失败: {e}")
|
18 |
-
model = None
|
19 |
-
tokenizer = None
|
20 |
-
|
21 |
-
def generate_medical_answer(question):
|
22 |
-
if model is None or tokenizer is None:
|
23 |
-
return "模型加载失败,无法生成回答。"
|
24 |
-
|
25 |
-
system_prompt = "你是一个专业的医学AI助手,请根据问题提供详细的医学分析和建议。请先进行推理分析,然后给出最终答案。请使用<reasoning>标签包裹推理过程,使用<answer>标签包裹最终答案。"
|
26 |
-
|
27 |
-
prompt = f"{system_prompt}\n\n问题: {question}\n\n回答:"
|
28 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
29 |
-
|
30 |
-
try:
|
31 |
-
with torch.no_grad():
|
32 |
-
outputs = model.generate(
|
33 |
-
**inputs,
|
34 |
-
max_new_tokens=1024,
|
35 |
-
temperature=0.7,
|
36 |
-
do_sample=True,
|
37 |
-
top_p=0.9,
|
38 |
-
repetition_penalty=1.1
|
39 |
-
)
|
40 |
-
|
41 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
-
|
43 |
-
if "回答:" in response:
|
44 |
-
response = response.split("回答:")[-1].strip()
|
45 |
-
else:
|
46 |
-
response = response[len(prompt):].strip()
|
47 |
-
|
48 |
-
return response
|
49 |
-
except Exception as e:
|
50 |
-
return f"生成回答时出错: {e}"
|
51 |
|
52 |
# 创建Gradio界面
|
53 |
with gr.Blocks() as demo:
|
54 |
gr.Markdown("# 医学AI助手")
|
55 |
gr.Markdown("基于GRPO微调的医学模型,可以回答医学相关问题并提供详细的推理过程和答案。")
|
|
|
56 |
|
57 |
chatbot = gr.Chatbot(height=500)
|
58 |
msg = gr.Textbox(placeholder="请输入您的医学问题...", label="问题")
|
59 |
clear = gr.Button("清除对话")
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def respond(message, chat_history):
|
62 |
if not message:
|
63 |
return "", chat_history
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# 格式化输出
|
68 |
formatted_response = bot_message.replace("<reasoning>", "<b>推理过程:</b><br>")
|
@@ -70,7 +71,14 @@ with gr.Blocks() as demo:
|
|
70 |
formatted_response = formatted_response.replace("<answer>", "<b>最终答案:</b><br>")
|
71 |
formatted_response = formatted_response.replace("</answer>", "")
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
return "", chat_history
|
75 |
|
76 |
def clear_chatbot():
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# 创建Gradio界面
|
6 |
with gr.Blocks() as demo:
|
7 |
gr.Markdown("# 医学AI助手")
|
8 |
gr.Markdown("基于GRPO微调的医学模型,可以回答医学相关问题并提供详细的推理过程和答案。")
|
9 |
+
gr.Markdown("⚠️ 注意:当前为演示模式,未加载实际模型。回答为模拟生成,仅供界面展示。")
|
10 |
|
11 |
chatbot = gr.Chatbot(height=500)
|
12 |
msg = gr.Textbox(placeholder="请输入您的医学问题...", label="问题")
|
13 |
clear = gr.Button("清除对话")
|
14 |
|
15 |
+
# 预设的医学回答模板
|
16 |
+
medical_templates = [
|
17 |
+
"""<reasoning>
|
18 |
+
对于"{question}"这个问题,我需要从以下几个方面进行分析:
|
19 |
+
1. 症状描述的完整性和特点
|
20 |
+
2. 可能的病因和相关疾病
|
21 |
+
3. 需要考虑的鉴别诊断
|
22 |
+
4. 建议的检查和治疗方案
|
23 |
+
|
24 |
+
基于提供的信息,这可能与以下几种情况有关:
|
25 |
+
- 常见病因A及其特点
|
26 |
+
- 可能的并发症B及其表现
|
27 |
+
- 需要排除的严重情况C
|
28 |
+
</reasoning>
|
29 |
+
|
30 |
+
<answer>
|
31 |
+
针对您描述的"{question}"问题,这是一个模拟的医学回答。在实际部署时,这里会显示由医学模型生成的专业回答,包含详细的分析和建议。
|
32 |
+
|
33 |
+
请注意,这只是一个界面演示,没有加载实际的医学模型。真实的医学建议应当由专业医生提供。
|
34 |
+
</answer>""",
|
35 |
+
|
36 |
+
"""<reasoning>
|
37 |
+
关于"{question}",我需要考虑:
|
38 |
+
1. 患者的基本情况(年龄、性别、既往史)
|
39 |
+
2. 症状的持续时间和严重程度
|
40 |
+
3. 是否有加重或缓解因素
|
41 |
+
4. 相关的实验室和影像学检查结果
|
42 |
+
|
43 |
+
可能的诊断包括:
|
44 |
+
- X疾病:特点是...
|
45 |
+
- Y综合征:通常表现为...
|
46 |
+
- Z病变:需要通过...检查确诊
|
47 |
+
</reasoning>
|
48 |
+
|
49 |
+
<answer>
|
50 |
+
这是一个关于"{question}"的模拟医学回答。实际部署时,这里将显示由经过GRPO训练的医学模型生成的专业回答。
|
51 |
+
|
52 |
+
该演示界面目前未连接到实际模型,仅用于展示用户界面的功能和交互方式。
|
53 |
+
</answer>"""
|
54 |
+
]
|
55 |
+
|
56 |
def respond(message, chat_history):
|
57 |
if not message:
|
58 |
return "", chat_history
|
59 |
+
|
60 |
+
# 模拟思考时间
|
61 |
+
yield "", chat_history + [("", "思考中...")]
|
62 |
+
time.sleep(1)
|
63 |
+
|
64 |
+
# 随机选择一个模板并填充问题
|
65 |
+
template = random.choice(medical_templates)
|
66 |
+
bot_message = template.format(question=message)
|
67 |
|
68 |
# 格式化输出
|
69 |
formatted_response = bot_message.replace("<reasoning>", "<b>推理过程:</b><br>")
|
|
|
71 |
formatted_response = formatted_response.replace("<answer>", "<b>最终答案:</b><br>")
|
72 |
formatted_response = formatted_response.replace("</answer>", "")
|
73 |
|
74 |
+
# 模拟打字效果
|
75 |
+
chat_history.append((message, ""))
|
76 |
+
for i in range(1, len(formatted_response), 10):
|
77 |
+
time.sleep(0.05)
|
78 |
+
chat_history[-1] = (message, formatted_response[:i])
|
79 |
+
yield "", chat_history
|
80 |
+
|
81 |
+
chat_history[-1] = (message, formatted_response)
|
82 |
return "", chat_history
|
83 |
|
84 |
def clear_chatbot():
|