Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,46 +3,42 @@ import requests
|
|
3 |
import time
|
4 |
|
5 |
# 设置 FastGPT API 的基本信息
|
6 |
-
API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改
|
7 |
-
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
|
8 |
|
9 |
|
10 |
# 设置 FastGPT API 的基本信息
|
11 |
-
|
12 |
-
|
13 |
|
|
|
14 |
def chat_with_fastgpt(user_input, chat_history):
|
15 |
headers = {
|
16 |
"Authorization": f"Bearer {API_KEY}",
|
17 |
"Content-Type": "application/json"
|
18 |
}
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
}
|
24 |
-
|
|
|
|
|
25 |
if response.status_code != 200:
|
26 |
return f"请求失败,状态码:{response.status_code}", chat_history
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
message = chunk.decode('utf-8')
|
33 |
-
if message:
|
34 |
-
bot_response += message
|
35 |
-
chat_history[-1][1] = bot_response
|
36 |
-
yield "", chat_history
|
37 |
-
except Exception as e:
|
38 |
-
print(f"解析响应时出错: {e}")
|
39 |
-
chat_history[-1][1] = bot_response
|
40 |
-
yield "", chat_history
|
41 |
|
|
|
42 |
def user(user_input, chat_history):
|
43 |
chat_history.append([user_input, None])
|
44 |
return "", chat_history
|
45 |
|
|
|
46 |
with gr.Blocks() as demo:
|
47 |
chatbot = gr.Chatbot()
|
48 |
msg = gr.Textbox(placeholder="请输入您的消息...")
|
@@ -54,4 +50,5 @@ with gr.Blocks() as demo:
|
|
54 |
)
|
55 |
clear.click(lambda: None, None, chatbot, queue=False)
|
56 |
|
|
|
57 |
demo.launch()
|
|
|
3 |
import time
|
4 |
|
5 |
# 设置 FastGPT API 的基本信息
|
6 |
+
#API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改
|
7 |
+
#API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
|
8 |
|
9 |
|
10 |
# 设置 FastGPT API 的基本信息
|
11 |
+
API_URL = "https://api.fastgpt.com/generate" # 请根据您的实际部署地址修改
|
12 |
+
API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥
|
13 |
|
14 |
+
# 调用 FastGPT API 的函数
|
15 |
def chat_with_fastgpt(user_input, chat_history):
|
16 |
headers = {
|
17 |
"Authorization": f"Bearer {API_KEY}",
|
18 |
"Content-Type": "application/json"
|
19 |
}
|
20 |
+
max_tokens = 50 # 生成文本的最大长度
|
21 |
+
temperature = 0.7 # 控制生成文本的随机性程度
|
22 |
+
|
23 |
+
# 构造API请求URL
|
24 |
+
url = f'{API_URL}?prompt={user_input}&max_tokens={max_tokens}&temperature={temperature}'
|
25 |
+
|
26 |
+
response = requests.get(url, headers=headers)
|
27 |
+
|
28 |
if response.status_code != 200:
|
29 |
return f"请求失败,状态码:{response.status_code}", chat_history
|
30 |
|
31 |
+
# 解析并返回生成的文本
|
32 |
+
generated_text = response.json().get('generated_text', '')
|
33 |
+
chat_history.append([user_input, generated_text])
|
34 |
+
return "", chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# 添加用户输入的处理逻辑
|
37 |
def user(user_input, chat_history):
|
38 |
chat_history.append([user_input, None])
|
39 |
return "", chat_history
|
40 |
|
41 |
+
# 创建 Gradio 界面
|
42 |
with gr.Blocks() as demo:
|
43 |
chatbot = gr.Chatbot()
|
44 |
msg = gr.Textbox(placeholder="请输入您的消息...")
|
|
|
50 |
)
|
51 |
clear.click(lambda: None, None, chatbot, queue=False)
|
52 |
|
53 |
+
# 启动 Gradio 界面
|
54 |
demo.launch()
|