Spaces:
Runtime error
Runtime error
extract from main
Browse files- π‘_Home.py +58 -63
π‘_Home.py
CHANGED
@@ -31,72 +31,67 @@ def create_answer_objects(predictions):
|
|
31 |
)
|
32 |
return results
|
33 |
|
34 |
-
def main():
|
35 |
-
build_sidebar()
|
36 |
|
37 |
-
|
38 |
-
set_state_if_absent("results", None)
|
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 |
-
logging.exception(e)
|
81 |
-
st.error("π An error occurred during the request.")
|
82 |
-
return
|
83 |
-
|
84 |
-
if st.session_state.results:
|
85 |
-
st.write('## Why this image?')
|
86 |
-
answers = st.session_state.results
|
87 |
-
for count, answer in enumerate(answers):
|
88 |
-
if answer["answer"]:
|
89 |
-
text, context = answer["answer"], answer["context"]
|
90 |
-
start_idx = context.find(text)
|
91 |
-
end_idx = start_idx + len(text)
|
92 |
-
st.write(
|
93 |
-
markdown(context[:start_idx] + str(annotation(body=text, label="ANSWER", background="#964448", color='#ffffff')) + context[end_idx:]),
|
94 |
-
unsafe_allow_html=True,
|
95 |
-
)
|
96 |
-
st.markdown(f"**Relevance:** {answer['relevance']}")
|
97 |
-
else:
|
98 |
-
st.info(
|
99 |
-
"π€ Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"
|
100 |
-
)
|
101 |
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
)
|
32 |
return results
|
33 |
|
|
|
|
|
34 |
|
35 |
+
build_sidebar()
|
|
|
36 |
|
37 |
+
set_state_if_absent("statement", "What is the fastest animal?")
|
38 |
+
set_state_if_absent("results", None)
|
39 |
+
|
40 |
+
st.write("# Look for images with MultiModalRetrieval π
")
|
41 |
+
st.markdown(
|
42 |
"""
|
43 |
+
##### Ask a question about animals in the Lisbon Zoo:
|
44 |
+
To learn more about this demo, check out the βοΈ Info section
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
# Search bar
|
48 |
+
statement = st.text_input(
|
49 |
+
"", value=st.session_state.statement, max_chars=100, on_change=reset_results
|
50 |
+
)
|
51 |
+
|
52 |
+
run_pressed = st.button("Run")
|
53 |
|
54 |
+
run_query = (
|
55 |
+
run_pressed or statement != st.session_state.statement
|
56 |
+
)
|
57 |
|
58 |
+
# Get results for query
|
59 |
+
if run_query and statement:
|
60 |
+
time_start = time.time()
|
61 |
+
reset_results()
|
62 |
+
st.session_state.statement = statement
|
63 |
+
with st.spinner("π πΌπ·π¦ Looking for the right animal"):
|
64 |
+
try:
|
65 |
+
docs = query(statement)
|
66 |
+
for doc in docs["documents"]:
|
67 |
+
image = Image.open(doc.content)
|
68 |
+
st.image(image)
|
69 |
+
st.session_state.results = create_answer_objects(docs["answers"])
|
70 |
+
time_end = time.time()
|
71 |
+
except JSONDecodeError as je:
|
72 |
+
st.error(
|
73 |
+
"π An error occurred reading the results. Is the document store working?"
|
74 |
+
)
|
75 |
+
|
76 |
+
except Exception as e:
|
77 |
+
logging.exception(e)
|
78 |
+
st.error("π An error occurred during the request.")
|
79 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
if st.session_state.results:
|
82 |
+
st.write('## Why this image?')
|
83 |
+
answers = st.session_state.results
|
84 |
+
for count, answer in enumerate(answers):
|
85 |
+
if answer["answer"]:
|
86 |
+
text, context = answer["answer"], answer["context"]
|
87 |
+
start_idx = context.find(text)
|
88 |
+
end_idx = start_idx + len(text)
|
89 |
+
st.write(
|
90 |
+
markdown(context[:start_idx] + str(annotation(body=text, label="ANSWER", background="#964448", color='#ffffff')) + context[end_idx:]),
|
91 |
+
unsafe_allow_html=True,
|
92 |
+
)
|
93 |
+
st.markdown(f"**Relevance:** {answer['relevance']}")
|
94 |
+
else:
|
95 |
+
st.info(
|
96 |
+
"π€ Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"
|
97 |
+
)
|