Threscomma commited on
Commit
97440c7
Β·
verified Β·
1 Parent(s): dfe72fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -5,45 +5,66 @@ import os
5
  from pdf2image import convert_from_path
6
  import logging
7
 
8
- # Verify LibreOffice exists
9
- try:
10
- subprocess.run(["libreoffice", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
11
- logging.info("βœ… LibreOffice is installed")
12
- except Exception as e:
13
- logging.error("❌ LibreOffice not found. Check your .dockerfile!")
14
- raise RuntimeError("LibreOffice not installed")
15
 
16
- def convert_docx_to_image(docx_path: str):
 
 
 
 
17
  with tempfile.TemporaryDirectory() as tmpdir:
18
  try:
19
- # DOCX β†’ PDF
20
  pdf_path = os.path.join(tmpdir, "output.pdf")
21
  cmd = [
22
- "libreoffice", "--headless", "--convert-to", "pdf",
23
- "--outdir", tmpdir, docx_path
 
 
 
24
  ]
25
  subprocess.run(cmd, check=True, timeout=30)
26
 
27
- # PDF β†’ PNG
28
- images = convert_from_path(pdf_path, dpi=300, fmt="png")
29
- if images:
30
- output_path = os.path.join(tmpdir, "output.png")
31
- images[0].save(output_path)
32
- return output_path
33
- raise ValueError("No images generated")
 
 
 
 
 
 
 
 
34
  except Exception as e:
35
- logging.error(f"Conversion failed: {str(e)}")
36
- raise gr.Error(f"Error: {str(e)}")
37
 
 
38
  with gr.Blocks() as app:
39
- gr.Markdown("# πŸ“„ screenshot my unit planner")
40
  with gr.Row():
41
- input_file = gr.File(label="Upload DOCX", file_types=[".docx"])
42
- output_image = gr.Image(label="Result")
 
 
 
 
 
43
  gr.Button("Convert").click(
44
- convert_docx_to_image,
45
  inputs=input_file,
46
  outputs=output_image
47
  )
48
 
49
- app.launch()
 
5
  from pdf2image import convert_from_path
6
  import logging
7
 
8
+ # ===== HEALTH CHECK =====
9
+ def verify_libreoffice():
10
+ try:
11
+ subprocess.run(["libreoffice", "--version"], check=True, capture_output=True)
12
+ return True
13
+ except (subprocess.CalledProcessError, FileNotFoundError):
14
+ return False
15
 
16
+ if not verify_libreoffice():
17
+ raise RuntimeError("❌ LibreOffice not installed. Check your .dockerfile!")
18
+
19
+ # ===== MAIN CONVERSION FUNCTION =====
20
+ def convert_docx_to_image(docx_path: str) -> str:
21
  with tempfile.TemporaryDirectory() as tmpdir:
22
  try:
23
+ # Step 1: DOCX β†’ PDF
24
  pdf_path = os.path.join(tmpdir, "output.pdf")
25
  cmd = [
26
+ "libreoffice",
27
+ "--headless",
28
+ "--convert-to", "pdf",
29
+ "--outdir", tmpdir,
30
+ docx_path
31
  ]
32
  subprocess.run(cmd, check=True, timeout=30)
33
 
34
+ if not os.path.exists(pdf_path):
35
+ raise FileNotFoundError("PDF was not generated")
36
+
37
+ # Step 2: PDF β†’ PNG
38
+ images = convert_from_path(
39
+ pdf_path,
40
+ dpi=300,
41
+ output_folder=tmpdir,
42
+ fmt="png",
43
+ single_file=True
44
+ )
45
+ output_path = os.path.join(tmpdir, "output.png")
46
+ images[0].save(output_path, "PNG")
47
+ return output_path
48
+
49
  except Exception as e:
50
+ logging.error(f"Conversion failed: {str(e)}", exc_info=True)
51
+ raise gr.Error(f"Conversion error: {str(e)}")
52
 
53
+ # ===== GRADIO UI =====
54
  with gr.Blocks() as app:
55
+ gr.Markdown("## πŸ“„ DOCX to Image (Pixel-Perfect)")
56
  with gr.Row():
57
+ input_file = gr.File(
58
+ label="Upload DOCX",
59
+ file_types=[".docx"],
60
+ type="filepath"
61
+ )
62
+ output_image = gr.Image(label="Output Image", type="filepath")
63
+
64
  gr.Button("Convert").click(
65
+ fn=convert_docx_to_image,
66
  inputs=input_file,
67
  outputs=output_image
68
  )
69
 
70
+ app.launch(server_name="0.0.0.0")