File size: 3,417 Bytes
4e2b533
 
 
 
 
 
 
 
8ab7d9a
4e2b533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b339fb8
4e2b533
b339fb8
 
 
6e3f366
 
b339fb8
4e2b533
b339fb8
 
 
 
 
 
 
 
 
 
4e2b533
b339fb8
 
 
4e2b533
b339fb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e2b533
b339fb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import streamlit as st
import time
import logging
from json import JSONDecodeError
from PIL import Image
from markdown import markdown
from annotated_text import annotation
from utils.haystack import query
from utils.frontend import reset_results, set_state_if_absent, build_sidebar

def create_answer_objects(predictions):
    results = []
    for answer in predictions:
        answer = answer.to_dict()
        if answer["answer"]:
            results.append(
                {
                    "context": "..." + answer["context"] + "...",
                    "answer": answer["answer"],
                    "relevance": round(answer["score"] * 100, 2),
                    "offset_start_in_doc": answer["offsets_in_document"][0]["start"],
                }
            )
        else:
            results.append(
                {
                    "context": None,
                    "answer": None,
                    "relevance": round(answer["score"] * 100, 2),
                }
            )
    return results


build_sidebar()

set_state_if_absent("statement", "What is the fastest animal?")
set_state_if_absent("results", None)

st.write("# Search for animals at Lisbon Zoo with MultiModalRetrieval πŸ…")
st.write("Disclaimer: This demo will produce odd results if the Lisbon Zoo does not have said animal πŸ™ˆ - Stay tuned for improvements")
st.markdown(
    """
##### Ask a question about animals in the Lisbon Zoo:
To learn more about this demo, check out the ⭐️ Info section
"""
)
# Search bar
statement = st.text_input(
    "", value=st.session_state.statement, max_chars=100, on_change=reset_results
)

run_pressed = st.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("πŸ”Ž 🐼🐷🦊    Looking for the right animal"):
        try:
            docs = query(statement)
            for doc in docs["documents"]:
                image = Image.open(doc.content)
                st.image(image)
            st.session_state.results = create_answer_objects(docs["answers"])
            time_end = time.time()
        except JSONDecodeError as je:
            st.error(
                "πŸ‘“    An error occurred reading the results. Is the document store working?"
            )
            
        except Exception as e:
            logging.exception(e)
            st.error("🐞    An error occurred during the request.")
            

if st.session_state.results:
    st.write('## Why this image?')
    answers = st.session_state.results
    for count, answer in enumerate(answers):
        if answer["answer"]:
            text, context = answer["answer"], answer["context"]
            start_idx = context.find(text)
            end_idx = start_idx + len(text)
            st.write(
                markdown(context[:start_idx] + str(annotation(body=text, label="ANSWER", background="#964448", color='#ffffff')) + context[end_idx:]),
                unsafe_allow_html=True,
            )
            st.markdown(f"**Relevance:** {answer['relevance']}")
        else:
            st.info(
                "πŸ€”    Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"
            )