Spaces:
Build error
Build error
| import gradio as gr | |
| import subprocess | |
| import tempfile | |
| from pathlib import Path | |
| import time | |
| def convert_docx_to_jpg(docx_file): | |
| try: | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| tmpdir = Path(tmpdir) | |
| # 1. Save the uploaded file | |
| docx_path = tmpdir / "input.docx" | |
| with open(docx_path, "wb") as f: | |
| f.write(open(docx_file.name, "rb").read()) | |
| # 2. Convert DOCX to JPG using LibreOffice's export function | |
| subprocess.run([ | |
| "soffice", | |
| "--headless", | |
| "--convert-to", "jpg", | |
| "--outdir", str(tmpdir), | |
| str(docx_path) | |
| ], check=True, timeout=30) | |
| # 3. Find the output file (LibreOffice adds '_1' sometimes) | |
| jpg_file = next(tmpdir.glob("*.jpg"), None) | |
| if not jpg_file: | |
| raise ValueError("No JPG file was created") | |
| # 4. Return the first page only | |
| return str(jpg_file) | |
| except Exception as e: | |
| raise gr.Error(f"Conversion failed. Try a simpler DOCX file. Error: {str(e)}") | |
| with gr.Blocks() as app: | |
| gr.Markdown("## π DOCX to JPG (Working Version)") | |
| file_input = gr.File(label="Upload DOCX", file_types=[".docx"]) | |
| output_image = gr.Image(label="Output JPG", type="filepath") | |
| gr.Button("Convert").click( | |
| fn=convert_docx_to_jpg, | |
| inputs=file_input, | |
| outputs=output_image | |
| ) | |
| app.launch() |