Laramie2 commited on
Commit
ef82173
·
verified ·
1 Parent(s): 0fc30ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -57
app.py CHANGED
@@ -108,12 +108,33 @@ def get_debug_info():
108
  def save_pdf(file):
109
  if file is None: return "❌ Please select a PDF first", get_debug_info()
110
  try:
 
 
 
 
 
 
111
  file_path = os.path.join(PAPERS_DIR, os.path.basename(file.name))
112
  shutil.copy(file.name, file_path)
113
  return f"✅ Saved: {os.path.basename(file.name)}", get_debug_info()
114
  except Exception as e:
115
  return f"❌ Error: {str(e)}", get_debug_info()
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  def save_api_settings(api_key, api_base_url=None):
118
  if not api_key: return "❌ Key cannot be empty", get_debug_info()
119
  try:
@@ -274,43 +295,38 @@ body, .gradio-container {
274
  .gradio-group:hover { box-shadow: 0 8px 32px rgba(168, 85, 247, 0.12) !important; border-color: #C084FC !important; }
275
  .dark .gradio-group { background: rgba(30, 30, 30, 0.9) !important; border-color: rgba(168, 85, 247, 0.3) !important; }
276
 
277
- /* ======== 新增1. 隐藏原生组件,自定义全局拖拽与点击上传区域 ======== */
278
  #pdf-upload-box {
279
- background: transparent !important;
280
- border: 2px dashed rgba(192, 132, 252, 0.5) !important;
281
  border-radius: 12px !important;
282
- min-height: 100px !important;
283
- position: relative;
284
- overflow: hidden;
285
- display: flex !important; align-items: center !important; justify-content: center !important;
286
  transition: all 0.3s ease !important;
287
  }
288
- #pdf-upload-box:hover { border-color: #A855F7 !important; background: rgba(168, 85, 247, 0.05) !important; }
289
-
290
- /* 将原生的输入框变为全透明但保留点击/拖拽属性 */
291
- #pdf-upload-box > * { opacity: 0 !important; z-index: 10 !important; position: relative; width: 100%; height: 100%; cursor: pointer;}
292
-
293
- /* 自定义文字显示 */
294
- #pdf-upload-box::before {
295
- content: "🖱️ 点击此处选择 PDF \A 🌍 或将文件拖拽至全局任意位置";
296
- white-space: pre-wrap; text-align: center; color: #7E22CE;
297
- font-weight: 600; font-size: 1.1rem; position: absolute;
298
- z-index: 1; pointer-events: none; line-height: 1.6;
299
  }
300
 
301
- /* 激活全局拖拽时的魔术全屏放大样式 */
302
- body.dragging-file #pdf-upload-box {
303
- position: fixed !important; top: 0 !important; left: 0 !important;
304
- width: 100vw !important; height: 100vh !important;
305
- z-index: 99999 !important; background: rgba(250, 245, 255, 0.85) !important;
306
- backdrop-filter: blur(8px) !important; border: 6px dashed #A855F7 !important; border-radius: 0 !important;
 
 
 
307
  }
308
- body.dragging-file #pdf-upload-box::before {
309
- content: "📥 松开鼠标,立即上传 PDF"; font-size: 2.5rem; color: #9333EA; font-weight: 800;
 
 
310
  }
 
311
 
