Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Commit
β’
ccbb5ef
1
Parent(s):
cd83437
Update app.py
Browse files
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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
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 |
-
|
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 =
|
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 |
-
#
|
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)
|