Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,100 +53,6 @@ uploads_dir = os.path.join(app.root_path,'static', 'uploads')
|
|
| 53 |
|
| 54 |
os.makedirs(uploads_dir, exist_ok=True)
|
| 55 |
|
| 56 |
-
vectordb = createVectorDB(loadKB(False, False, uploads_dir, None))
|
| 57 |
-
|
| 58 |
-
@app.route('/', methods=['GET'])
|
| 59 |
-
def test():
|
| 60 |
-
return "Docker hello"
|
| 61 |
-
|
| 62 |
-
@app.route('/KBUploader')
|
| 63 |
-
def KBUpload():
|
| 64 |
-
return render_template("KBTrain.html")
|
| 65 |
-
|
| 66 |
-
@app.route('/aiassist')
|
| 67 |
-
def aiassist():
|
| 68 |
-
return render_template("index.html")
|
| 69 |
-
|
| 70 |
-
@app.route('/agent/chat/suggestion', methods=['POST'])
|
| 71 |
-
def process_json():
|
| 72 |
-
print(f"\n{'*' * 100}\n")
|
| 73 |
-
print("Request Received >>>>>>>>>>>>>>>>>>", datetime.now().strftime("%H:%M:%S"))
|
| 74 |
-
content_type = request.headers.get('Content-Type')
|
| 75 |
-
if (content_type == 'application/json'):
|
| 76 |
-
requestQuery = request.get_json()
|
| 77 |
-
print(type(requestQuery))
|
| 78 |
-
custDetailsPresent=False
|
| 79 |
-
customerName=""
|
| 80 |
-
customerDistrict=""
|
| 81 |
-
if("custDetails" in requestQuery):
|
| 82 |
-
custDetailsPresent = True
|
| 83 |
-
customerName=requestQuery['custDetails']['cName']
|
| 84 |
-
customerDistrict=requestQuery['custDetails']['cDistrict']
|
| 85 |
-
|
| 86 |
-
print("chain initiation")
|
| 87 |
-
chainRAG=getRAGChain(customerName, customerDistrict, custDetailsPresent,vectordb)
|
| 88 |
-
print("chain created")
|
| 89 |
-
suggestionArray = []
|
| 90 |
-
|
| 91 |
-
for index, query in enumerate(requestQuery['message']):
|
| 92 |
-
#message = answering(query)
|
| 93 |
-
relevantDoc = vectordb.similarity_search_with_score(query)
|
| 94 |
-
for doc in relevantDoc:
|
| 95 |
-
print(f"\n{'-' * 100}\n")
|
| 96 |
-
print("Document Source>>>>>> " + doc[len(doc) - 2].metadata['source'] + "\n\n")
|
| 97 |
-
print("Page Content>>>>>> " + doc[len(doc) - 2].page_content + "\n\n")
|
| 98 |
-
print("Similarity Score>>>> " + str(doc[len(doc) - 1]))
|
| 99 |
-
print(f"\n{'-' * 100}\n")
|
| 100 |
-
message = chainRAG.run({"query": query})
|
| 101 |
-
print("query:",query)
|
| 102 |
-
print("Response:", message)
|
| 103 |
-
if "I don't know" in message:
|
| 104 |
-
message = "Dear Sir/ Ma'am, Could you please ask questions relevant to Jio?"
|
| 105 |
-
responseJSON={"message":message,"id":index}
|
| 106 |
-
suggestionArray.append(responseJSON)
|
| 107 |
-
return jsonify(suggestions=suggestionArray)
|
| 108 |
-
else:
|
| 109 |
-
return 'Content-Type not supported!'
|
| 110 |
-
|
| 111 |
-
@app.route('/file_upload', methods=['POST'])
|
| 112 |
-
def file_Upload():
|
| 113 |
-
fileprovided = not request.files.getlist('files[]')[0].filename == ''
|
| 114 |
-
urlProvided = not request.form.getlist('weburl')[0] == ''
|
| 115 |
-
print("*******")
|
| 116 |
-
print("File Provided:" + str(fileprovided))
|
| 117 |
-
print("URL Provided:" + str(urlProvided))
|
| 118 |
-
print("*******")
|
| 119 |
-
|
| 120 |
-
print(uploads_dir)
|
| 121 |
-
documents = loadKB(fileprovided, urlProvided, uploads_dir, request)
|
| 122 |
-
vectordb=createVectorDB(documents)
|
| 123 |
-
return render_template("index.html")
|
| 124 |
-
|
| 125 |
-
def createPrompt(cName, cCity, custDetailsPresent):
|
| 126 |
-
cProfile = "Customer's Name is " + cName + "\nCustomer's lives in or customer's Resident State or Customer's place is " + cCity + "\n"
|
| 127 |
-
print(cProfile)
|
| 128 |
-
|
| 129 |
-
template1 = """You role is of a Professional Customer Support Executive and your name is Jio AIAssist.
|
| 130 |
-
You are talking to the below customer whose information is provided in block delimited by <cp></cp>.
|
| 131 |
-
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:
|
| 132 |
-
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 133 |
-
Use the customer information to replace entities in the question before answering\n
|
| 134 |
-
\n"""
|
| 135 |
-
|
| 136 |
-
template2 = """
|
| 137 |
-
<ctx>
|
| 138 |
-
{context}
|
| 139 |
-
</ctx>
|
| 140 |
-
<hs>
|
| 141 |
-
{history}
|
| 142 |
-
</hs>
|
| 143 |
-
Question: {question}
|
| 144 |
-
Answer: """
|
| 145 |
-
|
| 146 |
-
prompt_template = template1 + "<cp>\n" + cProfile + "\n</cp>\n" + template2
|
| 147 |
-
PROMPT = PromptTemplate(template=prompt_template, input_variables=["history", "context", "question"])
|
| 148 |
-
return PROMPT
|
| 149 |
-
|
| 150 |
|
| 151 |
def pretty_print_docs(docs):
|
| 152 |
print(f"\n{'-' * 100}\n".join([f"Document {i + 1}:\n\n" + "Document Length>>>" + str(
|
|
@@ -236,6 +142,99 @@ def createVectorDB(documents):
|
|
| 236 |
vectordb = Chroma.from_documents(texts, embeddings)
|
| 237 |
return vectordb
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
if __name__ == '__main__':
|
| 241 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 53 |
|
| 54 |
os.makedirs(uploads_dir, exist_ok=True)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def pretty_print_docs(docs):
|
| 58 |
print(f"\n{'-' * 100}\n".join([f"Document {i + 1}:\n\n" + "Document Length>>>" + str(
|
|
|
|
| 142 |
vectordb = Chroma.from_documents(texts, embeddings)
|
| 143 |
return vectordb
|
| 144 |
|
| 145 |
+
def createPrompt(cName, cCity, custDetailsPresent):
|
| 146 |
+
cProfile = "Customer's Name is " + cName + "\nCustomer's lives in or customer's Resident State or Customer's place is " + cCity + "\n"
|
| 147 |
+
print(cProfile)
|
| 148 |
+
|
| 149 |
+
template1 = """You role is of a Professional Customer Support Executive and your name is Jio AIAssist.
|
| 150 |
+
You are talking to the below customer whose information is provided in block delimited by <cp></cp>.
|
| 151 |
+
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:
|
| 152 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 153 |
+
Use the customer information to replace entities in the question before answering\n
|
| 154 |
+
\n"""
|
| 155 |
+
|
| 156 |
+
template2 = """
|
| 157 |
+
<ctx>
|
| 158 |
+
{context}
|
| 159 |
+
</ctx>
|
| 160 |
+
<hs>
|
| 161 |
+
{history}
|
| 162 |
+
</hs>
|
| 163 |
+
Question: {question}
|
| 164 |
+
Answer: """
|
| 165 |
+
|
| 166 |
+
prompt_template = template1 + "<cp>\n" + cProfile + "\n</cp>\n" + template2
|
| 167 |
+
PROMPT = PromptTemplate(template=prompt_template, input_variables=["history", "context", "question"])
|
| 168 |
+
return PROMPT
|
| 169 |
+
|
| 170 |
+
vectordb = createVectorDB(loadKB(False, False, uploads_dir, None))
|
| 171 |
+
|
| 172 |
+
@app.route('/', methods=['GET'])
|
| 173 |
+
def test():
|
| 174 |
+
return "Docker hello"
|
| 175 |
+
|
| 176 |
+
@app.route('/KBUploader')
|
| 177 |
+
def KBUpload():
|
| 178 |
+
return render_template("KBTrain.html")
|
| 179 |
+
|
| 180 |
+
@app.route('/aiassist')
|
| 181 |
+
def aiassist():
|
| 182 |
+
return render_template("index.html")
|
| 183 |
+
|
| 184 |
+
@app.route('/agent/chat/suggestion', methods=['POST'])
|
| 185 |
+
def process_json():
|
| 186 |
+
print(f"\n{'*' * 100}\n")
|
| 187 |
+
print("Request Received >>>>>>>>>>>>>>>>>>", datetime.now().strftime("%H:%M:%S"))
|
| 188 |
+
content_type = request.headers.get('Content-Type')
|
| 189 |
+
if (content_type == 'application/json'):
|
| 190 |
+
requestQuery = request.get_json()
|
| 191 |
+
print(type(requestQuery))
|
| 192 |
+
custDetailsPresent=False
|
| 193 |
+
customerName=""
|
| 194 |
+
customerDistrict=""
|
| 195 |
+
if("custDetails" in requestQuery):
|
| 196 |
+
custDetailsPresent = True
|
| 197 |
+
customerName=requestQuery['custDetails']['cName']
|
| 198 |
+
customerDistrict=requestQuery['custDetails']['cDistrict']
|
| 199 |
+
|
| 200 |
+
print("chain initiation")
|
| 201 |
+
chainRAG=getRAGChain(customerName, customerDistrict, custDetailsPresent,vectordb)
|
| 202 |
+
print("chain created")
|
| 203 |
+
suggestionArray = []
|
| 204 |
+
|
| 205 |
+
for index, query in enumerate(requestQuery['message']):
|
| 206 |
+
#message = answering(query)
|
| 207 |
+
relevantDoc = vectordb.similarity_search_with_score(query)
|
| 208 |
+
for doc in relevantDoc:
|
| 209 |
+
print(f"\n{'-' * 100}\n")
|
| 210 |
+
print("Document Source>>>>>> " + doc[len(doc) - 2].metadata['source'] + "\n\n")
|
| 211 |
+
print("Page Content>>>>>> " + doc[len(doc) - 2].page_content + "\n\n")
|
| 212 |
+
print("Similarity Score>>>> " + str(doc[len(doc) - 1]))
|
| 213 |
+
print(f"\n{'-' * 100}\n")
|
| 214 |
+
message = chainRAG.run({"query": query})
|
| 215 |
+
print("query:",query)
|
| 216 |
+
print("Response:", message)
|
| 217 |
+
if "I don't know" in message:
|
| 218 |
+
message = "Dear Sir/ Ma'am, Could you please ask questions relevant to Jio?"
|
| 219 |
+
responseJSON={"message":message,"id":index}
|
| 220 |
+
suggestionArray.append(responseJSON)
|
| 221 |
+
return jsonify(suggestions=suggestionArray)
|
| 222 |
+
else:
|
| 223 |
+
return 'Content-Type not supported!'
|
| 224 |
+
|
| 225 |
+
@app.route('/file_upload', methods=['POST'])
|
| 226 |
+
def file_Upload():
|
| 227 |
+
fileprovided = not request.files.getlist('files[]')[0].filename == ''
|
| 228 |
+
urlProvided = not request.form.getlist('weburl')[0] == ''
|
| 229 |
+
print("*******")
|
| 230 |
+
print("File Provided:" + str(fileprovided))
|
| 231 |
+
print("URL Provided:" + str(urlProvided))
|
| 232 |
+
print("*******")
|
| 233 |
+
|
| 234 |
+
print(uploads_dir)
|
| 235 |
+
documents = loadKB(fileprovided, urlProvided, uploads_dir, request)
|
| 236 |
+
vectordb=createVectorDB(documents)
|
| 237 |
+
return render_template("index.html")
|
| 238 |
|
| 239 |
if __name__ == '__main__':
|
| 240 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|