Spaces:
Build error
Build error
bentrevett
commited on
Commit
•
ff3b85a
1
Parent(s):
439b5aa
initial commit
Browse files- app.py +46 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
import streamlit as st
|
3 |
+
from annotated_text import annotated_text
|
4 |
+
|
5 |
+
|
6 |
+
@st.cache(allow_output_mutation=True, show_spinner=False)
|
7 |
+
def get_pipe():
|
8 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
|
9 |
+
model = transformers.AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
|
10 |
+
pipe = transformers.pipeline("question-answering", model=model, tokenizer=tokenizer)
|
11 |
+
return pipe
|
12 |
+
|
13 |
+
|
14 |
+
def parse_context(context, prediction):
|
15 |
+
parsed_context = []
|
16 |
+
parsed_context.append(context[:prediction["start"]])
|
17 |
+
parsed_context.append((prediction["answer"], "ANSWER", "#afa"))
|
18 |
+
parsed_context.append(context[prediction["end"]:])
|
19 |
+
return parsed_context
|
20 |
+
|
21 |
+
|
22 |
+
st.set_page_config(page_title="Question Answering")
|
23 |
+
st.title("Question Answering")
|
24 |
+
st.write("Enter context and a question and press 'Predict' to extract the answer from the context.")
|
25 |
+
|
26 |
+
default_context = "My name is Wolfgang and I live in Berlin."
|
27 |
+
default_question = "What is my name?"
|
28 |
+
|
29 |
+
context = st.text_area("Enter context here:", value=default_context)
|
30 |
+
question = st.text_input("Enter question here:", value=default_question)
|
31 |
+
submit = st.button('Predict')
|
32 |
+
|
33 |
+
with st.spinner("Loading model..."):
|
34 |
+
pipe = get_pipe()
|
35 |
+
|
36 |
+
if (submit and len(context.strip()) > 0 and len(question.strip()) > 0):
|
37 |
+
|
38 |
+
prediction = pipe(question, context)
|
39 |
+
|
40 |
+
parsed_context = parse_context(context, prediction)
|
41 |
+
|
42 |
+
st.header("Prediction:")
|
43 |
+
annotated_text(*parsed_context)
|
44 |
+
|
45 |
+
st.header('Raw values:')
|
46 |
+
st.json(prediction)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
1 |
+
st-annotated-text
|
2 |
+
transformers
|