faldeus0092 commited on
Commit
d5b718d
1 Parent(s): 443d4b8

i hate commit

Browse files
Files changed (5) hide show
  1. app.py +160 -0
  2. flows.py +166 -0
  3. intents.py +68 -0
  4. requirements.txt +3 -0
  5. utils.py +41 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title Model Loading
2
+ from sentence_transformers import SentenceTransformer, util
3
+ from utils import get_latest_user_input_from_prompt, get_top_intents, create_embedding
4
+ from intents import intents, intents_sentence_similarity_en
5
+ import flows
6
+ import gradio as gr
7
+ import pandas as pd
8
+
9
+ model_en = SentenceTransformer("intfloat/multilingual-e5-base")
10
+ intents_embedding = create_embedding(intents_sentence_similarity_en, model_en)
11
+
12
+
13
+ def raw_inference(input, state, n_samples, threshold):
14
+ state = flows.STATE_FLOWS_MAP[state]
15
+ query_embedding = model_en.encode(input)
16
+ similarity = util.pytorch_cos_sim(query_embedding, intents_embedding)
17
+ result = get_top_intents(intents, similarity, n=n_samples, threshold=threshold, flow=state)
18
+ return result
19
+
20
+ def process_csv(files):
21
+ global df
22
+ df = pd.read_csv(files, low_memory=False)
23
+ df = df[df['chatbot_response'].isin(intents)]
24
+ df = df[["user_message","prompt", "chatbot_response", "state"]]
25
+ df.dropna(inplace=True)
26
+ df = df.reset_index()
27
+ df.drop('index', axis='columns')
28
+ df_length = len(df.index)
29
+
30
+ chat = get_latest_user_input_from_prompt(df.iloc[1]["prompt"])
31
+ state = flows.STATE_FLOWS_MAP[df.iloc[1]['state']]
32
+ label = df.iloc[1]['chatbot_response']
33
+
34
+ accuracy = gr.Markdown("""
35
+
36
+ You can also check accuracy on how well the model predict the intents based on your provided CSV files. This might take 1-2 minutes.
37
+
38
+ """, visible=True)
39
+ accuracy_button = gr.Button("Calculate Accuracy", visible=True)
40
+
41
+ return (gr.UploadButton("Upload CSV...", file_types=["file"], file_count="single", visible=False),
42
+ files,
43
+ gr.Slider(1, df_length, value=1, step=1, visible=True, label="Index", info="Select which index of data to check the intents"),
44
+ gr.Textbox(label="Input Chat", info="Input in index", visible=True, value=chat, interactive=False),
45
+ gr.Textbox(label="State", info="State on which the chat currently on. Some state will exclude some intents", visible=True, value=state, interactive=False),
46
+ gr.Textbox(label="Ground Truth", info="The label in which the IntentClassification predict in the CSV", visible=True, value=label, interactive=False))
47
+
48
+ def update_index(index):
49
+ chat = get_latest_user_input_from_prompt(df.iloc[int(index)]["prompt"])
50
+ state = df.iloc[int(index)]['state']
51
+ label = df.iloc[int(index)]['chatbot_response']
52
+ return gr.Textbox(label="Input Chat", info="Input in index", visible=True, value=chat, interactive=False), gr.Textbox(label="State", info="State on which the chat currently on. Some state will exclude some intents", visible=True, value=state, interactive=False), gr.Textbox(label="Ground Truth", info="The label in which the IntentClassification predict in the CSV", visible=True, value=label, interactive=False)
53
+
54
+ def check_accuracy(n_samples, threshold):
55
+ global df
56
+ res_list = []
57
+ for index, row in df.iterrows():
58
+ # chat = get_history_from_prompt(row["prompt"])
59
+ chat = get_latest_user_input_from_prompt(row["prompt"])
60
+ query_embedding = model_en.encode(chat)
61
+ flow = flows.STATE_FLOWS_MAP[row['state']]
62
+ similarity = util.pytorch_cos_sim(query_embedding, intents_embedding)
63
+ result = get_top_intents(intents, similarity, n=n_samples, threshold=threshold, flow=flow)
64
+
65
+ label = row['chatbot_response']
66
+ isPredictedTrue=0
67
+ for item in result:
68
+ if label in item:
69
+ isPredictedTrue=1
70
+ break
71
+ res_list.append({'state': row['state'], 'gt': label, 'isPredictedTrue': isPredictedTrue})
72
+
73
+ res_df = pd.DataFrame(res_list)
74
+
75
+ # dataframe result
76
+ grouped_data = res_df.groupby('gt')['isPredictedTrue'].agg(['sum', 'count']).reset_index()
77
+ grouped_data['percentage'] = (grouped_data['sum'] / grouped_data['count']) * 100
78
+
79
+ # accuracy score
80
+ score = (res_df['isPredictedTrue'] == 1).sum()/res_df['isPredictedTrue'].count() * 100 #raw
81
+
82
+ print(score, grouped_data)
83
+ return score, grouped_data
84
+
85
+ theme = gr.themes.Default(
86
+ primary_hue="indigo",
87
+ secondary_hue="pink",
88
+ neutral_hue="slate",
89
+ )
90
+
91
+ with gr.Blocks(title="Intent Classification Demo", theme=theme) as interface:
92
+ gr.Markdown("""# Demo for Intent Classification""")
93
+ with gr.Row(equal_height=True):
94
+ with gr.Column():
95
+ with gr.Tab("Input from raw text"):
96
+ raw_input_text = gr.Textbox(label="Input Chat", info="Input your chat here, the model will predict the intent")
97
+ raw_state = gr.Dropdown(["GeneralState",
98
+ "HomeworkState",
99
+ "ExerciseState",
100
+ "UnderstandState",
101
+ "RecommendMaterialState",
102
+ "PersonalState",
103
+ "AssessKnowledgeState"],
104
+ label="State",
105
+ info="Select state on which the chat currently on. Some state will exclude some intents")
106
+ raw_ask_button = gr.Button("Ask")
107
+
108
+ with gr.Tab("Input from Big Query data"):
109
+ gr.Markdown("""
110
+ ## Guide:
111
+
112
+ Assuming have access to BigQuery, you can query the table `silicon-airlock-153323.chatbot_ai_dwp.fact_chatbot_ai_conversation_raw`, export result as CSV file, and upload here (make sure your query contains these columns: `prompt, user_message, chatbot_response, state`)
113
+
114
+ ```SELECT prompt, user_message, chatbot_response, state FROM `silicon-airlock-153323.chatbot_ai_dwp.fact_chatbot_ai_conversation_raw` WHERE DATE(_PARTITIONTIME) BETWEEN DATE("2023-11-13") AND DATE("2023-11-19") AND service_name = 'learning_companion' LIMIT 1000```
115
+
116
+ Adjust the date according to needs. After that, export as CSV and upload to this gradio
117
+
118
+ example CSV files to use:
119
+
120
+ https://drive.google.com/file/d/1iDLywKP5JxDJXaAzomSUYLZRWvoGqpt5/view?usp=sharing
121
+
122
+ https://drive.google.com/file/d/1Jh_hP7U2JGQXsRo9OponyVSHL_s1Yx8w/view?usp=sharing
123
+
124
+ """)
125
+
126
+ file_output = gr.File()
127
+ upload_button = gr.UploadButton("Upload CSV...", file_types=["file"], file_count="single")
128
+
129
+ index = gr.Slider(1, 1000, value=5, step=1, visible=False, label="Index", info="Select which index of data to check the intents")
130
+ input_text = gr.Textbox(label="Input Chat", info="Input in index", visible=False)
131
+ state = gr.Textbox(label="State", info="State on which the chat currently on. Some state will exclude some intents", visible=False)
132
+ gt = gr.Textbox(label="Ground Truth", info="The label in which the IntentClassification predict in the CSV", visible=False)
133
+ ask_button = gr.Button("Ask With CSV")
134
+
135
+
136
+ index.change(fn=update_index, inputs=index, outputs=[input_text, state, gt])
137
+ upload_button.upload(process_csv, upload_button, [upload_button, file_output, index, input_text, state, gt])
138
+ with gr.Column():
139
+ with gr.Row():
140
+ accuracy = gr.Markdown("""
141
+
142
+ You can also check accuracy on how well the model predict the intents based on your provided CSV files. This might take 1-2 minutes.
143
+
144
+ """, visible=True)
145
+ accuracy_button = gr.Button("Calculate Accuracy", visible=True)
146
+ accuracy_score = gr.Textbox(label="Accuracy", info="Accuracy result tested on CSV file you uploaded", visible=True)
147
+ accuracy_table = gr.Dataframe(visible=True)
148
+
149
+ with gr.Column():
150
+ n_samples = gr.Slider(1, 10, value=5, step=1, label="N samples", info="Number of samples to be retrieved. Default is 5")
151
+ threshold = gr.Slider(0.0, 1.0, value=0.75, step=0.01, label="Threshold", info="Threshold of cosine similarity which intent will be considered similar to the input. The higher, the more similar the intent will be. Default is 0.75")
152
+ answer = gr.JSON(label="Prediction", show_label=True)
153
+
154
+
155
+ accuracy_button.click(fn=check_accuracy, inputs=[n_samples, threshold], outputs=[accuracy_score, accuracy_table])
156
+ raw_ask_button.click(fn=raw_inference, inputs=[raw_input_text, raw_state, n_samples, threshold], outputs=answer)
157
+ ask_button.click(fn=raw_inference, inputs=[input_text, state, n_samples, threshold], outputs=answer)
158
+
159
+
160
+ interface.launch(debug=True)
flows.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title flows.py
2
+ GENERAL_STATE_FLOWS = [
3
+ "homework",
4
+ "homework_with_question",
5
+ "recommend",
6
+ "recommend_with_topic",
7
+ "assess_knowledge",
8
+ "assess_knowledge_with_topic",
9
+ "explain_drill",
10
+ "exercise",
11
+ "exercise_with_topic",
12
+ "personal",
13
+ "finish_learning",
14
+ "provide_topic",
15
+ "provide_subject",
16
+ "provide_subtopic",
17
+ "provide_concept",
18
+ "general_intent",
19
+ "explain_answer",
20
+ "explain_answer_with_topic",
21
+ "back_to_study",
22
+ "summarize",
23
+ "unknown"
24
+ ]
25
+
26
+ UNDERSTAND_STATE_FLOWS = [
27
+ "homework",
28
+ "homework_with_question",
29
+ "provide_topic",
30
+ "provide_subject",
31
+ "explain_answer",
32
+ "explain_answer_with_topic",
33
+ "assess_knowledge",
34
+ "assess_knowledge_with_topic",
35
+ "recommend",
36
+ "recommend_with_topic",
37
+ "exercise",
38
+ "exercise_with_topic",
39
+ "exercise_multiple_question",
40
+ "exercise_multiple_question_with_topic",
41
+ "personal",
42
+ "finish_learning",
43
+ "back_to_study",
44
+ "unknown"
45
+ ]
46
+
47
+ HOMEWORK_STATE_FLOWS = ["homework_with_question",
48
+ "explain_answer",
49
+ "explain_answer_with_topic",
50
+ "provide_topic",
51
+ "provide_subject",
52
+ "homework",
53
+ "recommend",
54
+ "recommend_with_topic",
55
+ "assess_knowledge",
56
+ "assess_knowledge_with_topic",
57
+ "exercise",
58
+ "exercise_with_topic",
59
+ "exercise_multiple_question",
60
+ "exercise_multiple_question_with_topic",
61
+ "similar_question",
62
+ "personal",
63
+ "finish_learning",
64
+ "unknown",
65
+ "back_to_study",
66
+ ]
67
+
68
+ RECOMMEND_MATERIAL_FLOWS = [
69
+ "homework",
70
+ "homework_with_question",
71
+ "understand",
72
+ "understand_with_topic",
73
+ "recommend",
74
+ "recommend_with_topic",
75
+ "provide_topic",
76
+ "provide_subject",
77
+ "explain_answer",
78
+ "assess_knowledge",
79
+ "assess_knowledge_with_topic",
80
+ "exercise",
81
+ "exercise_with_topic",
82
+ "exercise_multiple_question",
83
+ "exercise_multiple_question_with_topic",
84
+ "personal",
85
+ "finish_learning",
86
+ "go_back_to_general",
87
+ "unknown"
88
+ ]
89
+
90
+ PERSONAL_STATE_FLOWS = [
91
+ "homework",
92
+ "homework_with_question",
93
+ "recommend",
94
+ "recommend_with_topic",
95
+ "assess_knowledge",
96
+ "assess_knowledge_with_topic",
97
+ "personal",
98
+ "exercise",
99
+ "exercise_with_topic",
100
+ "explain_answer",
101
+ "explain_answer_with_topic",
102
+ "provide_topic",
103
+ "provide_subject",
104
+ "back_to_study",
105
+ "finish_learning",
106
+ "unknown"
107
+ ]
108
+
109
+ EXERCISE_STATE_FLOWS = [
110
+ "exercise",
111
+ "exercise_with_image",
112
+ "exercise_with_topic",
113
+ "exercise_multiple_question",
114
+ "exercise_multiple_question_with_topic",
115
+ "provide_topic",
116
+ "provide_subject",
117
+ "explain_answer",
118
+ "explain_answer_with_topic",
119
+ "provide_answer",
120
+ "similar_question",
121
+ "assess_knowledge",
122
+ "assess_knowledge_with_topic",
123
+ "homework",
124
+ "homework_with_question",
125
+ "recommend",
126
+ "recommend_with_topic",
127
+ "finish_learning",
128
+ "personal",
129
+ "back_to_study",
130
+ "unknown"
131
+ ]
132
+
133
+ ASSESS_KNOWLEDGE_STATE_FLOWS = [
134
+ "assess_knowledge",
135
+ "assess_knowledge_with_topic",
136
+ "assess_knowledge_answer",
137
+ "explain_drill",
138
+ "provide_topic",
139
+ "provide_subject",
140
+ "diagnosis_result",
141
+ "explain_answer",
142
+ "explain_answer_with_topic",
143
+ "homework",
144
+ "homework_with_question",
145
+ "recommend",
146
+ "recommend_with_topic",
147
+ "exercise",
148
+ "exercise_with_topic",
149
+ "exercise_multiple_question",
150
+ "exercise_multiple_question_with_topic",
151
+ "general_intent",
152
+ "finish_learning",
153
+ "personal",
154
+ "back_to_study",
155
+ "unknown"
156
+ ]
157
+
158
+ STATE_FLOWS_MAP = {
159
+ "GeneralState":GENERAL_STATE_FLOWS,
160
+ "HomeworkState":HOMEWORK_STATE_FLOWS,
161
+ "ExerciseState":EXERCISE_STATE_FLOWS,
162
+ "UnderstandState":UNDERSTAND_STATE_FLOWS,
163
+ "RecommendMaterialState":RECOMMEND_MATERIAL_FLOWS,
164
+ "PersonalState":PERSONAL_STATE_FLOWS,
165
+ "AssessKnowledgeState":ASSESS_KNOWLEDGE_STATE_FLOWS,
166
+ }
intents.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title intents.py
2
+ intents_sentence_similarity_en = {
3
+ "homework":"Intent to do exercise questions or homework. (Bantu Kerjakan PR, Bantu PR,tanya soal, tanya pr)",
4
+ "homework_with_question":"Curious and asking spesific questions or homework.(Bantu Kerjakan soal ini, Bisa kerjakan soal ini, Beritahu jawabannya)",
5
+ "explain_answer":"Wants explanation/study about the spesific answer (Belajar Lebih Dalam/Ceritakan lebih lanjut/jelaskan dari awal/jelaskan/jelasin lebih lanjut/belum paham)",
6
+ "explain_answer_with_topic":"Wants explanation/study about spesific topics (Belajar Lebih Dalam terkait/Ceritakan lebih lanjut bab /jelaskan dari awal materi/jelaskan lebih lanjut pelajaran /belum paham terkait materi)",
7
+ "core_explanation_repeat":"Repeat the explanation of a question (Jelasin ulang/Jelaskan ulang/Jelaskan lagi)",
8
+ "core_explanation_exercise":"Example questions from the material explanation. (Contoh Soal/Contoh soal dong/bahas dengan soal)",
9
+ "core_explanation_video":"Explanation of the material with a video. (Video Penjelasan/Bahas dengan video/mau video penjelasan)",
10
+ "core_explanation_protips":"Explanation or questions about Tips, tricks, and effective methods (protips/tips dan trik)",
11
+ "negative_respond":"Did not understand the explanation (aku belum paham, masih belum paham, aku gangerti, aku belum mengerti, masih bingung, aku kesusahan)",
12
+ "positive_respond":"Understanding the explanation (aku sudah paham, oke paham kak, oke mengerti, oke aman, lanjut kak)",
13
+ "recommend":"Suggestions or recommendations (Rekomendasi Materi/Suggested Materials/Materi Terkait)",
14
+ "recommend_with_topic":"Suggestions or recommendations about certain lesson topic (Aku mau materi /lagi pengen materi /mau materi dong kak/Rekomendasi materi kak/Materi terkait)",
15
+ "assess_knowledge":"Assess skill/knowledge",
16
+ "assess_knowledge_with_topic":"Assess skill/knowledge from given topics",
17
+ "assess_knowledge_answer":"Follow up to assess knowledge result. (jawabanku kok salah ya, kenapa bisa salah ya jawabanku, aku gabisa jawab, wah jawabanku bener wkwk, yeay bener semua jawabanku, jawabanku salah 1 aja yeay, cuman bener 1 jawabanku, secara garis besar udah bagus ya hasilku, perlu peningkatan belajar lagi nih)",
18
+ "exercise":"Want to do exercise questions (Latihan Soal, aku mau soal, aku butuh soal, bantu carikan soal dong, mau soal dong, mau pertanyaan dong, latihan pertanyaan, coba ulang pertanyaan nya, ulangi pertanyaanya karena tadi kurang jelas, minta soal)",
19
+ "exercise_with_image":"Want to do exercise questions with an image",
20
+ "exercise_with_topic":"Want to do exercise questions of a topic (carikan soal integral, mau soal fisika, berikan aku soal tata surya, soal geografi, latihan soal ekonomi, soal biologi)",
21
+ "exercise_multiple_question":"Wants to do exercise with a number of question",
22
+ "exercise_multiple_question_with_topic":"Wants to do exercise with a number of question with topics",
23
+ "personal":"Sharing personal story (aku mau curhat, kak lagi sedih)",
24
+ "general_intent":"Greetings, Asking/desire about study and exams in general. (aku pusing, aku mau belajar kak, besok ulangan, butuh temen belajar, besok ujian, temenin aku, bantu aku, aku butuh bantuan)",
25
+ "provide_topic":"Lesson material Topic",
26
+ "provide_subject":"Lesson material Subject (Fisika, Kimia, Matematika, Biologi, Sejarah, Bahasa Indonesia, Geografi, Biologi, Bahasa Inggris)",
27
+ "provide_subtopic":"Lesson material Subtopic",
28
+ "provide_concept":"Concept",
29
+ "finish_learning":"Finished/already learned (Terimakasih /Thank you/No/Sudah paham!/Ok/Tidak)",
30
+ "back_to_study":"Return to study (Kembali Belajar) /Mulai Belajar?/langsung mulai belajar",
31
+ "similar_question":"Create similar exercise questions (Buatkan Soal Serupa / Cari Soal Serupa / Soal lagi yang mirip / Latihan soal yang mirip)",
32
+ "provide_answer":"Answering exercise questions (A, B, C, D, E) or more than one characters (Jawabanya A, B, C, D, E, Menurutku jawabanya A, B, C, D, E)",
33
+ "diagnosis_result":"See diagnosis or score result (Lihat hasil diagnosis / hasil drill)",
34
+ "explain_drill":"Explain exercise question drill material (bahas lewat chat/bahas soal nomer 1 ya/bahas soal nomer 2 ya/bahas soal nomer 3 ya/bahas soal nomer 4 ya/bahas soal nomer 5 ya)"
35
+ }
36
+
37
+ intents = ['homework',
38
+ 'homework_with_question',
39
+ 'explain_answer',
40
+ 'explain_answer_with_topic',
41
+ 'core_explanation_repeat',
42
+ 'core_explanation_exercise',
43
+ 'core_explanation_video',
44
+ 'core_explanation_protips',
45
+ 'negative_respond',
46
+ 'positive_respond',
47
+ 'recommend',
48
+ 'recommend_with_topic',
49
+ 'assess_knowledge',
50
+ 'assess_knowledge_with_topic',
51
+ 'assess_knowledge_answer',
52
+ 'exercise',
53
+ 'exercise_with_image',
54
+ 'exercise_with_topic',
55
+ 'exercise_multiple_question',
56
+ 'exercise_multiple_question_with_topic',
57
+ 'personal',
58
+ 'general_intent',
59
+ 'provide_topic',
60
+ 'provide_subject',
61
+ 'provide_subtopic',
62
+ 'provide_concept',
63
+ 'finish_learning',
64
+ 'back_to_study',
65
+ 'similar_question',
66
+ 'provide_answer',
67
+ 'diagnosis_result',
68
+ 'explain_drill']
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ sentence-transformers
2
+ transformers
3
+ gradio
utils.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title Utility Functions
2
+ def get_history_from_prompt(prompt:str):
3
+ if "Here are previous chats for your reference (only use this if you need further information to infer the intent):" in prompt:
4
+ history = prompt.split("Here are previous chats for your reference (only use this if you need further information to infer the intent):")
5
+ else:
6
+ history = prompt.split("Here are previous chats or summary conversation for your reference (only use this if you need further information to infer the intent):")
7
+ return history[1].replace("""The Intent:""", '')
8
+
9
+ def get_latest_user_input_from_prompt(prompt:str):
10
+ input = prompt.split("Here is the message you are to classify:")
11
+ if "Here are previous chats for your reference (only use this if you need further information to infer the intent):" in prompt:
12
+ input = input[1].split("Here are previous chats for your reference (only use this if you need further information to infer the intent):")
13
+ else:
14
+ input = input[1].split("Here are previous chats or summary conversation for your reference (only use this if you need further information to infer the intent)")
15
+ return input[0]
16
+
17
+ # Get the top 5 intents with the highest values
18
+ def get_top_intents(intent_list:list, similarity, n=5, threshold=0.3, flow=None) -> str:
19
+ result = dict()
20
+ for i in range(len(intent_list)):
21
+ if flow:
22
+ if intent_list[i] in flow:
23
+ # print("intent {} is ignored, because it's not in the possible intent".format(intent_list[i]))
24
+ if similarity[0][i].item() > threshold:
25
+ result[intent_list[i]] = similarity[0][i].item()
26
+ else:
27
+ if similarity[0][i].item() > threshold:
28
+ result[intent_list[i]] = similarity[0][i].item()
29
+
30
+ top_intents = sorted(result.items(), key=lambda item: item[1], reverse=True)[:n]
31
+
32
+ if not top_intents:
33
+ top_intents.append(('unknown', 1.0))
34
+ return top_intents
35
+
36
+ def create_embedding(intents:dict, model_en):
37
+ intents_description_en = []
38
+ for k,v in intents.items():
39
+ intents_description_en.append(v)
40
+ intents_embedding = model_en.encode(intents_description_en)
41
+ return intents_embedding