Spaces:
Sleeping
Sleeping
Update app.py
Browse files18 unmodified lines1920212223242549 unmodified lines7576777879808182838485868788899091929394959697989910010110210322 unmodified lines12612712812913013113218 unmodified linesnow = datetime.now().strftime("%H:%M:%S")files = os.listdir(PAPERS_DIR) if os.path.exists(PAPERS_DIR) else "Directory missing"# 检查输出目录是否存在output_status = "Not generated"if os.path.exists(OUTPUT_DIR):out_files = os.listdir(OUTPUT_DIR)49 unmodified linesreturn "❌ 未发现已上传的 PDF 文件,请先执行步骤 2。", get_debug_info()try:# 2. 设置环境变量env = os.environ.copy()env["MINERU_FORMULA_ENABLE"] = "false"env["MINERU_TABLE_ENABLE"] = "false"# 强制指定为 cpu 模式,防止 mineru 内部检测 torch/cuda 时报错env["MINERU_DEVICE_MODE"] = "cpu"# 3. 执行 Mineru 命令command = ["mineru", "-p", "papers", "-o", "mineru_outputs"]print(f"Executing: {' '.join(command)}", file=sys.stderr, flush=True)# 使用 subprocess 执行result = subprocess.run(command, env=env, capture_output=True, text=True)if result.returncode == 0:return "✅ PDF解析完成", get_debug_info()else:# 如果报错,返回具体的错误信息供调试return f"❌ 解析过程中出错: {result.stderr}", get_debug_info()except Exception as e:return f"❌ 执行命令时发生异常: {str(e)}", get_debug_info()# --- 构建单页 UI ---with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📑 PDF 助手管理后台")22 unmodified lines# 第三部分:解析步骤with gr.Group():gr.Markdown("### 3. 解析 PDF")gr.Markdown("<small>说明:禁用公式与表格提取以加快速度</small>")parse_btn = gr.Button("🚀 开始解析 (Run Mineru)", variant="secondary")parse_status = gr.Textbox(label="解析进度/结果", interactive=False)18 unmodified lines1920212223242549 unmodified lines7576777879808182838485868788899091929394959697989910010110210310410510610710810922 unmodified lines13213313413513613713818 unmodified linesnow = datetime.now().strftime("%H:%M:%S")files = os.listdir(PAPERS_DIR) if os.path.exists(PAPERS_DIR) else "Directory missing"# 检查输出目录状态output_status = "Not generated"if os.path.exists(OUTPUT_DIR):out_files = os.listdir(OUTPUT_DIR)49 unmodified linesreturn "❌ 未发现已上传的 PDF 文件,请先执行步骤 2。", get_debug_info()try:# 2. 设置环境变量 (包含你发现的虚拟显存设置)env = os.environ.copy()env["MINERU_FORMULA_ENABLE"] = "false"env["MINERU_TABLE_ENABLE"] = "false"env["MINERU_DEVICE_MODE"] = "cpu"# 核心修改:添加虚拟显存大小设置,规避 torch 检测错误env["MINERU_VIRTUAL_VRAM_SIZE"] = "8"# 3. 执行 Mineru 命令command = ["mineru", "-p", "papers", "-o", "mineru_outputs"]# 将启动信息打印到 stderr 以便在 HF Log 中查看print(f">>> 开始解析任务: {' '.join(command)}", file=sys.stderr, flush=True)# 执行子进程# capture_output=True 将捕获输出,text=True 将结果作为字符串处理result = subprocess.run(command, env=env, capture_output=True, text=True)if result.returncode == 0:print(">>> 解析任务成功结束", file=sys.stderr, flush=True)return "✅ PDF解析完成", get_debug_info()else:# 打印详细错误到后台日志print(f">>> 解析任务失败: {result.stderr}", file=sys.stderr, flush=True)return f"❌ 解析过程中出错: {result.stderr}", get_debug_info()except Exception as e:return f"❌ 执行命令时发生异常: {str(e)}", get_debug_info()# --- 构建单页 UI ---with gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# 📑 PDF 助手管理后台")22 unmodified lines# 第三部分:解析步骤with gr.Group():gr.Markdown("### 3. 解析 PDF")gr.Markdown("<small>已应用环境变量:MINERU_VIRTUAL_VRAM_SIZE=8</small>")parse_btn = gr.Button("🚀 开始解析 (Run Mineru)", variant="secondary")parse_status = gr.Textbox(label="解析进度/结果", interactive=False)
app.py+13-7