alexandraroze commited on
Commit
643ce30
·
1 Parent(s): ca0f29e
Files changed (2) hide show
  1. app.py +16 -7
  2. invoice_generator.py +2 -1
app.py CHANGED
@@ -21,10 +21,18 @@ def get_image_from_pdf(pdf_path):
21
  def display_invoice(image_path):
22
  output_pdf = "invoice_" + os.path.basename(image_path).split(".")[0] + ".pdf"
23
  path_to_output_pdf = f"{output_folder}/{output_pdf}"
24
- generate_invoice(image_path, output_pdf, template, output_folder)
25
- print(f"Generated invoice: {path_to_output_pdf}")
26
- st.query_params["generated_pdf"] = path_to_output_pdf
27
- return get_image_from_pdf(path_to_output_pdf)
 
 
 
 
 
 
 
 
28
 
29
 
30
  st.title("Upload FNOL photo")
@@ -48,8 +56,9 @@ with col2:
48
  if st.query_params.get("image"):
49
  if st.button("Generate invoice"):
50
  with st.spinner("Generating..."):
51
- st.session_state["other_image"] = display_invoice(st.query_params["image"])
52
- st.query_params["status"] = "loaded"
 
53
  else:
54
  st.button("Generate invoice", disabled=True)
55
  if st.query_params.get("generated_pdf"):
@@ -64,4 +73,4 @@ with col2:
64
 
65
  with col3:
66
  if st.query_params.get("status") == "loaded":
67
- st.image(st.session_state["other_image"], caption="Generated invoice", use_column_width=True)
 
21
  def display_invoice(image_path):
22
  output_pdf = "invoice_" + os.path.basename(image_path).split(".")[0] + ".pdf"
23
  path_to_output_pdf = f"{output_folder}/{output_pdf}"
24
+ try:
25
+ status = generate_invoice(image_path, output_pdf, template, output_folder)
26
+ if status == 0:
27
+ st.write("Image is irrelevant, upload another one")
28
+ return
29
+ print(f"Generated invoice: {path_to_output_pdf}")
30
+ st.query_params["generated_pdf"] = path_to_output_pdf
31
+ return get_image_from_pdf(path_to_output_pdf)
32
+ except Exception as e:
33
+ st.write("Could not generate invoice, please try again")
34
+ print(e)
35
+ return None
36
 
37
 
38
  st.title("Upload FNOL photo")
 
56
  if st.query_params.get("image"):
57
  if st.button("Generate invoice"):
58
  with st.spinner("Generating..."):
59
+ st.session_state["invoice"] = display_invoice(st.query_params["image"])
60
+ if st.session_state["invoice"]:
61
+ st.query_params["status"] = "loaded"
62
  else:
63
  st.button("Generate invoice", disabled=True)
64
  if st.query_params.get("generated_pdf"):
 
73
 
74
  with col3:
75
  if st.query_params.get("status") == "loaded":
76
+ st.image(st.session_state["invoice"], caption="Generated invoice", use_column_width=True)
invoice_generator.py CHANGED
@@ -228,7 +228,7 @@ def generate_invoice(image_path, output_file, template, output_folder):
228
  description = generate_accident_description(client, image_path)
229
  if not description:
230
  print(f"Image {image_path} is irrelevant.")
231
- return
232
 
233
  assert "damage_description" in description, "damage_description not found"
234
  assert "accident_story" in description, "accident_story not found"
@@ -242,3 +242,4 @@ def generate_invoice(image_path, output_file, template, output_folder):
242
  complete_pdf(
243
  f"{output_folder}/{output_file}", image_path, description["accident_story"]
244
  )
 
 
228
  description = generate_accident_description(client, image_path)
229
  if not description:
230
  print(f"Image {image_path} is irrelevant.")
231
+ return 0
232
 
233
  assert "damage_description" in description, "damage_description not found"
234
  assert "accident_story" in description, "accident_story not found"
 
242
  complete_pdf(
243
  f"{output_folder}/{output_file}", image_path, description["accident_story"]
244
  )
245
+ return 1