Threscomma commited on
Commit
dfe72fd
Β·
verified Β·
1 Parent(s): 95142da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -56
app.py CHANGED
@@ -5,73 +5,45 @@ import os
5
  from pdf2image import convert_from_path
6
  import logging
7
 
8
- # Configure logging
9
- logging.basicConfig(level=logging.INFO)
10
- logger = logging.getLogger(__name__)
11
-
12
- def convert_docx_to_image(docx_path: str) -> str:
13
- """
14
- Converts DOCX to image via LibreOffice and pdf2image.
15
- Returns path to the generated image.
16
- """
17
  with tempfile.TemporaryDirectory() as tmpdir:
18
  try:
19
- # Step 1: DOCX β†’ PDF using LibreOffice
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, capture_output=True)
26
-
27
- if not os.path.exists(pdf_path):
28
- raise FileNotFoundError("PDF conversion failed")
29
-
30
- # Step 2: PDF β†’ Image
31
- images = convert_from_path(
32
- pdf_path,
33
- dpi=300, # High resolution
34
- output_folder=tmpdir,
35
- fmt="png",
36
- single_file=True
37
- )
38
- output_path = os.path.join(tmpdir, "output.png")
39
- images[0].save(output_path, "PNG")
40
 
41
- return output_path
42
-
43
- except subprocess.CalledProcessError as e:
44
- logger.error(f"LibreOffice error: {e.stderr.decode()}")
45
- raise
 
 
46
  except Exception as e:
47
- logger.error(f"Conversion failed: {str(e)}")
48
- raise
49
 
50
- # Gradio Interface
51
- with gr.Blocks(title="DOCX to Image (Pixel-Perfect)") as app:
52
- gr.Markdown("## πŸ“„β†’πŸ–ΌοΈ Convert Word to Image (Exact Visual Copy)")
53
  with gr.Row():
54
- input_file = gr.File(
55
- label="Upload DOCX",
56
- file_types=[".docx"],
57
- type="filepath"
58
- )
59
- output_image = gr.Image(label="Rendered Image", type="filepath")
60
-
61
- convert_btn = gr.Button("Convert", variant="primary")
62
- status = gr.Textbox(label="Status", interactive=False)
63
-
64
- def process(file_path: str) -> tuple[str, str]:
65
- try:
66
- image_path = convert_docx_to_image(file_path)
67
- return image_path, "βœ… Conversion successful!"
68
- except Exception as e:
69
- return None, f"❌ Error: {str(e)}"
70
-
71
- convert_btn.click(
72
- fn=process,
73
  inputs=input_file,
74
- outputs=[output_image, status]
75
  )
76
 
77
- app.launch(server_name="0.0.0.0")
 
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()