File size: 1,987 Bytes
8eed8e7
 
 
 
 
 
 
 
 
 
 
 
e2e0f97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8eed8e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
import fitz  # PyMuPDF

# Function to process the uploaded PDF file
def process_pdf(uploaded_file, qa_model, tokenizer):
    # Check if file is uploaded
    if uploaded_file is not None:
        # Read the file as bytes
        file_contents = uploaded_file.read()

        # Process the PDF file
        doc = fitz.open(file_contents, filetype="pdf")
        if doc is not None:
            text = ""
            for page in doc:
                text += page.get_text()

            # Tokenize the text
            inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)

            # Perform question answering
            outputs = qa_model(**inputs)
            start_scores = outputs.start_logits
            end_scores = outputs.end_logits

            # Display the generated questions and answers
            for i, (start, end) in enumerate(zip(start_scores, end_scores)):
                answer = tokenizer.decode(inputs["input_ids"][i][start.argmax():end.argmax()+1])
                st.write("Answer:", answer)
                st.write("---")
        else:
            st.error("Error occurred while opening the PDF file.")

# Main function
def main():
    # Load the question answering model and tokenizer
    qa_model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
    tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")

    # Set title and description
    st.title("PDF QA Generator")
    st.write("Upload a PDF file and generate questions and answers!")

    # Create a sidebar for file upload
    st.sidebar.title("Upload File")
    uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=['pdf'])

    # Process the uploaded PDF file
    process_pdf(uploaded_file, qa_model, tokenizer)

if __name__ == "__main__":
    main()