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