OpenIPOS commited on
Commit
5f85af6
·
verified ·
1 Parent(s): 28073be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -9,73 +9,72 @@ from huggingface_hub import InferenceClient
9
  hf_token = os.getenv("HF_TOKEN")
10
  client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
11
 
12
- # 2. 全能文档解析引擎
13
  def extract_text_from_file(file_path):
14
  try:
15
- if file_path.lower().endswith(".pdf"):
 
16
  doc = fitz.open(file_path)
17
  return "".join([page.get_text() for page in doc])
18
- elif file_path.lower().endswith(".docx"):
19
  doc = docx.Document(file_path)
20
  return "\n".join([para.text for para in doc.paragraphs])
 
 
21
  else:
22
  with open(file_path, "r", encoding="utf-8") as f:
23
  return f.read()
24
  except Exception as e:
25
- return f"[文档读取失败: {str(e)}]"
26
 
27
  @spaces.GPU(duration=120)
28
  def chat_fn(message, history):
29
- # 处理当前输入
30
  files = message.get("files", [])
31
  current_text = message.get("text", "")
32
 
33
  if files:
34
  file_path = files[0] if isinstance(files[0], str) else files[0].path
35
  doc_content = extract_text_from_file(file_path)
36
- # 截取前 40,000 字以防溢出
37
- user_payload = f"【参考文档内容】:\n{doc_content[:40000]}\n\n【用户指令】: {current_text}"
38
  else:
39
  user_payload = current_text
40
 
41
- # 3. 构建严谨的消息列表 (适配 Gradio 6.0)
42
  messages = []
43
  for turn in history:
44
- # 只保留 role 和 content,过滤掉其他干扰字段
45
- if isinstance(turn, dict) and "role" in turn and "content" in turn:
46
- # 过滤掉空的 content 或非字符串内容
47
- if turn["content"] and isinstance(turn["content"], str):
48
- messages.append({"role": turn["role"], "content": turn["content"]})
49
 
50
- # 加入当前请求
51
  messages.append({"role": "user", "content": user_payload})
52
 
53
  response = ""
54
  try:
55
- # 这里的 client.chat_completion 对 payload 非常挑剔
56
  for msg in client.chat_completion(
57
  messages,
58
  max_tokens=4096,
59
  stream=True,
60
  temperature=0.6
61
  ):
62
- if len(msg.choices) > 0:
63
  token = msg.choices[0].delta.content
64
  if token:
65
  response += token
66
  yield response
67
 
68
- # 注入品牌签名
69
  response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
70
  yield response
71
 
72
  except Exception as e:
73
- yield f"⚠️ OpenIPOS 调度异常 (API 拒绝请求): {str(e)}\n提示:请尝试清空对话历史后再试。"
 
74
 
75
- # 4. 构建 UI
76
  with gr.Blocks(fill_height=True) as demo:
77
  gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
78
- gr.Markdown("### 适配 Gradio 6.0 的高稳定性通用算力节点")
79
 
80
  gr.ChatInterface(chat_fn, multimodal=True)
81
 
 
9
  hf_token = os.getenv("HF_TOKEN")
10
  client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
11
 
12
+ # 2. 全能文档解析引擎 (增加格式容错)
13
  def extract_text_from_file(file_path):
14
  try:
15
+ suffix = file_path.lower()
16
+ if suffix.endswith(".pdf"):
17
  doc = fitz.open(file_path)
18
  return "".join([page.get_text() for page in doc])
19
+ elif suffix.endswith(".docx"):
20
  doc = docx.Document(file_path)
21
  return "\n".join([para.text for para in doc.paragraphs])
22
+ elif suffix.endswith(".doc"):
23
+ return "⚠️ 提示:系统暂不支持旧版 .doc 格式,请将其另存为 .docx 或 .pdf 后再次上传。"
24
  else:
25
  with open(file_path, "r", encoding="utf-8") as f:
26
  return f.read()
27
  except Exception as e:
28
+ return f"[解析失败: {str(e)}]"
29
 
30
  @spaces.GPU(duration=120)
31
  def chat_fn(message, history):
 
32
  files = message.get("files", [])
33
  current_text = message.get("text", "")
34
 
35
  if files:
36
  file_path = files[0] if isinstance(files[0], str) else files[0].path
37
  doc_content = extract_text_from_file(file_path)
38
+ # 严格控制输入长度:截取前 30,000 字以换取更高的翻译稳定性
39
+ user_payload = f"【文档内容参考】:\n{doc_content[:30000]}\n\n【用户指令】: {current_text}"
40
  else:
41
  user_payload = current_text
42
 
43
+ # 严谨的消息过滤
44
  messages = []
45
  for turn in history:
46
+ if isinstance(turn, dict) and turn.get("role") and turn.get("content"):
47
+ messages.append({"role": turn["role"], "content": turn["content"]})
 
 
 
48
 
 
49
  messages.append({"role": "user", "content": user_payload})
50
 
51
  response = ""
52
  try:
53
+ # 增加 stream 稳定性处理
54
  for msg in client.chat_completion(
55
  messages,
56
  max_tokens=4096,
57
  stream=True,
58
  temperature=0.6
59
  ):
60
+ if msg.choices:
61
  token = msg.choices[0].delta.content
62
  if token:
63
  response += token
64
  yield response
65
 
66
+ # 正常结束注入指纹
67
  response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
68
  yield response
69
 
70
  except Exception as e:
71
+ # 针对超时的友好提示
72
+ yield f"{response}\n\n⚠️ OpenIPOS 调度异常 (可能是文档过长导致超时): {str(e)}\n提示:请尝试清空对话或分段上传文档。"
73
 
74
+ # 3. 极简 UI
75
  with gr.Blocks(fill_height=True) as demo:
76
  gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
77
+ gr.Markdown("### 适配 Gradio 6.0 | 支持 PDF/DOCX/TXT")
78
 
79
  gr.ChatInterface(chat_fn, multimodal=True)
80