File size: 2,110 Bytes
df997fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import fitz  # PyMuPDF
import google.generativeai as genai

# Configure the Generative AI model
genai.configure(api_key="AIzaSyBqPmsZAJrCsXme9wJBx9o4K71M9c1qzy8")

def ai_output(input_text, pdf_text, prompt):
    model = genai.GenerativeModel('gemini-1.5-flash')
    combined_input = f"{input_text}\n\n{pdf_text}\n\n{prompt}"
    response = model.generate_content(combined_input)
    return response.text

def ai_ans(input_text, prompt):
    model = genai.GenerativeModel('gemini-1.5-flash')
    combined_input = f"{input_text}\n\n{prompt}"
    response = model.generate_content(combined_input)
    return response.text

def input_pdf_setup(uploaded_file):
    if uploaded_file is not None:
        pdf_document = fitz.open(stream=uploaded_file.read(), filetype="pdf")
        text = ""
        for page_num in range(len(pdf_document)):
            page = pdf_document.load_page(page_num)
            text += page.get_text()
        pdf_document.close()
        return text
    else:
        raise FileNotFoundError("No file uploaded")

st.set_page_config(page_title="PDF Can Answer")
st.header("PDFchats")


uploaded_file = st.file_uploader("Upload your Document...", type=["pdf"])
input_text = st.text_area("Tell me! What is your question:", key="input")

if uploaded_file is not None:
    pdf_text = input_pdf_setup(uploaded_file)
    st.write("PDF file uploaded and processed successfully.")
else:
    pdf_text = ""
    st.write("Please upload the PDF.")

submit1 = st.button("Answer my question")


input_prompt1 = """

Act as an intelligent PDF reader. When a PDF document is uploaded, read and comprehend its content. Respond accurately to any questions asked, using the information found solely within the provided PDF and your data too get it in detaild explanation and also do web search too

"""


if submit1:
    if pdf_text:
        response = ai_output(input_text, pdf_text, input_prompt1)
        st.subheader("The Response is:")
        st.write(response)
    else:
        st.write("Please upload the pdf.")