zhang commited on
Commit
f3f9dbd
·
verified ·
1 Parent(s): 97de0a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -10,37 +10,33 @@ client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_to
10
  # 2. 定义核心推理逻辑
11
  @spaces.GPU
12
  def chat_fn(message, history):
13
- # 获取上传的文件列表
14
  files = message.get("files", [])
15
  text_content = ""
16
 
17
- # 如果有上传文件,读取第一个文件的内容
18
  if files:
19
  try:
20
- # 使用 Python 原生方式读取文本
21
- with open(files[0], "r", encoding="utf-8") as f:
 
22
  text_content = f.read()
23
  user_input = f"【参考文件内容】:\n{text_content}\n\n【用户指令】: {message['text']}"
24
  except Exception as e:
25
- user_input = f"{message['text']} (文件读取失败: {str(e)})"
26
  else:
27
  user_input = message['text']
28
 
29
- # 构建对话历史
30
  messages = []
 
31
  for turn in history:
32
- # 兼容 Gradio 5.x 的消息字典格式
33
- if isinstance(turn, dict):
34
- messages.append({"role": turn["role"], "content": turn["content"]})
35
- elif isinstance(turn, (list, tuple)) and len(turn) >= 2:
36
- messages.append({"role": "user", "content": turn[0]})
37
- messages.append({"role": "assistant", "content": turn[1]})
38
 
39
  messages.append({"role": "user", "content": user_input})
40
 
41
  response = ""
42
  try:
43
- # 尝试推向 32k 级别的上下文
44
  for msg in client.chat_completion(
45
  messages,
46
  max_tokens=4096,
@@ -55,20 +51,20 @@ def chat_fn(message, history):
55
  except Exception as e:
56
  yield f"⚠️ OpenIPOS 算力调度异常: {str(e)}"
57
 
58
- # 3. 构建 OpenIPOS 通用算力界面
59
- with gr.Blocks(fill_height=True, theme=gr.themes.Soft()) as demo:
60
  gr.Markdown("# 🚀 OpenIPOS Global 通用算力节点")
61
  gr.Markdown("### DeepSeek-R1 32B 满血推理引擎")
62
- gr.Markdown("本节点提供**大规模、高逻辑密度**的通用计算服务。支持 `.txt`, `.md`, `.py` 等文本文件上传分析。")
63
 
64
- # 开启多模态输入(左侧会出现 + 号)
65
  gr.ChatInterface(
66
  chat_fn,
67
  multimodal=True,
68
- type="messages", # 显式声明消息格式
69
  )
70
 
71
  gr.Markdown("---")
72
- gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU | 上下文支持:测试峰值 32K Token")
73
 
74
- demo.launch()
 
 
10
  # 2. 定义核心推理逻辑
11
  @spaces.GPU
12
  def chat_fn(message, history):
13
+ # 处理上传的文件(Gradio 6.0 中文件是以列表形式传在 message["files"] 里的)
14
  files = message.get("files", [])
15
  text_content = ""
16
 
 
17
  if files:
18
  try:
19
+ # 读取第一个上传的文本文件
20
+ file_path = files[0] if isinstance(files[0], str) else files[0].path
21
+ with open(file_path, "r", encoding="utf-8") as f:
22
  text_content = f.read()
23
  user_input = f"【参考文件内容】:\n{text_content}\n\n【用户指令】: {message['text']}"
24
  except Exception as e:
25
+ user_input = f"{message['text']} (文件解析提示: 目前仅支持纯文本文件读取)"
26
  else:
27
  user_input = message['text']
28
 
29
+ # 构建符合 DeepSeek 标准的消息列表
30
  messages = []
31
+ # 在 Gradio 6.0 的 ChatInterface 中,history 默认已经是消息字典列表
32
  for turn in history:
33
+ messages.append({"role": turn["role"], "content": turn["content"]})
 
 
 
 
 
34
 
35
  messages.append({"role": "user", "content": user_input})
36
 
37
  response = ""
38
  try:
39
+ # 上下文测试:最大 token 设为 8k(对应约 32k 上下文窗口)
40
  for msg in client.chat_completion(
41
  messages,
42
  max_tokens=4096,
 
51
  except Exception as e:
52
  yield f"⚠️ OpenIPOS 算力调度异常: {str(e)}"
53
 
54
+ # 3. 构建 OpenIPOS 通用算力界面 (适配 Gradio 6.0)
55
+ with gr.Blocks(fill_height=True) as demo:
56
  gr.Markdown("# 🚀 OpenIPOS Global 通用算力节点")
57
  gr.Markdown("### DeepSeek-R1 32B 满血推理引擎")
58
+ gr.Markdown("本节点已适配 **Gradio 6.0** 环境。支持超长上下文测试与文本文件上传解析。")
59
 
60
+ # 开启多模态输入(删除了导致报错的 type 参数)
61
  gr.ChatInterface(
62
  chat_fn,
63
  multimodal=True,
 
64
  )
65
 
66
  gr.Markdown("---")
67
+ gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU | Gradio 6.0 Ready")
68
 
69
+ # 主题 theme 移到了这里,符合新版规范
70
+ demo.launch(theme=gr.themes.Soft())