import gradio as gr import subprocess import tempfile import os from pdf2image import convert_from_path import logging # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def verify_libreoffice(): """Check if LibreOffice exists before conversion""" try: result = subprocess.run( ["/usr/bin/libreoffice", "--version"], check=True, capture_output=True, text=True ) logger.info(f"LibreOffice verified: {result.stdout.strip()}") return True except Exception as e: logger.error(f"LibreOffice check failed: {str(e)}") return False def convert_docx_to_image(docx_path): if not verify_libreoffice(): raise gr.Error("System Error: LibreOffice not installed!") with tempfile.TemporaryDirectory() as tmpdir: try: # Conversion steps... pdf_path = os.path.join(tmpdir, "output.pdf") subprocess.run([ "/usr/bin/libreoffice", "--headless", "--convert-to", "pdf", "--outdir", tmpdir, docx_path ], check=True, timeout=30) images = convert_from_path( pdf_path, dpi=100, poppler_path="/usr/bin" ) return images[0] if images else None except Exception as e: logger.error(f"Conversion error: {str(e)}") raise gr.Error(f"Failed: {str(e)}") # Gradio Interface with gr.Blocks() as app: gr.Markdown("## 🖥️ DOCX to Image Converter") with gr.Row(): input_file = gr.File( label="Upload DOCX", file_types=[".docx"], type="filepath" ) output_image = gr.Image(label="Output Image") gr.Button("Convert").click( fn=convert_docx_to_image, inputs=input_file, outputs=output_image ) app.launch()