Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,9 +9,10 @@ logging.basicConfig(level=logging.DEBUG)
|
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
class FastGPTChat:
|
12 |
-
def __init__(self, api_key
|
|
|
13 |
self.api_key = api_key
|
14 |
-
self.base_url =
|
15 |
self.system_prompt = system_prompt
|
16 |
self.headers = {
|
17 |
"Authorization": f"Bearer {api_key}",
|
@@ -43,7 +44,7 @@ class FastGPTChat:
|
|
43 |
# 准备请求数据
|
44 |
data = {
|
45 |
"chatId": chat_id,
|
46 |
-
"stream": True,
|
47 |
"detail": True,
|
48 |
"model": "gpt-4o",
|
49 |
"messages": messages
|
@@ -94,105 +95,134 @@ class FastGPTChat:
|
|
94 |
chat_history.append((message, f"Error: {error_msg}"))
|
95 |
return "", chat_history
|
96 |
|
97 |
-
def create_chat_interface(
|
98 |
# 创建FastGPT聊天实例
|
99 |
-
fastgpt_chat = FastGPTChat(
|
100 |
|
101 |
# 创建美化后的Gradio界面
|
102 |
with gr.Blocks(
|
103 |
-
title="
|
104 |
css="""
|
105 |
.container { max-width: 800px; margin: auto; }
|
106 |
-
.chat-header {
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
"""
|
109 |
) as interface:
|
110 |
with gr.Column(elem_classes="container"):
|
111 |
gr.Markdown(
|
112 |
"""
|
113 |
-
#
|
114 |
-
###
|
115 |
"""
|
116 |
)
|
117 |
|
118 |
-
with gr.Row():
|
119 |
-
api_key_input = gr.Textbox(
|
120 |
-
label="API Key",
|
121 |
-
value=api_key,
|
122 |
-
type="password",
|
123 |
-
container=False,
|
124 |
-
scale=4
|
125 |
-
)
|
126 |
-
|
127 |
-
with gr.Row():
|
128 |
-
system_prompt = gr.Textbox(
|
129 |
-
label="System Prompt",
|
130 |
-
placeholder="Set the behavior of AI assistant...",
|
131 |
-
lines=2,
|
132 |
-
container=False
|
133 |
-
)
|
134 |
-
|
135 |
with gr.Column(elem_classes="chat-container"):
|
136 |
chatbot = gr.Chatbot(
|
137 |
height=500,
|
138 |
container=False,
|
139 |
-
elem_classes="chatbot"
|
|
|
140 |
)
|
141 |
|
142 |
-
with gr.Row():
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
with gr.Row():
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
def update_chat_settings(new_key, new_prompt):
|
159 |
-
nonlocal fastgpt_chat
|
160 |
-
fastgpt_chat = FastGPTChat(new_key, new_prompt)
|
161 |
-
return "Settings updated"
|
162 |
|
163 |
# 设置事件处理
|
164 |
-
|
165 |
fastgpt_chat.chat,
|
166 |
inputs=[message, chatbot],
|
167 |
outputs=[message, chatbot],
|
168 |
api_name="chat"
|
169 |
)
|
170 |
|
|
|
171 |
message.submit(
|
172 |
fastgpt_chat.chat,
|
173 |
inputs=[message, chatbot],
|
174 |
outputs=[message, chatbot]
|
175 |
)
|
176 |
|
|
|
177 |
clear.click(lambda: None, None, chatbot, queue=False)
|
178 |
|
179 |
-
# 更新设置
|
180 |
-
api_key_input.change(
|
181 |
-
update_chat_settings,
|
182 |
-
inputs=[api_key_input, system_prompt],
|
183 |
-
outputs=[status]
|
184 |
-
)
|
185 |
-
|
186 |
-
system_prompt.change(
|
187 |
-
update_chat_settings,
|
188 |
-
inputs=[api_key_input, system_prompt],
|
189 |
-
outputs=[status]
|
190 |
-
)
|
191 |
-
|
192 |
return interface
|
193 |
|
194 |
# 使用示例
|
195 |
if __name__ == "__main__":
|
196 |
-
|
197 |
-
demo = create_chat_interface(API_KEY)
|
198 |
demo.launch(debug=True)
|
|
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
class FastGPTChat:
|
12 |
+
def __init__(self, api_key="fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF",
|
13 |
+
system_prompt="我是一名AI用药咨询顾问,请向我提问有关用药的问题"):
|
14 |
self.api_key = api_key
|
15 |
+
self.base_url = "https://api.fastgpt.in/api"
|
16 |
self.system_prompt = system_prompt
|
17 |
self.headers = {
|
18 |
"Authorization": f"Bearer {api_key}",
|
|
|
44 |
# 准备请求数据
|
45 |
data = {
|
46 |
"chatId": chat_id,
|
47 |
+
"stream": True,
|
48 |
"detail": True,
|
49 |
"model": "gpt-4o",
|
50 |
"messages": messages
|
|
|
95 |
chat_history.append((message, f"Error: {error_msg}"))
|
96 |
return "", chat_history
|
97 |
|
98 |
+
def create_chat_interface():
|
99 |
# 创建FastGPT聊天实例
|
100 |
+
fastgpt_chat = FastGPTChat()
|
101 |
|
102 |
# 创建美化后的Gradio界面
|
103 |
with gr.Blocks(
|
104 |
+
title="AI用药咨询助手",
|
105 |
css="""
|
106 |
.container { max-width: 800px; margin: auto; }
|
107 |
+
.chat-header {
|
108 |
+
text-align: center;
|
109 |
+
padding: 20px;
|
110 |
+
background: linear-gradient(135deg, #00b4d8, #0077b6);
|
111 |
+
color: white;
|
112 |
+
border-radius: 15px 15px 0 0;
|
113 |
+
margin-bottom: 20px;
|
114 |
+
}
|
115 |
+
.chat-container {
|
116 |
+
background-color: #ffffff;
|
117 |
+
border-radius: 15px;
|
118 |
+
padding: 20px;
|
119 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
120 |
+
border: 1px solid #e0e0e0;
|
121 |
+
}
|
122 |
+
.message-box {
|
123 |
+
border: 2px solid #e0e0e0;
|
124 |
+
border-radius: 10px;
|
125 |
+
background-color: white;
|
126 |
+
transition: all 0.3s ease;
|
127 |
+
}
|
128 |
+
.message-box:focus {
|
129 |
+
border-color: #00b4d8;
|
130 |
+
box-shadow: 0 0 0 2px rgba(0, 180, 216, 0.2);
|
131 |
+
}
|
132 |
+
.submit-btn {
|
133 |
+
background-color: #00b4d8 !important;
|
134 |
+
border: none !important;
|
135 |
+
color: white !important;
|
136 |
+
padding: 10px 20px !important;
|
137 |
+
border-radius: 8px !important;
|
138 |
+
transition: all 0.3s ease !important;
|
139 |
+
font-weight: 600 !important;
|
140 |
+
}
|
141 |
+
.submit-btn:hover {
|
142 |
+
background-color: #0077b6 !important;
|
143 |
+
transform: translateY(-2px);
|
144 |
+
}
|
145 |
+
.clear-btn {
|
146 |
+
background-color: #ff4757 !important;
|
147 |
+
border: none !important;
|
148 |
+
color: white !important;
|
149 |
+
padding: 10px 20px !important;
|
150 |
+
border-radius: 8px !important;
|
151 |
+
transition: all 0.3s ease !important;
|
152 |
+
font-weight: 600 !important;
|
153 |
+
}
|
154 |
+
.clear-btn:hover {
|
155 |
+
background-color: #ff6b81 !important;
|
156 |
+
transform: translateY(-2px);
|
157 |
+
}
|
158 |
+
.chatbot {
|
159 |
+
background-color: #f8f9fa;
|
160 |
+
border-radius: 10px;
|
161 |
+
padding: 10px;
|
162 |
+
margin-bottom: 20px;
|
163 |
+
}
|
164 |
"""
|
165 |
) as interface:
|
166 |
with gr.Column(elem_classes="container"):
|
167 |
gr.Markdown(
|
168 |
"""
|
169 |
+
# 🏥 AI用药咨询助手
|
170 |
+
### 您的专业用药咨询顾问,随时为您解答用药相关问题
|
171 |
"""
|
172 |
)
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
with gr.Column(elem_classes="chat-container"):
|
175 |
chatbot = gr.Chatbot(
|
176 |
height=500,
|
177 |
container=False,
|
178 |
+
elem_classes="chatbot",
|
179 |
+
show_label=False
|
180 |
)
|
181 |
|
182 |
+
with gr.Row(elem_id="input-container"):
|
183 |
+
with gr.Column(scale=8):
|
184 |
+
message = gr.Textbox(
|
185 |
+
show_label=False,
|
186 |
+
placeholder="请输入您的用药问题...",
|
187 |
+
container=False,
|
188 |
+
elem_classes="message-box",
|
189 |
+
lines=2
|
190 |
+
)
|
191 |
+
with gr.Column(scale=1, min_width=100):
|
192 |
+
submit = gr.Button(
|
193 |
+
"发送 💊",
|
194 |
+
variant="primary",
|
195 |
+
elem_classes="submit-btn"
|
196 |
+
)
|
197 |
|
198 |
+
with gr.Row(elem_id="control-container"):
|
199 |
+
clear = gr.Button(
|
200 |
+
"清空对话 🗑️",
|
201 |
+
variant="secondary",
|
202 |
+
elem_classes="clear-btn"
|
203 |
+
)
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
# 设置事件处理
|
206 |
+
submit_click = submit.click(
|
207 |
fastgpt_chat.chat,
|
208 |
inputs=[message, chatbot],
|
209 |
outputs=[message, chatbot],
|
210 |
api_name="chat"
|
211 |
)
|
212 |
|
213 |
+
# 添加回车发送支持
|
214 |
message.submit(
|
215 |
fastgpt_chat.chat,
|
216 |
inputs=[message, chatbot],
|
217 |
outputs=[message, chatbot]
|
218 |
)
|
219 |
|
220 |
+
# 清空对话
|
221 |
clear.click(lambda: None, None, chatbot, queue=False)
|
222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
return interface
|
224 |
|
225 |
# 使用示例
|
226 |
if __name__ == "__main__":
|
227 |
+
demo = create_chat_interface()
|
|
|
228 |
demo.launch(debug=True)
|