Spaces:
Running on Zero
Running on Zero
zhang commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,47 @@ import gradio as gr
|
|
| 2 |
import spaces
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# 初始化客户端
|
| 6 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")
|
| 7 |
|
| 8 |
@spaces.GPU
|
| 9 |
def chat_fn(message, history):
|
| 10 |
-
# 构建
|
| 11 |
messages = []
|
| 12 |
for turn in history:
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
messages.append({"role": "user", "content": message})
|
| 16 |
|
| 17 |
-
generate_kwargs = dict(
|
| 18 |
-
max_tokens=2048,
|
| 19 |
-
stream=True,
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
response = ""
|
| 23 |
-
# 流式
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# 构建 OpenIPOS 品牌界面
|
| 31 |
with gr.Blocks(fill_height=True) as demo:
|
| 32 |
gr.Markdown("# 🚀 OpenIPOS Global 算力节点")
|
| 33 |
gr.Markdown("### DeepSeek-R1 32B 满血版演示站")
|
| 34 |
|
| 35 |
-
#
|
| 36 |
gr.ChatInterface(
|
| 37 |
-
|
| 38 |
-
description="本节点由 OpenIPOS 提供法律级高逻辑推理算力支持。支持
|
| 39 |
)
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 2 |
import spaces
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# 初始化客户端
|
| 6 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")
|
| 7 |
|
| 8 |
@spaces.GPU
|
| 9 |
def chat_fn(message, history):
|
| 10 |
+
# 构建对话消息列表
|
| 11 |
messages = []
|
| 12 |
for turn in history:
|
| 13 |
+
# turn 可能是一个字典(新版)或元组(旧版)
|
| 14 |
+
if isinstance(turn, dict):
|
| 15 |
+
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 16 |
+
else:
|
| 17 |
+
messages.append({"role": "user", "content": turn[0]})
|
| 18 |
+
messages.append({"role": "assistant", "content": turn[1]})
|
| 19 |
+
|
| 20 |
messages.append({"role": "user", "content": message})
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
response = ""
|
| 23 |
+
# 调用流式接口
|
| 24 |
+
try:
|
| 25 |
+
for msg in client.chat_completion(
|
| 26 |
+
messages,
|
| 27 |
+
max_tokens=2048,
|
| 28 |
+
stream=True,
|
| 29 |
+
):
|
| 30 |
+
token = msg.choices[0].delta.content
|
| 31 |
+
if token:
|
| 32 |
+
response += token
|
| 33 |
+
yield response
|
| 34 |
+
except Exception as e:
|
| 35 |
+
yield f"抱歉,算力节点暂时忙碌,请稍后再试。错误信息: {str(e)}"
|
| 36 |
|
| 37 |
# 构建 OpenIPOS 品牌界面
|
| 38 |
with gr.Blocks(fill_height=True) as demo:
|
| 39 |
gr.Markdown("# 🚀 OpenIPOS Global 算力节点")
|
| 40 |
gr.Markdown("### DeepSeek-R1 32B 满血版演示站")
|
| 41 |
|
| 42 |
+
# 使用最简洁的参数配置
|
| 43 |
gr.ChatInterface(
|
| 44 |
+
chat_fn,
|
| 45 |
+
description="本节点由 OpenIPOS 提供法律级高逻辑推理算力支持。支持完整思考过程。",
|
| 46 |
)
|
| 47 |
|
| 48 |
demo.launch()
|