Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,45 +5,66 @@ import os
|
|
| 5 |
from pdf2image import convert_from_path
|
| 6 |
import logging
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 18 |
try:
|
| 19 |
-
# DOCX β PDF
|
| 20 |
pdf_path = os.path.join(tmpdir, "output.pdf")
|
| 21 |
cmd = [
|
| 22 |
-
"libreoffice",
|
| 23 |
-
"--
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
subprocess.run(cmd, check=True, timeout=30)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
logging.error(f"Conversion failed: {str(e)}")
|
| 36 |
-
raise gr.Error(f"
|
| 37 |
|
|
|
|
| 38 |
with gr.Blocks() as app:
|
| 39 |
-
gr.Markdown("
|
| 40 |
with gr.Row():
|
| 41 |
-
input_file = gr.File(
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|