OpenIPOS commited on
Commit
10d779f
·
verified ·
1 Parent(s): ee714b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -27
app.py CHANGED
@@ -1,46 +1,53 @@
1
  import os
2
  import gradio as gr
3
  import spaces
 
4
  from huggingface_hub import InferenceClient
5
 
6
- # 1. 密钥与客户端初始化
7
  hf_token = os.getenv("HF_TOKEN")
8
  client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
9
 
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,
43
- stream=True,
44
  temperature=0.6
45
  ):
46
  if len(msg.choices) > 0:
@@ -48,23 +55,28 @@ def chat_fn(message, history):
48
  if token:
49
  response += token
50
  yield response
 
 
 
 
 
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())
 
1
  import os
2
  import gradio as gr
3
  import spaces
4
+ import fitz # PyMuPDF
5
  from huggingface_hub import InferenceClient
6
 
7
+ # 1. 客户端初始化 (利用你的 Pro 权限调用 ZeroGPU 资源)
8
  hf_token = os.getenv("HF_TOKEN")
9
  client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
10
 
11
+ # 2. 增强型文档解析引擎 (支持 PDF/TXT)
12
+ def extract_text_from_file(file_path):
13
+ try:
14
+ if file_path.lower().endswith(".pdf"):
15
+ doc = fitz.open(file_path)
16
+ text = "".join([page.get_text() for page in doc])
17
+ return text
18
+ else:
19
+ with open(file_path, "r", encoding="utf-8") as f:
20
+ return f.read()
21
+ except Exception as e:
22
+ return f"[文档解析异常: {str(e)}]"
23
+
24
+ @spaces.GPU(duration=120) # 延长单次推理时长以支持 64K 长文本
25
  def chat_fn(message, history):
 
26
  files = message.get("files", [])
27
  text_content = ""
28
 
29
  if files:
30
+ file_obj = files[0]
31
+ file_path = file_obj if isinstance(file_obj, str) else file_obj.path
32
+ text_content = extract_text_from_file(file_path)
33
+ # 64K 上下文优化:截取前 50,000 字(约 64K Token 缓冲区)
34
+ user_input = f"【参考文档内容】:\n{text_content[:50000]}\n\n【用户指令】: {message['text']}"
 
 
 
35
  else:
36
  user_input = message['text']
37
 
38
+ # 构建符合标准的消息列表
39
  messages = []
 
40
  for turn in history:
41
  messages.append({"role": turn["role"], "content": turn["content"]})
 
42
  messages.append({"role": "user", "content": user_input})
43
 
44
  response = ""
45
  try:
46
+ # 开启流式输出
47
  for msg in client.chat_completion(
48
+ messages,
49
  max_tokens=4096,
50
+ stream=True,
51
  temperature=0.6
52
  ):
53
  if len(msg.choices) > 0:
 
55
  if token:
56
  response += token
57
  yield response
58
+
59
+ # 回复结束,注入品牌指纹
60
+ response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 inipa.com 获取企业级私有部署**"
61
+ yield response
62
+
63
  except Exception as e:
64
+ yield f"⚠️ OpenIPOS 调度异常: {str(e)}。建议缩短文档长度或检查 HF_TOKEN 设置。"
65
 
66
+ # 3. 极客风格 UI 构建 (适配 Gradio 6.0)
67
  with gr.Blocks(fill_height=True) as demo:
68
+ gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
69
+ gr.Markdown("### 全球领先的通用高算力节点 (Beta)")
70
+ gr.Markdown("本节点由 **OpenIPOS Global** 部署,支持 **64K 超长上下文** 及 **PDF/TXT** 文档分析。")
71
 
 
72
  gr.ChatInterface(
73
  chat_fn,
74
  multimodal=True,
75
+ examples=[{"text": "请基于这份 PDF 文件总结三个核心逻辑点", "files": []}]
76
  )
77
+
78
  gr.Markdown("---")
79
+ gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU (H200/A100)")
80
 
81
+ # 4. 启动应用
82
  demo.launch(theme=gr.themes.Soft())