snym04 commited on
Commit
8444eea
·
verified ·
1 Parent(s): c9d143b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -25
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import os
3
  import yaml
4
  import shutil
 
5
  import sys
6
  from datetime import datetime
7
 
@@ -9,6 +10,8 @@ from datetime import datetime
9
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
10
  PAPERS_DIR = os.path.join(BASE_DIR, "papers")
11
  CONFIG_PATH = os.path.join(BASE_DIR, "config.yaml")
 
 
12
  os.makedirs(PAPERS_DIR, exist_ok=True)
13
 
14
  def get_debug_info():
@@ -16,6 +19,12 @@ def get_debug_info():
16
  now = datetime.now().strftime("%H:%M:%S")
17
  files = os.listdir(PAPERS_DIR) if os.path.exists(PAPERS_DIR) else "Directory missing"
18
 
 
 
 
 
 
 
19
  config_content = "Not found"
20
  if os.path.exists(CONFIG_PATH):
21
  try:
@@ -24,7 +33,7 @@ def get_debug_info():
24
  except Exception:
25
  config_content = "Error reading config"
26
 
27
- return f"[{now}] 📁 papers/ 文件夹下的文件:\n{files}\n\n[{now}] 📄 config.yaml 完整内容:\n{config_content}"
28
 
29
  def save_pdf(file):
30
  if file is None:
@@ -59,6 +68,36 @@ def save_api_key(api_key):
59
  except Exception as e:
60
  return f"❌ 保存 Key 出错: {str(e)}", get_debug_info()
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  # --- 构建单页 UI ---
63
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
64
  gr.Markdown("# 📑 PDF 助手管理后台")
@@ -69,15 +108,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
69
  # 第一部分:API 配置
70
  with gr.Group():
71
  gr.Markdown("### 1. 密钥配置")
72
- key_input = gr.Textbox(
73
- label="Gemini API Key",
74
- type="password",
75
- placeholder="在此输入您的 API Key..."
76
- )
77
  key_btn = gr.Button("保存配置", variant="primary")
78
  key_status = gr.Textbox(label="配置状态", interactive=False)
79
 
80
- gr.Markdown("---") # 分割线
81
 
82
  # 第二部分:PDF 上传
83
  with gr.Group():
@@ -86,34 +121,31 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
  pdf_btn = gr.Button("保存到 papers 文件夹", variant="primary")
87
  pdf_status = gr.Textbox(label="上传状态", interactive=False)
88
 
 
 
 
 
 
 
 
 
 
89
  # 右侧调试监控区
90
  with gr.Column(scale=1):
91
  gr.Markdown("### 🔍 实时系统监控")
92
  debug_view = gr.Textbox(
93
- label="服务器文件状态 (每操作一次即自动更新)",
94
  value=get_debug_info(),
95
- lines=20,
96
  interactive=False
97
  )
98
  refresh_btn = gr.Button("🔄 手动刷新状态")
99
 
100
  # 绑定事件逻辑
101
- key_btn.click(
102
- fn=save_api_key,
103
- inputs=key_input,
104
- outputs=[key_status, debug_view]
105
- )
106
-
107
- pdf_btn.click(
108
- fn=save_pdf,
109
- inputs=pdf_input,
110
- outputs=[pdf_status, debug_view]
111
- )
112
-
113
- refresh_btn.click(
114
- fn=get_debug_info,
115
- outputs=debug_view
116
- )
117
 
118
  if __name__ == "__main__":
119
  demo.launch()
 
2
  import os
3
  import yaml
4
  import shutil
5
+ import subprocess
6
  import sys
7
  from datetime import datetime
8
 
 
10
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
11
  PAPERS_DIR = os.path.join(BASE_DIR, "papers")
12
  CONFIG_PATH = os.path.join(BASE_DIR, "config.yaml")
13
+ OUTPUT_DIR = os.path.join(BASE_DIR, "mineru_outputs")
14
+
15
  os.makedirs(PAPERS_DIR, exist_ok=True)
16
 
17
  def get_debug_info():
 
19
  now = datetime.now().strftime("%H:%M:%S")
20
  files = os.listdir(PAPERS_DIR) if os.path.exists(PAPERS_DIR) else "Directory missing"
21
 
22
+ # 检查输出目录是否存在
23
+ output_status = "Not generated"
24
+ if os.path.exists(OUTPUT_DIR):
25
+ out_files = os.listdir(OUTPUT_DIR)
26
+ output_status = f"Exists ({len(out_files)} items)"
27
+
28
  config_content = "Not found"
