Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
import subprocess
|
| 4 |
import tempfile
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
-
def
|
| 8 |
try:
|
| 9 |
-
# Create temp directory
|
| 10 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 11 |
# Step 1: Save uploaded file
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
f.write(input_file.read())
|
| 16 |
|
| 17 |
-
# Step 2: Convert
|
| 18 |
-
|
| 19 |
-
|
| 20 |
"soffice",
|
| 21 |
"--headless",
|
| 22 |
-
"--convert-to",
|
| 23 |
"--outdir", tmpdir,
|
| 24 |
-
str(
|
| 25 |
-
]
|
| 26 |
-
subprocess.run(cmd, check=True, timeout=30)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
return str(
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"pdf", "docx", "odt", "html",
|
| 40 |
-
"txt", "rtf", "pptx", "xlsx"
|
| 41 |
-
]
|
| 42 |
-
|
| 43 |
-
with gr.Blocks(title="File Converter") as app:
|
| 44 |
-
gr.Markdown("## 📁 File Converter (LibreOffice)")
|
| 45 |
with gr.Row():
|
| 46 |
-
file_input = gr.File(label="Upload
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
label="Output Format",
|
| 50 |
-
value="pdf"
|
| 51 |
-
)
|
| 52 |
-
output_file = gr.File(label="Download Converted File")
|
| 53 |
|
| 54 |
gr.Button("Convert").click(
|
| 55 |
-
fn=
|
| 56 |
-
inputs=[file_input,
|
| 57 |
-
outputs=
|
| 58 |
)
|
| 59 |
|
| 60 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import tempfile
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
+
def convert_to_image(docx_file, output_format):
|
| 7 |
try:
|
|
|
|
| 8 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 9 |
# Step 1: Save uploaded file
|
| 10 |
+
docx_path = Path(tmpdir) / "input.docx"
|
| 11 |
+
with open(docx_path, "wb") as f:
|
| 12 |
+
f.write(docx_file.read())
|
|
|
|
| 13 |
|
| 14 |
+
# Step 2: Convert DOCX → PDF
|
| 15 |
+
pdf_path = Path(tmpdir) / "output.pdf"
|
| 16 |
+
subprocess.run([
|
| 17 |
"soffice",
|
| 18 |
"--headless",
|
| 19 |
+
"--convert-to", "pdf",
|
| 20 |
"--outdir", tmpdir,
|
| 21 |
+
str(docx_path)
|
| 22 |
+
], check=True, timeout=30)
|
|
|
|
| 23 |
|
| 24 |
+
# Step 3: Convert PDF → Image
|
| 25 |
+
img_path = Path(tmpdir) / f"output.{output_format}"
|
| 26 |
+
subprocess.run([
|
| 27 |
+
"pdftoppm",
|
| 28 |
+
"-singlefile",
|
| 29 |
+
"-jpeg" if output_format == "jpg" else "-png",
|
| 30 |
+
"-r", "150", # DPI (150 for free tier)
|
| 31 |
+
str(pdf_path),
|
| 32 |
+
str(img_path.with_suffix(''))
|
| 33 |
+
], check=True)
|
| 34 |
|
| 35 |
+
return str(img_path) if img_path.exists() else "Conversion failed"
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error: {str(e)}"
|
| 39 |
|
| 40 |
+
with gr.Blocks() as app:
|
| 41 |
+
gr.Markdown("## 📄 DOCX to Image Converter")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
with gr.Row():
|
| 43 |
+
file_input = gr.File(label="Upload DOCX", file_types=[".docx"])
|
| 44 |
+
format_radio = gr.Radio(["jpg", "png"], label="Output Format", value="jpg")
|
| 45 |
+
output_image = gr.Image(label="Result", type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
gr.Button("Convert").click(
|
| 48 |
+
fn=convert_to_image,
|
| 49 |
+
inputs=[file_input, format_radio],
|
| 50 |
+
outputs=output_image
|
| 51 |
)
|
| 52 |
|
| 53 |
app.launch()
|