Spaces:
Sleeping
Sleeping
annafilina
commited on
Commit
•
2190b1e
1
Parent(s):
b75f34c
Upload 2 files
Browse files- answers.py +35 -0
- requirements.txt +3 -0
answers.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
3 |
+
|
4 |
+
model_name = "deepset/roberta-base-squad2"
|
5 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
|
8 |
+
def get_answer(context, question):
|
9 |
+
nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
|
10 |
+
QA_input = {'question': question, 'context': context}
|
11 |
+
res = nlp(QA_input)
|
12 |
+
answer = res['answer']
|
13 |
+
return answer
|
14 |
+
|
15 |
+
def main():
|
16 |
+
st.title("Question Answering App")
|
17 |
+
st.markdown("Enter the context and question, then click on 'Get Answer' to retrieve the answer.")
|
18 |
+
|
19 |
+
|
20 |
+
context = st.text_area("Context", "Enter the context here...")
|
21 |
+
question = st.text_input("Question", "Enter the question here...")
|
22 |
+
|
23 |
+
|
24 |
+
if st.button("Get Answer"):
|
25 |
+
|
26 |
+
if context.strip() == "" or question.strip() == "":
|
27 |
+
st.warning("Please enter the context and question.")
|
28 |
+
else:
|
29 |
+
|
30 |
+
answer = get_answer(context, question)
|
31 |
+
st.success(f"Answer: {answer}")
|
32 |
+
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.21.0
|
2 |
+
transformers==4.11.2
|
3 |
+
torch==2.0.1
|