Stefano Fiorucci commited on
Commit
7c3a548
β€’
1 Parent(s): 592c3d9

improved get_backlink

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -6,7 +6,7 @@ import pandas as pd
6
  from json import JSONDecodeError
7
  from markdown import markdown
8
  import random
9
- from typing import List, Dict, Any, Tuple
10
 
11
  from haystack.document_stores import FAISSDocumentStore
12
  from haystack.nodes import EmbeddingRetriever
@@ -48,15 +48,15 @@ def query(pipe, question):
48
  """Run query and get answers"""
49
  return (pipe.run(question, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}}), None)
50
 
51
- def get_backlink(result) -> Tuple[str, str]:
52
  if result.get("document", None):
53
  doc = result["document"]
54
  if isinstance(doc, dict):
55
  if doc.get("meta", None):
56
  if isinstance(doc["meta"], dict):
57
- if doc["meta"].get("url", None) :
58
- return doc["meta"]["url"]
59
- return None
60
 
61
  def main():
62
  # st.set_page_config(page_title='Who killed Laura Palmer?',
@@ -203,14 +203,14 @@ Ask any question on [Twin Peaks] (https://twinpeaks.fandom.com/wiki/Twin_Peaks)
203
  end_idx = start_idx + len(answer)
204
  #url = get_backlink(result, my_ip)
205
  # Hack due to this bug: https://github.com/streamlit/streamlit/issues/3190
206
- st.write(markdown("- ..."+context[:start_idx] + str(annotation(answer, "ANSWER", "#8ef")) + context[end_idx:]+"..."), unsafe_allow_html=True)
207
  source = ""
208
- url = get_backlink(result)
209
- if url:
210
- source = f"({result['document']['meta']['url']})"
211
  else:
212
  source = f"{result['source']}"
213
- st.markdown(f"**Score:** {result['score']:.2f} - **Source:** {source}")
214
  else:
215
  st.info("πŸ€”    Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!")
216
  main()
 
6
  from json import JSONDecodeError
7
  from markdown import markdown
8
  import random
9
+ from typing import List, Dict, Any, Tuple, Optional
10
 
11
  from haystack.document_stores import FAISSDocumentStore
12
  from haystack.nodes import EmbeddingRetriever
 
48
  """Run query and get answers"""
49
  return (pipe.run(question, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}}), None)
50
 
51
+ def get_backlink(result) -> Tuple[Optional[str], Optional[str]]:
52
  if result.get("document", None):
53
  doc = result["document"]
54
  if isinstance(doc, dict):
55
  if doc.get("meta", None):
56
  if isinstance(doc["meta"], dict):
57
+ if doc["meta"].get("url", None) and doc["meta"].get("name", None):
58
+ return doc["meta"]["url"], doc["meta"]["name"]
59
+ return None, None
60
 
61
  def main():
62
  # st.set_page_config(page_title='Who killed Laura Palmer?',
 
203
  end_idx = start_idx + len(answer)
204
  #url = get_backlink(result, my_ip)
205
  # Hack due to this bug: https://github.com/streamlit/streamlit/issues/3190
206
+ st.write(markdown("- ..."+context[:start_idx] + str(annotation(answer, "ANSWER", "#3e1c21")) + context[end_idx:]+"..."), unsafe_allow_html=True)
207
  source = ""
208
+ url, title = get_backlink(result)
209
+ if url and title:
210
+ source = f"[{result['document']['meta']['title']}]({result['document']['meta']['url']})"
211
  else:
212
  source = f"{result['source']}"
213
+ st.markdown(f"**Score:** {result['relevance']} - **Source:** {source}")
214
  else:
215
  st.info("πŸ€”    Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!")
216
  main()