|
import numpy as np |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
from utils.convert_embedding import GetEmbedding |
|
import random |
|
import pickle |
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process(user_query:str): |
|
|
|
user_embedding = GetEmbedding([user_query]).user_query_emb() |
|
with open(r"all_mix_embedding.pkl","rb") as f: |
|
load_embedding = pickle.load(f) |
|
|
|
with open(r"all_answers.pkl","rb") as f: |
|
ans = pickle.load(f) |
|
similarity_scores = cosine_similarity(user_embedding, load_embedding) |
|
index = np.argmax(similarity_scores) |
|
answer = ans[index] |
|
print(f"{index}:\t {user_query}") |
|
score = similarity_scores[0,index] |
|
if score > 50 : |
|
final_output = random.choice(answer) |
|
else: |
|
final_output = "Sorry, I didn't understand." |
|
|
|
return final_output |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|