Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,150 +1,27 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
import logging
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
BASE_URL = "https://api.fastgpt.in/api" # FastGPT base URL
|
17 |
-
API_KEY = os.getenv("API_KEY", "") # Ensure you have your API key set
|
18 |
-
|
19 |
-
# Verify the API key is available
|
20 |
-
if not API_KEY:
|
21 |
-
raise ValueError("Please ensure that the API_KEY environment variable is set.")
|
22 |
-
|
23 |
-
class ChatBot:
|
24 |
-
def __init__(self):
|
25 |
-
self.headers = {
|
26 |
-
"Authorization": f"Bearer {API_KEY}",
|
27 |
-
"Content-Type": "application/json"
|
28 |
-
}
|
29 |
-
|
30 |
-
def format_message(role, content):
|
31 |
-
return {"role": role, "content": content}
|
32 |
-
|
33 |
-
def chat_stream(message, history):
|
34 |
-
chatbot = ChatBot()
|
35 |
-
|
36 |
-
logger.info(f"Sending message: {message}")
|
37 |
-
|
38 |
-
messages = [format_message("user", message)]
|
39 |
-
for human, assistant in history:
|
40 |
-
messages.append(format_message("user", human))
|
41 |
-
messages.append(format_message("assistant", assistant))
|
42 |
-
|
43 |
-
payload = {
|
44 |
-
"messages": messages,
|
45 |
-
"stream": True # Set to True if you want streaming responses
|
46 |
}
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
return history
|
63 |
-
|
64 |
-
partial_message = ""
|
65 |
-
|
66 |
-
for line in response.iter_lines():
|
67 |
-
if line:
|
68 |
-
try:
|
69 |
-
line = line.decode('utf-8')
|
70 |
-
logger.info(f"Received data: {line}")
|
71 |
-
|
72 |
-
if line.startswith('data: '):
|
73 |
-
line = line[6:]
|
74 |
-
|
75 |
-
if line == '[DONE]':
|
76 |
-
break
|
77 |
-
|
78 |
-
chunk = json.loads(line)
|
79 |
-
if chunk and "choices" in chunk and len(chunk["choices"]) > 0:
|
80 |
-
delta = chunk["choices"][0]["delta"]
|
81 |
-
if "content" in delta:
|
82 |
-
content = delta["content"]
|
83 |
-
partial_message += content
|
84 |
-
history.append((message, partial_message))
|
85 |
-
yield history
|
86 |
-
history.pop()
|
87 |
-
else:
|
88 |
-
logger.warning("Received empty or malformed choice from API.")
|
89 |
-
except json.JSONDecodeError as e:
|
90 |
-
logger.error(f"JSON parsing error: {e}")
|
91 |
-
continue
|
92 |
-
|
93 |
-
if not partial_message:
|
94 |
-
error_msg = "No valid response content received"
|
95 |
-
logger.error(error_msg)
|
96 |
-
history.append((message, error_msg))
|
97 |
-
else:
|
98 |
-
history.append((message, partial_message))
|
99 |
-
|
100 |
-
except Exception as e:
|
101 |
-
error_msg = f"Request error: {str(e)}"
|
102 |
-
logger.error(error_msg)
|
103 |
-
history.append((message, error_msg))
|
104 |
-
|
105 |
-
return history
|
106 |
-
|
107 |
-
# Gradio interface configuration
|
108 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
109 |
-
chatbot = gr.Chatbot(
|
110 |
-
height=600,
|
111 |
-
show_copy_button=True,
|
112 |
-
avatar_images=["assets/user.png", "assets/assistant.png"],
|
113 |
-
)
|
114 |
-
msg = gr.Textbox(
|
115 |
-
placeholder="Type your question here...",
|
116 |
-
container=False,
|
117 |
-
scale=7,
|
118 |
-
)
|
119 |
-
with gr.Row():
|
120 |
-
submit = gr.Button("Send", scale=2, variant="primary")
|
121 |
-
clear = gr.Button("Clear Conversation", scale=1)
|
122 |
-
|
123 |
-
# Event handling
|
124 |
-
msg.submit(
|
125 |
-
chat_stream,
|
126 |
-
[msg, chatbot],
|
127 |
-
[chatbot],
|
128 |
-
api_name="chat"
|
129 |
-
).then(
|
130 |
-
lambda: "",
|
131 |
-
None,
|
132 |
-
[msg],
|
133 |
-
api_name="clear_input"
|
134 |
-
)
|
135 |
-
|
136 |
-
submit.click(
|
137 |
-
chat_stream,
|
138 |
-
[msg, chatbot],
|
139 |
-
[chatbot],
|
140 |
-
).then(
|
141 |
-
lambda: "",
|
142 |
-
None,
|
143 |
-
[msg],
|
144 |
-
)
|
145 |
-
|
146 |
-
clear.click(lambda: [], None, chatbot)
|
147 |
-
|
148 |
-
# Start the application
|
149 |
-
if __name__ == "__main__":
|
150 |
-
demo.queue().launch(debug=True)
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
3 |
|
4 |
+
# 设置 FastGPT API 的基本信息
|
5 |
+
API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改
|
6 |
+
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
|
7 |
|
8 |
+
def chat_with_fastgpt(user_input):
|
9 |
+
headers = {
|
10 |
+
"Authorization": f"Bearer {API_KEY}",
|
11 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
+
data = {
|
14 |
+
"stream": False,
|
15 |
+
"chatId": "unique_chat_id", # 请替换为唯一的聊天 ID
|
16 |
+
"messages": [{"role": "user", "content": user_input}]
|
17 |
+
}
|
18 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
19 |
+
if response.status_code == 200:
|
20 |
+
response_data = response.json()
|
21 |
+
return response_data['choices'][0]['message']['content']
|
22 |
+
else:
|
23 |
+
return f"请求失败,状态码:{response.status_code}"
|
24 |
+
|
25 |
+
# 创建 Gradio 接口
|
26 |
+
interface = gr.Interface(fn=chat_with_fastgpt, inputs="text", outputs="text", live=True)
|
27 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|