Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +77 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain.chains import create_retrieval_chain
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
from langchain_community.document_loaders import PyPDFLoader
|
10 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
11 |
+
import tempfile
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
## Load the GROQ and Google API key
|
17 |
+
|
18 |
+
groq_api_key = os.getenv('GROQ_API_KEY')
|
19 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv('GOOGLE_API_KEY')
|
20 |
+
|
21 |
+
st.title("Gemma Model Document Q&A")
|
22 |
+
|
23 |
+
llm = ChatGroq(groq_api_key=groq_api_key, model_name="gemma2-9b-it")
|
24 |
+
|
25 |
+
prompt = ChatPromptTemplate.from_template(
|
26 |
+
"""
|
27 |
+
Answer the questions based on the provided context only.
|
28 |
+
Please provide the most accurate response based on the question
|
29 |
+
<context>
|
30 |
+
{context}
|
31 |
+
<context>
|
32 |
+
Questions: {input}
|
33 |
+
"""
|
34 |
+
)
|
35 |
+
|
36 |
+
def vector_embedding(pdf_files):
|
37 |
+
st.session_state.embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
38 |
+
docs = []
|
39 |
+
for pdf_file in pdf_files:
|
40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
41 |
+
temp_file.write(pdf_file.read())
|
42 |
+
temp_file_path = temp_file.name
|
43 |
+
loader = PyPDFLoader(temp_file_path)
|
44 |
+
docs.extend(loader.load())
|
45 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
46 |
+
final_documents = text_splitter.split_documents(docs[:20])
|
47 |
+
st.session_state.vectors = FAISS.from_documents(final_documents, st.session_state.embeddings)
|
48 |
+
|
49 |
+
# File uploader
|
50 |
+
uploaded_files = st.file_uploader("Upload PDF files", accept_multiple_files=True, type=["pdf"])
|
51 |
+
|
52 |
+
if uploaded_files and st.button("Process Uploaded Files"):
|
53 |
+
vector_embedding(uploaded_files)
|
54 |
+
st.write("Vector Store DB is Ready")
|
55 |
+
|
56 |
+
prompt1 = st.text_input("What do you want to ask from the documents?")
|
57 |
+
|
58 |
+
import time
|
59 |
+
|
60 |
+
if prompt1:
|
61 |
+
if "vectors" in st.session_state:
|
62 |
+
document_chain = create_stuff_documents_chain(llm, prompt)
|
63 |
+
retriever = st.session_state.vectors.as_retriever()
|
64 |
+
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
65 |
+
|
66 |
+
start = time.process_time()
|
67 |
+
response = retrieval_chain.invoke({'input': prompt1})
|
68 |
+
st.write(response['answer'])
|
69 |
+
|
70 |
+
# With a Streamlit expander
|
71 |
+
with st.expander("Document Similarity Search"):
|
72 |
+
# Find the relevant chunks
|
73 |
+
for i, doc in enumerate(response["context"]):
|
74 |
+
st.write(doc.page_content)
|
75 |
+
st.write("--------------------------------")
|
76 |
+
else:
|
77 |
+
st.write("Please upload and process PDF files first.")
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
faiss-cpu
|
2 |
+
groq
|
3 |
+
langchain-groq
|
4 |
+
PyPDF2
|
5 |
+
langchain_google_genai
|
6 |
+
langchain
|
7 |
+
streamlit
|
8 |
+
langchain_community
|
9 |
+
python-dotenv
|
10 |
+
pypdf
|