Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,39 +10,33 @@ def convert_pdf_to_html(pdf_file):
|
|
| 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, "
|
| 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 |
-
|
| 29 |
-
|
| 30 |
-
if not os.path.exists(converted_file):
|
| 31 |
-
return "Conversion failed: HTML file not found."
|
| 32 |
|
| 33 |
-
return
|
| 34 |
|
| 35 |
finally:
|
| 36 |
-
# Optional: remove temp files after Gradio session ends
|
| 37 |
shutil.rmtree(work_dir, ignore_errors=True)
|
| 38 |
|
| 39 |
-
|
| 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
|
| 44 |
-
description="
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
if __name__ == "__main__":
|
| 48 |
-
demo.launch()
|
|
|
|
| 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, "input.html")
|
| 14 |
|
| 15 |
with open(input_path, "wb") as f:
|
| 16 |
f.write(pdf_file.read())
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 19 |
result = subprocess.run(
|
| 20 |
["pdf2htmlEX", "--dest-dir", work_dir, "--embed", "cfijo", input_path],
|
| 21 |
capture_output=True,
|
| 22 |
+
text=True
|
| 23 |
)
|
| 24 |
+
|
| 25 |
if result.returncode != 0:
|
| 26 |
return f"Error during conversion:\n{result.stderr}"
|
| 27 |
|
| 28 |
+
if not os.path.exists(output_path):
|
| 29 |
+
return "Conversion failed: output HTML file not found."
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
return output_path
|
| 32 |
|
| 33 |
finally:
|
|
|
|
| 34 |
shutil.rmtree(work_dir, ignore_errors=True)
|
| 35 |
|
| 36 |
+
gr.Interface(
|
| 37 |
fn=convert_pdf_to_html,
|
| 38 |
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
|
| 39 |
outputs=gr.File(label="Download HTML"),
|
| 40 |
+
title="PDF to HTML Converter",
|
| 41 |
+
description="Uses pdf2htmlEX to convert PDFs to standalone HTML."
|
| 42 |
+
).launch()
|
|
|
|
|
|
|
|
|