Push Bot commited on
Commit
67147ac
Β·
1 Parent(s): b7526db

Add image preview box; serve PDF preview via iframe /file=; hook image render after run; fix merge markers

Browse files
Files changed (1) hide show
  1. app.py +57 -2
app.py CHANGED
@@ -499,6 +499,59 @@ def _pdf_to_iframe_html(pdf_path: Path, width="100%", height="900px") -> str:
499
  except Exception:
500
  return ""
501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
  def _compile_tex_to_pdf(tex_path: Path, logs):
503
  """Generic TeX compile helper for a .tex file. Returns Path to PDF or None."""
504
  try:
@@ -1161,7 +1214,7 @@ def run_pipeline(arxiv_url, pdf_file, openai_key, logo_files, meeting_logo_file,
1161
  try:
1162
  pdf_path = _compile_poster_pdf(OUTPUT_DIR, logs)
1163
  if pdf_path and pdf_path.exists():
1164
- pdf_html = _pdf_to_iframe_html(pdf_path)
1165
  compiled_pdf_file = str(pdf_path)
1166
  logs.append("πŸ–¨οΈ PDF ready for preview in UI.")
1167
  except Exception as e:
@@ -1267,6 +1320,7 @@ The framework builds upon [CAMEL-ai](https://github.com/camel-ai/camel).
1267
  with gr.Accordion("Output", open=True):
1268
  logs_out = gr.Textbox(label="🧾 Logs (8–10 minutes)", lines=30, max_lines=50)
1269
  pdf_out = gr.HTML(label="πŸ“„ Poster (PDF Preview)")
 
1270
  pdf_file_out = gr.File(label="πŸ“„ Download Poster (PDF)")
1271
  zip_out = gr.File(label="πŸ“¦ Download Results (.zip)")
1272
  overleaf_out = gr.HTML(label="Open in Overleaf")
@@ -1274,11 +1328,12 @@ The framework builds upon [CAMEL-ai](https://github.com/camel-ai/camel).
1274
  debug_zip_out= gr.HTML(label="🐞 Real Output Preview")
1275
  debug_last_btn= gr.Button("🐞 Test last pipeline zip", variant="secondary")
1276
  debug_last_out= gr.HTML(label="🐞 Last Pipeline Preview")
1277
- run_btn.click(
1278
  fn=run_pipeline,
1279
  inputs=[arxiv_in, pdf_in, key_in, inst_logo_in, conf_logo_in, theme_in],
1280
  outputs=[logs_out, pdf_out, pdf_file_out, zip_out, overleaf_out],
1281
  )
 
1282
  debug_zip_btn.click(fn=debug_compile_output_zip, inputs=[], outputs=[debug_zip_out])
1283
  debug_last_btn.click(fn=debug_compile_last_pipeline_zip, inputs=[], outputs=[debug_last_out])
1284
 
 
499
  except Exception:
500
  return ""
501
 
502
+ def _pdf_to_iframe_file(pdf_path: Path, width="100%", height="900px") -> str:
503
+ """Build an iframe HTML that references the served file path, avoiding data: URIs."""
504
+ try:
505
+ from urllib.parse import quote
506
+ p = str(pdf_path)
507
+ src = f"/file={quote(p)}"
508
+ return (
509
+ f"<div style='border:1px solid #ddd;border-radius:8px;overflow:hidden'>"
510
+ f"<iframe src='{src}' width='{width}' height='{height}' style='border:0'></iframe>"
511
+ f"</div>"
512
+ )
513
+ except Exception:
514
+ return ""
515
+
516
+ def preview_image_from_pdf(pdf_file):
517
+ """Render first page of PDF to PNG for image preview. Returns path or None."""
518
+ try:
519
+ # Accept path str or gradio File-like
520
+ path = pdf_file
521
+ if hasattr(pdf_file, 'name'):
522
+ path = pdf_file.name
523
+ if not path:
524
+ return None
525
+ pdf_path = Path(path)
526
+ out_png = pdf_path.parent / "poster_preview.png"
527
+ # Prefer PyMuPDF
528
+ try:
529
+ import fitz # PyMuPDF
530
+ doc = fitz.open(str(pdf_path))
531
+ if doc.page_count == 0:
532
+ return None
533
+ page = doc.load_page(0)
534
+ mat = fitz.Matrix(2, 2)
535
+ pix = page.get_pixmap(matrix=mat, alpha=False)
536
+ pix.save(str(out_png))
537
+ return str(out_png) if out_png.exists() else None
538
+ except Exception:
539
+ pass
540
+ # Fallback: pypdfium2
541
+ try:
542
+ import pypdfium2 as pdfium
543
+ pdf = pdfium.PdfDocument(str(pdf_path))
544
+ if len(pdf) == 0:
545
+ return None
546
+ page = pdf[0]
547
+ pil = page.render(scale=2).to_pil()
548
+ pil.save(out_png)
549
+ return str(out_png) if out_png.exists() else None
550
+ except Exception:
551
+ return None
552
+ except Exception:
553
+ return None
554
+
555
  def _compile_tex_to_pdf(tex_path: Path, logs):
556
  """Generic TeX compile helper for a .tex file. Returns Path to PDF or None."""
557
  try:
 
1214
  try:
1215
  pdf_path = _compile_poster_pdf(OUTPUT_DIR, logs)
1216
  if pdf_path and pdf_path.exists():
1217
+ pdf_html = _pdf_to_iframe_file(pdf_path)
1218
  compiled_pdf_file = str(pdf_path)
1219
  logs.append("πŸ–¨οΈ PDF ready for preview in UI.")
1220
  except Exception as e:
 
1320
  with gr.Accordion("Output", open=True):
1321
  logs_out = gr.Textbox(label="🧾 Logs (8–10 minutes)", lines=30, max_lines=50)
1322
  pdf_out = gr.HTML(label="πŸ“„ Poster (PDF Preview)")
1323
+ img_out = gr.Image(label="πŸ–ΌοΈ Poster (Image Preview)", interactive=False)
1324
  pdf_file_out = gr.File(label="πŸ“„ Download Poster (PDF)")
1325
  zip_out = gr.File(label="πŸ“¦ Download Results (.zip)")
1326
  overleaf_out = gr.HTML(label="Open in Overleaf")
 
1328
  debug_zip_out= gr.HTML(label="🐞 Real Output Preview")
1329
  debug_last_btn= gr.Button("🐞 Test last pipeline zip", variant="secondary")
1330
  debug_last_out= gr.HTML(label="🐞 Last Pipeline Preview")
1331
+ _run_evt = run_btn.click(
1332
  fn=run_pipeline,
1333
  inputs=[arxiv_in, pdf_in, key_in, inst_logo_in, conf_logo_in, theme_in],
1334
  outputs=[logs_out, pdf_out, pdf_file_out, zip_out, overleaf_out],
1335
  )
1336
+ _run_evt.then(fn=preview_image_from_pdf, inputs=[pdf_file_out], outputs=[img_out])
1337
  debug_zip_btn.click(fn=debug_compile_output_zip, inputs=[], outputs=[debug_zip_out])
1338
  debug_last_btn.click(fn=debug_compile_last_pipeline_zip, inputs=[], outputs=[debug_last_out])
1339