Tuana commited on
Commit
b339fb8
β€’
1 Parent(s): b1113e9

extract from main

Browse files
Files changed (1) hide show
  1. 🏑_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
- 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
- print(f"S: {statement}")
71
- time_end = time.time()
72
- print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
73
- print(f"elapsed time: {time_end - time_start}")
74
- except JSONDecodeError as je:
75
- st.error(
76
- "πŸ‘“    An error occurred reading the results. Is the document store working?"
77
- )
78
- return
79
- except Exception as e:
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
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )