Update app.py
Browse files
app.py
CHANGED
|
@@ -68,11 +68,6 @@ def getEmbeddingModel(embeddingId):
|
|
| 68 |
return OpenAIEmbeddings()
|
| 69 |
|
| 70 |
|
| 71 |
-
def getLLMModel(LLMID):
|
| 72 |
-
llm = OpenAI(temperature=0.0)
|
| 73 |
-
return llm
|
| 74 |
-
|
| 75 |
-
|
| 76 |
def clearKBUploadDirectory(uploads_dir):
|
| 77 |
for filename in os.listdir(uploads_dir):
|
| 78 |
file_path = os.path.join(uploads_dir, filename)
|
|
@@ -86,90 +81,11 @@ def clearKBUploadDirectory(uploads_dir):
|
|
| 86 |
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
| 87 |
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# Read and Embed New Files provided
|
| 95 |
-
for file in request.files.getlist('files[]'):
|
| 96 |
-
print("File Received>>>" + file.filename)
|
| 97 |
-
file.save(os.path.join(uploads_dir, secure_filename(file.filename)))
|
| 98 |
-
loader = PyPDFLoader(os.path.join(uploads_dir, secure_filename(file.filename)))
|
| 99 |
-
documents.extend(loader.load())
|
| 100 |
-
else:
|
| 101 |
-
loader = TextLoader('Jio.txt')
|
| 102 |
-
documents.extend(loader.load())
|
| 103 |
-
|
| 104 |
-
if urlProvided:
|
| 105 |
-
weburl = request.form.getlist('weburl')
|
| 106 |
-
print(weburl)
|
| 107 |
-
urlList = weburl[0].split(';')
|
| 108 |
-
print(urlList)
|
| 109 |
-
print("Selenium Started", datetime.now().strftime("%H:%M:%S"))
|
| 110 |
-
# urlLoader=RecursiveUrlLoader(urlList[0])
|
| 111 |
-
urlLoader = SeleniumURLLoader(urlList)
|
| 112 |
-
print("Selenium Completed", datetime.now().strftime("%H:%M:%S"))
|
| 113 |
-
documents.extend(urlLoader.load())
|
| 114 |
-
print("inside selenium loader:")
|
| 115 |
-
print(documents)
|
| 116 |
-
|
| 117 |
-
return documents
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def getRAGChain(customerName,customerDistrict, custDetailsPresent,vectordb):
|
| 121 |
-
chain = RetrievalQA.from_chain_type(
|
| 122 |
-
llm=getLLMModel(0),
|
| 123 |
-
chain_type='stuff',
|
| 124 |
-
retriever=vectordb.as_retriever(),
|
| 125 |
-
verbose=False,
|
| 126 |
-
chain_type_kwargs={
|
| 127 |
-
"verbose": False,
|
| 128 |
-
"prompt": createPrompt(customerName, customerDistrict, custDetailsPresent),
|
| 129 |
-
"memory": ConversationBufferWindowMemory(
|
| 130 |
-
k=3,
|
| 131 |
-
memory_key="history",
|
| 132 |
-
input_key="question"),
|
| 133 |
-
}
|
| 134 |
-
)
|
| 135 |
-
return chain
|
| 136 |
-
|
| 137 |
-
def createVectorDB(documents):
|
| 138 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=150)
|
| 139 |
-
texts = text_splitter.split_documents(documents)
|
| 140 |
-
print("All chunk List START ***********************\n\n")
|
| 141 |
-
pretty_print_docs(texts)
|
| 142 |
-
print("All chunk List END ***********************\n\n")
|
| 143 |
-
embeddings = getEmbeddingModel(0)
|
| 144 |
-
vectordb = Chroma.from_documents(texts, embeddings)
|
| 145 |
-
return vectordb
|
| 146 |
-
|
| 147 |
-
def createPrompt(cName, cCity, custDetailsPresent):
|
| 148 |
-
cProfile = "Customer's Name is " + cName + "\nCustomer's lives in or customer's Resident State or Customer's place is " + cCity + "\n"
|
| 149 |
-
print(cProfile)
|
| 150 |
-
|
| 151 |
-
template1 = """You role is of a Professional Customer Support Executive and your name is Jio AIAssist.
|
| 152 |
-
You are talking to the below customer whose information is provided in block delimited by <cp></cp>.
|
| 153 |
-
Use the following customer related information (delimited by <cp></cp>) and context (delimited by <ctx></ctx>) to answer the question at the end by thinking step by step alongwith reaonsing steps:
|
| 154 |
-
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 155 |
-
Use the customer information to replace entities in the question before answering\n
|
| 156 |
-
\n"""
|
| 157 |
-
|
| 158 |
-
template2 = """
|
| 159 |
-
<ctx>
|
| 160 |
-
{context}
|
| 161 |
-
</ctx>
|
| 162 |
-
<hs>
|
| 163 |
-
{history}
|
| 164 |
-
</hs>
|
| 165 |
-
Question: {question}
|
| 166 |
-
Answer: """
|
| 167 |
-
|
| 168 |
-
prompt_template = template1 + "<cp>\n" + cProfile + "\n</cp>\n" + template2
|
| 169 |
-
PROMPT = PromptTemplate(template=prompt_template, input_variables=["history", "context", "question"])
|
| 170 |
-
return PROMPT
|
| 171 |
-
|
| 172 |
-
vectordb = createVectorDB(loadKB(False, False, uploads_dir, None))
|
| 173 |
|
| 174 |
@app.route('/', methods=['GET'])
|
| 175 |
def test():
|
|
@@ -278,7 +194,7 @@ def file_Upload():
|
|
| 278 |
global vectordb
|
| 279 |
#vectordb = Chroma.from_documents(texts,embeddings)
|
| 280 |
vectordb=Chroma.from_documents(documents=texts, embedding=embeddings, collection_metadata={"hnsw:space": "cosine"})
|
| 281 |
-
return render_template("
|
| 282 |
|
| 283 |
if __name__ == '__main__':
|
| 284 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 68 |
return OpenAIEmbeddings()
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def clearKBUploadDirectory(uploads_dir):
|
| 72 |
for filename in os.listdir(uploads_dir):
|
| 73 |
file_path = os.path.join(uploads_dir, filename)
|
|
|
|
| 81 |
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
| 82 |
|
| 83 |
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
@app.route('/', methods=['GET'])
|
| 91 |
def test():
|
|
|
|
| 194 |
global vectordb
|
| 195 |
#vectordb = Chroma.from_documents(texts,embeddings)
|
| 196 |
vectordb=Chroma.from_documents(documents=texts, embedding=embeddings, collection_metadata={"hnsw:space": "cosine"})
|
| 197 |
+
return render_template("index.html")
|
| 198 |
|
| 199 |
if __name__ == '__main__':
|
| 200 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|