OpenIPOS commited on
Commit
13f8892
·
verified ·
1 Parent(s): 79f6025

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -11,15 +11,22 @@ client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_to
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):
 
11
  # 2. 增强型文档解析引擎 (支持 PDF/TXT)
12
  def extract_text_from_file(file_path):
13
  try:
14
+ # 处理 PDF
15
  if file_path.lower().endswith(".pdf"):
16
  doc = fitz.open(file_path)
17
+ return "".join([page.get_text() for page in doc])
18
+
19
+ # 处理 Word (.docx)
20
+ elif file_path.lower().endswith(".docx"):
21
+ doc = docx.Document(file_path)
22
+ return "\n".join([para.text for para in doc.paragraphs])
23
+
24
+ # 处理 TXT/MD
25
  else:
26
  with open(file_path, "r", encoding="utf-8") as f:
27
  return f.read()
28
  except Exception as e:
29
+ return f"[解析失败: 目前仅支持 PDF, TXT 及 DOCX 格式]"
30
 
31
  @spaces.GPU(duration=120) # 延长单次推理时长以支持 64K 长文本
32
  def chat_fn(message, history):