File size: 2,897 Bytes
608ef59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import os
from tempfile import NamedTemporaryFile
from langchain.document_loaders import PyPDFLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline

# Function to save the uploaded PDF to a temporary file
def save_uploaded_file(uploaded_file):
    with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
        temp_file.write(uploaded_file.read())
        return temp_file.name

# Function to get answers from the PDF
def get_answer(question, db, model, tokenizer):
    doc = db.similarity_search(question, k=4)
    context = doc[0].page_content + doc[1].page_content + doc[2].page_content + doc[3].page_content

    # Load the model & tokenizer for question-answering
    model_name = "deepset/roberta-base-squad2"
    model = AutoModelForQuestionAnswering.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)

    # Create a question-answering pipeline
    nlp = pipeline("question-answering", model=model, tokenizer=tokenizer)

    # Prepare the input
    QA_input = {
        "question": question,
        "context": context,
    }

    # Get the answer
    result = nlp(**QA_input)

    return result["answer"]

# Streamlit UI
st.title("PDF Question Answering App")
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if uploaded_file is not None:
    # Save the uploaded file to a temporary location
    temp_file_path = save_uploaded_file(uploaded_file)

    # Load the PDF document using PyPDFLoader
    loader = PyPDFLoader(temp_file_path)
    pages = loader.load_and_split()

    # Initialize embeddings and Chroma
    embed = HuggingFaceEmbeddings()
    db = Chroma.from_documents(pages, embed)

    # Load the model & tokenizer for question-answering
    model_name = "deepset/roberta-base-squad2"
    model = AutoModelForQuestionAnswering.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)

    # Initializations
    conversation = []
    st.write("Ask your questions, and I'll provide answers:")
    
    # Continuous question-answering loop
    while True:
        question = st.text_input("Enter your question:")
        if st.button("Get Answer"):
            answer = get_answer(question, db, model, tokenizer)
            st.write("Answer:")
            st.write(answer)
            conversation.append({"question": question, "answer": answer})
        
        # Add an option to end the conversation
        if st.button("End Conversation"):
            break

    # Display the conversation history
    st.write("Conversation History:")
    for entry in conversation:
        st.write(f"Q: {entry['question']}")
        st.write(f"A: {entry['answer']}")

    # Cleanup: Delete the temporary file
    os.remove(temp_file_path)