Spaces:
Sleeping
Sleeping
Stefano Fiorucci
commited on
Commit
·
be93ed6
1
Parent(s):
c557ead
random questions and other...
Browse files- app.py +17 -22
- data/questions.txt +18 -0
- {index_creation → notebooks}/index_creation.ipynb +0 -0
app.py
CHANGED
|
@@ -38,7 +38,13 @@ def start_haystack():
|
|
| 38 |
)
|
| 39 |
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=True)
|
| 40 |
pipe = ExtractiveQAPipeline(reader, retriever)
|
| 41 |
-
return pipe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
def set_state_if_absent(key, value):
|
| 44 |
if key not in st.session_state:
|
|
@@ -48,21 +54,13 @@ 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[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?',
|
| 63 |
# page_icon="https://static.wikia.nocookie.net/twinpeaks/images/4/4a/Site-favicon.ico/revision/latest?cb=20210710003705")
|
| 64 |
|
| 65 |
pipe=start_haystack()
|
|
|
|
| 66 |
|
| 67 |
# Persistent state
|
| 68 |
set_state_if_absent('question', "Where is Twin Peaks?")
|
|
@@ -157,15 +155,14 @@ and see if the AI can find an answer...
|
|
| 157 |
# Run button
|
| 158 |
run_pressed = col1.button("Run")
|
| 159 |
|
| 160 |
-
df=''
|
| 161 |
# Get next random question from the CSV
|
| 162 |
if col2.button("Random question"):
|
| 163 |
reset_results()
|
| 164 |
-
|
| 165 |
-
while
|
| 166 |
-
|
| 167 |
-
st.session_state.question =
|
| 168 |
-
st.session_state.answer = new_row["Answer"].values[0]
|
| 169 |
st.session_state.random_question_requested = True
|
| 170 |
# Re-runs the script setting the random question as the textbox value
|
| 171 |
# Unfortunately necessary as the Random Question button is _below_ the textbox
|
|
@@ -220,12 +217,10 @@ and see if the AI can find an answer...
|
|
| 220 |
# Hack due to this bug: https://github.com/streamlit/streamlit/issues/3190
|
| 221 |
st.write(markdown("- ..."+context[:start_idx] + str(annotation(answer, "ANSWER", "#3e1c21")) + context[end_idx:]+"..."), unsafe_allow_html=True)
|
| 222 |
source = ""
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
source = f"{result['source']}"
|
| 228 |
-
st.markdown(f"**Score:** {result['relevance']} - **Source:** {source}")
|
| 229 |
else:
|
| 230 |
st.info("🤔 Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!")
|
| 231 |
main()
|
|
|
|
| 38 |
)
|
| 39 |
reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=True)
|
| 40 |
pipe = ExtractiveQAPipeline(reader, retriever)
|
| 41 |
+
return pipe
|
| 42 |
+
|
| 43 |
+
@st.cache()
|
| 44 |
+
def load_questions():
|
| 45 |
+
with open('./data/questions.txt') as fin:
|
| 46 |
+
questions = [line.strip() for line in fin.readlines()]
|
| 47 |
+
return questions
|
| 48 |
|
| 49 |
def set_state_if_absent(key, value):
|
| 50 |
if key not in st.session_state:
|
|
|
|
| 54 |
"""Run query and get answers"""
|
| 55 |
return (pipe.run(question, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}}), None)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def main():
|
| 59 |
# st.set_page_config(page_title='Who killed Laura Palmer?',
|
| 60 |
# page_icon="https://static.wikia.nocookie.net/twinpeaks/images/4/4a/Site-favicon.ico/revision/latest?cb=20210710003705")
|
| 61 |
|
| 62 |
pipe=start_haystack()
|
| 63 |
+
questions = load_questions()
|
| 64 |
|
| 65 |
# Persistent state
|
| 66 |
set_state_if_absent('question', "Where is Twin Peaks?")
|
|
|
|
| 155 |
# Run button
|
| 156 |
run_pressed = col1.button("Run")
|
| 157 |
|
|
|
|
| 158 |
# Get next random question from the CSV
|
| 159 |
if col2.button("Random question"):
|
| 160 |
reset_results()
|
| 161 |
+
question = random.choice(questions)
|
| 162 |
+
while question == st.session_state.question: # Avoid picking the same question twice (the change is not visible on the UI)
|
| 163 |
+
question = random.choice(questions)
|
| 164 |
+
st.session_state.question = question
|
| 165 |
+
# st.session_state.answer = new_row["Answer"].values[0]
|
| 166 |
st.session_state.random_question_requested = True
|
| 167 |
# Re-runs the script setting the random question as the textbox value
|
| 168 |
# Unfortunately necessary as the Random Question button is _below_ the textbox
|
|
|
|
| 217 |
# Hack due to this bug: https://github.com/streamlit/streamlit/issues/3190
|
| 218 |
st.write(markdown("- ..."+context[:start_idx] + str(annotation(answer, "ANSWER", "#3e1c21")) + context[end_idx:]+"..."), unsafe_allow_html=True)
|
| 219 |
source = ""
|
| 220 |
+
name = result['meta']['name']
|
| 221 |
+
url = result['meta']['url']
|
| 222 |
+
source = f"[{name}]({url})"
|
| 223 |
+
st.markdown(f"**Score:** {result['score']:.2f} - **Source:** {source}")
|
|
|
|
|
|
|
| 224 |
else:
|
| 225 |
st.info("🤔 Haystack is unsure whether any of the documents contain an answer to your question. Try to reformulate it!")
|
| 226 |
main()
|
data/questions.txt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Who killed Laura Palmer?
|
| 2 |
+
Who found Laura's body?
|
| 3 |
+
What is garmonbozia?
|
| 4 |
+
What is a doppelganger?
|
| 5 |
+
Who is Teresa Banks?
|
| 6 |
+
What was found under Laura's nail?
|
| 7 |
+
Who is Dale Cooper's fiancèe?
|
| 8 |
+
Who is Mrs Tremond
|
| 9 |
+
Who is Ray Monroe?
|
| 10 |
+
Where is Twin Peaks
|
| 11 |
+
Who is the real owner of One Eyed Jack?
|
| 12 |
+
Who works as a croupier?
|
| 13 |
+
Why was Laura Palmer killed?
|
| 14 |
+
Who is Evelyn Marsh?
|
| 15 |
+
Who is the log lady?
|
| 16 |
+
Who is Bobby Briggs' father?
|
| 17 |
+
who is Susan Hurley
|
| 18 |
+
Who is Mike
|
{index_creation → notebooks}/index_creation.ipynb
RENAMED
|
File without changes
|