Spaces:
Running
Running
File size: 3,836 Bytes
c0b418a cb162de c0b418a cb162de c0b418a c8c2297 c0b418a c8c2297 c0b418a cb162de c0b418a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import gradio as gr
import os
import requests
# 从环境变量中读取 API 密钥
DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
DASHSCOPE_API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
# 设置 DeepSeek API 客户端
class DeepSeekClient:
def __init__(self, api_key, base_url):
self.api_key = api_key
self.base_url = base_url
def chat(self, question):
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}
data = {
"model": "deepseek-r1", # 使用 DeepSeek 模型
"messages": [{"role": "user", "content": question}],
}
try:
response = requests.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=data
)
response.raise_for_status()
result = response.json()
# 获取思考过程和最终答案
reasoning_content = result["choices"][0].get("reasoning_content", "没有思考过程")
bot_response = result["choices"][0]["message"]["content"]
return bot_response, reasoning_content
except Exception as e:
return f"Error: {e}", None
# 创建 DeepSeek 客户端
client = DeepSeekClient(api_key=DASHSCOPE_API_KEY, base_url=DASHSCOPE_API_URL)
# 定义聊天函数
def answer(question, history):
if history is None:
history = []
# 获取模型响应
bot_response, reasoning_content = client.chat(question)
# 输出思考过程与最终答案(用于调试)
print("思考过程:", reasoning_content)
print("最终答案:", bot_response)
# 更新历史记录
history.append({"role": "user", "content": question})
history.append({"role": "assistant", "content": bot_response})
return history, history
# 创建 Gradio 界面
with gr.Blocks(css="""
/* 修改布局适应手机端 */
.gradio-container {
background-color: #f0f0f0;
border-radius: 15px;
padding: 20px;
max-width: 500px;
margin: auto;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.gr-button {
background-color: #4CAF50;
color: white;
border-radius: 10px;
padding: 12px 25px;
border: none;
font-weight: bold;
}
.gr-button:hover {
background-color: #45a049;
}
.gr-chatbot {
font-family: 'Arial', sans-serif;
height: 300px;
max-height: 300px;
overflow-y: auto;
padding: 10px;
margin-bottom: 20px;
background-color: #ffffff;
border-radius: 15px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.chatbot-message {
margin: 10px;
padding: 10px;
border-radius: 12px;
background-color: #e1f5fe;
}
.user-message {
background-color: #a5d6a7;
color: #ffffff;
}
.assistant-message {
background-color: #e3f2fd;
color: #000000;
}
input[type="text"] {
font-size: 16px;
border-radius: 10px;
padding: 12px;
width: 100%;
background-color: #f9f9f9;
margin-top: 10px;
}
/* 手机端优化:输入框宽度和响应式设计 */
@media (max-width: 600px) {
.gradio-container {
padding: 15px;
width: 90%;
}
.gr-button {
padding: 10px 20px;
}
}
""") as demo:
chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # 使用 Gradio 的聊天组件
state = gr.State([])
with gr.Row():
txt = gr.Textbox(placeholder="请输入您的问题并按回车发送", lines=1)
txt.submit(answer, [txt, state], [chatbot, state])
demo.launch()
|