Spaces:
Sleeping
Sleeping
Update pages/answers_2.py
Browse files- pages/answers_2.py +45 -0
pages/answers_2.py
CHANGED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
3 |
+
from streamlit_extras.let_it_rain import rain
|
4 |
+
from data/context_examples.py import context, questions
|
5 |
+
|
6 |
+
rain(
|
7 |
+
emoji="❔",
|
8 |
+
font_size=54,
|
9 |
+
falling_speed=5,
|
10 |
+
animation_length="infinite",
|
11 |
+
)
|
12 |
+
|
13 |
+
model_name = "deepset/roberta-base-squad2"
|
14 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
|
17 |
+
def get_answer(context, question):
|
18 |
+
nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
|
19 |
+
QA_input = {'question': question, 'context': context}
|
20 |
+
res = nlp(QA_input)
|
21 |
+
answer = res['answer']
|
22 |
+
return answer
|
23 |
+
|
24 |
+
def main():
|
25 |
+
st.title("Question Answering App :robot_face:")
|
26 |
+
st.divider()
|
27 |
+
st.markdown("### **Enter the context and question, then click on ':blue[Get Answer]' to retrieve the answer:**")
|
28 |
+
|
29 |
+
selected_index = st.selectbox("Select an index:", range(len(contexts)))
|
30 |
+
context = st.text_area("**:blue[Context]**", contexts[selected_index])
|
31 |
+
question = st.text_input("**:blue[Question]**", questions[selected_index])
|
32 |
+
|
33 |
+
|
34 |
+
if st.button(":blue[**Get Answer**]"):
|
35 |
+
|
36 |
+
if context.strip() == "" or question.strip() == "":
|
37 |
+
st.warning("Please enter the context and question.")
|
38 |
+
else:
|
39 |
+
|
40 |
+
answer = get_answer(context, question)
|
41 |
+
st.success(f"Answer: {answer}")
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
main()
|