Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import discord
|
|
|
2 |
import logging
|
3 |
import os
|
4 |
from huggingface_hub import InferenceClient
|
@@ -27,8 +28,11 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
27 |
API_KEY = os.getenv("JSONKEY")
|
28 |
CX = "c01abc75e1b95483d" # μ¬μ©μ 컀μ€ν
κ²μ μμ§ ID
|
29 |
|
30 |
-
# λν νμ€ν 리λ₯Ό μ μ₯ν
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
def google_search(query):
|
34 |
logger.info(f"Searching for query: {query}")
|
@@ -63,9 +67,9 @@ def google_search(query):
|
|
63 |
logger.error(f"Request failed: {e}")
|
64 |
return f"An error occurred: {e}"
|
65 |
|
66 |
-
class MyClient(
|
67 |
-
def __init__(self
|
68 |
-
super().__init__(
|
69 |
self.is_processing = False
|
70 |
|
71 |
async def on_ready(self):
|
@@ -83,20 +87,56 @@ class MyClient(discord.Client):
|
|
83 |
|
84 |
self.is_processing = True
|
85 |
try:
|
86 |
-
|
87 |
-
await
|
|
|
|
|
|
|
|
|
88 |
finally:
|
89 |
self.is_processing = False
|
90 |
|
91 |
def is_message_in_specific_channel(self, message):
|
92 |
-
return message.channel.id == SPECIFIC_CHANNEL_ID
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
)
|
|
|
|
|
95 |
|
96 |
-
async def
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
user_input = message.content
|
99 |
user_mention = message.author.mention
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
system_message = f"{user_mention}, DISCORDμμ μ¬μ©μλ€μ μ§λ¬Έμ λ΅νλ μ΄μμ€ν΄νΈμ
λλ€."
|
101 |
system_prefix = """
|
102 |
λ°λμ νκΈλ‘ λ΅λ³νμμμ€. μΆλ ₯μ markdown νμμΌλ‘ μΆλ ₯νλΌ.
|
@@ -145,9 +185,9 @@ async def generate_response(message):
|
|
145 |
conversation_history.append({"role": "assistant", "content": full_response_text})
|
146 |
|
147 |
# λν νμ€ν 리 κΈΈμ΄ μ ν (μ΅κ·Ό 10κ° λ©μμ§λ§ μ μ§)
|
148 |
-
|
149 |
|
150 |
-
logging.debug(f'Conversation history updated: {
|
151 |
|
152 |
return f"{user_mention}, {full_response_text}"
|
153 |
except Exception as e:
|
@@ -156,7 +196,7 @@ async def generate_response(message):
|
|
156 |
|
157 |
if __name__ == "__main__":
|
158 |
# Discord ν΄λΌμ΄μΈνΈ μ€ν
|
159 |
-
|
160 |
|
161 |
# Discord λ΄ μ€ν
|
162 |
-
|
|
|
1 |
import discord
|
2 |
+
from discord.ext import commands
|
3 |
import logging
|
4 |
import os
|
5 |
from huggingface_hub import InferenceClient
|
|
|
28 |
API_KEY = os.getenv("JSONKEY")
|
29 |
CX = "c01abc75e1b95483d" # μ¬μ©μ 컀μ€ν
κ²μ μμ§ ID
|
30 |
|
31 |
+
# λν νμ€ν 리λ₯Ό μ μ₯ν λμ
λ리
|
32 |
+
conversation_histories = {}
|
33 |
+
|
34 |
+
# μ€λ λ IDλ₯Ό μ μ₯ν λμ
λ리
|
35 |
+
user_threads = {}
|
36 |
|
37 |
def google_search(query):
|
38 |
logger.info(f"Searching for query: {query}")
|
|
|
67 |
logger.error(f"Request failed: {e}")
|
68 |
return f"An error occurred: {e}"
|
69 |
|
70 |
+
class MyClient(commands.Bot):
|
71 |
+
def __init__(self):
|
72 |
+
super().__init__(command_prefix="!", intents=intents)
|
73 |
self.is_processing = False
|
74 |
|
75 |
async def on_ready(self):
|
|
|
87 |
|
88 |
self.is_processing = True
|
89 |
try:
|
90 |
+
# μ€λ λ μμ± λλ κΈ°μ‘΄ μ€λ λ μ°ΎκΈ°
|
91 |
+
thread = await self.get_or_create_thread(message)
|
92 |
+
|
93 |
+
# μ€λ λμμ μλ΅ μμ±
|
94 |
+
response = await generate_response(message, thread.id)
|
95 |
+
await self.send_long_message(thread, response)
|
96 |
finally:
|
97 |
self.is_processing = False
|
98 |
|
99 |
def is_message_in_specific_channel(self, message):
|
100 |
+
return message.channel.id == SPECIFIC_CHANNEL_ID
|
101 |
+
|
102 |
+
async def get_or_create_thread(self, message):
|
103 |
+
if message.author.id in user_threads:
|
104 |
+
thread_id = user_threads[message.author.id]
|
105 |
+
thread = self.get_channel(thread_id)
|
106 |
+
if thread is None:
|
107 |
+
# μ€λ λκ° μμ λμλ€λ©΄ μλ‘ μμ±
|
108 |
+
thread = await self.create_thread(message)
|
109 |
+
else:
|
110 |
+
thread = await self.create_thread(message)
|
111 |
+
return thread
|
112 |
+
|
113 |
+
async def create_thread(self, message):
|
114 |
+
thread = await message.channel.create_thread(
|
115 |
+
name=f"λν - {message.author.name}",
|
116 |
+
auto_archive_duration=60
|
117 |
)
|
118 |
+
user_threads[message.author.id] = thread.id
|
119 |
+
return thread
|
120 |
|
121 |
+
async def send_long_message(self, channel, message):
|
122 |
+
if len(message) <= 2000:
|
123 |
+
await channel.send(message)
|
124 |
+
else:
|
125 |
+
parts = [message[i:i+2000] for i in range(0, len(message), 2000)]
|
126 |
+
for part in parts:
|
127 |
+
await channel.send(part)
|
128 |
+
|
129 |
+
async def generate_response(message, thread_id):
|
130 |
+
global conversation_histories
|
131 |
user_input = message.content
|
132 |
user_mention = message.author.mention
|
133 |
+
|
134 |
+
# μ€λ λλ³ λν νμ€ν 리 κ΄λ¦¬
|
135 |
+
if thread_id not in conversation_histories:
|
136 |
+
conversation_histories[thread_id] = []
|
137 |
+
|
138 |
+
conversation_history = conversation_histories[thread_id]
|
139 |
+
|
140 |
system_message = f"{user_mention}, DISCORDμμ μ¬μ©μλ€μ μ§λ¬Έμ λ΅νλ μ΄μμ€ν΄νΈμ
λλ€."
|
141 |
system_prefix = """
|
142 |
λ°λμ νκΈλ‘ λ΅λ³νμμμ€. μΆλ ₯μ markdown νμμΌλ‘ μΆλ ₯νλΌ.
|
|
|
185 |
conversation_history.append({"role": "assistant", "content": full_response_text})
|
186 |
|
187 |
# λν νμ€ν 리 κΈΈμ΄ μ ν (μ΅κ·Ό 10κ° λ©μμ§λ§ μ μ§)
|
188 |
+
conversation_histories[thread_id] = conversation_history[-10:]
|
189 |
|
190 |
+
logging.debug(f'Conversation history updated: {conversation_histories[thread_id]}')
|
191 |
|
192 |
return f"{user_mention}, {full_response_text}"
|
193 |
except Exception as e:
|
|
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
# Discord ν΄λΌμ΄μΈνΈ μ€ν
|
199 |
+
bot = MyClient()
|
200 |
|
201 |
# Discord λ΄ μ€ν
|
202 |
+
bot.run(os.getenv('DISCORD_TOKEN'))
|