Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,48 +2,40 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
| 4 |
from pathlib import Path
|
| 5 |
-
import
|
| 6 |
|
| 7 |
def convert_docx_to_jpg(docx_file):
|
| 8 |
-
if not docx_file:
|
| 9 |
-
raise gr.Error("No file uploaded. Please upload a DOCX file.")
|
| 10 |
-
if not docx_file.name.lower().endswith('.docx'):
|
| 11 |
-
raise gr.Error("Please upload a valid .docx file.")
|
| 12 |
-
|
| 13 |
try:
|
| 14 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
with open(docx_path, "wb") as f:
|
| 18 |
f.write(open(docx_file.name, "rb").read())
|
| 19 |
|
| 20 |
-
# Convert
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
capture_output=True,
|
| 29 |
-
text=True
|
| 30 |
-
)
|
| 31 |
-
except subprocess.CalledProcessError as e:
|
| 32 |
-
raise gr.Error(f"LibreOffice conversion failed: {e.stderr}")
|
| 33 |
-
except subprocess.TimeoutExpired:
|
| 34 |
-
raise gr.Error("Conversion timed out after 60 seconds.")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
-
raise gr.Error(f"
|
| 44 |
|
| 45 |
with gr.Blocks() as app:
|
| 46 |
-
gr.Markdown("## 📄
|
| 47 |
file_input = gr.File(label="Upload DOCX", file_types=[".docx"])
|
| 48 |
output_image = gr.Image(label="Output JPG", type="filepath")
|
| 49 |
|
|
@@ -53,7 +45,4 @@ with gr.Blocks() as app:
|
|
| 53 |
outputs=output_image
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
app.launch()
|
| 58 |
-
except Exception as e:
|
| 59 |
-
raise gr.Error(f"Failed to launch Gradio app: {str(e)}")
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
| 4 |
from pathlib import Path
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
def convert_docx_to_jpg(docx_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 10 |
+
tmpdir = Path(tmpdir)
|
| 11 |
+
|
| 12 |
+
# 1. Save the uploaded file
|
| 13 |
+
docx_path = tmpdir / "input.docx"
|
| 14 |
with open(docx_path, "wb") as f:
|
| 15 |
f.write(open(docx_file.name, "rb").read())
|
| 16 |
|
| 17 |
+
# 2. Convert DOCX to JPG using LibreOffice's export function
|
| 18 |
+
subprocess.run([
|
| 19 |
+
"soffice",
|
| 20 |
+
"--headless",
|
| 21 |
+
"--convert-to", "jpg",
|
| 22 |
+
"--outdir", str(tmpdir),
|
| 23 |
+
str(docx_path)
|
| 24 |
+
], check=True, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# 3. Find the output file (LibreOffice adds '_1' sometimes)
|
| 27 |
+
jpg_file = next(tmpdir.glob("*.jpg"), None)
|
| 28 |
+
if not jpg_file:
|
| 29 |
+
raise ValueError("No JPG file was created")
|
| 30 |
|
| 31 |
+
# 4. Return the first page only
|
| 32 |
+
return str(jpg_file)
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
+
raise gr.Error(f"Conversion failed. Try a simpler DOCX file. Error: {str(e)}")
|
| 36 |
|
| 37 |
with gr.Blocks() as app:
|
| 38 |
+
gr.Markdown("## 📄 DOCX to JPG (Working Version)")
|
| 39 |
file_input = gr.File(label="Upload DOCX", file_types=[".docx"])
|
| 40 |
output_image = gr.Image(label="Output JPG", type="filepath")
|
| 41 |
|
|
|
|
| 45 |
outputs=output_image
|
| 46 |
)
|
| 47 |
|
| 48 |
+
app.launch()
|
|
|
|
|
|
|
|
|