hunterXdk commited on
Commit
50e4be7
1 Parent(s): 34001f9

Initial Commit With ❤

Browse files
Files changed (2) hide show
  1. chatbot.py +79 -0
  2. requirements.txt +7 -0
chatbot.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def get_pdf_text(pdf_docs):
2
+ text = ""
3
+ for pdf in pdf_docs:
4
+ pdf_reader = PdfReader(pdf)
5
+ for page in pdf_reader.pages:
6
+ text += page.extract_text()
7
+ return text
8
+
9
+ # chuck_size = 1000, chunk_overlap = 200 (for shorted PDFs)
10
+ def get_text_chunks(text):
11
+ text_splitter= RecursiveCharacterTextSplitter(
12
+ chunk_size=10000,
13
+ chunk_overlap=1000,
14
+ # length_function=len
15
+ )
16
+ chunks=text_splitter.split_text(text)
17
+ return chunks
18
+
19
+ # Converting into Vector data/store (can also be stored)
20
+ def get_vector_store(text_chunks):
21
+ # embeddings = GoogleGenerativeAIEmbeddings(model='embedding-gecko-001')
22
+ embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001')
23
+ vector_store = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
24
+ vector_store.save_local("faiss_index")
25
+ # return vector_store
26
+
27
+ def get_conversation_chain():
28
+ prompt_template="""Answer the query as detailed as possible from the provided context, make sure to provide all the details, if answeris not in
29
+ the provided context, just say, "Answer is not available in the provided documents", don't provide the wrong answer:\n {context}? \n Query: {query}? \n
30
+ Answer:
31
+ """
32
+
33
+ model=ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
34
+ prompt=PromptTemplate(template=prompt_template, input_variables=["context", "query"])
35
+ # chain=load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
36
+ chain=load_qa_chain(model, chain_type="stuff", prompt=prompt)
37
+ return chain
38
+
39
+ def user_input(user_question):
40
+ # embeddings = GoogleGenerativeAIEmbeddings(model='embedding-gecko-001')
41
+ embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001')
42
+
43
+ # Loading the embeddings
44
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
45
+ docs = new_db.similarity_search(user_question)
46
+
47
+ chain=get_conversation_chain()
48
+
49
+ response = chain(
50
+ {"input_documents": docs, "question": user_question}
51
+ , return_only_outputs=True)
52
+
53
+ print(response)
54
+ st.write("Reply: ", response["output_text"])
55
+
56
+ # Frontend page Processor
57
+ def main():
58
+ st.set_page_config(page_title="PDF Chatbot")
59
+ st.header("PDF Chatbot made with ❤")
60
+
61
+ user_question = st.text_input("Ask a question about your documents:")
62
+
63
+ if user_question:
64
+ user_input(user_question)
65
+
66
+ with st.sidebar:
67
+ st.title("Menu:")
68
+ pdf_docs = st.file_uploader(
69
+ "Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
70
+ if st.button("Submit & Process"):
71
+ with st.spinner("Ruko Padh raha hu..."):
72
+ raw_text = get_pdf_text(pdf_docs)
73
+ text_chunks = get_text_chunks(raw_text)
74
+ get_vector_store(text_chunks)
75
+ st.success("Saare documents padh liya. Ab swaal pucho 😤")
76
+
77
+
78
+ if __name__ == '__main__':
79
+ main()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ langchain
4
+ PyPDF2
5
+ chromadb
6
+ faiss-cpu
7
+ langchain_google_genai