leonsimon23 commited on
Commit
bb536e7
·
verified ·
1 Parent(s): b53d6c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -12
app.py CHANGED
@@ -1,27 +1,57 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  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
+ #API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改
12
+ #API_KEY = "您的_API_密钥" # 请替换为您的实际 API 密钥
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
  data = {
20
+ "stream": True,
21
  "chatId": "unique_chat_id", # 请替换为唯一的聊天 ID
22
  "messages": [{"role": "user", "content": user_input}]
23
  }
24
+ response = requests.post(API_URL, headers=headers, json=data, stream=True)
25
+ if response.status_code != 200:
26
+ return f"请求失败,状态码:{response.status_code}", chat_history
27
+
28
+ bot_response = ""
29
+ for chunk in response.iter_lines():
30
+ if chunk:
31
+ try:
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="请输入您的消息...")
49
+ clear = gr.Button("清除")
50
+ submit = gr.Button("发送")
51
+
52
+ submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
53
+ chat_with_fastgpt, [msg, chatbot], [msg, chatbot]
54
+ )
55
+ clear.click(lambda: None, None, chatbot, queue=False)
56
+
57
+ demo.launch()