312
- /* ======== 新增:2. 等长圆角按钮及悬浮动效 ======== */
313
- .action-row { display: flex !important; gap: 15px !important; margin-bottom: 10px !important; }
314
  .action-btn {
315
  border-radius: 30px !important;
316
  background: linear-gradient(135deg, #A855F7, #9333EA) !important;
@@ -337,26 +353,6 @@ body.dragging-file #pdf-upload-box::before {
337
  ::-webkit-scrollbar-thumb:hover { background: linear-gradient(135deg, #9333EA, #A855F7); }
338
  """
339
 
340
- # ======== 新增:全局拖拽监听 JavaScript ========
341
- global_drag_js = """
342
- function() {
343
- let dragCounter = 0;
344
- window.addEventListener('dragenter', (e) => {
345
- e.preventDefault(); dragCounter++; document.body.classList.add('dragging-file');
346
- });
347
- window.addEventListener('dragleave', (e) => {
348
- e.preventDefault(); dragCounter--;
349
- if(dragCounter === 0) document.body.classList.remove('dragging-file');
350
- });
351
- window.addEventListener('drop', (e) => {
352
- dragCounter = 0;
353
- // 延迟移除class以确保Gradio能捕捉到底层组件的drop事件
354
- setTimeout(() => document.body.classList.remove('dragging-file'), 150);
355
- });
356
- window.addEventListener('dragover', (e) => { e.preventDefault(); });
357
- }
358
- """
359
-
360
  with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
361
  with gr.Column(elem_id="col-container"):
362
  gr.Markdown("# **PaperX / Mineru Parsing Platform**", elem_id="main-title")
@@ -377,10 +373,16 @@ with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
377
  with gr.Group(elem_classes="gradio-group"):
378
  gr.Markdown("### 📄 2. Document Parsing")
379
 
380
- # 调整了这里控件CSS做UI隐藏和特效
381
- pdf_input = gr.File(label="", file_types=[".pdf"], elem_id="pdf-upload-box")
 
 
 
 
 
 
 
382
 
383
- parse_btn = gr.Button("🚀 Start Mineru & DAG Extraction", variant="primary", size="lg")
384
  parse_status = gr.Textbox(
385
  show_label=False, placeholder="Waiting for document upload...", lines=1, interactive=False, elem_classes="status-text"
386
  )
@@ -390,13 +392,13 @@ with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
390
  gr.Markdown("### 🎯 3. Asset Generation")
391
  gr.Markdown("Generate final formats based on DAG structure:")
392
 
393
- # 使用自定义的 .action-row 和 .action-btn 实现等宽高丝滑悬浮按钮
394
  with gr.Row(elem_classes="action-row"):
395
  gen_ppt_btn = gr.Button("📊 Gen PPT", elem_classes="action-btn")
396
  gen_poster_btn = gr.Button("🖼️ Gen Poster", elem_classes="action-btn")
397
  gen_pr_btn = gr.Button("📰 Gen Article", elem_classes="action-btn")
398
 
399
- gen_all_btn = gr.Button("✨ Generate All Assets (ALL)", variant="primary")
 
400
 
401
  # ================= RIGHT COLUMN: OUTPUTS & LOGS =================
402
  with gr.Column(scale=1):
@@ -428,7 +430,8 @@ with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
428
  key_btn.click(fn=save_api_settings, inputs=[key_input, api_base_url_input], outputs=[parse_status, debug_view])
429
 
430
  pdf_input.upload(fn=save_pdf, inputs=pdf_input, outputs=[parse_status, debug_view])
431
- pdf_input.clear(fn=lambda: ("ℹ️ File cleared", get_debug_info()), outputs=[parse_status, debug_view])
 
432
 
433
  parse_btn.click(fn=run_mineru_parsing_and_dag_gen, outputs=[parse_status, debug_view, cmd_logs])
434
 
@@ -444,8 +447,5 @@ with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
444
 
445
  refresh_btn.click(fn=get_debug_info, outputs=debug_view)
446
 
447
- # 在页面加载时注入全局拖拽脚本
448
- demo.load(js=global_drag_js)
449
-
450
  if __name__ == "__main__":
451
  demo.launch()
 
108
  def save_pdf(file):
109
  if file is None: return "❌ Please select a PDF first", get_debug_info()
110
  try:
111
+ # 先清空原有 PDF,确保只有最新的一份
112
+ for f in os.listdir(PAPERS_DIR):
113
+ file_to_del = os.path.join(PAPERS_DIR, f)
114
+ if os.path.isfile(file_to_del):
115
+ os.remove(file_to_del)
116
+
117
  file_path = os.path.join(PAPERS_DIR, os.path.basename(file.name))
118
  shutil.copy(file.name, file_path)
119
  return f"✅ Saved: {os.path.basename(file.name)}", get_debug_info()
120
  except Exception as e:
121
  return f"❌ Error: {str(e)}", get_debug_info()
122
 
123
+ def clear_pdf():
124
+ """Delete PDF from the backend directory when cleared in UI"""
125
+ try:
126
+ deleted_files = []
127
+ for f in os.listdir(PAPERS_DIR):
128
+ file_to_del = os.path.join(PAPERS_DIR, f)
129
+ if os.path.isfile(file_to_del):
130
+ os.remove(file_to_del)
131
+ deleted_files.append(f)
132
+ if deleted_files:
133
+ return "🗑️ File deleted from workspace", get_debug_info()
134
+ return "ℹ️ Workspace is already empty", get_debug_info()
135
+ except Exception as e:
136
+ return f"❌ Error deleting file: {str(e)}", get_debug_info()
137
+
138
  def save_api_settings(api_key, api_base_url=None):
139
  if not api_key: return "❌ Key cannot be empty", get_debug_info()
140
  try:
 
295
  .gradio-group:hover { box-shadow: 0 8px 32px rgba(168, 85, 247, 0.12) !important; border-color: #C084FC !important; }
296
  .dark .gradio-group { background: rgba(30, 30, 30, 0.9) !important; border-color: rgba(168, 85, 247, 0.3) !important; }
297
 
298
+ /* ======== 修改使用原生组件但优化视觉样式 ======== */
299
  #pdf-upload-box {
300
+ border: 2px dashed rgba(192, 132, 252, 0.6) !important;
 
301
  border-radius: 12px !important;
302
+ background-color: rgba(250, 245, 255, 0.5) !important;
 
 
 
303
  transition: all 0.3s ease !important;
304
  }
305
+ #pdf-upload-box:hover {
306
+ border-color: #A855F7 !important;
307
+ background-color: rgba(243, 232, 255, 0.8) !important;
308
+ box-shadow: 0 4px 15px rgba(168, 85, 247, 0.15) !important;
 
 
 
 
 
 
 
309
  }
310
 
311
+ /* ======== 新增:主要操作按钮 (Start Mineru & Generate All) 悬浮与圆角动效 ======== */
312
+ .primary-action-btn {
313
+ border-radius: 30px !important;
314
+ background: linear-gradient(135deg, #9333EA, #7E22CE) !important;
315
+ color: white !important; font-weight: 700 !important; border: none !important;
316
+ height: 60px !important; width: 100% !important;
317
+ transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.3s ease !important;
318
+ box-shadow: 0 4px 15px rgba(126, 34, 206, 0.3) !important;
319
+ cursor: pointer !important; font-size: 1.1rem !important; margin-top: 10px !important;
320
  }
321
+ .primary-action-btn:hover {
322
+ transform: translateY(-5px) scale(1.02) !important;
323
+ box-shadow: 0 10px 25px rgba(126, 34, 206, 0.5) !important;
324
+ background: linear-gradient(135deg, #A855F7, #9333EA) !important;
325
  }
326
+ .primary-action-btn:active { transform: translateY(2px) scale(0.98) !important; box-shadow: 0 2px 10px rgba(126, 34, 206, 0.2) !important; }
327
 
328
+ /* 3个小按钮的样式保持不变 */
329
+ .action-row { display: flex !important; gap: 15px !important; margin-bottom: 10px !important; margin-top: 10px !important;}
330
  .action-btn {
331
  border-radius: 30px !important;
332
  background: linear-gradient(135deg, #A855F7, #9333EA) !important;
 
353
  ::-webkit-scrollbar-thumb:hover { background: linear-gradient(135deg, #9333EA, #A855F7); }
354
  """
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  with gr.Blocks(theme=purple_theme, css=custom_css) as demo:
357
  with gr.Column(elem_id="col-container"):
358
  gr.Markdown("# **PaperX / Mineru Parsing Platform**", elem_id="main-title")
 
373
  with gr.Group(elem_classes="gradio-group"):
374
  gr.Markdown("### 📄 2. Document Parsing")
375
 
376
+ # 修复:移除破坏原生功能全局遮罩使用原生 label 参数自义英文提示,保留完美的原生拖拽识别机制
377
+ pdf_input = gr.File(
378
+ label="🖱️ Click here to select a PDF or Drag & Drop the file here",
379
+ file_types=[".pdf"],
380
+ elem_id="pdf-upload-box"
381
+ )
382
+
383
+ # 应用全新的 primary-action-btn 悬浮/圆角类
384
+ parse_btn = gr.Button("🚀 Start Mineru & DAG Extraction", elem_classes="primary-action-btn")
385
 
 
386
  parse_status = gr.Textbox(
387
  show_label=False, placeholder="Waiting for document upload...", lines=1, interactive=False, elem_classes="status-text"
388
  )
 
392
  gr.Markdown("### 🎯 3. Asset Generation")
393
  gr.Markdown("Generate final formats based on DAG structure:")
394
 
 
395
  with gr.Row(elem_classes="action-row"):
396
  gen_ppt_btn = gr.Button("📊 Gen PPT", elem_classes="action-btn")
397
  gen_poster_btn = gr.Button("🖼️ Gen Poster", elem_classes="action-btn")
398
  gen_pr_btn = gr.Button("📰 Gen Article", elem_classes="action-btn")
399
 
400
+ # 应用全新的 primary-action-btn 悬浮/圆角类
401
+ gen_all_btn = gr.Button("✨ Generate All Assets (ALL)", elem_classes="primary-action-btn")
402
 
403
  # ================= RIGHT COLUMN: OUTPUTS & LOGS =================
404
  with gr.Column(scale=1):
 
430
  key_btn.click(fn=save_api_settings, inputs=[key_input, api_base_url_input], outputs=[parse_status, debug_view])
431
 
432
  pdf_input.upload(fn=save_pdf, inputs=pdf_input, outputs=[parse_status, debug_view])
433
+ # 增加清除逻辑,点击 "x" 后彻底删除后端文件
434
+ pdf_input.clear(fn=clear_pdf, outputs=[parse_status, debug_view])
435
 
436
  parse_btn.click(fn=run_mineru_parsing_and_dag_gen, outputs=[parse_status, debug_view, cmd_logs])
437
 
 
447
 
448
  refresh_btn.click(fn=get_debug_info, outputs=debug_view)
449
 
 
 
 
450
  if __name__ == "__main__":
451
  demo.launch()