Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import openai
|
| 2 |
import os
|
| 3 |
-
openai.api_key=os.getenv("OPENAI_API_KEY")
|
| 4 |
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
load_dotenv()
|
|
@@ -49,7 +49,7 @@ warnings.filterwarnings("ignore")
|
|
| 49 |
app = Flask(__name__, template_folder="./")
|
| 50 |
|
| 51 |
# Create a directory in a known location to save files to.
|
| 52 |
-
uploads_dir = os.path.join(app.root_path,'static', '
|
| 53 |
|
| 54 |
os.makedirs(uploads_dir, exist_ok=True)
|
| 55 |
|
|
@@ -183,6 +183,14 @@ def KBUpload():
|
|
| 183 |
def aiassist():
|
| 184 |
return render_template("index.html")
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
@app.route('/agent/chat/suggestion', methods=['POST'])
|
| 187 |
def process_json():
|
| 188 |
print(f"\n{'*' * 100}\n")
|
|
@@ -190,53 +198,87 @@ def process_json():
|
|
| 190 |
content_type = request.headers.get('Content-Type')
|
| 191 |
if (content_type == 'application/json'):
|
| 192 |
requestQuery = request.get_json()
|
| 193 |
-
print(
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
print("Document Source>>>>>> " + doc[len(doc) - 2].metadata['source'] + "\n\n")
|
| 213 |
-
print("Page Content>>>>>> " + doc[len(doc) - 2].page_content + "\n\n")
|
| 214 |
-
print("Similarity Score>>>> " + str(doc[len(doc) - 1]))
|
| 215 |
-
print(f"\n{'-' * 100}\n")
|
| 216 |
-
message = chainRAG.run({"query": query})
|
| 217 |
-
print("query:",query)
|
| 218 |
-
print("Response:", message)
|
| 219 |
-
if "I don't know" in message:
|
| 220 |
-
message = "Dear Sir/ Ma'am, Could you please ask questions relevant to Jio?"
|
| 221 |
-
responseJSON={"message":message,"id":index}
|
| 222 |
-
suggestionArray.append(responseJSON)
|
| 223 |
-
return jsonify(suggestions=suggestionArray)
|
| 224 |
else:
|
| 225 |
return 'Content-Type not supported!'
|
| 226 |
|
| 227 |
@app.route('/file_upload', methods=['POST'])
|
| 228 |
-
def file_Upload():
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
if __name__ == '__main__':
|
| 242 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 1 |
import openai
|
| 2 |
import os
|
| 3 |
+
# openai.api_key=os.getenv("OPENAI_API_KEY")
|
| 4 |
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
load_dotenv()
|
|
|
|
| 49 |
app = Flask(__name__, template_folder="./")
|
| 50 |
|
| 51 |
# Create a directory in a known location to save files to.
|
| 52 |
+
uploads_dir = os.path.join(app.root_path,'static', 'searchUploads')
|
| 53 |
|
| 54 |
os.makedirs(uploads_dir, exist_ok=True)
|
| 55 |
|
|
|
|
| 183 |
def aiassist():
|
| 184 |
return render_template("index.html")
|
| 185 |
|
| 186 |
+
@app.route('/aiSearch')
|
| 187 |
+
def html():
|
| 188 |
+
return render_template("AISearch.html")
|
| 189 |
+
|
| 190 |
+
@app.route('/searchKB')
|
| 191 |
+
def KBUpload():
|
| 192 |
+
return render_template("SearchKB.html")
|
| 193 |
+
|
| 194 |
@app.route('/agent/chat/suggestion', methods=['POST'])
|
| 195 |
def process_json():
|
| 196 |
print(f"\n{'*' * 100}\n")
|
|
|
|
| 198 |
content_type = request.headers.get('Content-Type')
|
| 199 |
if (content_type == 'application/json'):
|
| 200 |
requestQuery = request.get_json()
|
| 201 |
+
print()
|
| 202 |
+
|
| 203 |
+
relevantDoc=vectordb.similarity_search_with_score(requestQuery['query'],distance_metric="cos", k = 3)
|
| 204 |
+
searchResultArray=[]
|
| 205 |
+
for doc in relevantDoc:
|
| 206 |
+
searchResult = {}
|
| 207 |
+
print(f"\n{'-' * 100}\n")
|
| 208 |
+
searchResult['documentSource']=doc[len(doc)-2].metadata['source']
|
| 209 |
+
searchResult['pageContent']=doc[len(doc)-2].page_content
|
| 210 |
+
searchResult['similarityScore']=str(doc[len(doc)-1])
|
| 211 |
+
print(doc)
|
| 212 |
+
print("Document Source>>>>>> "+searchResult['documentSource']+"\n\n")
|
| 213 |
+
print("Page Content>>>>>> "+searchResult['pageContent']+"\n\n")
|
| 214 |
+
print("Similarity Score>>>> "+searchResult['similarityScore'])
|
| 215 |
+
print(f"\n{'-' * 100}\n")
|
| 216 |
+
searchResultArray.append(searchResult)
|
| 217 |
+
print(f"\n{'*' * 100}\n")
|
| 218 |
+
|
| 219 |
+
return jsonify(botMessage=searchResultArray)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
else:
|
| 221 |
return 'Content-Type not supported!'
|
| 222 |
|
| 223 |
@app.route('/file_upload', methods=['POST'])
|
| 224 |
+
def file_Upload():
|
| 225 |
+
fileprovided=not request.files.getlist('files[]')[0].filename==''
|
| 226 |
+
urlProvided=not request.form.getlist('weburl')[0]==''
|
| 227 |
+
print("*******")
|
| 228 |
+
print("File Provided:"+str(fileprovided))
|
| 229 |
+
print("URL Provided:"+str(urlProvided))
|
| 230 |
+
print("*******")
|
| 231 |
+
|
| 232 |
+
documents = []
|
| 233 |
+
if fileprovided:
|
| 234 |
+
#Delete Files
|
| 235 |
+
for filename in os.listdir(uploads_dir):
|
| 236 |
+
file_path = os.path.join(uploads_dir, filename)
|
| 237 |
+
print("Clearing Doc Directory. Trying to delete"+file_path)
|
| 238 |
+
try:
|
| 239 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
| 240 |
+
os.unlink(file_path)
|
| 241 |
+
elif os.path.isdir(file_path):
|
| 242 |
+
shutil.rmtree(file_path)
|
| 243 |
+
except Exception as e:
|
| 244 |
+
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
| 245 |
+
#Read and Embed New Files provided
|
| 246 |
+
for file in request.files.getlist('files[]'):
|
| 247 |
+
print("File Received>>>"+file.filename)
|
| 248 |
+
file.save(os.path.join(uploads_dir, secure_filename(file.filename)))
|
| 249 |
+
#loader = UnstructuredFileLoader(os.path.join(uploads_dir, secure_filename(file.filename)), mode='elements')
|
| 250 |
+
loader = PyPDFLoader(os.path.join(uploads_dir, secure_filename(file.filename)))
|
| 251 |
+
documents.extend(loader.load())
|
| 252 |
+
if urlProvided:
|
| 253 |
+
weburl=request.form.getlist('weburl')
|
| 254 |
+
print(weburl)
|
| 255 |
+
urlList=weburl[0].split(';')
|
| 256 |
+
print(urlList)
|
| 257 |
+
print("Selenium Started", datetime.now().strftime("%H:%M:%S"))
|
| 258 |
+
#urlLoader=RecursiveUrlLoader(urlList[0])
|
| 259 |
+
urlLoader=SeleniumURLLoader(urlList)
|
| 260 |
+
print("Selenium Completed", datetime.now().strftime("%H:%M:%S"))
|
| 261 |
+
documents.extend(urlLoader.load())
|
| 262 |
+
|
| 263 |
+
print(uploads_dir)
|
| 264 |
+
global chain;
|
| 265 |
+
|
| 266 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=150)
|
| 267 |
+
#text_splitter = CharacterTextSplitter(chunk_size=1500, chunk_overlap=150,separator="</Q>")
|
| 268 |
+
texts = text_splitter.split_documents(documents)
|
| 269 |
+
|
| 270 |
+
print("All chunk List START ***********************\n\n")
|
| 271 |
+
pretty_print_docs(texts)
|
| 272 |
+
|
| 273 |
+
print("All chunk List END ***********************\n\n")
|
| 274 |
+
|
| 275 |
+
#embeddings = OpenAIEmbeddings()
|
| 276 |
+
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
| 277 |
+
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 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("AISearch.html")
|
| 282 |
|
| 283 |
if __name__ == '__main__':
|
| 284 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|