Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,73 +5,45 @@ import os
|
|
| 5 |
from pdf2image import convert_from_path
|
| 6 |
import logging
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
""
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 18 |
try:
|
| 19 |
-
#
|
| 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,
|
| 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 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
-
|
| 48 |
-
raise
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Markdown("## πβπΌοΈ Convert Word to Image (Exact Visual Copy)")
|
| 53 |
with gr.Row():
|
| 54 |
-
input_file = gr.File(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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=
|
| 75 |
)
|
| 76 |
|
| 77 |
-
app.launch(
|
|
|
|
| 5 |
from pdf2image import convert_from_path
|
| 6 |
import logging
|
| 7 |
|
| 8 |
+
# Verify LibreOffice exists
|
| 9 |
+
try:
|
| 10 |
+
subprocess.run(["libreoffice", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 11 |
+
logging.info("β
LibreOffice is installed")
|
| 12 |
+
except Exception as e:
|
| 13 |
+
logging.error("β LibreOffice not found. Check your .dockerfile!")
|
| 14 |
+
raise RuntimeError("LibreOffice not installed")
|
| 15 |
+
|
| 16 |
+
def convert_docx_to_image(docx_path: str):
|
| 17 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 18 |
try:
|
| 19 |
+
# DOCX β PDF
|
| 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, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# PDF β PNG
|
| 28 |
+
images = convert_from_path(pdf_path, dpi=300, fmt="png")
|
| 29 |
+
if images:
|
| 30 |
+
output_path = os.path.join(tmpdir, "output.png")
|
| 31 |
+
images[0].save(output_path)
|
| 32 |
+
return output_path
|
| 33 |
+
raise ValueError("No images generated")
|
| 34 |
except Exception as e:
|
| 35 |
+
logging.error(f"Conversion failed: {str(e)}")
|
| 36 |
+
raise gr.Error(f"Error: {str(e)}")
|
| 37 |
|
| 38 |
+
with gr.Blocks() as app:
|
| 39 |
+
gr.Markdown("# π screenshot my unit planner")
|
|
|
|
| 40 |
with gr.Row():
|
| 41 |
+
input_file = gr.File(label="Upload DOCX", file_types=[".docx"])
|
| 42 |
+
output_image = gr.Image(label="Result")
|
| 43 |
+
gr.Button("Convert").click(
|
| 44 |
+
convert_docx_to_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
inputs=input_file,
|
| 46 |
+
outputs=output_image
|
| 47 |
)
|
| 48 |
|
| 49 |
+
app.launch()
|