Spaces:
Build error
Build error
File size: 1,438 Bytes
b7aaaf5 faddd0a e479c17 faddd0a e479c17 faddd0a e479c17 faddd0a 97440c7 e479c17 97440c7 e479c17 a1f6e97 e479c17 faddd0a e479c17 faddd0a e479c17 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import gradio as gr
import subprocess
import tempfile
import os
from pdf2image import convert_from_path
def convert_docx_to_image(docx_path: str) -> str:
"""Convert DOCX to PNG with LibreOffice (pixel-perfect)"""
with tempfile.TemporaryDirectory() as tmpdir:
# Step 1: DOCX β PDF using LibreOffice's render engine
pdf_path = os.path.join(tmpdir, "output.pdf")
cmd = [
"libreoffice",
"--headless",
"--convert-to", "pdf",
"--outdir", tmpdir,
docx_path
]
subprocess.run(cmd, check=True, timeout=30)
# Step 2: PDF β PNG (300 DPI for sharpness)
images = convert_from_path(
pdf_path,
dpi=300,
fmt="png",
poppler_path="/usr/bin" # Critical for Hugging Face
)
output_path = os.path.join(tmpdir, "output.png")
images[0].save(output_path, "PNG")
return output_path
# Gradio UI
with gr.Blocks() as app:
gr.Markdown("## π₯οΈ DOCX to Image (1:1 Conversion)")
with gr.Row():
input_file = gr.File(
label="Upload DOCX",
file_types=[".docx"],
type="filepath"
)
output_image = gr.Image(label="Output", type="filepath")
gr.Button("Convert").click(
fn=convert_docx_to_image,
inputs=input_file,
outputs=output_image
)
app.launch() |