Spaces:
Sleeping
Sleeping
arnabbumba077
commited on
Commit
•
12facfe
1
Parent(s):
9d95dcd
Upload 2 files
Browse files- app.py +107 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
7 |
+
from langchain_core.prompts import ChatPromptTemplate
|
8 |
+
from langchain.chains import create_retrieval_chain
|
9 |
+
from langchain_community.vectorstores import FAISS
|
10 |
+
from langchain_community.document_loaders import PyPDFLoader
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
import tempfile
|
13 |
+
|
14 |
+
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
|
18 |
+
groq_api_key = os.getenv('GROQ_API_KEY')
|
19 |
+
|
20 |
+
|
21 |
+
st.markdown("<h2 style='text-align: center;'>PDF Insights: Interactive Q&A Chatbot with Groq API</h2>", unsafe_allow_html=True)
|
22 |
+
|
23 |
+
|
24 |
+
llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
|
25 |
+
|
26 |
+
|
27 |
+
prompt = ChatPromptTemplate.from_template(
|
28 |
+
"""
|
29 |
+
Answer the questions based on the provided context only.
|
30 |
+
Please provide the most accurate response based on the question.
|
31 |
+
<context>
|
32 |
+
{context}
|
33 |
+
<context>
|
34 |
+
Questions: {input}
|
35 |
+
"""
|
36 |
+
)
|
37 |
+
|
38 |
+
def create_vector_db_out_of_the_uploaded_pdf_file(pdf_file):
|
39 |
+
|
40 |
+
|
41 |
+
if "vector_store" not in st.session_state:
|
42 |
+
|
43 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
44 |
+
|
45 |
+
temp_file.write(pdf_file.read())
|
46 |
+
|
47 |
+
pdf_file_path = temp_file.name
|
48 |
+
|
49 |
+
st.session_state.embeddings = HuggingFaceEmbeddings(model_name='BAAI/bge-small-en-v1.5', model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': True})
|
50 |
+
|
51 |
+
st.session_state.loader = PyPDFLoader(pdf_file_path)
|
52 |
+
|
53 |
+
st.session_state.text_document_from_pdf = st.session_state.loader.load()
|
54 |
+
|
55 |
+
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
56 |
+
|
57 |
+
st.session_state.final_document_chunks = st.session_state.text_splitter.split_documents(st.session_state.text_document_from_pdf)
|
58 |
+
|
59 |
+
st.session_state.vector_store = FAISS.from_documents(st.session_state.final_document_chunks, st.session_state.embeddings)
|
60 |
+
|
61 |
+
|
62 |
+
pdf_input_from_user = st.file_uploader("Upload the PDF file", type=['pdf'])
|
63 |
+
|
64 |
+
|
65 |
+
if pdf_input_from_user is not None:
|
66 |
+
|
67 |
+
if st.button("Create the Vector DB from the uploaded PDF file"):
|
68 |
+
|
69 |
+
if pdf_input_from_user is not None:
|
70 |
+
|
71 |
+
create_vector_db_out_of_the_uploaded_pdf_file(pdf_input_from_user)
|
72 |
+
|
73 |
+
st.success("Vector Store DB for this PDF file Is Ready")
|
74 |
+
|
75 |
+
else:
|
76 |
+
|
77 |
+
st.write("Please upload a PDF file first")
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
if "vector_store" in st.session_state:
|
82 |
+
|
83 |
+
user_prompt = st.text_input("Enter Your Question related to the uploaded PDF")
|
84 |
+
|
85 |
+
if st.button('Submit Prompt'):
|
86 |
+
|
87 |
+
if user_prompt:
|
88 |
+
|
89 |
+
if "vector_store" in st.session_state:
|
90 |
+
|
91 |
+
document_chain = create_stuff_documents_chain(llm, prompt)
|
92 |
+
|
93 |
+
retriever = st.session_state.vector_store.as_retriever()
|
94 |
+
|
95 |
+
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
96 |
+
|
97 |
+
response = retrieval_chain.invoke({'input': user_prompt})
|
98 |
+
|
99 |
+
st.write(response['answer'])
|
100 |
+
|
101 |
+
else:
|
102 |
+
|
103 |
+
st.write("Please embed the document first by uploading a PDF file.")
|
104 |
+
|
105 |
+
else:
|
106 |
+
|
107 |
+
st.error('Please write your prompt')
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.2.7
|
2 |
+
langchain_community==0.2.7
|
3 |
+
langchain_core==0.2.18
|
4 |
+
langchain_groq==0.1.6
|
5 |
+
langchain_huggingface==0.0.3
|
6 |
+
python-dotenv==1.0.1
|
7 |
+
streamlit==1.12.0
|
8 |
+
pypdf==4.2.0
|
9 |
+
faiss-cpu==1.8.0.post1
|