Update app.py
Browse files
app.py
CHANGED
@@ -2,48 +2,29 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import re
|
4 |
import json
|
5 |
-
from typing import Iterator
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
API_KEY = "sk-5c2b6a56-2e2f-45f7-9a26-3fe42a218eb9"
|
8 |
API_BASE = "https://api.visionsic.com/v1"
|
9 |
|
10 |
|
11 |
-
def
|
12 |
-
"""处理<think>标签的函数"""
|
13 |
-
if not content:
|
14 |
-
return ""
|
15 |
-
|
16 |
-
formatted_content = ""
|
17 |
-
current_pos = 0
|
18 |
-
|
19 |
-
think_pattern = r"<think>(.*?)</think>"
|
20 |
-
matches = re.finditer(think_pattern, content, re.DOTALL)
|
21 |
-
|
22 |
-
for match in matches:
|
23 |
-
formatted_content += content[current_pos:match.start()]
|
24 |
-
|
25 |
-
think_content = match.group(1)
|
26 |
-
formatted_content += f"""
|
27 |
-
<details class="think-container" open>
|
28 |
-
<summary class="think-summary">思考过程(点击展开)</summary>
|
29 |
-
<div class="think-content">{think_content}</div>
|
30 |
-
</details>
|
31 |
-
"""
|
32 |
-
current_pos = match.end()
|
33 |
-
|
34 |
-
formatted_content += content[current_pos:]
|
35 |
-
return formatted_content
|
36 |
-
|
37 |
-
def chat_stream(message: str, history: List[Tuple[str, str]], system_prompt: str) -> Iterator[List[Tuple[str, str]]]:
|
38 |
"""处理聊天流式响应"""
|
39 |
messages = [{"role": "system", "content": system_prompt}]
|
40 |
|
|
|
41 |
for human, assistant in history:
|
42 |
messages.append({"role": "user", "content": human})
|
43 |
messages.append({"role": "assistant", "content": assistant})
|
44 |
|
45 |
messages.append({"role": "user", "content": message})
|
46 |
|
|
|
47 |
headers = {
|
48 |
"Authorization": f"Bearer {API_KEY}",
|
49 |
"Content-Type": "application/json"
|
@@ -54,33 +35,126 @@ def chat_stream(message: str, history: List[Tuple[str, str]], system_prompt: str
|
|
54 |
"stream": True
|
55 |
}
|
56 |
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
line = line.decode('utf-8')
|
63 |
-
if line.startswith("data: "):
|
64 |
-
json_str = line[6:]
|
65 |
-
if json_str.strip() == "[DONE]":
|
66 |
-
break
|
67 |
-
try:
|
68 |
-
chunk = json.loads(json_str)
|
69 |
-
if chunk['choices'][0]['delta'].get('content'):
|
70 |
-
chunk_message = chunk['choices'][0]['delta']['content']
|
71 |
-
collected_messages.append(chunk_message)
|
72 |
-
partial_message = "".join(collected_messages)
|
73 |
-
formatted_message = format_think_tags(partial_message)
|
74 |
-
yield history + [(message, formatted_message)]
|
75 |
-
except json.JSONDecodeError:
|
76 |
-
continue
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
}
|
85 |
.think-container {
|
86 |
background: #f5f5f5;
|
@@ -100,53 +174,9 @@ custom_css = """
|
|
100 |
background: #fff;
|
101 |
border-radius: 3px;
|
102 |
}
|
103 |
-
.code-block {
|
104 |
-
background: #f8f8f8;
|
105 |
-
padding: 10px;
|
106 |
-
border-radius: 5px;
|
107 |
-
font-family: monospace;
|
108 |
-
}
|
109 |
"""
|
110 |
|
111 |
-
# 创建Gradio界面
|
112 |
-
with gr.Blocks(css=custom_css) as demo:
|
113 |
-
gr.Markdown("# Tifa-Deepsex-COT-14B")
|
114 |
-
gr.Markdown("### 请设置你喜欢的角色聊天吧")
|
115 |
-
|
116 |
-
chatbot = gr.Chatbot(height=600, show_copy_button=True)
|
117 |
-
|
118 |
-
with gr.Row():
|
119 |
-
with gr.Column(scale=4):
|
120 |
-
message = gr.Textbox(
|
121 |
-
label="发送消息",
|
122 |
-
placeholder="在这里输入你的消息...",
|
123 |
-
lines=3
|
124 |
-
)
|
125 |
-
with gr.Column(scale=1):
|
126 |
-
system_prompt = gr.Textbox(
|
127 |
-
value="你是tifa",
|
128 |
-
label="System Prompt",
|
129 |
-
lines=3
|
130 |
-
)
|
131 |
-
|
132 |
-
with gr.Row():
|
133 |
-
submit = gr.Button("发送", variant="primary")
|
134 |
-
clear = gr.Button("清除对话")
|
135 |
-
|
136 |
-
# 事件处理
|
137 |
-
submit.click(
|
138 |
-
chat_stream,
|
139 |
-
inputs=[message, chatbot, system_prompt],
|
140 |
-
outputs=chatbot
|
141 |
-
)
|
142 |
-
message.submit(
|
143 |
-
chat_stream,
|
144 |
-
inputs=[message, chatbot, system_prompt],
|
145 |
-
outputs=chatbot
|
146 |
-
)
|
147 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
148 |
-
|
149 |
# 启动应用
|
150 |
if __name__ == "__main__":
|
151 |
demo.queue()
|
152 |
-
demo.launch()
|
|
|
2 |
import requests
|
3 |
import re
|
4 |
import json
|
5 |
+
from typing import Iterator, List, Tuple
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
# API配置
|
11 |
|
12 |
API_KEY = "sk-5c2b6a56-2e2f-45f7-9a26-3fe42a218eb9"
|
13 |
API_BASE = "https://api.visionsic.com/v1"
|
14 |
|
15 |
|
16 |
+
def chat_stream(message: str, history: list, system_prompt: str) -> Iterator[list]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
"""处理聊天流式响应"""
|
18 |
messages = [{"role": "system", "content": system_prompt}]
|
19 |
|
20 |
+
# 添加历史对话
|
21 |
for human, assistant in history:
|
22 |
messages.append({"role": "user", "content": human})
|
23 |
messages.append({"role": "assistant", "content": assistant})
|
24 |
|
25 |
messages.append({"role": "user", "content": message})
|
26 |
|
27 |
+
# 调用API进行流式对话
|
28 |
headers = {
|
29 |
"Authorization": f"Bearer {API_KEY}",
|
30 |
"Content-Type": "application/json"
|
|
|
35 |
"stream": True
|
36 |
}
|
37 |
|
38 |
+
try:
|
39 |
+
response = requests.post(
|
40 |
+
f"{API_BASE}/chat/completions",
|
41 |
+
headers=headers,
|
42 |
+
json=data,
|
43 |
+
stream=True
|
44 |
+
)
|
45 |
+
|
46 |
+
collected_messages = []
|
47 |
+
new_history = history.copy()
|
48 |
+
new_history.append([message, ""]) # 添加用户消息和空的助手消息
|
49 |
+
|
50 |
+
for line in response.iter_lines():
|
51 |
+
if line:
|
52 |
+
line = line.decode('utf-8')
|
53 |
+
if line.startswith("
|
54 |
+
json_str = line[6:]
|
55 |
+
if json_str.strip() == "[DONE]":
|
56 |
+
break
|
57 |
+
try:
|
58 |
+
chunk = json.loads(json_str)
|
59 |
+
if chunk['choices'][0]['delta'].get('content'):
|
60 |
+
chunk_message = chunk['choices'][0]['delta']['content']
|
61 |
+
collected_messages.append(chunk_message)
|
62 |
+
partial_message = "".join(collected_messages)
|
63 |
+
new_history[-1][1] = partial_message # 更新最后一条助手消息
|
64 |
+
yield new_history
|
65 |
+
except json.JSONDecodeError:
|
66 |
+
continue
|
67 |
+
except Exception as e:
|
68 |
+
print(f"Error occurred: {str(e)}")
|
69 |
+
new_history[-1][1] = f"Error: {str(e)}"
|
70 |
+
yield new_history
|
71 |
+
|
72 |
+
# 创建Gradio界面
|
73 |
+
theme = gr.themes.Soft(
|
74 |
+
primary_hue="blue",
|
75 |
+
secondary_hue="blue",
|
76 |
+
).set(
|
77 |
+
body_background_fill="#F5F5F5",
|
78 |
+
block_background_fill="#FFFFFF",
|
79 |
+
button_primary_background_fill="#2196F3",
|
80 |
+
)
|
81 |
+
|
82 |
+
with gr.Blocks(theme=theme) as demo:
|
83 |
+
gr.Markdown(
|
84 |
+
"""
|
85 |
+
# Tifa-Deepsex-COT-14B
|
86 |
+
### 请设置你喜欢的角色聊天吧
|
87 |
+
"""
|
88 |
+
)
|
89 |
+
|
90 |
+
chatbot = gr.Chatbot(
|
91 |
+
height=500,
|
92 |
+
show_copy_button=True,
|
93 |
+
container=True,
|
94 |
+
elem_classes="chatbot"
|
95 |
+
)
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
with gr.Column(scale=4):
|
99 |
+
msg = gr.Textbox(
|
100 |
+
label="发送消息",
|
101 |
+
placeholder="在这里输入你的消息...",
|
102 |
+
show_label=True,
|
103 |
+
lines=3,
|
104 |
+
container=True
|
105 |
+
)
|
106 |
+
with gr.Column(scale=1):
|
107 |
+
system = gr.Textbox(
|
108 |
+
label="System Prompt",
|
109 |
+
value="你是tifa",
|
110 |
+
lines=3,
|
111 |
+
show_label=True,
|
112 |
+
container=True
|
113 |
+
)
|
114 |
|
115 |
+
with gr.Row():
|
116 |
+
submit = gr.Button("发送", variant="primary", size="lg")
|
117 |
+
clear = gr.Button("清除对话", size="lg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
# 事件处理
|
120 |
+
submit_event = submit.click(
|
121 |
+
chat_stream,
|
122 |
+
inputs=[msg, chatbot, system],
|
123 |
+
outputs=chatbot,
|
124 |
+
queue=True
|
125 |
+
)
|
126 |
+
|
127 |
+
msg_event = msg.submit(
|
128 |
+
chat_stream,
|
129 |
+
inputs=[msg, chatbot, system],
|
130 |
+
outputs=chatbot,
|
131 |
+
queue=True
|
132 |
+
)
|
133 |
+
|
134 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
135 |
+
|
136 |
+
# 发送后清空输入框
|
137 |
+
submit_event.then(lambda: "", None, msg)
|
138 |
+
msg_event.then(lambda: "", None, msg)
|
139 |
+
|
140 |
+
# 自定义样式
|
141 |
+
css = """
|
142 |
+
.chatbot {
|
143 |
+
height: 500px;
|
144 |
+
overflow-y: auto;
|
145 |
+
}
|
146 |
+
.message {
|
147 |
+
padding: 10px;
|
148 |
+
margin: 5px;
|
149 |
+
border-radius: 10px;
|
150 |
+
}
|
151 |
+
.user-message {
|
152 |
+
background-color: #E3F2FD;
|
153 |
+
margin-left: 20%;
|
154 |
+
}
|
155 |
+
.bot-message {
|
156 |
+
background-color: #F5F5F5;
|
157 |
+
margin-right: 20%;
|
158 |
}
|
159 |
.think-container {
|
160 |
background: #f5f5f5;
|
|
|
174 |
background: #fff;
|
175 |
border-radius: 3px;
|
176 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
"""
|
178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
# 启动应用
|
180 |
if __name__ == "__main__":
|
181 |
demo.queue()
|
182 |
+
demo.launch(share=False, css=css)
|