Spaces:
Build error
Build error
| 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() |