seawolf2357 commited on
Commit
aab7a25
โ€ข
1 Parent(s): ce5155e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -61,6 +61,7 @@ class MyClient(discord.Client):
61
  if not self.is_message_in_specific_channel(message):
62
  return
63
  if self.is_processing:
 
64
  return
65
 
66
  self.is_processing = True
@@ -71,41 +72,35 @@ class MyClient(discord.Client):
71
  await message.channel.send(response)
72
  finally:
73
  self.is_processing = False
 
74
 
75
  def is_message_in_specific_channel(self, message):
76
- return message.channel.id == SPECIFIC_CHANNEL_ID or (
77
- isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
78
- )
79
 
80
 
81
 
82
  async def generate_response(message):
83
  global conversation_history
84
- user_input = message.content
85
  user_mention = message.author.mention
86
- system_message = f"{user_mention}, DISCORD์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
87
-
88
  # ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๋ฐ ์‘๋‹ต ์ค€๋น„
89
- answer = search_in_dataset(user_input, law_dataset)
90
- if not answer:
91
- answer = "๊ด€๋ จ ๋ฒ•๋ฅ  ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
92
-
93
- full_response_text = system_message + "\n\n" + answer
94
  max_length = 2000
95
  if len(full_response_text) > max_length:
96
- response_parts = []
97
  for i in range(0, len(full_response_text), max_length):
98
  part_response = full_response_text[i:i+max_length]
99
  await message.channel.send(part_response)
100
- response_parts.append(part_response)
101
  logging.debug(f'Full model response sent: {full_response_text}')
102
  conversation_history.append({"role": "assistant", "content": full_response_text})
103
- return "".join(response_parts) # ์กฐ๊ฐ๋‚œ ๋ฉ”์‹œ์ง€๋“ค์„ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
104
  else:
105
  await message.channel.send(full_response_text)
106
  logging.debug(f'Full model response sent: {full_response_text}')
107
  conversation_history.append({"role": "assistant", "content": full_response_text})
108
- return full_response_text # ์ „์ฒด ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ˜ํ™˜
109
 
110
 
111
  def search_in_dataset(query, dataset):
@@ -115,6 +110,12 @@ def search_in_dataset(query, dataset):
115
  return record['์‚ฌ๊ฑด๋ฒˆํ˜ธ']
116
  return "๊ด€๋ จ ๋ฒ•๋ฅ  ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
117
 
 
 
 
 
 
 
118
 
119
  if __name__ == "__main__":
120
  discord_client = MyClient(intents=intents)
 
61
  if not self.is_message_in_specific_channel(message):
62
  return
63
  if self.is_processing:
64
+ logging.debug("Currently processing another message, skipping this one.")
65
  return
66
 
67
  self.is_processing = True
 
72
  await message.channel.send(response)
73
  finally:
74
  self.is_processing = False
75
+ logging.debug("Message processing completed, ready for the next one.")
76
 
77
  def is_message_in_specific_channel(self, message):
78
+ channel_condition = message.channel.id == SPECIFIC_CHANNEL_ID
79
+ thread_condition = isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
80
+ return channel_condition or thread_condition
81
 
82
 
83
 
84
  async def generate_response(message):
85
  global conversation_history
86
+ case_number = message.content.strip()
87
  user_mention = message.author.mention
88
+ system_message = f"{user_mention}, ๋‹ค์Œ์€ ์š”์ฒญํ•˜์‹  ์‚ฌ๊ฑด์˜ ์ „๋ฌธ์ž…๋‹ˆ๋‹ค:"
89
+
90
  # ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๋ฐ ์‘๋‹ต ์ค€๋น„
91
+ full_text = search_in_dataset_by_case_number(case_number, law_dataset)
92
+ full_response_text = f"{system_message}\n\n{full_text}"
 
 
 
93
  max_length = 2000
94
  if len(full_response_text) > max_length:
 
95
  for i in range(0, len(full_response_text), max_length):
96
  part_response = full_response_text[i:i+max_length]
97
  await message.channel.send(part_response)
 
98
  logging.debug(f'Full model response sent: {full_response_text}')
99
  conversation_history.append({"role": "assistant", "content": full_response_text})
 
100
  else:
101
  await message.channel.send(full_response_text)
102
  logging.debug(f'Full model response sent: {full_response_text}')
103
  conversation_history.append({"role": "assistant", "content": full_response_text})
 
104
 
105
 
106
  def search_in_dataset(query, dataset):
 
110
  return record['์‚ฌ๊ฑด๋ฒˆํ˜ธ']
111
  return "๊ด€๋ จ ๋ฒ•๋ฅ  ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
112
 
113
+ def search_in_dataset_by_case_number(case_number, dataset):
114
+ for record in dataset['train']:
115
+ if record['์‚ฌ๊ฑด๋ฒˆํ˜ธ'] == case_number:
116
+ return record.get('์ „๋ฌธ', "ํ•ด๋‹น ์‚ฌ๊ฑด์— ๋Œ€ํ•œ ์ „๋ฌธ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
117
+ return "๊ด€๋ จ ๋ฒ•๋ฅ  ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
118
+
119
 
120
  if __name__ == "__main__":
121
  discord_client = MyClient(intents=intents)