test-api / main.py
LalitMahale
data push
eb66dcb
raw
history blame
1.36 kB
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from utils.convert_embedding import GetEmbedding
import random
import pickle
import os
user_query = "hi"
def dump_user_question(query):
try:
if os.path.exists:
with open(r"data\question_data.pkl","rb") as f:
que = pickle.load(f)
else:
que = []
que.append(query)
with open(r"data\question_data.pkl","wb") as f:
que = pickle.dump(que,f)
except:
with open(r"data\question_data.pkl","wb") as f:
pickle.dump([],f)
def process(user_query):
dump_user_question(user_query)
user_embedding = GetEmbedding([user_query]).user_query_emb()
with open(r"data\question_embedding_latest.pkl","rb") as f:
load_embedding = pickle.load(f)
with open(r"data\answer.pkl","rb") as f:
ans = pickle.load(f)
similarity_scores = cosine_similarity(user_embedding, load_embedding)
index = np.argmax(similarity_scores)
answer = ans[index]
return random.choice(answer)
if __name__ == "__main__":
pass
# for _ in range(3):
# user = input("How can i help you :? \n")
# result = process(user)
# print(result)
# with open(r"data\question_data.pkl","rb") as f:
# que = pickle.load(f)
# print(que)