baobuiquang commited on
Commit
d5b4eea
1 Parent(s): 14e2623
Files changed (1) hide show
  1. app.py +85 -20
app.py CHANGED
@@ -13,7 +13,7 @@ import numpy as np
13
  import torch
14
  import time
15
  from transformers import AutoTokenizer, AutoModel
16
- from datetime import datetime
17
  # pd.options.mode.chained_assignment = None # default='warn'
18
 
19
  # ===========================
@@ -148,13 +148,78 @@ for i in range(len(preprocessed_df_map)):
148
  # ========== MAIN ==========
149
 
150
  def chatbot_mechanism(message, history, additional_input_1):
 
151
  # Clarify namings
152
  question = message
153
  sheet_id = additional_input_1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  # Select the right data
155
  df = preprocessed_df_map[sheet_id]
156
  x_list_embeddings = x_list_embeddings_map[sheet_id]
157
  y_list_embeddings = y_list_embeddings_map[sheet_id]
 
158
  # Find the position of the needed cell
159
  question_embedding = text_to_embedding(question)
160
  x_sim = similarity(question_embedding, x_list_embeddings)
@@ -173,9 +238,9 @@ def chatbot_mechanism(message, history, additional_input_1):
173
  # Just add some text to warn users
174
  eval_text = ""
175
  eval_text_sub_title = ""
176
- if x_score < 0.85 or y_score < 0.85:
177
  eval_text_sub_title = "Cảnh báo:"
178
- eval_text = "⚠️ Đặc trưng trích xuất không rõ ràng ⚠️"
179
 
180
  # Score display
181
  x_score_display = str(round((x_score - 0.8) / (1.0 - 0.8) * 100, 1))
@@ -186,28 +251,28 @@ def chatbot_mechanism(message, history, additional_input_1):
186
 
187
  # Final print
188
  final_output_message = f"\
189
- <div style='color: gray; font-size: 80%; font-family: courier, monospace;'>\
190
- Kết quả:\
191
- </div>\
192
- <div style='font-weight: bold;'>\
193
- {cell_value}\
194
- </div>\
195
  <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
196
  Đặc trưng trích xuất được:\
197
  </div>\
198
  • {x_text}<br>\
199
- • {y_text}<br>\
200
- <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
201
- Đánh giá:\
202
- </div>\
203
- Độ tương quan: [x={x_score_display}%, y={y_score_display}%]<br>\
204
- <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
205
- {eval_text_sub_title}\
206
- </div>\
207
- <div style='color: red; font-weight: bold;'>\
208
- {eval_text}\
209
- </div>\
210
  "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  return final_output_message
212
  # for i in range(len(final_output_message)):
213
  # time.sleep(0.1)
 
13
  import torch
14
  import time
15
  from transformers import AutoTokenizer, AutoModel
16
+ from datetime import datetime, timedelta
17
  # pd.options.mode.chained_assignment = None # default='warn'
18
 
19
  # ===========================
 
148
  # ========== MAIN ==========
149
 
150
  def chatbot_mechanism(message, history, additional_input_1):
151
+
152
  # Clarify namings
153
  question = message
154
  sheet_id = additional_input_1
