Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
from sseclient import SSEClient
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# 加载环境变量
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# API配置
|
| 12 |
+
API_URL = os.getenv("API_URL", "https://your-api-endpoint/v1/chat/completions")
|
| 13 |
+
API_KEY = os.getenv("API_KEY", "your-api-key")
|
| 14 |
+
|
| 15 |
+
def format_message(role, content):
|
| 16 |
+
return {"role": role, "content": content}
|
| 17 |
+
|
| 18 |
+
class ChatBot:
|
| 19 |
+
def __init__(self):
|
| 20 |
+
self.headers = {
|
| 21 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 22 |
+
"Content-Type": "application/json"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def generate_stream(self, messages):
|
| 26 |
+
data = {
|
| 27 |
+
"model": "gpt-3.5-turbo", # 或其他模型
|
| 28 |
+
"messages": messages,
|
| 29 |
+
"stream": True,
|
| 30 |
+
"temperature": 0.7
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
response = requests.post(
|
| 34 |
+
API_URL,
|
| 35 |
+
headers=self.headers,
|
| 36 |
+
json=data,
|
| 37 |
+
stream=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
client = SSEClient(response)
|
| 41 |
+
return client
|
| 42 |
+
|
| 43 |
+
def chat_stream(message, history):
|
| 44 |
+
chatbot = ChatBot()
|
| 45 |
+
|
| 46 |
+
# 构建消息历史
|
| 47 |
+
messages = []
|
| 48 |
+
for human, assistant in history:
|
| 49 |
+
messages.append(format_message("user", human))
|
| 50 |
+
messages.append(format_message("assistant", assistant))
|
| 51 |
+
messages.append(format_message("user", message))
|
| 52 |
+
|
| 53 |
+
# 流式响应
|
| 54 |
+
response_stream = chatbot.generate_stream(messages)
|
| 55 |
+
partial_message = ""
|
| 56 |
+
|
| 57 |
+
for event in response_stream:
|
| 58 |
+
if event.data != "[DONE]":
|
| 59 |
+
try:
|
| 60 |
+
chunk = json.loads(event.data)
|
| 61 |
+
if chunk and "choices" in chunk:
|
| 62 |
+
delta = chunk["choices"][0]["delta"]
|
| 63 |
+
if "content" in delta:
|
| 64 |
+
partial_message += delta["content"]
|
| 65 |
+
yield partial_message
|
| 66 |
+
except json.JSONDecodeError:
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
return partial_message
|
| 70 |
+
|
| 71 |
+
# Gradio界面配置
|
| 72 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 73 |
+
chatbot = gr.Chatbot(
|
| 74 |
+
height=600,
|
| 75 |
+
show_copy_button=True,
|
| 76 |
+
avatar_images=["assets/user.png", "assets/assistant.png"],
|
| 77 |
+
)
|
| 78 |
+
msg = gr.Textbox(
|
| 79 |
+
placeholder="在这里输入您的问题...",
|
| 80 |
+
container=False,
|
| 81 |
+
scale=7,
|
| 82 |
+
)
|
| 83 |
+
with gr.Row():
|
| 84 |
+
submit = gr.Button("发送", scale=2, variant="primary")
|
| 85 |
+
clear = gr.Button("清除对话", scale=1)
|
| 86 |
+
|
| 87 |
+
# 事件处理
|
| 88 |
+
msg.submit(
|
| 89 |
+
chat_stream,
|
| 90 |
+
[msg, chatbot],
|
| 91 |
+
[chatbot],
|
| 92 |
+
api_name="chat"
|
| 93 |
+
).then(
|
| 94 |
+
lambda: "",
|
| 95 |
+
None,
|
| 96 |
+
[msg],
|
| 97 |
+
api_name="clear_input"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
submit.click(
|
| 101 |
+
chat_stream,
|
| 102 |
+
[msg, chatbot],
|
| 103 |
+
[chatbot],
|
| 104 |
+
).then(
|
| 105 |
+
lambda: "",
|
| 106 |
+
None,
|
| 107 |
+
[msg],
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
clear.click(lambda: None, None, chatbot)
|
| 111 |
+
|
| 112 |
+
# 启动应用
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.queue().launch()
|