seawolf2357
commited on
Commit
β’
8892907
1
Parent(s):
c0cea59
Update app.py
Browse files
app.py
CHANGED
@@ -44,6 +44,7 @@ class MyClient(discord.Client):
|
|
44 |
def __init__(self, *args, **kwargs):
|
45 |
super().__init__(*args, **kwargs)
|
46 |
self.is_processing = False
|
|
|
47 |
|
48 |
async def on_ready(self):
|
49 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
@@ -52,6 +53,9 @@ class MyClient(discord.Client):
|
|
52 |
subprocess.Popen(["python", "web.py"])
|
53 |
logging.info("Web.py μλ²κ° μμλμμ΅λλ€.")
|
54 |
|
|
|
|
|
|
|
55 |
# λ΄μ΄ μμλ λ μλ΄ λ©μμ§λ₯Ό μ μ‘
|
56 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
57 |
if channel:
|
@@ -85,6 +89,12 @@ class MyClient(discord.Client):
|
|
85 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
86 |
)
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
def extract_video_id(url):
|
89 |
video_id = None
|
90 |
youtube_regex = (
|
@@ -144,7 +154,7 @@ async def generate_replies(comments, transcript):
|
|
144 |
]
|
145 |
loop = asyncio.get_event_loop()
|
146 |
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
147 |
-
messages, max_tokens=
|
148 |
|
149 |
if response.choices and response.choices[0].message:
|
150 |
reply = response.choices[0].message['content'].strip()
|
@@ -155,6 +165,21 @@ async def generate_replies(comments, transcript):
|
|
155 |
logging.debug(f'μμ±λ λ΅κΈ: {replies}')
|
156 |
return replies
|
157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
async def create_thread_and_send_replies(message, video_id, comments, replies):
|
159 |
thread = await message.channel.create_thread(name=f"{message.author.name}μ λκΈ λ΅κΈ", message=message)
|
160 |
webhook_data = {"video_id": video_id, "replies": []}
|
@@ -167,22 +192,14 @@ async def create_thread_and_send_replies(message, video_id, comments, replies):
|
|
167 |
webhook_data["replies"].append({"comment": comment, "reply": reply, "comment_id": comment_id})
|
168 |
|
169 |
# λ°μ΄ν°λ₯Ό μ¬λ¬ λ² λλμ΄ μ μ‘
|
170 |
-
chunk_size =
|
171 |
for i in range(0, len(webhook_data["replies"]), chunk_size):
|
172 |
chunk = webhook_data["replies"][i:i+chunk_size]
|
173 |
chunk_data = {"video_id": video_id, "replies": chunk}
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
async with session.post(WEBHOOK_URL, json=chunk_data) as resp:
|
179 |
-
if resp.status == 200:
|
180 |
-
logging.info(f"μΉν
μΌλ‘ λ°μ΄ν° μ μ‘ μ±κ³΅: {i // chunk_size + 1} λ²μ§Έ μλ")
|
181 |
-
break # μ±κ³΅ μ 루ν νμΆ
|
182 |
-
else:
|
183 |
-
logging.error(f"μΉν
μΌλ‘ λ°μ΄ν° μ μ‘ μ€ν¨: {resp.status}, {i // chunk_size + 1} λ²μ§Έ μλ")
|
184 |
-
except aiohttp.ClientError as e:
|
185 |
-
logging.error(f"μΉν
μ μ‘ μ€ μ€λ₯ λ°μ: {e}, {i // chunk_size + 1} λ²μ§Έ μλ")
|
186 |
|
187 |
if __name__ == "__main__":
|
188 |
discord_client = MyClient(intents=intents)
|
|
|
44 |
def __init__(self, *args, **kwargs):
|
45 |
super().__init__(*args, **kwargs)
|
46 |
self.is_processing = False
|
47 |
+
self.session = None
|
48 |
|
49 |
async def on_ready(self):
|
50 |
logging.info(f'{self.user}λ‘ λ‘κ·ΈμΈλμμ΅λλ€!')
|
|
|
53 |
subprocess.Popen(["python", "web.py"])
|
54 |
logging.info("Web.py μλ²κ° μμλμμ΅λλ€.")
|
55 |
|
56 |
+
# aiohttp ν΄λΌμ΄μΈνΈ μΈμ
μμ±
|
57 |
+
self.session = aiohttp.ClientSession()
|
58 |
+
|
59 |
# λ΄μ΄ μμλ λ μλ΄ λ©μμ§λ₯Ό μ μ‘
|
60 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
61 |
if channel:
|
|
|
89 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
90 |
)
|
91 |
|
92 |
+
async def close(self):
|
93 |
+
# aiohttp ν΄λΌμ΄μΈνΈ μΈμ
μ’
λ£
|
94 |
+
if self.session:
|
95 |
+
await self.session.close()
|
96 |
+
await super().close()
|
97 |
+
|
98 |
def extract_video_id(url):
|
99 |
video_id = None
|
100 |
youtube_regex = (
|
|
|
154 |
]
|
155 |
loop = asyncio.get_event_loop()
|
156 |
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
157 |
+
messages, max_tokens=250, temperature=0.7, top_p=0.85))
|
158 |
|
159 |
if response.choices and response.choices[0].message:
|
160 |
reply = response.choices[0].message['content'].strip()
|
|
|
165 |
logging.debug(f'μμ±λ λ΅κΈ: {replies}')
|
166 |
return replies
|
167 |
|
168 |
+
async def send_webhook_data(session, chunk_data, chunk_number):
|
169 |
+
for attempt in range(MAX_RETRIES):
|
170 |
+
try:
|
171 |
+
async with session.post(WEBHOOK_URL, json=chunk_data) as resp:
|
172 |
+
if resp.status == 200:
|
173 |
+
logging.info(f"μΉν
μΌλ‘ λ°μ΄ν° μ μ‘ μ±κ³΅: {chunk_number} λ²μ§Έ μλ")
|
174 |
+
return True # μ±κ³΅ μ μ’
λ£
|
175 |
+
else:
|
176 |
+
logging.error(f"μΉν
μΌλ‘ λ°μ΄ν° μ μ‘ μ€ν¨: {resp.status}, {chunk_number} λ²μ§Έ μλ")
|
177 |
+
except aiohttp.ClientError as e:
|
178 |
+
logging.error(f"μΉν
μ μ‘ μ€ μ€λ₯ λ°μ: {e}, {chunk_number} λ²μ§Έ μλ")
|
179 |
+
await asyncio.sleep(1) # μ¬μλ μ μ μ μ λκΈ°
|
180 |
+
|
181 |
+
return False # μ¬μλ νμ μ΄κ³Ό μ μ€ν¨λ‘ κ°μ£Ό
|
182 |
+
|
183 |
async def create_thread_and_send_replies(message, video_id, comments, replies):
|
184 |
thread = await message.channel.create_thread(name=f"{message.author.name}μ λκΈ λ΅κΈ", message=message)
|
185 |
webhook_data = {"video_id": video_id, "replies": []}
|
|
|
192 |
webhook_data["replies"].append({"comment": comment, "reply": reply, "comment_id": comment_id})
|
193 |
|
194 |
# λ°μ΄ν°λ₯Ό μ¬λ¬ λ² λλμ΄ μ μ‘
|
195 |
+
chunk_size = 10 # μ μ‘ν λ°μ΄ν°μ κ°μ
|
196 |
for i in range(0, len(webhook_data["replies"]), chunk_size):
|
197 |
chunk = webhook_data["replies"][i:i+chunk_size]
|
198 |
chunk_data = {"video_id": video_id, "replies": chunk}
|
199 |
|
200 |
+
success = await send_webhook_data(self.session, chunk_data, i // chunk_size + 1)
|
201 |
+
if not success:
|
202 |
+
logging.error(f"λ°μ΄ν° μ μ‘ μ€ν¨: {i // chunk_size + 1} λ²μ§Έ μ²ν¬")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
if __name__ == "__main__":
|
205 |
discord_client = MyClient(intents=intents)
|