Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 verify_libreoffice():
|
| 13 |
+
"""Check if LibreOffice exists before conversion"""
|
| 14 |
+
try:
|
| 15 |
+
result = subprocess.run(
|
| 16 |
+
["/usr/bin/libreoffice", "--version"],
|
| 17 |
+
check=True,
|
| 18 |
+
capture_output=True,
|
| 19 |
+
text=True
|
| 20 |
+
)
|
| 21 |
+
logger.info(f"LibreOffice verified: {result.stdout.strip()}")
|
| 22 |
+
return True
|
| 23 |
+
except Exception as e:
|
| 24 |
+
logger.error(f"LibreOffice check failed: {str(e)}")
|
| 25 |
+
return False
|
| 26 |
+
|
| 27 |
+
def convert_docx_to_image(docx_path):
|
| 28 |
+
if not verify_libreoffice():
|
| 29 |
+
raise gr.Error("System Error: LibreOffice not installed!")
|
| 30 |
+
|
| 31 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 32 |
+
try:
|
| 33 |
+
# Conversion steps...
|
| 34 |
+
pdf_path = os.path.join(tmpdir, "output.pdf")
|
| 35 |
+
subprocess.run([
|
| 36 |
+
"/usr/bin/libreoffice",
|
| 37 |
+
"--headless",
|
| 38 |
+
"--convert-to", "pdf",
|
| 39 |
+
"--outdir", tmpdir,
|
| 40 |
+
docx_path
|
| 41 |
+
], check=True, timeout=30)
|
| 42 |
+
|
| 43 |
+
images = convert_from_path(
|
| 44 |
+
pdf_path,
|
| 45 |
+
dpi=100,
|
| 46 |
+
poppler_path="/usr/bin"
|
| 47 |
+
)
|
| 48 |
+
return images[0] if images else None
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logger.error(f"Conversion error: {str(e)}")
|
| 52 |
+
raise gr.Error(f"Failed: {str(e)}")
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
with gr.Blocks() as app:
|
| 56 |
+
gr.Markdown("## 🖥️ DOCX to Image Converter")
|
| 57 |
+
with gr.Row():
|
| 58 |
+
input_file = gr.File(
|
| 59 |
+
label="Upload DOCX",
|
| 60 |
+
file_types=[".docx"],
|
| 61 |
+
type="filepath"
|
| 62 |
+
)
|
| 63 |
+
output_image = gr.Image(label="Output Image")
|
| 64 |
+
|
| 65 |
+
gr.Button("Convert").click(
|
| 66 |
+
fn=convert_docx_to_image,
|
| 67 |
+
inputs=input_file,
|
| 68 |
+
outputs=output_image
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
app.launch()
|