seawolf2357 commited on
Commit
ccbb5ef
β€’
1 Parent(s): cd83437

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import discord
2
  import logging
3
  import os
 
4
  from huggingface_hub import InferenceClient
5
  import asyncio
6
  import subprocess
7
- from datasets import load_dataset
8
 
9
  # λ‘œκΉ… μ„€μ •
10
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
@@ -26,7 +26,16 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
26
  conversation_history = []
27
 
28
  # 법λ₯  데이터셋 λ‘œλ“œ
29
- law_dataset = load_dataset("aiqtech/kolaw")
 
 
 
 
 
 
 
 
 
30
 
31
  class MyClient(discord.Client):
32
  def __init__(self, *args, **kwargs):
@@ -39,35 +48,25 @@ class MyClient(discord.Client):
39
  logging.info("Web.py server has been started.")
40
 
41
  async def on_message(self, message):
42
- logging.debug(f"Message received: {message.content} from {message.author}")
43
  if message.author == self.user:
44
- logging.debug("Message from bot itself; ignoring.")
45
  return
46
  if not self.is_message_in_specific_channel(message):
47
- logging.debug(f"Message not in the specific channel or its thread: {message.channel}")
48
  return
49
  if self.is_processing:
50
- logging.debug("Already processing another message.")
51
  return
52
- logging.debug("Processing message...")
53
  self.is_processing = True
54
  try:
55
- response = await self.generate_response(message) # Note the use of self here
56
  await message.channel.send(response)
57
- except Exception as e:
58
- logging.error(f"Error during message processing: {e}", exc_info=True)
59
  finally:
60
  self.is_processing = False
61
 
62
  def is_message_in_specific_channel(self, message):
63
- result = message.channel.id == SPECIFIC_CHANNEL_ID or (
64
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
65
  )
66
- logging.debug(f"Checking if message is in specific channel: {result}")
67
- return result
68
 
69
  async def generate_response(self, message):
70
- global conversation_history
71
  user_input = message.content
72
  user_mention = message.author.mention
73
  system_message = f"{user_mention}, 법λ₯ μ— λŒ€ν•œ 닡변을 μ œκ³΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
@@ -85,17 +84,15 @@ class MyClient(discord.Client):
85
 
86
  answer = self.search_in_dataset(user_input, law_dataset)
87
 
88
- full_response_text = system_message + "\n\n" + answer
89
  logging.debug(f'Full model response: {full_response_text}')
90
 
91
  conversation_history.append({"role": "assistant", "content": full_response_text})
92
  return f"{user_mention}, {full_response_text}"
93
 
94
  def search_in_dataset(self, query, dataset):
95
- # μ—¬κΈ°μ—μ„œ query에 따라 λ°μ΄ν„°μ…‹μ—μ„œ μ μ ˆν•œ 정보λ₯Ό κ²€μƒ‰ν•˜λŠ” λ‘œμ§μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€.
96
- # μ˜ˆμ œλŠ” λ‹¨μˆœν™”λ₯Ό μœ„ν•΄ 첫 번째 ν•­λͺ©μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.
97
- return dataset['train'][0]['document'] # 'text' λŒ€μ‹  'document'둜 λ³€κ²½
98
-
99
 
100
  if __name__ == "__main__":
101
  discord_client = MyClient(intents=intents)
 
1
  import discord
2
  import logging
3
  import os
4
+ from datasets import load_dataset
5
  from huggingface_hub import InferenceClient
6
  import asyncio
7
  import subprocess
 
8
 
9
  # λ‘œκΉ… μ„€μ •
10
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
 
26
  conversation_history = []
27
 
28
  # 법λ₯  데이터셋 λ‘œλ“œ
29
+ law_dataset = load_dataset('csv', data_files={
30
+ 'train': [
31
+ '/path/to/train_0.csv',
32
+ '/path/to/train_1.csv',
33
+ '/path/to/train_2.csv',
34
+ '/path/to/train_3.csv',
35
+ '/path/to/train_4.csv',
36
+ '/path/to/train_5.csv'
37
+ ]
38
+ })
39
 
40
  class MyClient(discord.Client):
41
  def __init__(self, *args, **kwargs):
 
48
  logging.info("Web.py server has been started.")
49
 
50
  async def on_message(self, message):
 
51
  if message.author == self.user:
 
52
  return
53
  if not self.is_message_in_specific_channel(message):
 
54
  return
55
  if self.is_processing:
 
56
  return
 
57
  self.is_processing = True
58
  try:
59
+ response = await self.generate_response(message)
60
  await message.channel.send(response)
 
 
61
  finally:
62
  self.is_processing = False
63
 
64
  def is_message_in_specific_channel(self, message):
65
+ return message.channel.id == SPECIFIC_CHANNEL_ID or (
66
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
67
  )
 
 
68
 
69
  async def generate_response(self, message):
 
70
  user_input = message.content
71
  user_mention = message.author.mention
72
  system_message = f"{user_mention}, 법λ₯ μ— λŒ€ν•œ 닡변을 μ œκ³΅ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
 
84
 
85
  answer = self.search_in_dataset(user_input, law_dataset)
86
 
87
+ full_response_text = system_prefix + "\n\n" + answer
88
  logging.debug(f'Full model response: {full_response_text}')
89
 
90
  conversation_history.append({"role": "assistant", "content": full_response_text})
91
  return f"{user_mention}, {full_response_text}"
92
 
93
  def search_in_dataset(self, query, dataset):
94
+ # 첫 번째 ν•­λͺ© λ°˜ν™˜ (ν•„λ“œ 이름 확인 ν•„μš”)
95
+ return dataset['train'][0]['νŒκ²°μš”μ§€'] # 'text' λŒ€μ‹  μ‹€μ œ ν•„λ“œ 이름 'νŒκ²°μš”μ§€' μ‚¬μš©
 
 
96
 
97
  if __name__ == "__main__":
98
  discord_client = MyClient(intents=intents)