Spaces:
Runtime error
Runtime error
Ankur Goyal
commited on
Commit
•
d229b67
1
Parent(s):
225fcc2
Adding Examples
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
4 |
|
5 |
from PIL import ImageDraw
|
6 |
import streamlit as st
|
|
|
7 |
|
8 |
st.set_page_config(layout="wide")
|
9 |
|
@@ -63,10 +64,15 @@ st.markdown("# DocQuery: Query Documents w/ NLP")
|
|
63 |
if "document" not in st.session_state:
|
64 |
st.session_state["document"] = None
|
65 |
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
with input_col:
|
69 |
-
input_type = st.radio(
|
|
|
|
|
70 |
|
71 |
with model_col:
|
72 |
model_type = st.radio("Pick a model", list(CHECKPOINTS.keys()), horizontal=True)
|
@@ -97,16 +103,62 @@ def load_url_cb():
|
|
97 |
st.session_state.document = document
|
98 |
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
if input_type == "Upload":
|
101 |
file = st.file_uploader(
|
102 |
"Upload a PDF or Image document", key="file_input", on_change=load_file_cb
|
103 |
)
|
104 |
-
|
105 |
elif input_type == "URL":
|
106 |
-
# url = st.text_input("URL", "", on_change=load_url_callback, key="url_input")
|
107 |
url = st.text_input("URL", "", key="url_input", on_change=load_url_cb)
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
document = st.session_state.document
|
112 |
loading_placeholder = st.empty()
|
@@ -114,13 +166,13 @@ if document is not None:
|
|
114 |
col1, col2 = st.columns(2)
|
115 |
image = document.preview
|
116 |
|
117 |
-
|
118 |
colors = ["blue", "red", "green"]
|
119 |
if document is not None and question is not None and len(question) > 0:
|
120 |
col2.header(f"Answers ({model_type})")
|
121 |
with col2:
|
122 |
-
answers_placeholder = st.
|
123 |
-
answers_loading_placeholder = st.
|
124 |
|
125 |
with answers_loading_placeholder:
|
126 |
# Run this (one-time) expensive operation outside of the processing
|
|
|
4 |
|
5 |
from PIL import ImageDraw
|
6 |
import streamlit as st
|
7 |
+
from st_clickable_images import clickable_images
|
8 |
|
9 |
st.set_page_config(layout="wide")
|
10 |
|
|
|
64 |
if "document" not in st.session_state:
|
65 |
st.session_state["document"] = None
|
66 |
|
67 |
+
if "last_clicked" not in st.session_state:
|
68 |
+
st.session_state["last_clicked"] = None
|
69 |
+
|
70 |
+
input_col, model_col = st.columns(2)
|
71 |
|
72 |
with input_col:
|
73 |
+
input_type = st.radio(
|
74 |
+
"Pick an input type", ["Upload", "URL", "Examples"], horizontal=True
|
75 |
+
)
|
76 |
|
77 |
with model_col:
|
78 |
model_type = st.radio("Pick a model", list(CHECKPOINTS.keys()), horizontal=True)
|
|
|
103 |
st.session_state.document = document
|
104 |
|
105 |
|
106 |
+
examples = [
|
107 |
+
(
|
108 |
+
"https://templates.invoicehome.com/invoice-template-us-neat-750px.png",
|
109 |
+
"What is the invoice number?",
|
110 |
+
),
|
111 |
+
(
|
112 |
+
"https://miro.medium.com/max/787/1*iECQRIiOGTmEFLdWkVIH2g.jpeg",
|
113 |
+
"What is the purchase amount?",
|
114 |
+
),
|
115 |
+
(
|
116 |
+
"https://www.accountingcoach.com/wp-content/uploads/2013/10/[email protected]",
|
117 |
+
"What are net sales for 2020?",
|
118 |
+
),
|
119 |
+
]
|
120 |
+
imgs_clicked = []
|
121 |
+
|
122 |
if input_type == "Upload":
|
123 |
file = st.file_uploader(
|
124 |
"Upload a PDF or Image document", key="file_input", on_change=load_file_cb
|
125 |
)
|
|
|
126 |
elif input_type == "URL":
|
|
|
127 |
url = st.text_input("URL", "", key="url_input", on_change=load_url_cb)
|
128 |
+
elif input_type == "Examples":
|
129 |
+
example_cols = st.columns(len(examples))
|
130 |
+
for (i, (path, question)) in enumerate(examples):
|
131 |
+
with example_cols[i]:
|
132 |
+
imgs_clicked.append(
|
133 |
+
clickable_images(
|
134 |
+
[path],
|
135 |
+
div_style={
|
136 |
+
"display": "flex",
|
137 |
+
"justify-content": "center",
|
138 |
+
"flex-wrap": "wrap",
|
139 |
+
"cursor": "pointer",
|
140 |
+
},
|
141 |
+
img_style={"margin": "5px", "height": "200px"},
|
142 |
+
)
|
143 |
+
)
|
144 |
+
st.markdown(
|
145 |
+
f"<p style='text-align: center'>{question}</p>",
|
146 |
+
unsafe_allow_html=True,
|
147 |
+
)
|
148 |
+
print(imgs_clicked)
|
149 |
+
imgs_clicked = [-1] * len(imgs_clicked)
|
150 |
+
|
151 |
+
# clicked = clickable_images(
|
152 |
+
# [x[0] for x in examples],
|
153 |
+
# titles=[x[1] for x in examples],
|
154 |
+
# div_style={"display": "flex", "justify-content": "center", "flex-wrap": "wrap"},
|
155 |
+
# img_style={"margin": "5px", "height": "200px"},
|
156 |
+
# )
|
157 |
+
#
|
158 |
+
# st.markdown(f"Image #{clicked} clicked" if clicked > -1 else "No image clicked")
|
159 |
+
|
160 |
+
|
161 |
+
question = st.text_input("QUESTION", "", key="question")
|
162 |
|
163 |
document = st.session_state.document
|
164 |
loading_placeholder = st.empty()
|
|
|
166 |
col1, col2 = st.columns(2)
|
167 |
image = document.preview
|
168 |
|
169 |
+
question = st.session_state.question
|
170 |
colors = ["blue", "red", "green"]
|
171 |
if document is not None and question is not None and len(question) > 0:
|
172 |
col2.header(f"Answers ({model_type})")
|
173 |
with col2:
|
174 |
+
answers_placeholder = st.container()
|
175 |
+
answers_loading_placeholder = st.container()
|
176 |
|
177 |
with answers_loading_placeholder:
|
178 |
# Run this (one-time) expensive operation outside of the processing
|