PdfChats / app.py
Vaishvik1618's picture
Upload 2 files
df997fe verified
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.")