Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,11 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class FastGPTChat:
|
| 7 |
def __init__(self, api_key, base_url="https://api.fastgpt.in/api"):
|
|
@@ -11,16 +16,18 @@ class FastGPTChat:
|
|
| 11 |
"Authorization": f"Bearer {api_key}",
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
| 14 |
-
|
| 15 |
def chat(self, message, chat_history):
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
chat_id = str(uuid.uuid4())
|
| 18 |
|
| 19 |
# 准备请求数据
|
| 20 |
data = {
|
| 21 |
"chatId": chat_id,
|
| 22 |
"stream": False,
|
| 23 |
-
"detail":
|
| 24 |
"messages": [
|
| 25 |
{
|
| 26 |
"role": "user",
|
|
@@ -30,24 +37,56 @@ class FastGPTChat:
|
|
| 30 |
}
|
| 31 |
|
| 32 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# 发送请求
|
| 34 |
response = requests.post(
|
| 35 |
f"{self.base_url}/v1/chat/completions",
|
| 36 |
headers=self.headers,
|
| 37 |
-
json=data
|
|
|
|
| 38 |
)
|
| 39 |
-
response.raise_for_status()
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def create_chat_interface(api_key):
|
| 53 |
# 创建FastGPT聊天实例
|
|
@@ -57,11 +96,39 @@ def create_chat_interface(api_key):
|
|
| 57 |
with gr.Blocks(title="FastGPT Chat") as interface:
|
| 58 |
gr.Markdown("# FastGPT Chat Interface")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
chatbot = gr.Chatbot(height=400)
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# 设置事件处理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
message.submit(
|
| 66 |
fastgpt_chat.chat,
|
| 67 |
inputs=[message, chatbot],
|
|
@@ -69,6 +136,7 @@ def create_chat_interface(api_key):
|
|
| 69 |
)
|
| 70 |
|
| 71 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
| 72 |
|
| 73 |
return interface
|
| 74 |
|
|
@@ -76,4 +144,4 @@ def create_chat_interface(api_key):
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 替换为你的API密钥
|
| 78 |
demo = create_chat_interface(API_KEY)
|
| 79 |
-
demo.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import uuid
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# 配置日志
|
| 8 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
class FastGPTChat:
|
| 12 |
def __init__(self, api_key, base_url="https://api.fastgpt.in/api"):
|
|
|
|
| 16 |
"Authorization": f"Bearer {api_key}",
|
| 17 |
"Content-Type": "application/json"
|
| 18 |
}
|
| 19 |
+
|
| 20 |
def chat(self, message, chat_history):
|
| 21 |
+
if not message.strip():
|
| 22 |
+
return "", chat_history
|
| 23 |
+
|
| 24 |
chat_id = str(uuid.uuid4())
|
| 25 |
|
| 26 |
# 准备请求数据
|
| 27 |
data = {
|
| 28 |
"chatId": chat_id,
|
| 29 |
"stream": False,
|
| 30 |
+
"detail": True, # 开启详细日志
|
| 31 |
"messages": [
|
| 32 |
{
|
| 33 |
"role": "user",
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# 打印请求信息
|
| 41 |
+
logger.debug(f"Request URL: {self.base_url}/v1/chat/completions")
|
| 42 |
+
logger.debug(f"Request Headers: {json.dumps(self.headers, indent=2)}")
|
| 43 |
+
logger.debug(f"Request Data: {json.dumps(data, indent=2)}")
|
| 44 |
+
|
| 45 |
# 发送请求
|
| 46 |
response = requests.post(
|
| 47 |
f"{self.base_url}/v1/chat/completions",
|
| 48 |
headers=self.headers,
|
| 49 |
+
json=data,
|
| 50 |
+
timeout=30 # 添加超时设置
|
| 51 |
)
|
|
|
|
| 52 |
|
| 53 |
+
# 打印响应信息
|
| 54 |
+
logger.debug(f"Response Status Code: {response.status_code}")
|
| 55 |
+
logger.debug(f"Response Headers: {dict(response.headers)}")
|
| 56 |
+
logger.debug(f"Response Content: {response.text}")
|
| 57 |
|
| 58 |
+
# 检查响应状态
|
| 59 |
+
if response.status_code != 200:
|
| 60 |
+
error_msg = f"API Error: Status {response.status_code}, Response: {response.text}"
|
| 61 |
+
logger.error(error_msg)
|
| 62 |
+
chat_history.append((message, f"Error: {error_msg}"))
|
| 63 |
+
return "", chat_history
|
| 64 |
|
| 65 |
+
# 解析响应
|
| 66 |
+
try:
|
| 67 |
+
response_data = response.json()
|
| 68 |
+
logger.debug(f"Parsed Response: {json.dumps(response_data, indent=2)}")
|
| 69 |
+
|
| 70 |
+
if "choices" in response_data and len(response_data["choices"]) > 0:
|
| 71 |
+
ai_message = response_data["choices"][0]["message"]["content"]
|
| 72 |
+
else:
|
| 73 |
+
ai_message = "No response generated from the API"
|
| 74 |
+
logger.warning("No choices in response data")
|
| 75 |
+
|
| 76 |
+
chat_history.append((message, ai_message))
|
| 77 |
+
return "", chat_history
|
| 78 |
+
|
| 79 |
+
except json.JSONDecodeError as e:
|
| 80 |
+
error_msg = f"Failed to parse API response: {str(e)}"
|
| 81 |
+
logger.error(error_msg)
|
| 82 |
+
chat_history.append((message, f"Error: {error_msg}"))
|
| 83 |
+
return "", chat_history
|
| 84 |
+
|
| 85 |
+
except requests.exceptions.RequestException as e:
|
| 86 |
+
error_msg = f"Request failed: {str(e)}"
|
| 87 |
+
logger.error(error_msg)
|
| 88 |
+
chat_history.append((message, f"Error: {error_msg}"))
|
| 89 |
+
return "", chat_history
|
| 90 |
|
| 91 |
def create_chat_interface(api_key):
|
| 92 |
# 创建FastGPT聊天实例
|
|
|
|
| 96 |
with gr.Blocks(title="FastGPT Chat") as interface:
|
| 97 |
gr.Markdown("# FastGPT Chat Interface")
|
| 98 |
|
| 99 |
+
with gr.Row():
|
| 100 |
+
api_key_input = gr.Textbox(
|
| 101 |
+
label="API Key",
|
| 102 |
+
value=api_key,
|
| 103 |
+
type="password"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
chatbot = gr.Chatbot(height=400)
|
| 107 |
+
with gr.Row():
|
| 108 |
+
message = gr.Textbox(
|
| 109 |
+
label="Type your message here...",
|
| 110 |
+
placeholder="Enter your message and press enter",
|
| 111 |
+
lines=2
|
| 112 |
+
)
|
| 113 |
+
with gr.Row():
|
| 114 |
+
submit = gr.Button("Submit")
|
| 115 |
+
clear = gr.Button("Clear Chat")
|
| 116 |
+
|
| 117 |
+
# 状态显示
|
| 118 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 119 |
+
|
| 120 |
+
def update_api_key(new_key):
|
| 121 |
+
nonlocal fastgpt_chat
|
| 122 |
+
fastgpt_chat = FastGPTChat(new_key)
|
| 123 |
+
return "API Key updated"
|
| 124 |
|
| 125 |
# 设置事件处理
|
| 126 |
+
submit_event = submit.click(
|
| 127 |
+
fastgpt_chat.chat,
|
| 128 |
+
inputs=[message, chatbot],
|
| 129 |
+
outputs=[message, chatbot]
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
message.submit(
|
| 133 |
fastgpt_chat.chat,
|
| 134 |
inputs=[message, chatbot],
|
|
|
|
| 136 |
)
|
| 137 |
|
| 138 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 139 |
+
api_key_input.change(update_api_key, inputs=[api_key_input], outputs=[status])
|
| 140 |
|
| 141 |
return interface
|
| 142 |
|
|
|
|
| 144 |
if __name__ == "__main__":
|
| 145 |
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 替换为你的API密钥
|
| 146 |
demo = create_chat_interface(API_KEY)
|
| 147 |
+
demo.launch(debug=True) # 启用debug模式
|