Threscomma commited on
Commit
8afc243
·
verified ·
1 Parent(s): 97dd954

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
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 convert_file(input_file, output_format):
8
  try:
9
- # Create temp directory
10
  with tempfile.TemporaryDirectory() as tmpdir:
11
  # Step 1: Save uploaded file
12
- input_path = Path(input_file.name)
13
- temp_input = Path(tmpdir) / input_path.name
14
- with open(temp_input, "wb") as f:
15
- f.write(input_file.read())
16
 
17
- # Step 2: Convert using soffice
18
- output_path = Path(tmpdir) / f"{temp_input.stem}.{output_format}"
19
- cmd = [
20
  "soffice",
21
  "--headless",
22
- "--convert-to", output_format,
23
  "--outdir", tmpdir,
24
- str(temp_input)
25
- ]
26
- subprocess.run(cmd, check=True, timeout=30)
27
 
28
- # Verify output
29
- if not output_path.exists():
30
- raise FileNotFoundError("Conversion failed - no output file")
 
 
 
 
 
 
 
31
 
32
- return str(output_path)
33
 
34
  except Exception as e:
35
  return f"Error: {str(e)}"
36
 
37
- # Supported formats (LibreOffice 7.4+)
38
- supported_formats = [
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 File")
47
- format_dropdown = gr.Dropdown(
48
- supported_formats,
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=convert_file,
56
- inputs=[file_input, format_dropdown],
57
- outputs=output_file
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()