Spaces:
Running
Running
import os | |
import streamlit as st | |
from transformers import pipeline | |
from PyPDF2 import PdfReader | |
# Function to perform question-answering | |
def question_answering(question, pdf_path): | |
pdf_reader = PdfReader(pdf_path) | |
pdf_text_with_pages = [] | |
for page_num, pdf_page in enumerate(pdf_reader.pages, start=1): | |
pdf_text = pdf_page.extract_text() | |
pdf_text_with_pages.append((page_num, pdf_text)) | |
pdf_text = "\n".join([text for _, text in pdf_text_with_pages]) | |
# Perform question-answering using Hugging Face's Transformers | |
question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad", tokenizer="distilbert-base-cased-distilled-squad") | |
answer = question_answerer(question=question, context=pdf_text) | |
return answer | |
def main(): | |
st.title("Question Answering on a PDF File") | |
uploaded_file = st.file_uploader("Upload a PDF file:", type=["pdf"]) | |
question = st.text_input("Ask your question:") | |
if st.button("Answer") and uploaded_file is not None: | |
pdf_path = os.path.join(os.getcwd(), uploaded_file.name) | |
with open(pdf_path, "wb") as f: | |
f.write(uploaded_file.read()) | |
answer = question_answering(question, pdf_path) | |
# Delete the uploaded file after processing | |
os.remove(pdf_path) | |
st.write(f"Question: '{question}'") | |
st.write("Answer:", answer['answer']) | |
st.write("Score:", answer['score']) | |
if __name__ == "__main__": | |
main() |