Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
-
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def load_dependencies():
|
| 9 |
-
import google.generativeai as genai
|
| 10 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 11 |
-
from langchain.vectorstores import FAISS
|
| 12 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 13 |
-
from langchain.prompts import PromptTemplate
|
| 14 |
-
|
| 15 |
load_dotenv()
|
| 16 |
-
os.getenv("GOOGLE_API_KEY")
|
| 17 |
|
| 18 |
def get_pdf_text(pdf_docs):
|
|
|
|
| 19 |
text = ""
|
| 20 |
for pdf in pdf_docs:
|
| 21 |
pdf_reader = PdfReader(pdf)
|
|
@@ -23,18 +17,33 @@ def get_pdf_text(pdf_docs):
|
|
| 23 |
text += page.extract_text()
|
| 24 |
return text
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Load API key from .env file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
load_dotenv()
|
| 9 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 10 |
|
| 11 |
def get_pdf_text(pdf_docs):
|
| 12 |
+
"""Extracts text from uploaded PDF files"""
|
| 13 |
text = ""
|
| 14 |
for pdf in pdf_docs:
|
| 15 |
pdf_reader = PdfReader(pdf)
|
|
|
|
| 17 |
text += page.extract_text()
|
| 18 |
return text
|
| 19 |
|
| 20 |
+
def get_gemini_response(question, context):
|
| 21 |
+
"""Sends question and context to Gemini for an answer"""
|
| 22 |
+
prompt = f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"
|
| 23 |
+
response = genai.generate_text(prompt=prompt)
|
| 24 |
+
return response.generations[0].text
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
st.set_page_config(page_title="Chat with PDF")
|
| 28 |
+
st.header("Chat with PDF using Gemini 💁")
|
| 29 |
+
|
| 30 |
+
# Text input for user's question
|
| 31 |
+
user_question = st.text_input("Ask a Question from the PDF Files")
|
| 32 |
|
| 33 |
+
# PDF upload section
|
| 34 |
+
with st.sidebar:
|
| 35 |
+
st.title("Menu:")
|
| 36 |
+
pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True)
|
| 37 |
|
| 38 |
+
if pdf_docs and st.button("Submit & Process"):
|
| 39 |
+
with st.spinner("Processing PDFs..."):
|
| 40 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 41 |
+
st.success("PDF content processed successfully!")
|
| 42 |
|
| 43 |
+
if user_question and pdf_docs:
|
| 44 |
+
st.write("Fetching answer...")
|
| 45 |
+
response = get_gemini_response(user_question, raw_text)
|
| 46 |
+
st.write("Reply:", response)
|
| 47 |
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
main()
|