leomaurodesenv commited on
Commit
d104f0e
1 Parent(s): c200df3

refactor(app): Refactoring the question loading

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -69,15 +69,28 @@ def get_question_pipeline(_doc_store):
69
  return pipe
70
 
71
 
72
- # # Create the retriever and reader
73
- # retriever = InMemoryBM25Retriever(document_store=document_store())
74
- # reader = ExtractiveReader(model="laurafcamargos/distilbert-qasports-basket-small")
75
- # reader.warm_up()
76
- # # Create the pipeline
77
- # pipe = Pipeline()
78
- # pipe.add_component(instance=retriever, name="retriever")
79
- # pipe.add_component(instance=reader, name="reader")
80
- # pipe.connect("retriever.documents", "reader.documents")
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- try:
106
- top_k = 3
107
- answer = pipe.run(
108
- data={
109
- "retriever": {"query": user_query, "top_k": 10},
110
- "reader": {"query": user_query, "top_k": top_k},
111
- }
112
- )
113
- # Display only the top k answers
114
- max_k = len(answer["reader"]["answers"])
115
- st.json(answer["reader"]["answers"][0 : min(top_k, max_k)])
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