155
+
156
+ # Small preprocess the message to handle unclear cases (ex: "tháng này")
157
+ extra_information_for_special_cases = ""
158
+ extra_information_for_special_cases_flag = False
159
+ unclear_cases = [
160
+ # Case 0: -> YEAR
161
+ ["năm này", "năm hiện tại", "năm nay"],
162
+ # Case 1: -> MONTH, YEAR
163
+ ["tháng này", "tháng hiện tại", "tháng nay", "tháng bây giờ", "tháng đang diễn ra", "tháng hiện nay", "tháng hiện giờ"],
164
+ # Case 2: -> DAY, MONTH, YEAR
165
+ ["ngày này" , "ngày hiện tại", "ngày hôm nay", "hôm nay", "bây giờ", "hiện tại", "thời điểm này", "thời gian này"],
166
+
167
+ # Case 3: -> YEAR
168
+ ["năm trước", "năm ngoái", "năm qua", "năm vừa rồi", "năm đã qua"],
169
+ # Case 4: -> MONTH, YEAR
170
+ ["tháng trước", "tháng qua", "tháng vừa rồi", "tháng đã qua"],
171
+ # Case 5: -> DAY, MONTH, YEAR
172
+ ["hôm qua", "hôm trước", "ngày qua", "ngày trước"],
173
+
174
+ # Case 6: -> YEAR
175
+ ["năm sau", "năm tới", "năm tiếp theo", "năm kế tiếp", "năm sắp tới"],
176
+ # Case 7: -> MONTH, YEAR
177
+ ["tháng sau", "tháng tới", "tháng tiếp theo", "tháng kế tiếp", "tháng sắp tới"],
178
+ # Case 8: -> DAY, MONTH, YEAR
179
+ ["ngày mai", "ngày sau", "ngày tới", "ngày tiếp theo", "ngày hôm sau", "ngày kế tiếp", "ngày sắp tới"],
180
+ ]
181
+ for i in range(len(unclear_cases)):
182
+ for u in range(len(unclear_cases[i])):
183
+ if unclear_cases[i][u] in question:
184
+ # Flag
185
+ extra_information_for_special_cases_flag = True
186
+ # Get the current time data
187
+ current_time = datetime.now()
188
+ target_time = datetime.now() # Just pre-define
189
+ # Handle specific cases
190
+ if i in [0, 1, 2]:
191
+ target_time = current_time # No change
192
+ elif i == 3:
193
+ target_time = current_time - timedelta(days = 365)
194
+ elif i == 4:
195
+ target_time = current_time - timedelta(days = 30)
196
+ elif i == 5:
197
+ target_time = current_time - timedelta(days = 1)
198
+ elif i == 6:
199
+ target_time = current_time + timedelta(days = 365)
200
+ elif i == 7:
201
+ target_time = current_time + timedelta(days = 30)
202
+ elif i == 8:
203
+ target_time = current_time + timedelta(days = 1)
204
+ # Extract time to day, month, year
205
+ day = str(target_time.strftime('%d').lstrip(''))
206
+ month = str(target_time.strftime('%m').lstrip(''))
207
+ year = str(target_time.strftime('%Y').lstrip(''))
208
+ # Handle specific cases
209
+ if i in [0, 3, 6]:
210
+ extra_information_for_special_cases = f"Năm {year}"
211
+ elif i in [1, 4, 7]:
212
+ extra_information_for_special_cases = f"Tháng {month} năm {year}"
213
+ elif i in [2, 5, 8]:
214
+ extra_information_for_special_cases = f"Ngày {day} tháng {month} năm {year}"
215
+ if extra_information_for_special_cases_flag == True:
216
+ question = extra_information_for_special_cases + " " + question
217
+
218
  # Select the right data
219
  df = preprocessed_df_map[sheet_id]
220
  x_list_embeddings = x_list_embeddings_map[sheet_id]
221
  y_list_embeddings = y_list_embeddings_map[sheet_id]
222
+
223
  # Find the position of the needed cell
224
  question_embedding = text_to_embedding(question)
225
  x_sim = similarity(question_embedding, x_list_embeddings)
 
238
  # Just add some text to warn users
239
  eval_text = ""
240
  eval_text_sub_title = ""
241
+ if x_score <= 0.87 or y_score <= 0.87:
242
  eval_text_sub_title = "Cảnh báo:"
243
+ eval_text = "⚠️"
244
 
245
  # Score display
246
  x_score_display = str(round((x_score - 0.8) / (1.0 - 0.8) * 100, 1))
 
251
 
252
  # Final print
253
  final_output_message = f"\
 
 
 
 
 
 
254
  <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
255
  Đặc trưng trích xuất được:\
256
  </div>\
257
  • {x_text}<br>\
258
+ • {y_text if extra_information_for_special_cases_flag == False else extra_information_for_special_cases}<br>\
 
 
 
 
 
 
 
 
 
 
259
  "
260
+ # <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
261
+ # Đánh giá:\
262
+ # </div>\
263
+ # Độ tương quan: [x={x_score_display}%, y={y_score_display}%]<br>\
264
+ # <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
265
+ # Kết quả:\
266
+ # </div>\
267
+ # <div style='font-weight: bold;'>\
268
+ # {cell_value}\
269
+ # </div>\
270
+ # <div style='color: gray; font-size: 80%; font-family: courier, monospace; margin-top: 6px;'>\
271
+ # {eval_text_sub_title}\
272
+ # </div>\
273
+ # <div style='color: red; font-weight: bold;'>\
274
+ # {eval_text}\
275
+ # </div>\
276
  return final_output_message
277
  # for i in range(len(final_output_message)):
278
  # time.sleep(0.1)