Spaces:
Sleeping
Sleeping
Create app_bak1.pyt
Browse files- app_bak1.pyt +72 -0
app_bak1.pyt
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# 设置第三方 API 基本 URL
|
| 5 |
+
API_BASE_URL = "http://key.aistory.uk/v1/chat/completions" # 替换为你自己的API URL
|
| 6 |
+
API_KEY = "sk-HfD4NYIN6bq2DkSfIiUcciRvo9MkgMdFCsahP9NWEOUPHe8H" # 替换为你自己的 API 密钥
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# 定义 AI 响应函数,调用第三方 API
|
| 10 |
+
def ai_response(message, chat_history):
|
| 11 |
+
# 定义系统提示词
|
| 12 |
+
system_prompt = "You are a helpful assistant. Please assist the user with their inquiries."
|
| 13 |
+
|
| 14 |
+
# 组合历史聊天记录和用户输入的信息
|
| 15 |
+
conversation = [{"role": "system", "content": system_prompt}]
|
| 16 |
+
for msg in chat_history:
|
| 17 |
+
conversation.append({"role": msg[0], "content": msg[1]})
|
| 18 |
+
conversation.append({"role": "user", "content": message})
|
| 19 |
+
|
| 20 |
+
# 构建请求体
|
| 21 |
+
payload = {
|
| 22 |
+
"model": "gpt-4o", # 使用 gpt-4o 模型(如果此模型为该 API 支持的模型)
|
| 23 |
+
"messages": conversation,
|
| 24 |
+
"max_tokens": 150
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# 设置请求头,包括 API 密钥
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 30 |
+
"Content-Type": "application/json"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# 发送请求到第三方 API
|
| 34 |
+
try:
|
| 35 |
+
response = requests.post(API_BASE_URL, json=payload, headers=headers)
|
| 36 |
+
response.raise_for_status() # 如果响应状态码不是 2xx,会抛出异常
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
# 获取 API 响应内容
|
| 39 |
+
response_data = response.json()
|
| 40 |
+
assistant_message = response_data['choices'][0]['message']['content']
|
| 41 |
+
|
| 42 |
+
# 返回新的聊天记录,转换为符合 gr.Chatbot 期望的元组格式
|
| 43 |
+
chat_history.append(("user", message))
|
| 44 |
+
chat_history.append(("assistant", assistant_message))
|
| 45 |
+
|
| 46 |
+
return chat_history
|
| 47 |
+
else:
|
| 48 |
+
# 如果请求失败,输出错误信息
|
| 49 |
+
return chat_history + [("assistant", f"API error: {response.status_code}, {response.text}")]
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
# 捕获任何请求错误,并输出详细错误信息
|
| 52 |
+
return chat_history + [("assistant", f"Request failed: {str(e)}")]
|
| 53 |
+
|
| 54 |
+
# 创建 Gradio 应用
|
| 55 |
+
def create_interface():
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
# 创建一个 Column 布局,用于将聊天记录和输入框放在同一列
|
| 58 |
+
with gr.Column():
|
| 59 |
+
# 创建一个聊天机器人输出组件,用于显示对话
|
| 60 |
+
chat_output = gr.Chatbot()
|
| 61 |
+
|
| 62 |
+
# 创建一个文本框用于输入消息
|
| 63 |
+
message_input = gr.Textbox(label="请输入你的问题", placeholder="输入你的问题并按回车发送", lines=1)
|
| 64 |
+
|
| 65 |
+
# 提交按钮,发送用户消息并获取AI回复
|
| 66 |
+
message_input.submit(ai_response, inputs=[message_input, chat_output], outputs=[chat_output])
|
| 67 |
+
|
| 68 |
+
return demo
|
| 69 |
+
|
| 70 |
+
# 启动 Gradio 应用
|
| 71 |
+
demo = create_interface()
|
| 72 |
+
demo.launch()
|