Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,151 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import re
|
4 |
+
import json
|
5 |
+
from typing import Iterator
|
|
|
|
|
6 |
|
7 |
+
# API配置
|
8 |
+
openai.api_key = "sk-5c2b6a56-2e2f-45f7-9a26-3fe42a218eb9"
|
9 |
+
openai.api_base = "https://api.visionsic.com/v1/"
|
10 |
|
11 |
+
def format_think_tags(content: str) -> str:
|
12 |
+
"""处理<think>标签的函数"""
|
13 |
+
if not content:
|
14 |
+
return ""
|
15 |
+
|
16 |
+
formatted_content = ""
|
17 |
+
current_pos = 0
|
18 |
+
|
19 |
+
# 查找所有<think>标签
|
20 |
+
think_pattern = r"<think>(.*?)</think>"
|
21 |
+
matches = re.finditer(think_pattern, content, re.DOTALL)
|
22 |
+
|
23 |
+
for match in matches:
|
24 |
+
# 添加<think>之前的内容
|
25 |
+
formatted_content += content[current_pos:match.start()]
|
26 |
+
|
27 |
+
think_content = match.group(1)
|
28 |
+
# 创建可折叠的思考内容区块
|
29 |
+
formatted_content += f"""
|
30 |
+
<details class="think-container" open>
|
31 |
+
<summary class="think-summary">思考过程(点击展开)</summary>
|
32 |
+
<div class="think-content">{think_content}</div>
|
33 |
+
</details>
|
34 |
+
"""
|
35 |
+
current_pos = match.end()
|
36 |
+
|
37 |
+
# 添加剩余内容
|
38 |
+
formatted_content += content[current_pos:]
|
39 |
+
return formatted_content
|
40 |
|
41 |
+
def chat_stream(message: str, history: list, system_prompt: str) -> Iterator[str]:
|
42 |
+
"""处理聊天流式响应"""
|
43 |
+
messages = [{"role": "system", "content": system_prompt}]
|
44 |
+
|
45 |
+
# 添加历史对话
|
46 |
+
for human, assistant in history:
|
47 |
+
messages.append({"role": "user", "content": human})
|
48 |
+
messages.append({"role": "assistant", "content": assistant})
|
49 |
+
|
50 |
messages.append({"role": "user", "content": message})
|
51 |
+
|
52 |
+
# 调用API进行流式对话
|
53 |
+
response = openai.ChatCompletion.create(
|
54 |
+
model="mini",
|
55 |
+
messages=messages,
|
56 |
+
stream=True
|
57 |
+
)
|
58 |
+
|
59 |
+
collected_messages = []
|
60 |
+
for chunk in response:
|
61 |
+
if chunk and hasattr(chunk.choices[0].delta, "content"):
|
62 |
+
chunk_message = chunk.choices[0].delta.content
|
63 |
+
collected_messages.append(chunk_message)
|
64 |
+
partial_message = "".join(collected_messages)
|
65 |
+
formatted_message = format_think_tags(partial_message)
|
66 |
+
yield formatted_message
|
67 |
|
68 |
+
# 自定义CSS样式
|
69 |
+
custom_css = """
|
70 |
+
.container {
|
71 |
+
max-width: 800px;
|
72 |
+
margin: 0 auto;
|
73 |
+
padding: 20px;
|
74 |
+
}
|
75 |
|
76 |
+
.think-container {
|
77 |
+
background: #f5f5f5;
|
78 |
+
border: 1px solid #ddd;
|
79 |
+
border-radius: 5px;
|
80 |
+
margin: 10px 0;
|
81 |
+
padding: 10px;
|
82 |
+
}
|
|
|
83 |
|
84 |
+
.think-summary {
|
85 |
+
cursor: pointer;
|
86 |
+
font-weight: bold;
|
87 |
+
color: #2196F3;
|
88 |
+
}
|
89 |
|
90 |
+
.think-content {
|
91 |
+
margin-top: 10px;
|
92 |
+
padding: 10px;
|
93 |
+
background: #fff;
|
94 |
+
border-radius: 3px;
|
95 |
+
}
|
96 |
|
97 |
+
.code-block {
|
98 |
+
background: #f8f8f8;
|
99 |
+
padding: 10px;
|
100 |
+
border-radius: 5px;
|
101 |
+
font-family: monospace;
|
102 |
+
}
|
103 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
# 创建Gradio界面
|
106 |
+
with gr.Blocks(css=custom_css) as demo:
|
107 |
+
gr.Markdown("# Tifa-Deepsex-COT-14B")
|
108 |
+
gr.Markdown("### 请设置你喜欢的角色聊天吧")
|
109 |
+
|
110 |
+
with gr.Row():
|
111 |
+
with gr.Column(scale=4):
|
112 |
+
chatbot = gr.Chatbot(
|
113 |
+
height=600,
|
114 |
+
show_copy_button=True,
|
115 |
+
layout="bubble",
|
116 |
+
)
|
117 |
+
with gr.Column(scale=1):
|
118 |
+
system_prompt = gr.Textbox(
|
119 |
+
value="你是tifa",
|
120 |
+
label="System Prompt",
|
121 |
+
lines=3
|
122 |
+
)
|
123 |
+
|
124 |
+
with gr.Row():
|
125 |
+
message = gr.Textbox(
|
126 |
+
label="发送消息",
|
127 |
+
placeholder="在这里输入你的消息...",
|
128 |
+
lines=2
|
129 |
+
)
|
130 |
+
submit = gr.Button("发送", variant="primary")
|
131 |
+
clear = gr.Button("清除对话")
|
132 |
+
|
133 |
+
# 事件处理
|
134 |
+
submit.click(
|
135 |
+
chat_stream,
|
136 |
+
inputs=[message, chatbot, system_prompt],
|
137 |
+
outputs=chatbot,
|
138 |
+
show_progress=True
|
139 |
+
)
|
140 |
+
message.submit(
|
141 |
+
chat_stream,
|
142 |
+
inputs=[message, chatbot, system_prompt],
|
143 |
+
outputs=chatbot,
|
144 |
+
show_progress=True
|
145 |
+
)
|
146 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
147 |
|
148 |
+
# 启动应用
|
149 |
if __name__ == "__main__":
|
150 |
+
demo.queue()
|
151 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|