File size: 1,337 Bytes
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
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):
    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)