Spaces:
Sleeping
Sleeping
leomaurodesenv
commited on
Commit
·
d104f0e
1
Parent(s):
c200df3
refactor(app): Refactoring the question loading
Browse files
app.py
CHANGED
@@ -69,15 +69,28 @@ def get_question_pipeline(_doc_store):
|
|
69 |
return pipe
|
70 |
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Streamlit interface
|
83 |
with st.status(
|
@@ -102,16 +115,14 @@ if user_query := st.text_input(
|
|
102 |
label="What do you want to know about Basketball?",
|
103 |
placeholder="How many field goals did Kobe Bryant score?",
|
104 |
):
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
st.
|
116 |
-
except Exception as e:
|
117 |
-
st.error(f"Error: We do not have an answer for your question.")
|
|
|
69 |
return pipe
|
70 |
|
71 |
|
72 |
+
def search(pipeline, question: str):
|
73 |
+
"""
|
74 |
+
Search for the answer to a question in the documents.
|
75 |
+
|
76 |
+
Args:
|
77 |
+
- pipeline: instance of the pipeline.
|
78 |
+
- question: string with the question.
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
- answer: dictionary with the answer.
|
82 |
+
"""
|
83 |
+
# Get the answers
|
84 |
+
top_k = 3
|
85 |
+
answer = pipeline.run(
|
86 |
+
data={
|
87 |
+
"retriever": {"query": question, "top_k": 10},
|
88 |
+
"reader": {"query": question, "top_k": top_k},
|
89 |
+
}
|
90 |
+
)
|
91 |
+
max_k = min(top_k, len(answer["reader"]["answers"]))
|
92 |
+
return answer["reader"]["answers"][0:max_k]
|
93 |
+
|
94 |
|
95 |
# Streamlit interface
|
96 |
with st.status(
|
|
|
115 |
label="What do you want to know about Basketball?",
|
116 |
placeholder="How many field goals did Kobe Bryant score?",
|
117 |
):
|
118 |
+
# Get the answers
|
119 |
+
with st.spinner("Waiting"):
|
120 |
+
try:
|
121 |
+
answer = search(pipe, user_query)
|
122 |
+
st.json(answer)
|
123 |
+
except Exception as e:
|
124 |
+
st.error("We do not have an answer for your question")
|
125 |
+
# Show balloons only once
|
126 |
+
if not st.session_state.get("run_once", False):
|
127 |
+
st.balloons()
|
128 |
+
st.session_state["run_once"] = True
|
|
|
|