Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ from datasets import load_dataset
|
|
8 |
import pandas as pd
|
9 |
from fuzzywuzzy import process
|
10 |
|
11 |
-
|
12 |
# ํ์ฌ ์์
๋๋ ํ ๋ฆฌ ์ถ๋ ฅ
|
13 |
print("Current Working Directory:", os.getcwd())
|
14 |
|
@@ -62,8 +61,8 @@ intents.messages = True
|
|
62 |
intents.guilds = True
|
63 |
intents.guild_messages = True
|
64 |
|
65 |
-
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์ (
|
66 |
-
hf_client = InferenceClient("
|
67 |
|
68 |
# ํน์ ์ฑ๋ ID
|
69 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
@@ -71,16 +70,36 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
71 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
72 |
conversation_history = []
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
class MyClient(discord.Client):
|
75 |
def __init__(self, *args, **kwargs):
|
76 |
super().__init__(*args, **kwargs)
|
77 |
self.is_processing = False
|
|
|
78 |
|
79 |
async def on_ready(self):
|
80 |
logging.info(f'{self.user}๋ก ๋ก๊ทธ์ธ๋์์ต๋๋ค!')
|
81 |
subprocess.Popen(["python", "web.py"])
|
82 |
logging.info("Web.py server has been started.")
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
async def on_message(self, message):
|
85 |
if message.author == self.user:
|
86 |
return
|
@@ -92,7 +111,13 @@ class MyClient(discord.Client):
|
|
92 |
|
93 |
self.is_processing = True
|
94 |
try:
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
if response_parts:
|
97 |
for part in response_parts:
|
98 |
await message.channel.send(part)
|
@@ -107,9 +132,8 @@ class MyClient(discord.Client):
|
|
107 |
thread_condition = isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
108 |
return channel_condition or thread_condition
|
109 |
|
110 |
-
async def
|
111 |
-
|
112 |
-
user_input = message.content.strip()
|
113 |
user_mention = message.author.mention
|
114 |
|
115 |
# ์ ์ฌํ ์ฌ๊ฑด๋ช
๋ฐ ํ์์ฌํญ ๊ฐ๊ฐ ์ฐพ๊ธฐ
|
@@ -145,7 +169,26 @@ async def generate_response(message):
|
|
145 |
|
146 |
return response_parts
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
if __name__ == "__main__":
|
149 |
discord_client = MyClient(intents=intents)
|
150 |
discord_client.run(os.getenv('DISCORD_TOKEN'))
|
151 |
-
|
|
|
8 |
import pandas as pd
|
9 |
from fuzzywuzzy import process
|
10 |
|
|
|
11 |
# ํ์ฌ ์์
๋๋ ํ ๋ฆฌ ์ถ๋ ฅ
|
12 |
print("Current Working Directory:", os.getcwd())
|
13 |
|
|
|
61 |
intents.guilds = True
|
62 |
intents.guild_messages = True
|
63 |
|
64 |
+
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์ (Cohere)
|
65 |
+
hf_client = InferenceClient("cohere-ai/cohere", token=os.getenv("HF_TOKEN"))
|
66 |
|
67 |
# ํน์ ์ฑ๋ ID
|
68 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
|
70 |
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
71 |
conversation_history = []
|
72 |
|
73 |
+
# ์์คํ
ํ๋กฌํํธ ๋ฉ์์ง
|
74 |
+
SYSTEM_PROMPT = """
|
75 |
+
์๋
ํ์ธ์! ๋๋ฒ์ ํ๋ก ์ ๋ณด LLM ์์ํ ๊ฒ์ ๋ฐ ๋ถ์ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค:
|
76 |
+
1. ํน์ ์ฌ๊ฑด์ ๊ฒ์ํ๊ณ ์ถ๋ค๋ฉด `!key ๊ตญ๊ฐ ๋ณด์๋ฒ ์๋ฐ` ํํ๋ก ์
๋ ฅํ์ธ์.
|
77 |
+
2. ์ผ๋ฐ์ ์ธ ๋ฒ๋ฅ ๊ด๋ จ ์ง๋ฌธ์ด ์๊ฑฐ๋ ๋ํ๋ฅผ ์ํ์๋ฉด ๊ทธ๋ฅ ๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์.
|
78 |
+
3. ๊ฒ์ ๊ฒฐ๊ณผ ์ถ๋ ฅ๋ '์ฌ๊ฑด๋ฒํธ'๋ฅผ ์
๋ ฅํ๋ฉด 'ํ๊ฒฐ ์ ๋ฌธ'์ด ์ถ๋ ฅ๋ฉ๋๋ค.
|
79 |
+
|
80 |
+
์์:
|
81 |
+
- `!key ์์ ๊ถ์ด์ ๋ฑ๊ธฐ` -> ํด๋น ์ฌ๊ฑด์ ๋ํ ์ฌ๊ฑด๋ฒํธ๋ฅผ ์ ๊ณตํฉ๋๋ค.
|
82 |
+
- `์์ ๊ถ์ด์ ๋ฑ๊ธฐ์ ๊ด๋ จ๋ ๋ฒ์ ์ ์ฐจ๋ ๋ฌด์์ธ๊ฐ์?` -> ์ผ๋ฐ ๋ฒ๋ฅ ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ ์ ๊ณตํฉ๋๋ค.
|
83 |
+
- `69๋1183` -> ํด๋น ์ฌ๊ฑด๋ฒํธ์ ์ ๋ฌธ์ ์ ๊ณตํฉ๋๋ค.
|
84 |
+
"""
|
85 |
+
|
86 |
class MyClient(discord.Client):
|
87 |
def __init__(self, *args, **kwargs):
|
88 |
super().__init__(*args, **kwargs)
|
89 |
self.is_processing = False
|
90 |
+
self.first_message_sent = False
|
91 |
|
92 |
async def on_ready(self):
|
93 |
logging.info(f'{self.user}๋ก ๋ก๊ทธ์ธ๋์์ต๋๋ค!')
|
94 |
subprocess.Popen(["python", "web.py"])
|
95 |
logging.info("Web.py server has been started.")
|
96 |
|
97 |
+
# ์์คํ
ํ๋กฌํํธ ๋ฉ์์ง ์ ์ก
|
98 |
+
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
99 |
+
if channel is not None:
|
100 |
+
await channel.send(SYSTEM_PROMPT)
|
101 |
+
logging.info("System prompt message sent.")
|
102 |
+
|
103 |
async def on_message(self, message):
|
104 |
if message.author == self.user:
|
105 |
return
|
|
|
111 |
|
112 |
self.is_processing = True
|
113 |
try:
|
114 |
+
if message.content.startswith("!key"):
|
115 |
+
# ํค์๋ ๊ฒ์
|
116 |
+
response_parts = await handle_keyword_search(message)
|
117 |
+
else:
|
118 |
+
# ์์ฐ์ด ์ฒ๋ฆฌ ๋ํ
|
119 |
+
response_parts = await handle_natural_language(message)
|
120 |
+
|
121 |
if response_parts:
|
122 |
for part in response_parts:
|
123 |
await message.channel.send(part)
|
|
|
132 |
thread_condition = isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
133 |
return channel_condition or thread_condition
|
134 |
|
135 |
+
async def handle_keyword_search(message):
|
136 |
+
user_input = message.content[4:].strip() # "!key"๋ฅผ ์ ์ธํ๊ณ ํธ๋ฆผ ์ฒ๋ฆฌ
|
|
|
137 |
user_mention = message.author.mention
|
138 |
|
139 |
# ์ ์ฌํ ์ฌ๊ฑด๋ช
๋ฐ ํ์์ฌํญ ๊ฐ๊ฐ ์ฐพ๊ธฐ
|
|
|
169 |
|
170 |
return response_parts
|
171 |
|
172 |
+
async def handle_natural_language(message):
|
173 |
+
user_input = message.content.strip()
|
174 |
+
user_mention = message.author.mention
|
175 |
+
|
176 |
+
# Cohere API๋ฅผ ์ฌ์ฉํ์ฌ ์์ฐ์ด ์๋ต ์์ฑ
|
177 |
+
prompt = f"{user_input}"
|
178 |
+
response = hf_client.generate(prompt=prompt, max_new_tokens=100)
|
179 |
+
cohere_response = response.generated_text.strip()
|
180 |
+
|
181 |
+
system_message = f"{user_mention}, {cohere_response}"
|
182 |
+
|
183 |
+
# ๋ฉ์์ง ๊ธธ์ด ์ ํ ์ฒ๋ฆฌ
|
184 |
+
max_length = 2000
|
185 |
+
response_parts = []
|
186 |
+
for i in range(0, len(system_message), max_length):
|
187 |
+
part_response = system_message[i:i + max_length]
|
188 |
+
response_parts.append(part_response)
|
189 |
+
|
190 |
+
return response_parts
|
191 |
+
|
192 |
if __name__ == "__main__":
|
193 |
discord_client = MyClient(intents=intents)
|
194 |
discord_client.run(os.getenv('DISCORD_TOKEN'))
|
|