Spaces:
Running
Running
Attempting sessions states and annotations
Browse files- app.py +66 -5
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -4,6 +4,9 @@ from haystack.schema import Answer
|
|
| 4 |
from haystack.document_stores import InMemoryDocumentStore
|
| 5 |
from haystack.pipelines import ExtractiveQAPipeline
|
| 6 |
from haystack.nodes import FARMReader, TfidfRetriever
|
|
|
|
|
|
|
|
|
|
| 7 |
import validators
|
| 8 |
import json
|
| 9 |
|
|
@@ -13,24 +16,82 @@ retriever = TfidfRetriever(document_store=document_store)
|
|
| 13 |
reader = FARMReader(model_name_or_path="deepset/tinyroberta-squad2", use_gpu=True)
|
| 14 |
pipeline = ExtractiveQAPipeline(reader, retriever)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def load_and_write_data():
|
| 17 |
doc_dir = './article_txt_got'
|
| 18 |
docs = convert_files_to_docs(dir_path=doc_dir, clean_func=clean_wiki_text, split_paragraphs=True)
|
| 19 |
|
| 20 |
document_store.write_documents(docs)
|
| 21 |
|
|
|
|
| 22 |
#Streamlit App
|
| 23 |
|
| 24 |
st.title('Game of Thrones QA with Haystack')
|
| 25 |
-
question = st.text_input(
|
| 26 |
|
| 27 |
load_and_write_data()
|
| 28 |
|
| 29 |
def ask_question(question):
|
| 30 |
prediction = pipeline.run(query=question, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}})
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if question:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from haystack.document_stores import InMemoryDocumentStore
|
| 5 |
from haystack.pipelines import ExtractiveQAPipeline
|
| 6 |
from haystack.nodes import FARMReader, TfidfRetriever
|
| 7 |
+
import logging
|
| 8 |
+
from markdown import markdown
|
| 9 |
+
from annotated_text import annotation
|
| 10 |
import validators
|
| 11 |
import json
|
| 12 |
|
|
|
|
| 16 |
reader = FARMReader(model_name_or_path="deepset/tinyroberta-squad2", use_gpu=True)
|
| 17 |
pipeline = ExtractiveQAPipeline(reader, retriever)
|
| 18 |
|
| 19 |
+
def set_state_if_absent(key, value):
|
| 20 |
+
if key not in st.session_state:
|
| 21 |
+
st.session_state[key] = value
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
st.set_page_config(page_title="Game of Thrones QA with Haystack", page_icon="https://haystack.deepset.ai/img/HaystackIcon.png")
|
| 25 |
+
|
| 26 |
+
set_state_if_absent("question", "Who is Arya's father")
|
| 27 |
+
set_state_if_absent("results", None)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def reset_results(*args):
|
| 31 |
+
st.session_state.results = None
|
| 32 |
+
|
| 33 |
def load_and_write_data():
|
| 34 |
doc_dir = './article_txt_got'
|
| 35 |
docs = convert_files_to_docs(dir_path=doc_dir, clean_func=clean_wiki_text, split_paragraphs=True)
|
| 36 |
|
| 37 |
document_store.write_documents(docs)
|
| 38 |
|
| 39 |
+
|
| 40 |
#Streamlit App
|
| 41 |
|
| 42 |
st.title('Game of Thrones QA with Haystack')
|
| 43 |
+
question = st.text_input("", value=st.session_state.question, max_chars=100, on_change=reset_results)
|
| 44 |
|
| 45 |
load_and_write_data()
|
| 46 |
|
| 47 |
def ask_question(question):
|
| 48 |
prediction = pipeline.run(query=question, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}})
|
| 49 |
+
results = []
|
| 50 |
+
for answer in prediction["answers"]:
|
| 51 |
+
if answer.get("answer", None):
|
| 52 |
+
results.append(
|
| 53 |
+
{
|
| 54 |
+
"context": "..." + answer["context"] + "...",
|
| 55 |
+
"answer": answer.get("answer", None),
|
| 56 |
+
"relevance": round(answer["score"] * 100, 2),
|
| 57 |
+
"offset_start_in_doc": answer["offsets_in_document"][0]["start"],
|
| 58 |
+
}
|
| 59 |
+
)
|
| 60 |
+
else:
|
| 61 |
+
results.append(
|
| 62 |
+
{
|
| 63 |
+
"context": None,
|
| 64 |
+
"answer": None,
|
| 65 |
+
"relevance": round(answer["score"] * 100, 2),
|
| 66 |
+
}
|
| 67 |
+
)
|
| 68 |
+
return results
|
| 69 |
+
# st.write(prediction['answers'][0].to_dict())
|
| 70 |
+
# st.write(prediction['answers'][1].to_dict())
|
| 71 |
+
# st.write(prediction['answers'][2].to_dict())
|
| 72 |
+
|
| 73 |
|
| 74 |
if question:
|
| 75 |
+
try:
|
| 76 |
+
st.session_state.results = ask_question(question)
|
| 77 |
+
except Exception as e:
|
| 78 |
+
logging.exception(e)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if st.session_state.results:
|
| 83 |
+
st.write('## Top Results')
|
| 84 |
+
for count, result in enumerate(st.session_state.results):
|
| 85 |
+
if result["answer"]:
|
| 86 |
+
answer, context = result["answer"], result["context"]
|
| 87 |
+
start_idx = context.find(answer)
|
| 88 |
+
end_idx = start_idx + len(answer)
|
| 89 |
+
st.write(
|
| 90 |
+
markdown(context[:start_idx] + str(annotation(answer, "ANSWER", "#8ef")) + context[end_idx:]),
|
| 91 |
+
unsafe_allow_html=True,
|
| 92 |
+
)
|
| 93 |
+
st.markdown(f"**Relevance:** {result['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 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
farm-haystack==1.4.0
|
| 2 |
-
validators==0.18.2
|
|
|
|
|
|
| 1 |
farm-haystack==1.4.0
|
| 2 |
+
validators==0.18.2
|
| 3 |
+
markdown
|