Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
from pdf2image import convert_from_path
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# Configure logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
def convert_docx_to_image(docx_path: str) -> str:
|
| 13 |
+
"""
|
| 14 |
+
Converts DOCX to image via LibreOffice and pdf2image.
|
| 15 |
+
Returns path to the generated image.
|
| 16 |
+
"""
|
| 17 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 18 |
+
try:
|
| 19 |
+
# Step 1: DOCX β PDF using LibreOffice
|
| 20 |
+
pdf_path = os.path.join(tmpdir, "output.pdf")
|
| 21 |
+
cmd = [
|
| 22 |
+
"libreoffice", "--headless", "--convert-to", "pdf",
|
| 23 |
+
"--outdir", tmpdir, docx_path
|
| 24 |
+
]
|
| 25 |
+
subprocess.run(cmd, check=True, capture_output=True)
|
| 26 |
+
|
| 27 |
+
if not os.path.exists(pdf_path):
|
| 28 |
+
raise FileNotFoundError("PDF conversion failed")
|
| 29 |
+
|
| 30 |
+
# Step 2: PDF β Image
|
| 31 |
+
images = convert_from_path(
|
| 32 |
+
pdf_path,
|
| 33 |
+
dpi=300, # High resolution
|
| 34 |
+
output_folder=tmpdir,
|
| 35 |
+
fmt="png",
|
| 36 |
+
single_file=True
|
| 37 |
+
)
|
| 38 |
+
output_path = os.path.join(tmpdir, "output.png")
|
| 39 |
+
images[0].save(output_path, "PNG")
|
| 40 |
+
|
| 41 |
+
return output_path
|
| 42 |
+
|
| 43 |
+
except subprocess.CalledProcessError as e:
|
| 44 |
+
logger.error(f"LibreOffice error: {e.stderr.decode()}")
|
| 45 |
+
raise
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logger.error(f"Conversion failed: {str(e)}")
|
| 48 |
+
raise
|
| 49 |
+
|
| 50 |
+
# Gradio Interface
|
| 51 |
+
with gr.Blocks(title="DOCX to Image (Pixel-Perfect)") as app:
|
| 52 |
+
gr.Markdown("## πβπΌοΈ Convert Word to Image (Exact Visual Copy)")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
input_file = gr.File(
|
| 55 |
+
label="Upload DOCX",
|
| 56 |
+
file_types=[".docx"],
|
| 57 |
+
type="filepath"
|
| 58 |
+
)
|
| 59 |
+
output_image = gr.Image(label="Rendered Image", type="filepath")
|
| 60 |
|
| 61 |
+
convert_btn = gr.Button("Convert", variant="primary")
|
| 62 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 63 |
+
|
| 64 |
+
def process(file_path: str) -> tuple[str, str]:
|
| 65 |
+
try:
|
| 66 |
+
image_path = convert_docx_to_image(file_path)
|
| 67 |
+
return image_path, "β
Conversion successful!"
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return None, f"β Error: {str(e)}"
|
| 70 |
+
|
| 71 |
+
convert_btn.click(
|
| 72 |
+
fn=process,
|
| 73 |
+
inputs=input_file,
|
| 74 |
+
outputs=[output_image, status]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
app.launch(server_name="0.0.0.0")
|