Darpan07 commited on
Commit
4cc2c20
·
verified ·
1 Parent(s): c636c04

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # importing necessary libraries
2
+ import os
3
+ import time
4
+ import streamlit as st
5
+ from dotenv import load_dotenv
6
+ from PyPDF2 import PdfReader
7
+ from langchain_openai import OpenAI, OpenAIEmbeddings
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain.chains import LLMChain
10
+ from langchain.memory import ConversationBufferWindowMemory
11
+ from langchain.text_splitter import CharacterTextSplitter
12
+ from langchain_community.vectorstores import FAISS
13
+
14
+
15
+ # load the environment variables into the python script
16
+ load_dotenv()
17
+ # fetching the openai_api_key environment variable
18
+ openai_api_key = os.getenv('OPENAI_API_KEY')
19
+
20
+
21
+ # Initialize session states
22
+ if 'vectorDB' not in st.session_state:
23
+ st.session_state.vectorDB = None
24
+ if "messages" not in st.session_state:
25
+ st.session_state.messages = []
26
+ if 'bot_name' not in st.session_state:
27
+ st.session_state.bot_name = ''
28
+ if 'chain' not in st.session_state:
29
+ st.session_state.chain = None
30
+
31
+
32
+ def get_pdf_text(pdf) -> str:
33
+ """ This function extracts the text from the PDF file """
34
+ text = ""
35
+ pdf_reader = PdfReader(pdf)
36
+ for page in pdf_reader.pages:
37
+ text += page.extract_text()
38
+
39
+ return text
40
+
41
+ def get_vectorstore(text_chunks):
42
+ """ This function will create a vector database as well as create and store the embedding of the text chunks into the VectorDB """
43
+ embeddings = OpenAIEmbeddings()
44
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
45
+ return vectorstore
46
+
47
+ def get_text_chunks(text: str):
48
+ """ This function will split the text into the smaller chunks"""
49
+ text_splitter = CharacterTextSplitter(
50
+ separator="\n",
51
+ chunk_size=1000,
52
+ chunk_overlap=264,
53
+ length_function=len
54
+ )
55
+ chunks = text_splitter.split_text(text)
56
+ return chunks
57
+
58
+ def processing(pdf):
59
+ """This function divides the PDF into smaller chunks and saves these segmented chunks in a vector database. And return the Vector Database"""
60
+ # getting all the raw text from the PDF
61
+ raw_text = get_pdf_text(pdf)
62
+
63
+ # divinding the raw text into smaller chunks
64
+ text_chunks = get_text_chunks(raw_text)
65
+
66
+ # Creating and storing the chunks in vector database
67
+ vectorDB = get_vectorstore(text_chunks)
68
+
69
+ return vectorDB
70
+
71
+ def get_response(query: str) -> str:
72
+ # getting the context from the database that is similar to the user query
73
+ query_context = st.session_state.vectorDB.similarity_search(query=query,k=2)
74
+ # calling the chain to get the output from the LLM
75
+ response = st.session_state.chain.invoke({'human_input':query,'context':query_context,'name':st.session_state.bot_name})['text']
76
+ # Iterate through each word in the 'response' string after splitting it based on whitespace
77
+ for word in response.split():
78
+ # Yield the current word followed by a space, effectively creating a generator
79
+ yield word + " "
80
+
81
+ # Pause execution for 0.05 seconds (50 milliseconds) to introduce a delay
82
+ time.sleep(0.05)
83
+
84
+ def get_conversation_chain(vectorDB):
85
+ # using OPENAI LLM
86
+ llm = OpenAI(temperature=0.5)
87
+
88
+ # creating a template to pass into LLM
89
+ template = """You are a Personalized ChatBot with a name: {name} for a company's customer support system, aiming to enhance the customer experience by providing tailored assistance and information. You are interacting with customer.
90
+
91
+ Answer the question as detailed as possible from the context: {context}\n , and make sure to provide all the details, if the answer is not in the provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
92
+
93
+ {chat_history}
94
+ Human: {human_input}
95
+ AI: """
96
+
97
+ # creating a prompt that is used to format the input of the user
98
+ prompt = PromptTemplate(template = template,input_variables=['chat_history','human_input','name','context'])
99
+
100
+ # creating a memory that will store the chat history between chatbot and user
101
+ memory = ConversationBufferWindowMemory(memory_key='chat_history',input_key="human_input",k=5)
102
+
103
+ chain = LLMChain(llm=llm,prompt=prompt,memory=memory,verbose=True)
104
+
105
+ return chain
106
+
107
+
108
+
109
+ if __name__ =='__main__':
110
+ #setting the config of WebPage
111
+ st.set_page_config(page_title="Personalized ChatBot",page_icon="🤖")
112
+ st.header('Personalized Customer Support Chatbot 🤖',divider='rainbow')
113
+
114
+ # taking input( bot name and pdf file) from the user
115
+ with st.sidebar:
116
+ st.caption('Please enter the **Bot Name** and Upload **PDF** File!')
117
+
118
+ bot_name = st.text_input(label='Bot Name',placeholder='Enter the bot name here....',key="bot_name")
119
+ file = st.file_uploader("Upload a PDF file!",type='pdf')
120
+
121
+ # moving forward only when both the inputs are given by the user
122
+ if file and bot_name:
123
+ # the Process File button will process the pdf file and save the chunks into the vector database
124
+ if st.button('Process File'):
125
+ # if there is existing chat history we will delete it
126
+ if st.session_state.messages != []:
127
+ st.session_state.messages = []
128
+
129
+ with st.spinner('Processing.....'):
130
+ st.session_state['vectorDB'] = processing(file)
131
+ st.session_state['chain'] = get_conversation_chain(st.session_state['vectorDB'])
132
+ st.write('File Processed')
133
+
134
+
135
+ # if the vector database is ready to use then only show the chatbot interface
136
+ if st.session_state.vectorDB:
137
+ # Display chat messages from history on app rerun
138
+ for message in st.session_state.messages:
139
+ with st.chat_message(message["role"]):
140
+ st.write(message["content"])
141
+
142
+ # taking the input i.e. query from the user (walrun operator)
143
+ if prompt := st.chat_input(f"Message {st.session_state.bot_name}"):
144
+ # Add user message to chat history
145
+ st.session_state.messages.append({"role": "user", "content": prompt})
146
+ # Display user message in chat message container
147
+ with st.chat_message("user"):
148
+ st.write(prompt)
149
+
150
+ # Display assistant response in chat message container
151
+ with st.chat_message("assistant"):
152
+ response = st.write_stream(get_response(prompt))
153
+ # Add assistant response to chat history
154
+ st.session_state.messages.append({"role": "assistant", "content": response})