seawolf2357 commited on
Commit
8892907
β€’
1 Parent(s): c0cea59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
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=400, temperature=0.7, top_p=0.85))
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 = 5 # 전솑할 λ°μ΄ν„°μ˜ 개수
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
- for attempt in range(MAX_RETRIES):
176
- async with aiohttp.ClientSession() as session:
177
- try:
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)