Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
|
| 4 |
|
| 5 |
@st.cache(allow_output_mutation=True)
|
| 6 |
def load_qa_model():
|
| 7 |
-
model_name = "
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
qa = load_qa_model()
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
data = pd.read_csv(uploaded_file)
|
| 33 |
-
|
| 34 |
-
if st.button("Process Batch"):
|
| 35 |
-
with st.spinner("Processing Batch..."):
|
| 36 |
-
results = process_batch(data)
|
| 37 |
-
st.write("Batch Processing Results:")
|
| 38 |
-
for result in results:
|
| 39 |
-
st.write("Question:", result['Question'])
|
| 40 |
-
st.write("Article:", result['Article'])
|
| 41 |
-
st.write("Answer:", result['Answer'])
|
| 42 |
-
st.write("Score:", result['Score'])
|
| 43 |
-
st.write("------")
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
+
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
|
|
|
| 4 |
|
| 5 |
@st.cache(allow_output_mutation=True)
|
| 6 |
def load_qa_model():
|
| 7 |
+
model_name = "mrm8488/mobilebert-uncased-finetuned-squadv2"
|
|
|
|
| 8 |
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
qa = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
| 11 |
+
return qa
|
| 12 |
+
|
| 13 |
+
def preprocess_text(text):
|
| 14 |
+
# Remove special characters and punctuation
|
| 15 |
+
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
| 16 |
+
# Convert to lowercase
|
| 17 |
+
text = text.lower()
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
def format_answer(answer):
|
| 21 |
+
# Add answer formatting logic here
|
| 22 |
+
# For example, add bold formatting
|
| 23 |
+
return f**{answer}**
|
| 24 |
+
|
| 25 |
+
def get_answers(qa, question, text, max, min, do_sample):
|
| 26 |
+
try:
|
| 27 |
+
answers = qa(question=question, context=text, max_answer_len=max, min_answer_len=min, do_sample=do_sample)
|
| 28 |
+
return format_answer(answers['answer'])
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Error: {str(e)}")
|
| 31 |
|
| 32 |
qa = load_qa_model()
|
| 33 |
|
| 34 |
+
st.title("Ask Questions about your Text")
|
| 35 |
+
sentence = st.text_area('Please paste your article :', height=30)
|
| 36 |
+
question = st.text_input("Questions from this article?")
|
| 37 |
+
button = st.button("Get me Answers")
|
| 38 |
+
|
| 39 |
+
with st.sidebar:
|
| 40 |
+
max = st.slider('Select max answer length', 50, 500, step=10, value=150)
|
| 41 |
+
min = st.slider('Select min answer length', 10, 450, step=10, value=50)
|
| 42 |
+
do_sample = st.checkbox("Do sample", value=False)
|
| 43 |
+
|
| 44 |
+
if button and sentence and question:
|
| 45 |
+
with st.spinner("Discovering Answers.."):
|
| 46 |
+
text = preprocess_text(sentence)
|
| 47 |
+
answer = get_answers(qa, question, text, max, min, do_sample)
|
| 48 |
+
st.write(answer)
|
| 49 |
+
else:
|
| 50 |
+
st.error("Please enter a question and text!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|