Spaces:
Sleeping
Sleeping
spinner and caching
Browse files
app.py
CHANGED
|
@@ -11,10 +11,22 @@ import validators
|
|
| 11 |
import json
|
| 12 |
|
| 13 |
#Haystack Components
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def set_state_if_absent(key, value):
|
| 20 |
if key not in st.session_state:
|
|
@@ -23,31 +35,23 @@ def set_state_if_absent(key, value):
|
|
| 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["answer"]:
|
| 52 |
results.append(
|
| 53 |
{
|
|
@@ -66,18 +70,15 @@ def ask_question(question):
|
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
|
|
|
|
| 11 |
import json
|
| 12 |
|
| 13 |
#Haystack Components
|
| 14 |
+
@st.cache(hash_funcs={"builtins.SwigPyObject": lambda _: None},allow_output_mutation=True)
|
| 15 |
+
def start_haystack():
|
| 16 |
+
document_store = InMemoryDocumentStore()
|
| 17 |
+
load_and_write_data(document_store)
|
| 18 |
+
retriever = TfidfRetriever(document_store=document_store)
|
| 19 |
+
reader = FARMReader(model_name_or_path="deepset/tinyroberta-squad2", use_gpu=True)
|
| 20 |
+
pipeline = ExtractiveQAPipeline(reader, retriever)
|
| 21 |
+
return pipeline
|
| 22 |
+
|
| 23 |
+
def load_and_write_data(document_store):
|
| 24 |
+
doc_dir = './article_txt_got'
|
| 25 |
+
docs = convert_files_to_docs(dir_path=doc_dir, clean_func=clean_wiki_text, split_paragraphs=True)
|
| 26 |
+
|
| 27 |
+
document_store.write_documents(docs)
|
| 28 |
+
|
| 29 |
+
pipeline = start_haystack()
|
| 30 |
|
| 31 |
def set_state_if_absent(key, value):
|
| 32 |
if key not in st.session_state:
|
|
|
|
| 35 |
|
| 36 |
st.set_page_config(page_title="Game of Thrones QA with Haystack", page_icon="https://haystack.deepset.ai/img/HaystackIcon.png")
|
| 37 |
|
| 38 |
+
set_state_if_absent("question", "Who is Arya's father?")
|
| 39 |
set_state_if_absent("results", None)
|
| 40 |
|
| 41 |
|
| 42 |
def reset_results(*args):
|
| 43 |
st.session_state.results = None
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
#Streamlit App
|
| 46 |
|
| 47 |
st.title('Game of Thrones QA with Haystack')
|
| 48 |
question = st.text_input("", value=st.session_state.question, max_chars=100, on_change=reset_results)
|
| 49 |
|
|
|
|
|
|
|
| 50 |
def ask_question(question):
|
| 51 |
prediction = pipeline.run(query=question, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}})
|
| 52 |
results = []
|
| 53 |
for answer in prediction["answers"]:
|
| 54 |
+
answer = answer.to_dict()
|
| 55 |
if answer["answer"]:
|
| 56 |
results.append(
|
| 57 |
{
|
|
|
|
| 70 |
}
|
| 71 |
)
|
| 72 |
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
if question:
|
| 75 |
+
with st.spinner("👑 Performing neural search on royal scripts..."):
|
| 76 |
+
try:
|
| 77 |
+
msg = 'Asked ' + question
|
| 78 |
+
logging.info(msg)
|
| 79 |
+
st.session_state.results = ask_question(question)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logging.exception(e)
|
| 82 |
|
| 83 |
|
| 84 |
|