Spaces:
Build error
Build error
Upload 7 files
Browse files- .env +1 -0
- .gitignore +2 -0
- Procfile +1 -0
- app.py +168 -0
- apppp.py +168 -0
- requirements.txt +0 -0
- setup.sh +9 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Google_API_Key='AIzaSyBPC1o6NSGFT2LumpdompngjOOzzUNwGqk'
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
.myenv
|
Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: sh setup.sh && streamlit run app.py
|
app.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
import os
|
| 5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import traceback
|
| 13 |
+
|
| 14 |
+
# Load environment variables
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
# Ensure the Google API key is loaded
|
| 18 |
+
google_api_key = os.getenv("Google_API_Key")
|
| 19 |
+
if not google_api_key:
|
| 20 |
+
raise ValueError("Google API key not found. Please check your .env file.")
|
| 21 |
+
|
| 22 |
+
genai.configure(api_key=google_api_key)
|
| 23 |
+
|
| 24 |
+
# Function to extract text from PDFs
|
| 25 |
+
def get_pdf_text(pdf_docs):
|
| 26 |
+
text = ""
|
| 27 |
+
try:
|
| 28 |
+
for pdf in pdf_docs:
|
| 29 |
+
pdf_reader = PdfReader(pdf)
|
| 30 |
+
for page in pdf_reader.pages:
|
| 31 |
+
text += page.extract_text()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"Error reading PDF files: {e}")
|
| 34 |
+
return text
|
| 35 |
+
|
| 36 |
+
# Function to split text into manageable chunks
|
| 37 |
+
def get_text_chunks(text):
|
| 38 |
+
try:
|
| 39 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
| 40 |
+
chunks = text_splitter.split_text(text)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"Error splitting text: {e}")
|
| 43 |
+
return []
|
| 44 |
+
return chunks
|
| 45 |
+
|
| 46 |
+
# Function to create an in-memory FAISS vector store
|
| 47 |
+
def get_vector_store(text_chunks):
|
| 48 |
+
try:
|
| 49 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 50 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
| 51 |
+
return vector_store
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error(f"Error creating vector store: {e}")
|
| 54 |
+
traceback.print_exc()
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
# Function to create a conversation chain with Google Generative AI
|
| 58 |
+
def get_conversational_chain():
|
| 59 |
+
try:
|
| 60 |
+
prompt_template = """
|
| 61 |
+
Answer the question as detailed as possible from the provided context. If the answer is not in
|
| 62 |
+
the provided context, say, "Answer is not available in the context." Do not provide a wrong answer.
|
| 63 |
+
|
| 64 |
+
Context:
|
| 65 |
+
{context}
|
| 66 |
+
|
| 67 |
+
Question:
|
| 68 |
+
{question}
|
| 69 |
+
|
| 70 |
+
Answer:
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
| 74 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
| 75 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
| 76 |
+
|
| 77 |
+
return chain
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error creating conversation chain: {e}")
|
| 80 |
+
traceback.print_exc()
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
# Function to process user input and provide a response
|
| 84 |
+
def user_input(user_question, vector_store):
|
| 85 |
+
try:
|
| 86 |
+
docs = vector_store.similarity_search(user_question)
|
| 87 |
+
|
| 88 |
+
chain = get_conversational_chain()
|
| 89 |
+
if chain:
|
| 90 |
+
response = chain(
|
| 91 |
+
{"input_documents": docs, "question": user_question},
|
| 92 |
+
return_only_outputs=True
|
| 93 |
+
)
|
| 94 |
+
st.markdown(f"<div style='font-size: 16px;'> 🤖 Response:: {response['output_text']}</div>", unsafe_allow_html=True)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.error(f"Error processing user input: {e}")
|
| 97 |
+
traceback.print_exc()
|
| 98 |
+
|
| 99 |
+
# Main function to handle Streamlit UI and actions
|
| 100 |
+
def main():
|
| 101 |
+
# Set page title and icon
|
| 102 |
+
st.set_page_config(page_title="📚 Chat PDF with Gemini AI", layout="centered", page_icon="📖")
|
| 103 |
+
|
| 104 |
+
# Add CSS for styling
|
| 105 |
+
st.markdown(
|
| 106 |
+
"""
|
| 107 |
+
<style>
|
| 108 |
+
.main-header {
|
| 109 |
+
font-size: 36px;
|
| 110 |
+
font-weight: bold;
|
| 111 |
+
color: #0A74DA;
|
| 112 |
+
}
|
| 113 |
+
.instruction {
|
| 114 |
+
font-size: 18px;
|
| 115 |
+
margin-bottom: 20px;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
</style>
|
| 119 |
+
""",
|
| 120 |
+
unsafe_allow_html=True
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Add header
|
| 124 |
+
st.markdown("<h1 class='main-header'>Chat with Your PDF using Gemini AI 🤖</h1>", unsafe_allow_html=True)
|
| 125 |
+
st.markdown("<p class='instruction'>Upload your PDF, ask questions, and get detailed AI responses!</p>", unsafe_allow_html=True)
|
| 126 |
+
|
| 127 |
+
# Create a 2-column layout for better structure
|
| 128 |
+
col1, col2 = st.columns([12, 2])
|
| 129 |
+
|
| 130 |
+
with col1:
|
| 131 |
+
user_question = st.text_input("🔍 Ask a Question from the PDF Files", placeholder="Type your question here...")
|
| 132 |
+
|
| 133 |
+
# Add a "Submit" button to process the question
|
| 134 |
+
if st.button("Submit"):
|
| 135 |
+
if user_question:
|
| 136 |
+
st.write("### 🧠 Thinking...")
|
| 137 |
+
# Only allow submission if vector_store is available
|
| 138 |
+
if 'vector_store' in st.session_state:
|
| 139 |
+
user_input(user_question, st.session_state.vector_store)
|
| 140 |
+
else:
|
| 141 |
+
st.error("Please upload and process a PDF file first.")
|
| 142 |
+
else:
|
| 143 |
+
st.warning("Please enter a question before submitting.")
|
| 144 |
+
|
| 145 |
+
with col2:
|
| 146 |
+
with st.sidebar:
|
| 147 |
+
st.title("📂 PDF Upload & Processing")
|
| 148 |
+
st.write("1. Upload multiple PDFs.")
|
| 149 |
+
st.write("2. Ask questions based on the content.")
|
| 150 |
+
pdf_docs = st.file_uploader("Upload PDF Files", accept_multiple_files=True, type=["pdf"])
|
| 151 |
+
|
| 152 |
+
if st.button("Submit & Process PDFs"):
|
| 153 |
+
if pdf_docs:
|
| 154 |
+
with st.spinner("📜 Extracting text and processing..."):
|
| 155 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 156 |
+
if raw_text:
|
| 157 |
+
text_chunks = get_text_chunks(raw_text)
|
| 158 |
+
if text_chunks:
|
| 159 |
+
vector_store = get_vector_store(text_chunks)
|
| 160 |
+
if vector_store:
|
| 161 |
+
# Store vector store in session state to avoid re-processing
|
| 162 |
+
st.session_state.vector_store = vector_store
|
| 163 |
+
st.success("✅ Processing complete!")
|
| 164 |
+
else:
|
| 165 |
+
st.warning("Please upload PDF files before processing.")
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
main()
|
apppp.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
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yPDF2 import PdfReader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
import os
|
| 5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import traceback
|
| 13 |
+
|
| 14 |
+
# Load environment variables
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
# Ensure the Google API key is loaded
|
| 18 |
+
google_api_key = os.getenv("Google_API_Key")
|
| 19 |
+
if not google_api_key:
|
| 20 |
+
raise ValueError("Google API key not found. Please check your .env file.")
|
| 21 |
+
|
| 22 |
+
genai.configure(api_key=google_api_key)
|
| 23 |
+
|
| 24 |
+
# Function to extract text from PDFs
|
| 25 |
+
def get_pdf_text(pdf_docs):
|
| 26 |
+
text = ""
|
| 27 |
+
try:
|
| 28 |
+
for pdf in pdf_docs:
|
| 29 |
+
pdf_reader = PdfReader(pdf)
|
| 30 |
+
for page in pdf_reader.pages:
|
| 31 |
+
text += page.extract_text()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"Error reading PDF files: {e}")
|
| 34 |
+
return text
|
| 35 |
+
|
| 36 |
+
# Function to split text into manageable chunks
|
| 37 |
+
def get_text_chunks(text):
|
| 38 |
+
try:
|
| 39 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
| 40 |
+
chunks = text_splitter.split_text(text)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"Error splitting text: {e}")
|
| 43 |
+
return []
|
| 44 |
+
return chunks
|
| 45 |
+
|
| 46 |
+
# Function to create an in-memory FAISS vector store
|
| 47 |
+
def get_vector_store(text_chunks):
|
| 48 |
+
try:
|
| 49 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 50 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
| 51 |
+
return vector_store
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error(f"Error creating vector store: {e}")
|
| 54 |
+
traceback.print_exc()
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
# Function to create a conversation chain with Google Generative AI
|
| 58 |
+
def get_conversational_chain():
|
| 59 |
+
try:
|
| 60 |
+
prompt_template = """
|
| 61 |
+
Answer the question as detailed as possible from the provided context. If the answer is not in
|
| 62 |
+
the provided context, say, "Answer is not available in the context." Do not provide a wrong answer.
|
| 63 |
+
|
| 64 |
+
Context:
|
| 65 |
+
{context}
|
| 66 |
+
|
| 67 |
+
Question:
|
| 68 |
+
{question}
|
| 69 |
+
|
| 70 |
+
Answer:
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
| 74 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
| 75 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
| 76 |
+
|
| 77 |
+
return chain
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error creating conversation chain: {e}")
|
| 80 |
+
traceback.print_exc()
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
# Function to process user input and provide a response
|
| 84 |
+
def user_input(user_question, vector_store):
|
| 85 |
+
try:
|
| 86 |
+
docs = vector_store.similarity_search(user_question)
|
| 87 |
+
|
| 88 |
+
chain = get_conversational_chain()
|
| 89 |
+
if chain:
|
| 90 |
+
response = chain(
|
| 91 |
+
{"input_documents": docs, "question": user_question},
|
| 92 |
+
return_only_outputs=True
|
| 93 |
+
)
|
| 94 |
+
st.markdown(f"<div style='font-size: 16px;'> 🤖 Response:: {response['output_text']}</div>", unsafe_allow_html=True)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.error(f"Error processing user input: {e}")
|
| 97 |
+
traceback.print_exc()
|
| 98 |
+
|
| 99 |
+
# Main function to handle Streamlit UI and actions
|
| 100 |
+
def main():
|
| 101 |
+
# Set page title and icon
|
| 102 |
+
st.set_page_config(page_title="📚 Chat PDF with Gemini AI", layout="centered", page_icon="📖")
|
| 103 |
+
|
| 104 |
+
# Add CSS for styling
|
| 105 |
+
st.markdown(
|
| 106 |
+
"""
|
| 107 |
+
<style>
|
| 108 |
+
.main-header {
|
| 109 |
+
font-size: 36px;
|
| 110 |
+
font-weight: bold;
|
| 111 |
+
color: #0A74DA;
|
| 112 |
+
}
|
| 113 |
+
.instruction {
|
| 114 |
+
font-size: 18px;
|
| 115 |
+
margin-bottom: 20px;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
</style>
|
| 119 |
+
""",
|
| 120 |
+
unsafe_allow_html=True
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Add header
|
| 124 |
+
st.markdown("<h1 class='main-header'>Chat with Your PDF using Gemini AI 🤖</h1>", unsafe_allow_html=True)
|
| 125 |
+
st.markdown("<p class='instruction'>Upload your PDF, ask questions, and get detailed AI responses!</p>", unsafe_allow_html=True)
|
| 126 |
+
|
| 127 |
+
# Create a 2-column layout for better structure
|
| 128 |
+
col1, col2 = st.columns([12, 2])
|
| 129 |
+
|
| 130 |
+
with col1:
|
| 131 |
+
user_question = st.text_input("🔍 Ask a Question from the PDF Files", placeholder="Type your question here...")
|
| 132 |
+
|
| 133 |
+
# Add a "Submit" button to process the question
|
| 134 |
+
if st.button("Submit"):
|
| 135 |
+
if user_question:
|
| 136 |
+
st.write("### 🧠 Thinking...")
|
| 137 |
+
# Only allow submission if vector_store is available
|
| 138 |
+
if 'vector_store' in st.session_state:
|
| 139 |
+
user_input(user_question, st.session_state.vector_store)
|
| 140 |
+
else:
|
| 141 |
+
st.error("Please upload and process a PDF file first.")
|
| 142 |
+
else:
|
| 143 |
+
st.warning("Please enter a question before submitting.")
|
| 144 |
+
|
| 145 |
+
with col2:
|
| 146 |
+
with st.sidebar:
|
| 147 |
+
st.title("📂 PDF Upload & Processing")
|
| 148 |
+
st.write("1. Upload multiple PDFs.")
|
| 149 |
+
st.write("2. Ask questions based on the content.")
|
| 150 |
+
pdf_docs = st.file_uploader("Upload PDF Files", accept_multiple_files=True, type=["pdf"])
|
| 151 |
+
|
| 152 |
+
if st.button("Submit & Process PDFs"):
|
| 153 |
+
if pdf_docs:
|
| 154 |
+
with st.spinner("📜 Extracting text and processing..."):
|
| 155 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 156 |
+
if raw_text:
|
| 157 |
+
text_chunks = get_text_chunks(raw_text)
|
| 158 |
+
if text_chunks:
|
| 159 |
+
vector_store = get_vector_store(text_chunks)
|
| 160 |
+
if vector_store:
|
| 161 |
+
# Store vector store in session state to avoid re-processing
|
| 162 |
+
st.session_state.vector_store = vector_store
|
| 163 |
+
st.success("✅ Processing complete!")
|
| 164 |
+
else:
|
| 165 |
+
st.warning("Please upload PDF files before processing.")
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
main()
|
requirements.txt
ADDED
|
Binary file (3.8 kB). View file
|
|
|
setup.sh
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mkdir -p ~/.streamlit/
|
| 2 |
+
|
| 3 |
+
echo "\
|
| 4 |
+
[server]\n\
|
| 5 |
+
port = $PORT\n\
|
| 6 |
+
enableCORS= false\n\
|
| 7 |
+
headless = true\n\
|
| 8 |
+
\n\
|
| 9 |
+
" > ~/.streamlit/config.toml
|