seawolf2357
commited on
Commit
โข
302cdd6
1
Parent(s):
f05454f
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
-
import os
|
2 |
-
import google_auth_oauthlib.flow
|
3 |
-
from google.oauth2.credentials import Credentials
|
4 |
-
from googleapiclient.discovery import build
|
5 |
-
from google.auth.transport.requests import Request
|
6 |
import discord
|
7 |
import logging
|
|
|
8 |
import re
|
9 |
import asyncio
|
|
|
10 |
import subprocess
|
11 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
12 |
from youtube_transcript_api import YouTubeTranscriptApi
|
13 |
from youtube_transcript_api.formatters import TextFormatter
|
14 |
from dotenv import load_dotenv
|
15 |
-
import time
|
16 |
|
17 |
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
18 |
load_dotenv()
|
@@ -20,7 +20,6 @@ load_dotenv()
|
|
20 |
# JSON_TOKEN.json ํ์ผ์ ๊ฒฝ๋ก
|
21 |
credentials_path = 'JSON_TOKEN.json'
|
22 |
token_path = 'token.json'
|
23 |
-
auth_code_path = 'auth_code.txt' # ์ธ์ฆ ์ฝ๋๋ฅผ ์ ์ฅํ ํ์ผ ๊ฒฝ๋ก
|
24 |
|
25 |
# ๋ก๊น
์ค์
|
26 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
@@ -39,50 +38,239 @@ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("
|
|
39 |
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
|
40 |
creds = None
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
# ํ์ผ์ ์ธ์ฆ ์ฝ๋๊ฐ ์ ์ฅ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
|
51 |
-
while not os.path.exists(auth_code_path):
|
52 |
-
print("Waiting for authorization code...")
|
53 |
-
time.sleep(5)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
# ๊ธฐ์กด ํ ํฐ์ ๋ก๋ํ๊ฑฐ๋ ์๋ก ์ธ์ฆ
|
70 |
if os.path.exists(token_path):
|
71 |
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
|
72 |
-
else:
|
73 |
-
creds = authorize()
|
74 |
-
|
75 |
-
# ํ ํฐ ๊ฐฑ์ ๋๋ ์ฌ์ธ์ฆ
|
76 |
if not creds or not creds.valid:
|
77 |
if creds and creds.expired and creds.refresh_token:
|
78 |
creds.refresh(Request())
|
79 |
else:
|
80 |
-
|
|
|
|
|
|
|
81 |
|
82 |
-
# YouTube API ํด๋ผ์ด์ธํธ ์์ฑ
|
83 |
youtube_service = build('youtube', 'v3', credentials=creds)
|
84 |
|
85 |
-
#
|
86 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
87 |
|
88 |
class MyClient(discord.Client):
|
@@ -233,7 +421,7 @@ async def post_replies_to_youtube(video_id, comments, replies):
|
|
233 |
"""
|
234 |
for (comment, comment_id), reply in zip(comments, replies):
|
235 |
try:
|
236 |
-
|
237 |
part='snippet',
|
238 |
body={
|
239 |
'snippet': {
|
@@ -242,11 +430,10 @@ async def post_replies_to_youtube(video_id, comments, replies):
|
|
242 |
}
|
243 |
}
|
244 |
).execute()
|
245 |
-
logging.debug(f'Posted reply to comment: {comment_id}
|
246 |
except Exception as e:
|
247 |
logging.error(f'Error posting reply to comment {comment_id}: {e}')
|
248 |
-
logging.debug(f'Response: {e.resp} | Content: {e.content}')
|
249 |
|
250 |
if __name__ == "__main__":
|
251 |
discord_client = MyClient(intents=intents)
|
252 |
-
discord_client.run(os.getenv('DISCORD_TOKEN'))
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import discord
|
2 |
import logging
|
3 |
+
import os
|
4 |
import re
|
5 |
import asyncio
|
6 |
+
import json
|
7 |
import subprocess
|
8 |
from huggingface_hub import InferenceClient
|
9 |
+
from googleapiclient.discovery import build
|
10 |
+
from google.oauth2.credentials import Credentials
|
11 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
12 |
+
from google.auth.transport.requests import Request
|
13 |
from youtube_transcript_api import YouTubeTranscriptApi
|
14 |
from youtube_transcript_api.formatters import TextFormatter
|
15 |
from dotenv import load_dotenv
|
|
|
16 |
|
17 |
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
18 |
load_dotenv()
|
|
|
20 |
# JSON_TOKEN.json ํ์ผ์ ๊ฒฝ๋ก
|
21 |
credentials_path = 'JSON_TOKEN.json'
|
22 |
token_path = 'token.json'
|
|
|
23 |
|
24 |
# ๋ก๊น
์ค์
|
25 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
|
|
38 |
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
|
39 |
creds = None
|
40 |
|
41 |
+
if os.path.exists(token_path):
|
42 |
+
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
|
43 |
+
if not creds or not creds.valid:
|
44 |
+
if creds and creds.expired and creds.refresh_token:
|
45 |
+
creds.refresh(Request())
|
46 |
+
else:
|
47 |
+
flow = InstalledAppFlow.from_client_secrets_file(credentials_path, SCOPES)
|
48 |
+
creds = flow.run_local_server(port=0)
|
49 |
+
with open(token_path, 'w') as token:
|
50 |
+
token.write(creds.to_json())
|
51 |
+
|
52 |
+
youtube_service = build('youtube', 'v3', credentials=creds)
|
53 |
|
54 |
+
# ํน์ ์ฑ๋ ID
|
55 |
+
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
class MyClient(discord.Client):
|
58 |
+
def __init__(self, *args, **kwargs):
|
59 |
+
super().__init__(*args, **kwargs)
|
60 |
+
self.is_processing = False
|
61 |
+
|
62 |
+
async def on_ready(self):
|
63 |
+
logging.info(f'{self.user}๋ก ๋ก๊ทธ์ธ๋์์ต๋๋ค!')
|
64 |
+
|
65 |
+
# web.py ํ์ผ ์คํ
|
66 |
+
subprocess.Popen(["python", "web.py"])
|
67 |
+
logging.info("Web.py server has been started.")
|
68 |
+
|
69 |
+
# ๋ด์ด ์์๋ ๋ ์๋ด ๋ฉ์์ง๋ฅผ ์ ์ก
|
70 |
+
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
71 |
+
if channel:
|
72 |
+
await channel.send("์ ํ๋ธ ๋น๋์ค URL์ ์
๋ ฅํ๋ฉด, ์๋ง๊ณผ ๋๊ธ์ ๊ธฐ๋ฐ์ผ๋ก ๋ต๊ธ์ ์์ฑํฉ๋๋ค.")
|
73 |
+
|
74 |
+
async def on_message(self, message):
|
75 |
+
if message.author == self.user:
|
76 |
+
return
|
77 |
+
if not self.is_message_in_specific_channel(message):
|
78 |
+
return
|
79 |
+
if self.is_processing:
|
80 |
+
return
|
81 |
+
self.is_processing = True
|
82 |
+
try:
|
83 |
+
video_id = extract_video_id(message.content)
|
84 |
+
if video_id:
|
85 |
+
transcript = await get_best_available_transcript(video_id)
|
86 |
+
comments = await get_video_comments(video_id)
|
87 |
+
if comments and transcript:
|
88 |
+
replies = await generate_replies(comments, transcript)
|
89 |
+
await create_thread_and_send_replies(message, video_id, comments, replies)
|
90 |
+
await post_replies_to_youtube(video_id, comments, replies)
|
91 |
+
else:
|
92 |
+
await message.channel.send("์๋ง์ด๋ ๋๊ธ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.")
|
93 |
+
else:
|
94 |
+
await message.channel.send("์ ํจํ ์ ํ๋ธ ๋น๋์ค URL์ ์ ๊ณตํด ์ฃผ์ธ์.")
|
95 |
+
finally:
|
96 |
+
self.is_processing = False
|
97 |
+
|
98 |
+
def is_message_in_specific_channel(self, message):
|
99 |
+
# ๋ฉ์์ง๊ฐ ์ง์ ๋ ์ฑ๋์ด๊ฑฐ๋, ํด๋น ์ฑ๋์ ์ฐ๋ ๋์ธ ๊ฒฝ์ฐ True ๋ฐํ
|
100 |
+
return message.channel.id == SPECIFIC_CHANNEL_ID or (
|
101 |
+
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
102 |
+
)
|
103 |
+
|
104 |
+
def extract_video_id(url):
|
105 |
+
"""
|
106 |
+
YouTube ๋น๋์ค URL์์ ๋น๋์ค ID๋ฅผ ์ถ์ถํฉ๋๋ค.
|
107 |
+
"""
|
108 |
+
video_id = None
|
109 |
+
youtube_regex = (
|
110 |
+
r'(https?://)?(www\.)?'
|
111 |
+
'(youtube|youtu|youtube-nocookie)\.(com|be)/'
|
112 |
+
'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
|
113 |
+
|
114 |
+
match = re.match(youtube_regex, url)
|
115 |
+
if match:
|
116 |
+
video_id = match.group(6)
|
117 |
+
logging.debug(f'Extracted video ID: {video_id}')
|
118 |
+
return video_id
|
119 |
+
|
120 |
+
async def get_best_available_transcript(video_id):
|
121 |
+
"""
|
122 |
+
YouTube ๋น๋์ค์ ์๋ง์ ๊ฐ์ ธ์ต๋๋ค.
|
123 |
+
"""
|
124 |
+
try:
|
125 |
+
# ํ๊ตญ์ด ์๋ง ์๋
|
126 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['ko'])
|
127 |
+
except Exception as e:
|
128 |
+
logging.warning(f'Error fetching Korean transcript: {e}')
|
129 |
+
try:
|
130 |
+
# ํ๊ตญ์ด ์๋ง์ด ์์ผ๋ฉด ์์ด ์๋ง ์๋
|
131 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
132 |
+
except Exception as e:
|
133 |
+
logging.warning(f'Error fetching English transcript: {e}')
|
134 |
+
try:
|
135 |
+
# ์์ด ์๋ง๋ ์์ผ๋ฉด ๋ค๋ฅธ ์ธ์ด ์๋ง ์๋
|
136 |
+
transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
|
137 |
+
transcript = transcripts.find_manually_created_transcript().fetch()
|
138 |
+
except Exception as e:
|
139 |
+
logging.error(f'Error fetching alternative transcript: {e}')
|
140 |
+
return None
|
141 |
+
|
142 |
+
# ์๋ง ํฌ๋งทํ
|
143 |
+
formatter = TextFormatter()
|
144 |
+
transcript_text = formatter.format_transcript(transcript)
|
145 |
+
logging.debug(f'Fetched transcript: {transcript_text}')
|
146 |
+
return transcript_text
|
147 |
+
|
148 |
+
async def get_video_comments(video_id):
|
149 |
+
"""
|
150 |
+
YouTube ๋น๋์ค์ ๋๊ธ์ ๊ฐ์ ธ์ต๋๋ค.
|
151 |
+
"""
|
152 |
+
comments = []
|
153 |
+
response = youtube_service.commentThreads().list(
|
154 |
+
part='snippet',
|
155 |
+
videoId=video_id,
|
156 |
+
maxResults=100 # ์ต๋ 100๊ฐ์ ๋๊ธ ๊ฐ์ ธ์ค๊ธฐ
|
157 |
+
).execute()
|
158 |
+
|
159 |
+
for item in response.get('items', []):
|
160 |
+
comment = item['snippet']['topLevelComment']['snippet']['textOriginal']
|
161 |
+
comment_id = item['snippet']['topLevelComment']['id']
|
162 |
+
comments.append((comment, comment_id)) # ๋๊ธ๊ณผ ๋๊ธ ID๋ฅผ ํจ๊ป ์ ์ฅ
|
163 |
|
164 |
+
logging.debug(f'Fetched comments: {comments}')
|
165 |
+
return comments
|
166 |
+
|
167 |
+
async def generate_replies(comments, transcript):
|
168 |
+
"""
|
169 |
+
๋๊ธ๊ณผ ์๋ง์ ๊ธฐ๋ฐ์ผ๋ก LLM ๋ต๊ธ์ ์์ฑํฉ๋๋ค.
|
170 |
+
"""
|
171 |
+
replies = []
|
172 |
+
for comment, _ in comments:
|
173 |
+
messages = [
|
174 |
+
{"role": "system", "content": f"๋น๋์ค ์๋ง: {transcript}"},
|
175 |
+
{"role": "user", "content": comment}
|
176 |
+
]
|
177 |
+
loop = asyncio.get_event_loop()
|
178 |
+
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
179 |
+
messages, max_tokens=400, temperature=0.7, top_p=0.85)) # max_tokens ๊ฐ์ ์กฐ์
|
180 |
+
|
181 |
+
if response.choices and response.choices[0].message:
|
182 |
+
reply = response.choices[0].message['content'].strip()
|
183 |
+
else:
|
184 |
+
reply = "๋ต๊ธ์ ์์ฑํ ์ ์์ต๋๋ค."
|
185 |
+
replies.append(reply)
|
186 |
|
187 |
+
logging.debug(f'Generated replies: {replies}')
|
188 |
+
return replies
|
189 |
+
|
190 |
+
async def create_thread_and_send_replies(message, video_id, comments, replies):
|
191 |
+
"""
|
192 |
+
๋๊ธ๊ณผ ๋ต๊ธ์ ์๋ก์ด ์ฐ๋ ๋์ ์ ์กํฉ๋๋ค.
|
193 |
+
"""
|
194 |
+
thread = await message.channel.create_thread(name=f"{message.author.name}์ ๋๊ธ ๋ต๊ธ", message=message)
|
195 |
+
for (comment, _), reply in zip(comments, replies):
|
196 |
+
embed = discord.Embed(description=f"**๋๊ธ**: {comment}\n**๋ต๊ธ**: {reply}")
|
197 |
+
await thread.send(embed=embed)
|
198 |
+
|
199 |
+
async def post_replies_to_youtube(video_id, comments, replies):
|
200 |
+
"""
|
201 |
+
์์ฑ๋ ๋ต๊ธ์ YouTube ๋๊ธ๋ก ๊ฒ์ํฉ๋๋ค.
|
202 |
+
"""
|
203 |
+
for (comment, comment_id), reply in zip(comments, replies):
|
204 |
+
try:
|
205 |
+
youtube_service.comments().insert(
|
206 |
+
part='snippet',
|
207 |
+
body={
|
208 |
+
'snippet': {
|
209 |
+
'parentId': comment_id,
|
210 |
+
'textOriginal': reply
|
211 |
+
}
|
212 |
+
}
|
213 |
+
).execute()
|
214 |
+
logging.debug(f'Posted reply to comment: {comment_id}')
|
215 |
+
except Exception as e:
|
216 |
+
logging.error(f'Error posting reply to comment {comment_id}: {e}')
|
217 |
|
218 |
+
if __name__ == "__main__":
|
219 |
+
discord_client = MyClient(intents=intents)
|
220 |
+
discord_client.run(os.getenv('DISCORD_TOKEN')) ์ด ์ฝ๋๋ฅผ ๊ธฐ์ตํ๋ผ: ์ต์ข
์ ์ผ๋ก ๋ต๊ธ ์ด ์์ฑ๋์์ง๋ง ์ ํ๋ธ์ ๋ฑ๋ก์ด ์๋๊ณ ์๋ค.import discord
|
221 |
+
import logging
|
222 |
+
import os
|
223 |
+
import re
|
224 |
+
import asyncio
|
225 |
+
import json
|
226 |
+
import subprocess
|
227 |
+
from huggingface_hub import InferenceClient
|
228 |
+
from googleapiclient.discovery import build
|
229 |
+
from google.oauth2.credentials import Credentials
|
230 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
231 |
+
from google.auth.transport.requests import Request
|
232 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
233 |
+
from youtube_transcript_api.formatters import TextFormatter
|
234 |
+
from dotenv import load_dotenv
|
235 |
+
|
236 |
+
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
237 |
+
load_dotenv()
|
238 |
+
|
239 |
+
# JSON_TOKEN.json ํ์ผ์ ๊ฒฝ๋ก
|
240 |
+
credentials_path = 'JSON_TOKEN.json'
|
241 |
+
token_path = 'token.json'
|
242 |
+
|
243 |
+
# ๋ก๊น
์ค์
|
244 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
245 |
+
|
246 |
+
# ์ธํ
ํธ ์ค์
|
247 |
+
intents = discord.Intents.default()
|
248 |
+
intents.message_content = True
|
249 |
+
intents.messages = True
|
250 |
+
intents.guilds = True
|
251 |
+
intents.guild_messages = True
|
252 |
+
|
253 |
+
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
254 |
+
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
255 |
+
|
256 |
+
# OAuth 2.0 ์ธ์ฆ ์ค์
|
257 |
+
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
|
258 |
+
creds = None
|
259 |
|
|
|
260 |
if os.path.exists(token_path):
|
261 |
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
|
|
|
|
|
|
|
|
|
262 |
if not creds or not creds.valid:
|
263 |
if creds and creds.expired and creds.refresh_token:
|
264 |
creds.refresh(Request())
|
265 |
else:
|
266 |
+
flow = InstalledAppFlow.from_client_secrets_file(credentials_path, SCOPES)
|
267 |
+
creds = flow.run_local_server(port=0)
|
268 |
+
with open(token_path, 'w') as token:
|
269 |
+
token.write(creds.to_json())
|
270 |
|
|
|
271 |
youtube_service = build('youtube', 'v3', credentials=creds)
|
272 |
|
273 |
+
# ํน์ ์ฑ๋ ID
|
274 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
275 |
|
276 |
class MyClient(discord.Client):
|
|
|
421 |
"""
|
422 |
for (comment, comment_id), reply in zip(comments, replies):
|
423 |
try:
|
424 |
+
youtube_service.comments().insert(
|
425 |
part='snippet',
|
426 |
body={
|
427 |
'snippet': {
|
|
|
430 |
}
|
431 |
}
|
432 |
).execute()
|
433 |
+
logging.debug(f'Posted reply to comment: {comment_id}')
|
434 |
except Exception as e:
|
435 |
logging.error(f'Error posting reply to comment {comment_id}: {e}')
|
|
|
436 |
|
437 |
if __name__ == "__main__":
|
438 |
discord_client = MyClient(intents=intents)
|
439 |
+
discord_client.run(os.getenv('DISCORD_TOKEN')) # ์ด ์ฝ๋๋ฅผ ๊ธฐ์ตํ๋ผ: ์ต์ข
์ ์ผ๋ก ๋ต๊ธ ์ด ์์ฑ๋์์ง๋ง ์ ํ๋ธ์ ๋ฑ๋ก์ด ์๋๊ณ ์๋ค.
|