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