seawolf2357 commited on
Commit
1d26e8c
โ€ข
1 Parent(s): 29513a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -1,16 +1,15 @@
1
  import discord
2
  import logging
3
  import os
 
4
  import asyncio
 
5
  import aiohttp
6
  from huggingface_hub import InferenceClient
7
  from googleapiclient.discovery import build
8
  from youtube_transcript_api import YouTubeTranscriptApi
9
  from youtube_transcript_api.formatters import TextFormatter
10
  from dotenv import load_dotenv
11
- import subprocess
12
- import re # re ๋ชจ๋“ˆ import ์ถ”๊ฐ€
13
-
14
 
15
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
16
  load_dotenv()
@@ -26,7 +25,7 @@ intents.guilds = True
26
  intents.guild_messages = True
27
 
28
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
29
- hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
30
 
31
  # YouTube API ์„ค์ •
32
  API_KEY = os.getenv("YOUTUBE_API_KEY")
@@ -35,14 +34,15 @@ youtube_service = build('youtube', 'v3', developerKey=API_KEY)
35
  # ํŠน์ • ์ฑ„๋„ ID
36
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
37
 
 
 
 
38
  class MyClient(discord.Client):
39
  def __init__(self, *args, **kwargs):
40
  super().__init__(*args, **kwargs)
41
  self.is_processing = False
42
  self.session = None
43
 
44
-
45
-
46
  async def on_ready(self):
47
  logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
48
 
@@ -61,6 +61,9 @@ class MyClient(discord.Client):
61
  async def on_message(self, message):
62
  if message.author == self.user or not self.is_message_in_specific_channel(message):
63
  return
 
 
 
64
  self.is_processing = True
65
  try:
66
  video_id = self.extract_video_id(message.content)
@@ -110,7 +113,11 @@ class MyClient(discord.Client):
110
  {"role": "user", "content": comment},
111
  {"role": "system", "content": f"๋น„๋””์˜ค ์ž๋ง‰: {transcript}"}
112
  ]
113
- response = await hf_client.chat_completion(messages, max_tokens=300, temperature=0.7, top_p=0.85)
 
 
 
 
114
  if response.choices and response.choices[0].message:
115
  return response.choices[0].message['content'].strip()
116
  else:
@@ -127,8 +134,22 @@ class MyClient(discord.Client):
127
  return None
128
 
129
  async def send_webhook_data(self, comment, reply, comment_id):
130
- # ์›นํ›… ๋ฐ์ดํ„ฐ ์ค€๋น„ ๋ฐ ์ „์†ก ๋กœ์ง ๊ตฌํ˜„
131
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  async def close(self):
134
  if self.session:
@@ -138,3 +159,5 @@ class MyClient(discord.Client):
138
  if __name__ == "__main__":
139
  discord_client = MyClient(intents=intents)
140
  discord_client.run(os.getenv('DISCORD_TOKEN'))
 
 
 
1
  import discord
2
  import logging
3
  import os
4
+ import re # re ๋ชจ๋“ˆ import ์ถ”๊ฐ€
5
  import asyncio
6
+ import subprocess
7
  import aiohttp
8
  from huggingface_hub import InferenceClient
9
  from googleapiclient.discovery import build
10
  from youtube_transcript_api import YouTubeTranscriptApi
11
  from youtube_transcript_api.formatters import TextFormatter
12
  from dotenv import load_dotenv
 
 
 
13
 
14
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
15
  load_dotenv()
 
25
  intents.guild_messages = True
26
 
27
  # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
28
+ hf_client = InferenceClient(token=os.getenv("HF_TOKEN"))
29
 
30
  # YouTube API ์„ค์ •
31
  API_KEY = os.getenv("YOUTUBE_API_KEY")
 
34
  # ํŠน์ • ์ฑ„๋„ ID
35
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
36
 
37
+ # ์›นํ›… URL ์„ค์ •
38
+ WEBHOOK_URL = "https://connect.pabbly.com/workflow/sendwebhookdata/IjU3NjUwNTY1MDYzMjA0MzA1MjY4NTUzMDUxMzUi_pc"
39
+
40
  class MyClient(discord.Client):
41
  def __init__(self, *args, **kwargs):
42
  super().__init__(*args, **kwargs)
43
  self.is_processing = False
44
  self.session = None
45
 
 
 
46
  async def on_ready(self):
47
  logging.info(f'{self.user}๋กœ ๋กœ๊ทธ์ธ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
48
 
 
61
  async def on_message(self, message):
62
  if message.author == self.user or not self.is_message_in_specific_channel(message):
63
  return
64
+ if self.is_processing:
65
+ await message.channel.send("ํ˜„์žฌ ๋‹ค๋ฅธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌ ์ค‘์ž…๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.")
66
+ return
67
  self.is_processing = True
68
  try:
69
  video_id = self.extract_video_id(message.content)
 
113
  {"role": "user", "content": comment},
114
  {"role": "system", "content": f"๋น„๋””์˜ค ์ž๋ง‰: {transcript}"}
115
  ]
116
+
117
+ # ๋™๊ธฐ์‹ ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉ
118
+ loop = asyncio.get_event_loop()
119
+ response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(messages, max_tokens=300, temperature=0.7, top_p=0.85))
120
+
121
  if response.choices and response.choices[0].message:
122
  return response.choices[0].message['content'].strip()
123
  else:
 
134
  return None
135
 
136
  async def send_webhook_data(self, comment, reply, comment_id):
137
+ webhook_data = {
138
+ "video_id": video_id,
139
+ "replies": [{"comment": comment, "reply": reply, "comment_id": comment_id}]
140
+ }
141
+ for attempt in range(3):
142
+ try:
143
+ async with self.session.post(WEBHOOK_URL, json=webhook_data) as resp:
144
+ if resp.status == 200:
145
+ logging.info("์›นํ›… ๋ฐ์ดํ„ฐ ์ „์†ก ์„ฑ๊ณต")
146
+ return True
147
+ else:
148
+ logging.error(f"์›นํ›… ๋ฐ์ดํ„ฐ ์ „์†ก ์‹คํŒจ: {resp.status}")
149
+ except aiohttp.ClientError as e:
150
+ logging.error(f"์›นํ›… ์ „์†ก ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
151
+ await asyncio.sleep(1)
152
+ return False
153
 
154
  async def close(self):
155
  if self.session:
 
159
  if __name__ == "__main__":
160
  discord_client = MyClient(intents=intents)
161
  discord_client.run(os.getenv('DISCORD_TOKEN'))
162
+
163
+