import gradio as gr import requests # 设置 FastGPT API 的基本信息 API_URL = "https://api.fastgpt.in/api/v1/chat/completions" # 请根据您的部署地址修改 API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc8gF" # 请替换为您的实际 API 密钥 def chat_with_fastgpt(user_input): headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } data = { "stream": False, "chatId": "unique_chat_id", # 请替换为唯一的聊天 ID "messages": [{"role": "user", "content": user_input}] } response = requests.post(API_URL, headers=headers, json=data) if response.status_code == 200: response_data = response.json() return response_data['choices'][0]['message']['content'] else: return f"请求失败,状态码:{response.status_code}" # 创建 Gradio 接口 interface = gr.Interface(fn=chat_with_fastgpt, inputs="text", outputs="text", live=True) interface.launch()