import transformers import streamlit as st from annotated_text import annotated_text from streamlit_lottie import st_lottie import requests def load_lottieurl(url: str): r = requests.get(url) #Make a request to a web page, and return the status code: if r.status_code != 200: #200 is the HTTP status code for "OK", a successful response. return None return r.json() @st.cache(allow_output_mutation=True, show_spinner=False) def get_pipe(): tokenizer = transformers.AutoTokenizer.from_pretrained("deepset/roberta-base-squad2") model = transformers.AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2") pipe = transformers.pipeline("question-answering", model=model, tokenizer=tokenizer) return pipe def parse_context(context, prediction): parsed_context = [] parsed_context.append(context[:prediction["start"]]) parsed_context.append((prediction["answer"], "ANSWER", "#afa")) parsed_context.append(context[prediction["end"]:]) return parsed_context st.set_page_config( page_title="Question And Answering", page_icon="✨", layout="centered", initial_sidebar_state="auto", menu_items={ #Configure the menu that appears on the top-right side of this app. 'About': 'https://www.linkedin.com/in/harsh-kashyap-79b87b193/', #A markdown string to show in the About dialog. Used my linkedIn id } ) dashboard1 = load_lottieurl("https://assets1.lottiefiles.com/packages/lf20_au4zdsr8.json") #get the animated gif from file st_lottie(dashboard1, key="Dashboard1", height=400) #change the size to height 400 st.title("Deep Question and Answering System 🗣") st.markdown("##") st.write("Enter context and a question and press 'Predict' to extract the answer from the context.") default_context = "My name is Harsh and I live in Patiala." default_question = "What is my name?" context = st.text_area("Enter context here:", value=default_context) question = st.text_input("Enter your Question: 🙋", value=default_question) submit = st.button('Predict') with st.spinner(f"Getting your Answer... 💫"): pipe = get_pipe() if (submit and len(context.strip()) > 0 and len(question.strip()) > 0) or \ (len(context.strip()) > 0 and len(question.strip()) > 0): prediction = pipe(question, context) parsed_context = parse_context(context, prediction) st.markdown("Here's the answer 🗣") annotated_text(*parsed_context) st.balloons() st.markdown("

Made with ❤️ by Harsh Kashyap

", unsafe_allow_html=True)