Spaces:
Runtime error
Runtime error
File size: 2,552 Bytes
75128dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 |
import streamlit as st
import time
import logging
from json import JSONDecodeError
from PIL import Image
from utils.haystack import query
from utils.frontend import reset_results, set_state_if_absent
def main():
set_state_if_absent("statement", "What is the fastest animal?")
set_state_if_absent("results", None)
st.write("# Look for images with MultiModalRetrieval π
")
st.write()
st.markdown(
"""
##### Enter a question about animals
"""
)
# Search bar
statement = st.text_input(
"", value=st.session_state.statement, max_chars=100, on_change=reset_results
)
col1, col2 = st.columns(2)
col1.markdown(
"<style>.stButton button {width:100%;}</style>", unsafe_allow_html=True
)
run_pressed = col1.button("Run")
run_query = (
run_pressed or statement != st.session_state.statement
)
# Get results for query
if run_query and statement:
time_start = time.time()
reset_results()
st.session_state.statement = statement
with st.spinner("π§ Performing neural search on documents..."):
try:
docs = query(statement)
st.write(docs["documents"])
for doc in docs["documents"]:
image = Image.open(doc.content)
st.image(image)
for answer in docs["answers"]:
st.write(answer)
print(f"S: {statement}")
time_end = time.time()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
print(f"elapsed time: {time_end - time_start}")
except JSONDecodeError as je:
st.error(
"π An error occurred reading the results. Is the document store working?"
)
return
except Exception as e:
logging.exception(e)
st.error("π An error occurred during the request.")
return
# if st.session_state.results:
# st.write("Got some results")
# print("GOT RESTULTS")
# st.write("Received Results")
# results = st.session_state.results
# print(results)
# docs = results["documents"]
# st.write(results)
# # show different messages depending on entailment results
# for doc in docs:
# image = Image(filename=doc.content)
# st.image(image)
main() |