Spaces:
Sleeping
Sleeping
更新应用:添加了思考过程和推理结论的格式化显示
Browse files
app.py
CHANGED
@@ -1,31 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
import time
|
|
|
3 |
from typing import Iterator, List, Tuple, Any
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
""
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
def stream_response(message: str, history: List[Tuple[str, str]]) -> Iterator[List[Tuple[str, Any]]]:
|
16 |
"""流式响应并格式化为Gradio Chatbot需要的格式"""
|
17 |
# 添加用户消息到历史
|
18 |
history = history + [(message, "")]
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
response_generator = simulate_stream_response(message)
|
23 |
|
24 |
-
for
|
25 |
-
bot_message += token
|
26 |
# 更新最后一条消息的AI回复部分
|
27 |
updated_history = history.copy()
|
28 |
-
updated_history[-1] = (message,
|
29 |
yield updated_history
|
30 |
|
31 |
# 创建自定义CSS样式
|
@@ -33,17 +88,40 @@ custom_css = """
|
|
33 |
.gradio-container {
|
34 |
font-family: 'Arial', sans-serif;
|
35 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
"""
|
37 |
|
38 |
# 创建Gradio界面
|
39 |
def create_demo():
|
40 |
with gr.Blocks(css=custom_css) as demo:
|
41 |
-
gr.Markdown("# AI 问答助手")
|
42 |
-
gr.Markdown("
|
43 |
|
44 |
chatbot = gr.Chatbot(
|
45 |
show_label=False,
|
46 |
height=500,
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
with gr.Row():
|
|
|
1 |
import gradio as gr
|
2 |
import time
|
3 |
+
import re
|
4 |
from typing import Iterator, List, Tuple, Any
|
5 |
+
from openai import OpenAI
|
6 |
|
7 |
+
# 初始化OpenAI客户端
|
8 |
+
client = OpenAI(
|
9 |
+
api_key = "37793d47-dfd4-4b3d-9cbc-8e4b32119033",
|
10 |
+
base_url = "https://ark.cn-beijing.volces.com/api/v3",
|
11 |
+
)
|
12 |
+
|
13 |
+
# 处理API响应,美化输出格式
|
14 |
+
def process_response(text):
|
15 |
+
"""移除标签并添加格式化的HTML"""
|
16 |
+
# 检测并处理thinking标签
|
17 |
+
thinking_match = re.search(r'<thinking>([\s\S]*?)<\/thinking>', text)
|
18 |
+
reasoning_match = re.search(r'<reasoning>([\s\S]*?)<\/reasoning>', text)
|
19 |
+
|
20 |
+
# 如果找到完整的thinking或reasoning标签,重新格式化整个文本
|
21 |
+
if thinking_match or reasoning_match:
|
22 |
+
# 提取思考和推理部分
|
23 |
+
thinking_content = thinking_match.group(1) if thinking_match else ""
|
24 |
+
reasoning_content = reasoning_match.group(1) if reasoning_match else ""
|
25 |
+
|
26 |
+
# 构建新的格式化文本
|
27 |
+
formatted_text = ""
|
28 |
+
if thinking_content:
|
29 |
+
formatted_text += f"<div class='section thinking-section'>思考过程:{thinking_content}</div>"
|
30 |
+
if reasoning_content:
|
31 |
+
formatted_text += f"<div class='section reasoning-section'><strong>推理结论:{reasoning_content}</strong></div>"
|
32 |
+
|
33 |
+
return formatted_text
|
34 |
|
35 |
+
# 如果没有找到完整标签,保留原文
|
36 |
+
return text
|
37 |
+
|
38 |
+
# 实际API流式响应
|
39 |
+
def api_stream_response(message: str) -> Iterator[str]:
|
40 |
+
"""调用实际API的流式响应"""
|
41 |
+
try:
|
42 |
+
# 流式调用API
|
43 |
+
stream = client.chat.completions.create(
|
44 |
+
model = "deepseek-r1-250120",
|
45 |
+
messages = [
|
46 |
+
{"role": "system", "content": "你是材料科学领域的AI助手,专注于材料管道相关问题。请将回答分为两个部分:\n1. <thinking>思考过程,分析问题和组织思路</thinking>\n2. <reasoning>推理过程,包含最终结论和完整答案</reasoning>\n请确保每个回答都包含这两个部分,并用正确的标签包裹。"},
|
47 |
+
{"role": "user", "content": message},
|
48 |
+
],
|
49 |
+
stream=True, # 启用流式输出
|
50 |
+
)
|
51 |
+
|
52 |
+
# 用于累积响应以处理标签
|
53 |
+
response_buffer = ""
|
54 |
+
processed_text = ""
|
55 |
+
|
56 |
+
for chunk in stream:
|
57 |
+
if chunk.choices[0].delta.content is not None:
|
58 |
+
# 添加新内容到缓冲区
|
59 |
+
response_buffer += chunk.choices[0].delta.content
|
60 |
+
|
61 |
+
# 处理缓冲区
|
62 |
+
current_processed = process_response(response_buffer)
|
63 |
+
|
64 |
+
# 如果处理后的文本与之前不同,返回新处理的文本
|
65 |
+
if current_processed != processed_text:
|
66 |
+
processed_text = current_processed
|
67 |
+
yield processed_text
|
68 |
+
except Exception as e:
|
69 |
+
yield f"API调用出错: {str(e)}"
|
70 |
|
71 |
+
# 流式响应函数
|
72 |
def stream_response(message: str, history: List[Tuple[str, str]]) -> Iterator[List[Tuple[str, Any]]]:
|
73 |
"""流式响应并格式化为Gradio Chatbot需要的格式"""
|
74 |
# 添加用户消息到历史
|
75 |
history = history + [(message, "")]
|
76 |
|
77 |
+
# 获取AI的回复
|
78 |
+
response_generator = api_stream_response(message)
|
|
|
79 |
|
80 |
+
for processed_text in response_generator:
|
|
|
81 |
# 更新最后一条消息的AI回复部分
|
82 |
updated_history = history.copy()
|
83 |
+
updated_history[-1] = (message, processed_text)
|
84 |
yield updated_history
|
85 |
|
86 |
# 创建自定义CSS样式
|
|
|
88 |
.gradio-container {
|
89 |
font-family: 'Arial', sans-serif;
|
90 |
}
|
91 |
+
|
92 |
+
.section {
|
93 |
+
margin-bottom: 15px;
|
94 |
+
}
|
95 |
+
|
96 |
+
.thinking-section {
|
97 |
+
color: #333333;
|
98 |
+
font-weight: normal;
|
99 |
+
padding-bottom: 10px;
|
100 |
+
border-bottom: 1px dashed #cccccc;
|
101 |
+
}
|
102 |
+
|
103 |
+
.reasoning-section {
|
104 |
+
color: #000000;
|
105 |
+
margin-top: 10px;
|
106 |
+
}
|
107 |
+
|
108 |
+
.reasoning-section strong {
|
109 |
+
font-weight: bold;
|
110 |
+
}
|
111 |
"""
|
112 |
|
113 |
# 创建Gradio界面
|
114 |
def create_demo():
|
115 |
with gr.Blocks(css=custom_css) as demo:
|
116 |
+
gr.Markdown("# 材料科学 AI 问答助手")
|
117 |
+
gr.Markdown("基于DeepSeek模型的材料管道专家系统。回答分为思考过程(常规字体)和推理结论(粗体)两部分。")
|
118 |
|
119 |
chatbot = gr.Chatbot(
|
120 |
show_label=False,
|
121 |
height=500,
|
122 |
+
render=True, # 启用HTML渲染
|
123 |
+
# bubble=True, # 使用气泡样式
|
124 |
+
avatar_images=("👤", "🤖") # 设置头像
|
125 |
)
|
126 |
|
127 |
with gr.Row():
|