Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,96 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import openai
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
client = openai.OpenAI(
|
| 6 |
api_key="sk-hm35RR1E0dfB26C8873BT3BlBKFJE681B3d87a6c4B3e8C44",
|
| 7 |
base_url="https://aigptx.top/"
|
| 8 |
)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"presence_penalty": 0,
|
| 34 |
-
"frequency_penalty": 0,
|
| 35 |
-
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
chat_counter += 1
|
| 39 |
-
history.append(inputs)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
send_btn = gr.Button("Send")
|
| 89 |
-
state = gr.State([])
|
| 90 |
-
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 91 |
-
reset_btn = gr.Button("Reset Chat")
|
| 92 |
-
|
| 93 |
-
# Input parameters for OpenAI API
|
| 94 |
-
with gr.Accordion("Model Parameters", open=False):
|
| 95 |
-
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (Nucleus Sampling)")
|
| 96 |
-
temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
|
| 97 |
-
|
| 98 |
-
# Submit input for model prediction with the send button
|
| 99 |
-
send_btn.click(predict, [inputs, top_p, temperature, system_prompt, chat_counter, chatbot, state],
|
| 100 |
-
[chatbot, state, chat_counter])
|
| 101 |
-
reset_btn.click(reset_textbox, [], [inputs])
|
| 102 |
-
inputs.submit(reset_textbox, [], [inputs])
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
+
# 初始化 OpenAI API 客户端
|
| 5 |
client = openai.OpenAI(
|
| 6 |
api_key="sk-hm35RR1E0dfB26C8873BT3BlBKFJE681B3d87a6c4B3e8C44",
|
| 7 |
base_url="https://aigptx.top/"
|
| 8 |
)
|
| 9 |
|
| 10 |
+
# 默认系统消息(可以通过UI定制)
|
| 11 |
+
system_prompt = "您好!欢迎来到聊天机器人。请随时提出您的问题或想法。"
|
| 12 |
+
|
| 13 |
+
# 添加用户消息到聊天记录
|
| 14 |
+
def add_text(history, text, custom_prompt):
|
| 15 |
+
if not custom_prompt:
|
| 16 |
+
custom_prompt = system_prompt # 使用默认系统提示
|
| 17 |
+
if text:
|
| 18 |
+
if not history:
|
| 19 |
+
history = [("System Message", custom_prompt)]
|
| 20 |
+
history.append((text, None))
|
| 21 |
+
else:
|
| 22 |
+
history.append(("System Message", "您发送了一个空消息,请输入您的问题或想法。"))
|
| 23 |
+
return history, ""
|
| 24 |
+
|
| 25 |
+
# 调用 OpenAI GPT 生成回复
|
| 26 |
+
def predict(history, custom_prompt):
|
| 27 |
+
if not custom_prompt:
|
| 28 |
+
custom_prompt = system_prompt # 使用默认系统提示
|
| 29 |
+
if not history or (len(history) == 1 and history[0][0] == "System Message"):
|
| 30 |
+
history = [("System Message", custom_prompt)]
|
| 31 |
+
yield history
|
| 32 |
+
return history, ""
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
history_ = history[1:] if history[0][0] == "System Message" else history
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
history_openai_format = [{"role": "user", "content": human} for human, assistant in history_ if human]
|
| 37 |
+
|
| 38 |
+
if not history_openai_format:
|
| 39 |
+
yield history
|
| 40 |
+
return history
|
| 41 |
+
|
| 42 |
+
response = client.chat.completions.create(
|
| 43 |
+
model='gpt-4-1106-preview',
|
| 44 |
+
messages=[{"role": "system", "content": custom_prompt}] + history_openai_format,
|
| 45 |
+
temperature=0.9,
|
| 46 |
)
|
| 47 |
|
| 48 |
+
history[-1][1] = response.choices[0].message.content
|
| 49 |
+
yield history
|
| 50 |
+
|
| 51 |
+
# 构建 Gradio 界面
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("<h1><center>聊天机器人</center></h1>")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=2):
|
| 57 |
+
chatbot = gr.Chatbot(
|
| 58 |
+
value=[("System Message", system_prompt)],
|
| 59 |
+
)
|
| 60 |
+
with gr.Row():
|
| 61 |
+
textbox = gr.Textbox(placeholder="请输入您的问题或想法", show_label=False, scale=2)
|
| 62 |
+
submit = gr.Button("💬 发送", scale=1)
|
| 63 |
|
| 64 |
+
# 自定义系统消息输入框
|
| 65 |
+
custom_system_prompt = gr.Textbox(
|
| 66 |
+
value=system_prompt,
|
| 67 |
+
placeholder="自定义系统消息,例如:'我是一个乐于助人的助手,请告诉我如何帮您'",
|
| 68 |
+
label="自定义系统提示",
|
| 69 |
+
lines=2
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# 在用户输入消息时,传递自定义系统提示给 GPT 模型
|
| 73 |
+
textbox_msg = textbox.submit(
|
| 74 |
+
add_text,
|
| 75 |
+
[chatbot, textbox, custom_system_prompt],
|
| 76 |
+
[chatbot, textbox],
|
| 77 |
+
queue=False
|
| 78 |
+
).then(
|
| 79 |
+
predict,
|
| 80 |
+
[chatbot, custom_system_prompt],
|
| 81 |
+
chatbot
|
| 82 |
+
)
|
| 83 |
|
| 84 |
+
submit.click(
|
| 85 |
+
add_text,
|
| 86 |
+
inputs=[chatbot, textbox, custom_system_prompt],
|
| 87 |
+
outputs=[chatbot, textbox],
|
| 88 |
+
queue=False
|
| 89 |
+
).then(
|
| 90 |
+
predict,
|
| 91 |
+
[chatbot, custom_system_prompt],
|
| 92 |
+
chatbot
|
| 93 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
demo.queue()
|
| 96 |
+
demo.launch()
|