CoolFace
Apppublic

MaawaKhalid/Extractive-QA-Bot

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py53 linesDownload Raw Back to root
1import streamlit as st2import torch3from transformers import pipeline4from transformers import AutoModelForQuestionAnswering5from transformers import AutoTokenizer6 7# Replace with your Hugging Face model repository path8model_repo_path = 'MaawaKhalid/Extractive-QA-Model'9 10# Load the model and tokenizer11model = AutoModelForQuestionAnswering.from_pretrained(model_repo_path)12tokenizer = AutoTokenizer.from_pretrained(model_repo_path)13 14# Initialize the question-answering pipeline15question_answerer = pipeline("question-answering", model=model, tokenizer=tokenizer)16 17# Streamlit app layout18st.title("Question Answering Bot")19 20# Define session state keys21if 'context' not in st.session_state:22    st.session_state.context = ""23if 'question' not in st.session_state:24    st.session_state.question = ""25 26# User input27text_input = st.text_area("Enter the Context first", value=st.session_state.context, height=180)28 29# Update session state with the new context30if st.button("Next"):31    if text_input:32        st.session_state.context = text_input33        st.session_state.question = ""  # Clear the previous question34    else:35        st.warning("Please enter some context to move to next part.")36 37# Show question input if context is available38if st.session_state.context:39    question_input = st.text_area("Enter the Question", value=st.session_state.question, height=100)40    41    if st.button("Answer"):42        if question_input:43            st.session_state.question = question_input44            with st.spinner("Generating Answer..."):45                try:46                    answer = question_answerer(question=question_input, context=st.session_state.context)47                    # Display the answer48                    st.subheader("Answer")49                    st.write(answer.get('answer', 'No answer found'))50                except Exception as e:51                    st.error(f"Error during Question Answering: {e}")52        else:53            st.warning("Please enter some Question to get Answer.")