|
import streamlit as st |
|
import spacy |
|
from transformers import pipeline |
|
|
|
|
|
nlp = spacy.load("en_core_web_sm") |
|
|
|
|
|
def preprocess_text(text): |
|
doc = nlp(text.lower()) |
|
tokens = [token.text for token in doc if not token.is_punct] |
|
return tokens |
|
|
|
|
|
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad") |
|
|
|
|
|
def answer_question(question, context): |
|
result = qa_model(question=question, context=context) |
|
return result['answer'] |
|
|
|
|
|
st.title("Question Answering App") |
|
st.write("Upload a text file, ask a question, and get an answer from the text!") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload a text file", type=["txt"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
data = uploaded_file.read().decode('utf-8') |
|
|
|
|
|
st.write("### File Content") |
|
st.write(data) |
|
|
|
|
|
processed_data = preprocess_text(data) |
|
|
|
|
|
question = st.text_input("Enter your question") |
|
|
|
if st.button("Get Answer"): |
|
if question: |
|
|
|
answer = answer_question(question, data) |
|
st.write(f"**Answer:** {answer}") |
|
else: |
|
st.write("Please enter a question.") |