seawolf2357 commited on
Commit
5535f24
Β·
verified Β·
1 Parent(s): 9a34640

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -39,6 +39,13 @@ class MyClient(discord.Client):
39
  def __init__(self, *args, **kwargs):
40
  super().__init__(*args, **kwargs)
41
  self.is_processing = False
 
 
 
 
 
 
 
42
 
43
  async def on_ready(self):
44
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
@@ -70,7 +77,7 @@ async def generate_response(message):
70
  user_mention = message.author.mention
71
 
72
  # μœ μ‚¬ν•œ 데이터 μ°ΎκΈ°
73
- most_similar_data = find_most_similar_data(user_input)
74
 
75
  system_message = f"{user_mention}, DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ κ΄‘κ³  μΉ΄ν”ΌλΌμ΄νŒ… μš”μ²­μ— λ‹΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
76
  system_prefix = """
@@ -110,11 +117,10 @@ async def generate_response(message):
110
  conversation_history.append({"role": "assistant", "content": full_response_text})
111
  return f"{user_mention}, {full_response_text}"
112
 
113
- def find_most_similar_data(query):
114
  query_embedding = model.encode(query, convert_to_tensor=True)
115
- all_embeddings = model.encode(all_datasets['text'].tolist(), convert_to_tensor=True)
116
 
117
- cosine_scores = util.pytorch_cos_sim(query_embedding, all_embeddings)
118
  best_match_index = torch.argmax(cosine_scores).item()
119
 
120
  if cosine_scores[0][best_match_index] > 0.5: # μœ μ‚¬λ„ μž„κ³„κ°’ μ„€μ •
 
39
  def __init__(self, *args, **kwargs):
40
  super().__init__(*args, **kwargs)
41
  self.is_processing = False
42
+ self.all_embeddings = None
43
+ self.initialize_embeddings()
44
+
45
+ def initialize_embeddings(self):
46
+ global all_datasets, model
47
+ text_data = all_datasets['text'].fillna('').astype(str).tolist()
48
+ self.all_embeddings = model.encode(text_data, convert_to_tensor=True)
49
 
50
  async def on_ready(self):
51
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
 
77
  user_mention = message.author.mention
78
 
79
  # μœ μ‚¬ν•œ 데이터 μ°ΎκΈ°
80
+ most_similar_data = find_most_similar_data(user_input, message.client)
81
 
82
  system_message = f"{user_mention}, DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ κ΄‘κ³  μΉ΄ν”ΌλΌμ΄νŒ… μš”μ²­μ— λ‹΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
83
  system_prefix = """
 
117
  conversation_history.append({"role": "assistant", "content": full_response_text})
118
  return f"{user_mention}, {full_response_text}"
119
 
120
+ def find_most_similar_data(query, client):
121
  query_embedding = model.encode(query, convert_to_tensor=True)
 
122
 
123
+ cosine_scores = util.pytorch_cos_sim(query_embedding, client.all_embeddings)
124
  best_match_index = torch.argmax(cosine_scores).item()
125
 
126
  if cosine_scores[0][best_match_index] > 0.5: # μœ μ‚¬λ„ μž„κ³„κ°’ μ„€μ •