zhang commited on
Commit
cb215a3
·
verified ·
1 Parent(s): e530bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -1,16 +1,22 @@
 
1
  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
- # turn 可能是一个字典(新版)或元组(旧版)
14
  if isinstance(turn, dict):
15
  messages.append({"role": turn["role"], "content": turn["content"]})
16
  else:
@@ -20,11 +26,11 @@ def chat_fn(message, history):
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
@@ -32,17 +38,22 @@ def chat_fn(message, history):
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()
 
1
+ import os
2
  import gradio as gr
3
  import spaces
4
  from huggingface_hub import InferenceClient
5
 
6
+ # 1. 自动读取你在 Settings 里面设置的保险箱密钥
7
+ hf_token = os.getenv("HF_TOKEN")
8
 
9
+ # 2. 初始化客户端,指向 DeepSeek-R1-32B 满血版
10
+ # 如果没有设置 HF_TOKEN,这里会报错,所以我们在下面加了异常处理
11
+ client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
12
+
13
+ # 3. 定义核心推理逻辑
14
  @spaces.GPU
15
  def chat_fn(message, history):
16
+ # 构建对话历史上下文
17
  messages = []
18
  for turn in history:
19
+ # 兼容不同版本的 Gradio 历史记录格式
20
  if isinstance(turn, dict):
21
  messages.append({"role": turn["role"], "content": turn["content"]})
22
  else:
 
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
+ # 开启流式输出 (Stream),让字一个一个跳出来
30
  try:
31
  for msg in client.chat_completion(
32
  messages,
33
+ max_tokens=4096, # 调大 token,支持长文本专利分析
34
  stream=True,
35
  ):
36
  token = msg.choices[0].delta.content
 
38
  response += token
39
  yield response
40
  except Exception as e:
41
+ yield f"⚠️ OpenIPOS 算力调度异常: {str(e)}。请确保已在 Settings 中设置 HF_TOKEN。"
42
 
43
+ # 4. 构建 OpenIPOS 品牌演示界面
44
+ with gr.Blocks(fill_height=True, theme=gr.themes.Soft()) as demo:
45
  gr.Markdown("# 🚀 OpenIPOS Global 算力节点")
46
+ gr.Markdown("### ⚖️ DeepSeek-R1 32B 法律级满血演示站")
47
+ gr.Markdown("本节点由 **OpenIPOS** 部署于全球高算力数据中心,支持完整 R1 思考链路,专为专利分析与法律逻辑设计。")
48
 
49
+ # 核心聊天界面
50
  gr.ChatInterface(
51
  chat_fn,
52
+ examples=["请分析这篇专利要求书的逻辑漏洞...", "对比香港与内地的发明专利申请流程", "帮我写一段专利侵权判定的法律意见书"],
53
  )
54
 
55
+ gr.Markdown("---")
56
+ gr.Markdown("© 2026 OpenIPOS Global Limited. All Rights Reserved. 算力驱动:Hugging Face ZeroGPU")
57
+
58
+ # 5. 启动程序
59
  demo.launch()