Commit
·
a303cd3
1
Parent(s):
fb6b388
updated app
Browse files
app.py
CHANGED
@@ -18,93 +18,93 @@ with open(f"{data_folder}/car_parts.json", "r") as f:
|
|
18 |
car_parts = json.load(f)
|
19 |
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
18 |
car_parts = json.load(f)
|
19 |
|
20 |
|
21 |
+
if "pipeline" not in st.session_state:
|
22 |
+
st.session_state.pipeline = InvoiceGenerator(
|
23 |
+
fais_index_path=f"{data_folder}/invoice_index.faiss",
|
24 |
+
image_invoice_index_path=f"{data_folder}/image_invoice.csv",
|
25 |
+
path_to_invoices=f"{data_folder}/invoices",
|
26 |
+
path_to_images=f"{data_folder}/images",
|
27 |
+
reranker_model="monovlm",
|
28 |
+
device=device,
|
29 |
+
gpu_memory_utilization=0.65
|
30 |
+
)
|
31 |
+
pipeline = st.session_state.pipeline
|
32 |
+
|
33 |
+
|
34 |
+
def get_image_from_pdf(pdf_path):
|
35 |
+
doc = fitz.open(pdf_path)
|
36 |
+
page = doc[0]
|
37 |
+
mat = fitz.Matrix(2, 2)
|
38 |
+
pix = page.get_pixmap(matrix=mat)
|
39 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
40 |
+
return img
|
41 |
+
|
42 |
+
|
43 |
+
def display_invoice(image_path):
|
44 |
+
output_pdf = "invoice_" + os.path.basename(image_path).split(".")[0] + ".pdf"
|
45 |
+
path_to_output_pdf = f"{output_folder}/{output_pdf}"
|
46 |
+
try:
|
47 |
+
result = pipeline.generate_invoice(
|
48 |
+
image_path=image_path, output_path=path_to_output_pdf, car_parts=car_parts
|
49 |
+
)
|
50 |
+
if result is None:
|
51 |
+
st.write("Image is irrelevant, upload another one")
|
52 |
+
st.session_state["status"] = "irrelevant"
|
53 |
+
return
|
54 |
+
print(f"Generated invoice: {path_to_output_pdf}")
|
55 |
+
st.session_state["generated_pdf"] = path_to_output_pdf
|
56 |
+
st.session_state["invoice_info"] = result["invoice_info"]
|
57 |
+
st.session_state["invoice_path"] = f"{data_folder}/invoices/{result['invoice_path']}"
|
58 |
+
st.session_state["similar_image"] = f"{data_folder}/images/{result['similar_image']}"
|
59 |
+
st.session_state["damage_description"] = result["damage_description"]
|
60 |
+
st.session_state["detailed_damage_description"] = result["detailed_damage_description"]
|
61 |
+
return get_image_from_pdf(path_to_output_pdf)
|
62 |
+
except Exception as e:
|
63 |
+
st.write("Could not generate invoice, please try again")
|
64 |
+
print(e)
|
65 |
+
return None
|
66 |
+
|
67 |
+
|
68 |
+
st.title("Upload FNOL photo")
|
69 |
+
|
70 |
+
col1, col2, col3 = st.columns([4, 1, 4])
|
71 |
+
|
72 |
+
with col1:
|
73 |
+
uploaded_image = st.file_uploader("Upload photo", type=["jpg", "jpeg", "png"])
|
74 |
+
if uploaded_image:
|
75 |
+
try:
|
76 |
+
image = Image.open(uploaded_image)
|
77 |
+
image_path = f"{output_folder}/{str(uuid4())[:5]}.png"
|
78 |
+
image.save(image_path)
|
79 |
+
print(f"Image: {image_path}")
|
80 |
+
st.image(image, caption="Uploaded photo", width=300)
|
81 |
+
st.session_state["image"] = image_path
|
82 |
+
except Exception as e:
|
83 |
+
st.write(f"Coudn't load image: {e}")
|
84 |
+
|
85 |
+
with col2:
|
86 |
+
if st.session_state.get("image"):
|
87 |
+
if st.button("Generate invoice"):
|
88 |
+
with st.spinner("Generating..."):
|
89 |
+
st.session_state["invoice"] = display_invoice(st.session_state["image"])
|
90 |
+
if st.session_state["invoice"]:
|
91 |
+
st.session_state["status"] = "loaded"
|
92 |
+
else:
|
93 |
+
st.button("Generate invoice", disabled=True)
|
94 |
+
if st.session_state.get("generated_pdf"):
|
95 |
+
with open(st.session_state["generated_pdf"], "rb") as f:
|
96 |
+
file_data = f.read()
|
97 |
+
st.download_button(
|
98 |
+
label="Download invoice",
|
99 |
+
data=file_data,
|
100 |
+
file_name="generated_invoice.pdf",
|
101 |
+
mime="application/pdf"
|
102 |
+
)
|
103 |
+
|
104 |
+
with col3:
|
105 |
+
if st.session_state.get("status") == "loaded":
|
106 |
+
st.image(st.session_state["invoice"], caption="Generated invoice", use_container_width=True)
|
107 |
+
st.image(st.session_state["similar_image"], caption="Similar accident", width=300)
|
108 |
+
st.write(f"Detailed damage description: {st.session_state['detailed_damage_description']}")
|
109 |
+
st.write(f"Damage description: {st.session_state['damage_description']}")
|
110 |
+
st.image(get_image_from_pdf(st.session_state["invoice_path"]), caption="Invoice of similar accident type", use_container_width=True)
|