Threscomma commited on
Commit
c3f09e3
·
verified ·
1 Parent(s): 8853c5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -2,40 +2,48 @@ import gradio as gr
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,4 +53,7 @@ with gr.Blocks() as app:
45
  outputs=output_image
46
  )
47
 
48
- app.launch()
 
 
 
 
2
  import subprocess
3
  import tempfile
4
  from pathlib import Path
5
+ import platform
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
+ # Save uploaded file
16
+ docx_path = Path(tmpdir) / "input.docx"
 
 
17
  with open(docx_path, "wb") as f:
18
  f.write(open(docx_file.name, "rb").read())
19
 
20
+ # Convert DIRECTLY to JPG using LibreOffice
21
+ jpg_path = Path(tmpdir) / "input.jpg" # Match LibreOffice’s naming
22
+ libreoffice_cmd = "soffice" if platform.system() != "Windows" else "soffice.exe"
23
+ try:
24
+ result = subprocess.run(
25
+ [libreoffice_cmd, "--headless", "--convert-to", "jpg", "--outdir", tmpdir, str(docx_path)],
26
+ check=True,
27
+ timeout=60,
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
+ # Verify output exists
37
+ if not jpg_path.exists():
38
+ raise ValueError("JPG conversion failed")
 
39
 
40
+ return str(jpg_path)
 
41
 
42
  except Exception as e:
43
+ raise gr.Error(f"Failed to convert: {str(e)}")
44
 
45
  with gr.Blocks() as app:
46
+ gr.Markdown("## 📄 Direct DOCX to JPG Converter")
47
  file_input = gr.File(label="Upload DOCX", file_types=[".docx"])
48
  output_image = gr.Image(label="Output JPG", type="filepath")
49
 
 
53
  outputs=output_image
54
  )
55
 
56
+ try:
57
+ app.launch()
58
+ except Exception as e:
59
+ raise gr.Error(f"Failed to launch Gradio app: {str(e)}")