leonarb commited on
Commit
07beedf
·
verified ·
1 Parent(s): 30f33fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
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, "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()
 
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()