arunavabasucom commited on
Commit
94f1d8d
·
unverified ·
1 Parent(s): 4aa6028

feat: adding output name field

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -35,38 +35,43 @@ def invert_pdf_pages(input_pdf, output_pdf, page_selection):
35
  doc.save(output_pdf)
36
  doc.close()
37
 
38
- def invert_pdf_document(input_file, page_selection):
39
- # If input_file is a file-like object, write its content to a temp file.
40
- # Otherwise, assume it's a file path.
41
  if hasattr(input_file, "read"):
42
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_in:
43
  tmp_in.write(input_file.read())
44
  tmp_in_path = tmp_in.name
45
  cleanup = True
 
46
  else:
47
  tmp_in_path = input_file
48
  cleanup = False
49
 
50
- # Create a temporary file to save the inverted PDF.
51
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_out:
52
  tmp_out_path = tmp_out.name
 
53
 
54
  invert_pdf_pages(tmp_in_path, tmp_out_path, page_selection)
 
55
 
56
- # Clean up temporary input file if it was created.
57
  if cleanup:
58
  os.remove(tmp_in_path)
59
- return tmp_out_path
 
 
 
 
60
 
61
  iface = gr.Interface(
62
  fn=invert_pdf_document,
63
  inputs=[
64
  gr.File(label="Input PDF"),
65
- gr.Textbox(label="Page Selection", value="1-12,14-20,22-32,56,66-78,82-97")
 
66
  ],
67
  outputs=gr.File(label="Output PDF"),
68
  title="PDF Color Inverter",
69
- description="Upload a PDF and specify the pages to invert colors."
70
  )
71
 
72
  if __name__ == "__main__":
 
35
  doc.save(output_pdf)
36
  doc.close()
37
 
38
+ def invert_pdf_document(input_file, page_selection, output_name):
39
+ print(f"[LOG] Starting PDF inversion for {input_file} on pages: {page_selection}")
 
40
  if hasattr(input_file, "read"):
41
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_in:
42
  tmp_in.write(input_file.read())
43
  tmp_in_path = tmp_in.name
44
  cleanup = True
45
+ print(f"[LOG] Created temporary input file: {tmp_in_path}")
46
  else:
47
  tmp_in_path = input_file
48
  cleanup = False
49
 
 
50
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_out:
51
  tmp_out_path = tmp_out.name
52
+ print(f"[LOG] Created temporary output file: {tmp_out_path}")
53
 
54
  invert_pdf_pages(tmp_in_path, tmp_out_path, page_selection)
55
+ print(f"[LOG] Successfully inverted pages: {page_selection}")
56
 
 
57
  if cleanup:
58
  os.remove(tmp_in_path)
59
+
60
+ final_output_path = os.path.join(os.path.dirname(tmp_out_path), output_name)
61
+ os.rename(tmp_out_path, final_output_path)
62
+ print(f"[LOG] Output saved as: {final_output_path}")
63
+ return final_output_path
64
 
65
  iface = gr.Interface(
66
  fn=invert_pdf_document,
67
  inputs=[
68
  gr.File(label="Input PDF"),
69
+ gr.Textbox(label="Page Selection", value="1-12,14-20,22-32,56,66-78,82-97"),
70
+ gr.Textbox(label="Output Name", value="inverted_output.pdf")
71
  ],
72
  outputs=gr.File(label="Output PDF"),
73
  title="PDF Color Inverter",
74
+ description="Upload a PDF, specify the pages to invert, and a custom output name."
75
  )
76
 
77
  if __name__ == "__main__":