Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import uuid
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def convert_pdf_to_html(pdf_file):
|
| 8 |
+
job_id = str(uuid.uuid4())
|
| 9 |
+
work_dir = f"/tmp/{job_id}"
|
| 10 |
+
os.makedirs(work_dir, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
input_path = os.path.join(work_dir, "input.pdf")
|
| 13 |
+
output_path = os.path.join(work_dir, "output.html")
|
| 14 |
+
|
| 15 |
+
with open(input_path, "wb") as f:
|
| 16 |
+
f.write(pdf_file.read())
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# Run pdf2htmlEX
|
| 20 |
+
result = subprocess.run(
|
| 21 |
+
["pdf2htmlEX", "--dest-dir", work_dir, "--embed", "cfijo", input_path],
|
| 22 |
+
capture_output=True,
|
| 23 |
+
text=True,
|
| 24 |
+
)
|
| 25 |
+
if result.returncode != 0:
|
| 26 |
+
return f"Error during conversion:\n{result.stderr}"
|
| 27 |
+
|
| 28 |
+
# Find the output HTML file (usually named input.html)
|
| 29 |
+
converted_file = os.path.join(work_dir, "input.html")
|
| 30 |
+
if not os.path.exists(converted_file):
|
| 31 |
+
return "Conversion failed: HTML file not found."
|
| 32 |
+
|
| 33 |
+
return converted_file
|
| 34 |
+
|
| 35 |
+
finally:
|
| 36 |
+
# Optional: remove temp files after Gradio session ends
|
| 37 |
+
shutil.rmtree(work_dir, ignore_errors=True)
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=convert_pdf_to_html,
|
| 41 |
+
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
|
| 42 |
+
outputs=gr.File(label="Download HTML"),
|
| 43 |
+
title="PDF to HTML Converter (pdf2htmlEX)",
|
| 44 |
+
description="Upload a PDF and download an HTML version using pdf2htmlEX."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|