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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -5,11 +5,11 @@ import fitz # PyMuPDF
5
  import docx # python-docx
6
  from huggingface_hub import InferenceClient
7
 
8
- # 1. 客户端初始化
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()
@@ -19,40 +19,44 @@ def extract_text_from_file(file_path):
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
@@ -63,19 +67,19 @@ def chat_fn(message, history):
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
 
81
  gr.Markdown("---")
 
5
  import docx # python-docx
6
  from huggingface_hub import InferenceClient
7
 
8
+ # 1. 初始化
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()
 
19
  elif suffix.endswith(".docx"):
20
  doc = docx.Document(file_path)
21
  return "\n".join([para.text for para in doc.paragraphs])
 
 
22
  else:
23
  with open(file_path, "r", encoding="utf-8") as f:
24
  return f.read()
25
  except Exception as e:
26
+ return f"[文档读取失败: {str(e)}]"
27
 
28
  @spaces.GPU(duration=120)
29
  def chat_fn(message, history):
30
+ # a. 提取当前文本和文件
31
  current_text = message.get("text", "")
32
+ files = message.get("files", [])
33
 
34
+ # b. 构造当前用户输入的 Payload
35
  if files:
36
  file_path = files[0] if isinstance(files[0], str) else files[0].path
37
+ content = extract_text_from_file(file_path)
38
+ # 为保证 100% 成功率,初次测试建议截取前 20,000 字
39
+ user_input = f"【参考文档】:\n{content[:20000]}\n\n【指令】: {current_text}"
40
  else:
41
+ user_input = current_text
42
 
43
+ # c. 严格的消息格式化(关键:只传字符串内容)
44
+ formatted_messages = []
45
  for turn in history:
46
+ # 只处理 role 为 user/assistant 且 content 为字符串的项
47
+ role = turn.get("role")
48
+ content = turn.get("content")
49
+ if role in ["user", "assistant"] and isinstance(content, str):
50
+ formatted_messages.append({"role": role, "content": content})
51
 
52
+ # 加入当前这轮
53
+ formatted_messages.append({"role": "user", "content": user_input})
54
 
55
  response = ""
56
  try:
57
+ # 调用 API
58
  for msg in client.chat_completion(
59
+ formatted_messages,
60
  max_tokens=4096,
61
  stream=True,
62
  temperature=0.6
 
67
  response += token
68
  yield response
69
 
70
+ # 注入品牌指纹
71
  response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
72
  yield response
73
 
74
  except Exception as e:
75
+ yield f"⚠️ OpenIPOS 算力调度异常: {str(e)}\n\n【解决方案】:请点击右上角垃圾桶图标『清空对话历史』后再重新上传文件。"
 
76
 
77
+ # 3. 极简 UI 构建
78
  with gr.Blocks(fill_height=True) as demo:
79
  gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
80
+ gr.Markdown("### 工业级稳定性适配 | 支持 PDF/DOCX/TXT")
81
 
82
+ # 使用官方推荐的 ChatInterface 默认配置
83
  gr.ChatInterface(chat_fn, multimodal=True)
84
 
85
  gr.Markdown("---")