iloncka commited on
Commit
dfe41ed
β€’
1 Parent(s): 5e035b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -4
app.py CHANGED
@@ -23,14 +23,13 @@ DEFAULT_QUESTION_AT_STARTUP = os.getenv("DEFAULT_QUESTION_AT_STARTUP", "What's t
23
  DEFAULT_ANSWER_AT_STARTUP = os.getenv("DEFAULT_ANSWER_AT_STARTUP", "Paris")
24
 
25
  def place_header_center(text, lottie_data):
26
- img, title= st.columns([3,3])
27
  with img:
28
- st_lottie(lottie_data, height=250)
29
  with title:
30
  st.title(text)
31
 
32
 
33
-
34
  @st.experimental_memo
35
  def get_lottie(path):
36
  with open(path, 'r', errors='ignore') as f:
@@ -46,6 +45,36 @@ def load_and_write_data(document_store):
46
  document_store.write_documents(docs)
47
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  # Haystack Components
50
  # @st.cache(allow_output_mutation=True)
51
  # def start_haystack():
@@ -94,6 +123,8 @@ def ask_question(question):
94
  "context": "..." + answer["context"] + "...",
95
  "answer": answer["answer"],
96
  "relevance": round(answer["score"] * 100, 2),
 
 
97
  "offset_start_in_doc": answer["offsets_in_document"][0]["start"],
98
  }
99
  )
@@ -129,7 +160,21 @@ if st.session_state.results:
129
  markdown(context[:start_idx] + str(annotation(body=answer, label="ANSWER", background="#ff700f", color='#ffffff')) + context[end_idx:]),
130
  unsafe_allow_html=True,
131
  )
132
- st.markdown(f"**Relevance:** {result['relevance']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  else:
134
  st.info(
135
  "πŸ€”    Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"
 
23
  DEFAULT_ANSWER_AT_STARTUP = os.getenv("DEFAULT_ANSWER_AT_STARTUP", "Paris")
24
 
25
  def place_header_center(text, lottie_data):
26
+ img, title= st.columns([1,3])
27
  with img:
28
+ st_lottie(lottie_data, height=150)
29
  with title:
30
  st.title(text)
31
 
32
 
 
33
  @st.experimental_memo
34
  def get_lottie(path):
35
  with open(path, 'r', errors='ignore') as f:
 
45
  document_store.write_documents(docs)
46
 
47
 
48
+ def get_backlink(result):
49
+ if result.get("document", None):
50
+ doc = result["document"]
51
+ if isinstance(doc, dict):
52
+ if doc.get("meta", None):
53
+ if isinstance(doc["meta"], dict):
54
+ if doc["meta"].get("url", None):
55
+ return doc["meta"]["url"]
56
+ return None
57
+
58
+
59
+ def get_doc_name(result):
60
+ if result.get("document", None):
61
+ doc = result["document"]
62
+ if isinstance(doc, dict):
63
+ if doc.get("meta", None):
64
+ if isinstance(doc["meta"], dict):
65
+ if doc["meta"].get("name", None):
66
+ return doc["meta"]["name"]
67
+ return None
68
+
69
+ def get_doc_reg_id(result):
70
+ if result.get("document", None):
71
+ doc = result["document"]
72
+ if isinstance(doc, dict):
73
+ if doc.get("meta", None):
74
+ if isinstance(doc["meta"], dict):
75
+ if doc["meta"].get("reg_id", None):
76
+ return doc["meta"]["reg_id"]
77
+ return None
78
  # Haystack Components
79
  # @st.cache(allow_output_mutation=True)
80
  # def start_haystack():
 
123
  "context": "..." + answer["context"] + "...",
124
  "answer": answer["answer"],
125
  "relevance": round(answer["score"] * 100, 2),
126
+ "document": [doc for doc in response["documents"] if doc["id"] == answer["document_id"]][0],
127
+ # "_raw": answer,
128
  "offset_start_in_doc": answer["offsets_in_document"][0]["start"],
129
  }
130
  )
 
160
  markdown(context[:start_idx] + str(annotation(body=answer, label="ANSWER", background="#ff700f", color='#ffffff')) + context[end_idx:]),
161
  unsafe_allow_html=True,
162
  )
163
+ source = ""
164
+ url = get_backlink(result)
165
+ name = get_doc_name(result)
166
+ reg_id = get_doc_reg_id(result)
167
+ if name:
168
+ source += f"[{result['document']['meta']['name']}]"
169
+
170
+ if url:
171
+ source += f"({result['document']['meta']['url']})"
172
+
173
+ if reg_id:
174
+ source += f"({result['document']['meta']['reg_id']})"
175
+
176
+ st.markdown(f"**Relevance:** {result['relevance']} - **Source:** {source}")
177
+
178
  else:
179
  st.info(
180
  "πŸ€”    Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!"