import streamlit as st from transformers import pipeline from transformers import AutoModelForQuestionAnswering, AutoTokenizer # set page title st.set_page_config(page_title="Automated Question Answering System") # heading st.markdown("

Question Answering on Academic Essays

", unsafe_allow_html=True) st.markdown("

What is extractive question answering about?

", unsafe_allow_html=True) st.write("Extractive question answering is a Natural Language Processing task where text is provided for a model so that the model can refer to it and make predictions about where the answer to a question is.") # st.markdown('___') # store the model in cache resources to enhance efficiency # ref: https://docs.streamlit.io/library/advanced-features/caching @st.cache_resource(show_spinner=True) def question_model(): # call my model for question answering model_name = "kxx-kkk/FYP_deberta-v3-base-squad2_mrqa" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForQuestionAnswering.from_pretrained(model_name) question_answerer = pipeline("question-answering", model=model, tokenizer=tokenizer) return question_answerer # choose the source with different tabs tab1, tab2 = st.tabs(["Input text", "Upload File"]) # if type the text as input with tab1: sample_question = "What is NLP?" with open("sample.txt", "r") as text_file: sample_text = text_file.read() example = st.button("Try example") context = st.text_area("Enter the essay below:", key="context", height=330) question = st.text_input(label="Enter the question: ", key="question") if example: st.session_state.context = sample_text st.session_state.question = sample_question button = st.button("Get answer") if button: with st.spinner(text="Loading question model..."): question_answerer = question_model() with st.spinner(text="Getting answer..."): answer = question_answerer(context=context, question=question) answer = answer["answer"] container = st.container(border=True) container.write("
Answer:
" + answer, unsafe_allow_html=True) # if upload file as input with tab2: uploaded_file = st.file_uploader("Choose a .txt file to upload", type=["txt"]) if uploaded_file is not None: raw_text = str(uploaded_file.read(),"utf-8") context = st.text_area("", value=raw_text, height=330) question = st.text_input(label="Enter your question", value=sample_question) button = st.button("Get answer") if button: with st.spinner(text="Loading question model..."): question_answerer = question_model() with st.spinner(text="Getting answer..."): answer = question_answerer(context=context, question=question) answer = answer["answer"] st.success(answer)