File size: 3,706 Bytes
4e2b533
 
 
 
 
 
 
 
8ab7d9a
4e2b533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab7d9a
4e2b533
 
 
 
 
 
 
76fbd7f
 
4e2b533
 
 
 
 
 
 
cc45b63
4e2b533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
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

def main():
    build_sidebar()

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

    st.write("# Look for images with MultiModalRetrieval πŸ…")
    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"])
                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('## 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!"
                )

main()