File size: 1,552 Bytes
eb66dcb
 
 
 
 
 
 
 
 
f631f81
 
 
 
 
 
 
 
 
 
 
 
 
eb66dcb
5916e02
f631f81
eb66dcb
7fa286a
eb66dcb
 
7fa286a
eb66dcb
 
 
 
d7b8d59
7fa286a
 
 
 
 
 
 
eb66dcb
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from utils.convert_embedding import GetEmbedding
import random
import pickle
import os 



# 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:str):
    # dump_user_question(user_query)
    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
    # 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)