29
  if os.path.exists(CONFIG_PATH):
30
  try:
 
33
  except Exception:
34
  config_content = "Error reading config"
35
 
36
+ return f"[{now}] 📁 papers/ 文件夹:\n{files}\n\n[{now}] 📂 mineru_outputs:\n{output_status}\n\n[{now}] 📄 config.yaml 内容:\n{config_content}"
37
 
38
  def save_pdf(file):
39
  if file is None:
 
68
  except Exception as e:
69
  return f"❌ 保存 Key 出错: {str(e)}", get_debug_info()
70
 
71
+ def run_mineru_parsing():
72
+ """执行 PDF 解析逻辑"""
73
+ # 1. 判断是否上传了 PDF (检查 papers 文件夹是否为空)
74
+ if not os.path.exists(PAPERS_DIR) or not any(f.endswith('.pdf') for f in os.listdir(PAPERS_DIR)):
75
+ return "❌ 未发现已上传的 PDF 文件,请先执行步骤 2。", get_debug_info()
76
+
77
+ try:
78
+ # 2. 设置环境变量
79
+ env = os.environ.copy()
80
+ env["MINERU_FORMULA_ENABLE"] = "false"
81
+ env["MINERU_TABLE_ENABLE"] = "false"
82
+
83
+ # 3. 执行 Mineru 命令
84
+ # 使用 list 形式传递参数更安全
85
+ command = ["mineru", "-p", "papers", "-o", "mineru_outputs"]
86
+
87
+ # 提示开始执行
88
+ print(f"Executing: {' '.join(command)}", file=sys.stderr, flush=True)
89
+
90
+ # 执行并等待结束
91
+ result = subprocess.run(command, env=env, capture_output=True, text=True)
92
+
93
+ if result.returncode == 0:
94
+ return "✅ PDF解析完成", get_debug_info()
95
+ else:
96
+ return f"❌ 解析过程中出错: {result.stderr}", get_debug_info()
97
+
98
+ except Exception as e:
99
+ return f"❌ 执行命令时发生异常: {str(e)}", get_debug_info()
100
+
101
  # --- 构建单页 UI ---
102
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
103
  gr.Markdown("# 📑 PDF 助手管理后台")
 
108
  # 第一部分:API 配置
109
  with gr.Group():
110
  gr.Markdown("### 1. 密钥配置")
111
+ key_input = gr.Textbox(label="Gemini API Key", type="password")
 
 
 
 
112
  key_btn = gr.Button("保存配置", variant="primary")
113
  key_status = gr.Textbox(label="配置状态", interactive=False)
114
 
115
+ gr.Markdown("---")
116
 
117
  # 第二部分:PDF 上传
118
  with gr.Group():
 
121
  pdf_btn = gr.Button("保存到 papers 文件夹", variant="primary")
122
  pdf_status = gr.Textbox(label="上传状态", interactive=False)
123
 
124
+ gr.Markdown("---")
125
+
126
+ # 第三部分:解析步骤
127
+ with gr.Group():
128
+ gr.Markdown("### 3. 解析 PDF")
129
+ gr.Markdown("<small>说明:禁用公式与表格提取以加快速度</small>")
130
+ parse_btn = gr.Button("🚀 开始解析 (Run Mineru)", variant="secondary")
131
+ parse_status = gr.Textbox(label="解析进度/结果", interactive=False)
132
+
133
  # 右侧调试监控区
134
  with gr.Column(scale=1):
135
  gr.Markdown("### 🔍 实时系统监控")
136
  debug_view = gr.Textbox(
137
+ label="服务器文件状态",
138
  value=get_debug_info(),
139
+ lines=25,
140
  interactive=False
141
  )
142
  refresh_btn = gr.Button("🔄 手动刷新状态")
143
 
144
  # 绑定事件逻辑
145
+ key_btn.click(fn=save_api_key, inputs=key_input, outputs=[key_status, debug_view])
146
+ pdf_btn.click(fn=save_pdf, inputs=pdf_input, outputs=[pdf_status, debug_view])
147
+ parse_btn.click(fn=run_mineru_parsing, outputs=[parse_status, debug_view])
148
+ refresh_btn.click(fn=get_debug_info, outputs=debug_view)
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  if __name__ == "__main__":
151
  demo.launch()