seawolf2357
commited on
Commit
โข
7edf3cc
1
Parent(s):
b4bdc7a
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
|
|
4 |
import asyncio
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from googleapiclient.discovery import build
|
|
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
@@ -29,9 +31,6 @@ youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
|
|
29 |
# ํน์ ์ฑ๋ ID
|
30 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
31 |
|
32 |
-
# ๋ํ ํ์คํ ๋ฆฌ๋ฅผ ์ ์ฅํ ์ ์ญ ๋ณ์
|
33 |
-
conversation_history = []
|
34 |
-
|
35 |
class MyClient(discord.Client):
|
36 |
def __init__(self, *args, **kwargs):
|
37 |
super().__init__(*args, **kwargs)
|
@@ -43,7 +42,7 @@ class MyClient(discord.Client):
|
|
43 |
# ๋ด์ด ์์๋ ๋ ์๋ด ๋ฉ์์ง๋ฅผ ์ ์ก
|
44 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
45 |
if channel:
|
46 |
-
await channel.send("
|
47 |
|
48 |
async def on_message(self, message):
|
49 |
if message.author == self.user:
|
@@ -54,18 +53,17 @@ class MyClient(discord.Client):
|
|
54 |
return
|
55 |
self.is_processing = True
|
56 |
try:
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
await create_thread_and_send_results(message, keywords, video_details)
|
65 |
else:
|
66 |
-
await message.channel.send(
|
67 |
else:
|
68 |
-
await message.channel.send("
|
69 |
finally:
|
70 |
self.is_processing = False
|
71 |
|
@@ -75,56 +73,84 @@ class MyClient(discord.Client):
|
|
75 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
76 |
)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
part='snippet',
|
103 |
-
|
104 |
-
|
105 |
-
maxResults=5 # ์ํ๋ ๊ฒฐ๊ณผ ์
|
106 |
).execute()
|
107 |
-
|
108 |
-
video_details = []
|
109 |
for item in response.get('items', []):
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
discord_client = MyClient(intents=intents)
|
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
4 |
+
import re
|
5 |
import asyncio
|
6 |
from huggingface_hub import InferenceClient
|
7 |
from googleapiclient.discovery import build
|
8 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
9 |
from dotenv import load_dotenv
|
10 |
|
11 |
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
|
|
31 |
# ํน์ ์ฑ๋ ID
|
32 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
33 |
|
|
|
|
|
|
|
34 |
class MyClient(discord.Client):
|
35 |
def __init__(self, *args, **kwargs):
|
36 |
super().__init__(*args, **kwargs)
|
|
|
42 |
# ๋ด์ด ์์๋ ๋ ์๋ด ๋ฉ์์ง๋ฅผ ์ ์ก
|
43 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
44 |
if channel:
|
45 |
+
await channel.send("์ ํ๋ธ ๋น๋์ค URL์ ์
๋ ฅํ๋ฉด, ์๋ง๊ณผ ๋๊ธ์ ๊ธฐ๋ฐ์ผ๋ก ๋ต๊ธ์ ์์ฑํฉ๋๋ค.")
|
46 |
|
47 |
async def on_message(self, message):
|
48 |
if message.author == self.user:
|
|
|
53 |
return
|
54 |
self.is_processing = True
|
55 |
try:
|
56 |
+
video_id = extract_video_id(message.content)
|
57 |
+
if video_id:
|
58 |
+
transcript = await get_video_transcript(video_id)
|
59 |
+
comments = await get_video_comments(video_id)
|
60 |
+
if comments and transcript:
|
61 |
+
replies = await generate_replies(comments, transcript)
|
62 |
+
await create_thread_and_send_replies(message, video_id, comments, replies)
|
|
|
63 |
else:
|
64 |
+
await message.channel.send("์๋ง์ด๋ ๋๊ธ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.")
|
65 |
else:
|
66 |
+
await message.channel.send("์ ํจํ ์ ํ๋ธ ๋น๋์ค URL์ ์ ๊ณตํด ์ฃผ์ธ์.")
|
67 |
finally:
|
68 |
self.is_processing = False
|
69 |
|
|
|
73 |
isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
|
74 |
)
|
75 |
|
76 |
+
def extract_video_id(url):
|
77 |
+
"""
|
78 |
+
YouTube ๋น๋์ค URL์์ ๋น๋์ค ID๋ฅผ ์ถ์ถํฉ๋๋ค.
|
79 |
+
"""
|
80 |
+
video_id = None
|
81 |
+
youtube_regex = (
|
82 |
+
r'(https?://)?(www\.)?'
|
83 |
+
'(youtube|youtu|youtube-nocookie)\.(com|be)/'
|
84 |
+
'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
|
85 |
+
|
86 |
+
match = re.match(youtube_regex, url)
|
87 |
+
if match:
|
88 |
+
video_id = match.group(6)
|
89 |
+
logging.debug(f'Extracted video ID: {video_id}')
|
90 |
+
return video_id
|
91 |
+
|
92 |
+
async def get_video_transcript(video_id):
|
93 |
+
"""
|
94 |
+
YouTube ๋น๋์ค์ ์๋ง์ ๊ฐ์ ธ์ต๋๋ค.
|
95 |
+
"""
|
96 |
+
try:
|
97 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
98 |
+
transcript_text = " ".join([entry['text'] for entry in transcript])
|
99 |
+
logging.debug(f'Fetched transcript: {transcript_text}')
|
100 |
+
return transcript_text
|
101 |
+
except Exception as e:
|
102 |
+
logging.error(f'Error fetching transcript: {e}')
|
103 |
+
return None
|
104 |
+
|
105 |
+
async def get_video_comments(video_id):
|
106 |
+
"""
|
107 |
+
YouTube ๋น๋์ค์ ๋๊ธ์ ๊ฐ์ ธ์ต๋๋ค.
|
108 |
+
"""
|
109 |
+
comments = []
|
110 |
+
response = youtube_service.commentThreads().list(
|
111 |
part='snippet',
|
112 |
+
videoId=video_id,
|
113 |
+
maxResults=100 # ์ต๋ 100๊ฐ์ ๋๊ธ ๊ฐ์ ธ์ค๊ธฐ
|
|
|
114 |
).execute()
|
115 |
+
|
|
|
116 |
for item in response.get('items', []):
|
117 |
+
comment = item['snippet']['topLevelComment']['snippet']['textOriginal']
|
118 |
+
comments.append(comment)
|
119 |
+
|
120 |
+
logging.debug(f'Fetched comments: {comments}')
|
121 |
+
return comments
|
122 |
+
|
123 |
+
async def generate_replies(comments, transcript):
|
124 |
+
"""
|
125 |
+
๋๊ธ๊ณผ ์๋ง์ ๊ธฐ๋ฐ์ผ๋ก LLM ๋ต๊ธ์ ์์ฑํฉ๋๋ค.
|
126 |
+
"""
|
127 |
+
replies = []
|
128 |
+
for comment in comments:
|
129 |
+
messages = [
|
130 |
+
{"role": "system", "content": f"๋น๋์ค ์๋ง: {transcript}"},
|
131 |
+
{"role": "user", "content": comment}
|
132 |
+
]
|
133 |
+
loop = asyncio.get_event_loop()
|
134 |
+
response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
|
135 |
+
messages, max_tokens=50, temperature=0.7, top_p=0.85))
|
136 |
+
|
137 |
+
if response.choices and response.choices[0].message:
|
138 |
+
reply = response.choices[0].message['content'].strip()
|
139 |
+
else:
|
140 |
+
reply = "๋ต๊ธ์ ์์ฑํ ์ ์์ต๋๋ค."
|
141 |
+
replies.append(reply)
|
142 |
+
|
143 |
+
logging.debug(f'Generated replies: {replies}')
|
144 |
+
return replies
|
145 |
+
|
146 |
+
async def create_thread_and_send_replies(message, video_id, comments, replies):
|
147 |
+
"""
|
148 |
+
๋๊ธ๊ณผ ๋ต๊ธ์ ์๋ก์ด ์ฐ๋ ๋์ ์ ์กํฉ๋๋ค.
|
149 |
+
"""
|
150 |
+
thread = await message.channel.create_thread(name=f"{message.author.name}์ ๋๊ธ ๋ต๊ธ", message=message)
|
151 |
+
for comment, reply in zip(comments, replies):
|
152 |
+
embed = discord.Embed(description=f"**๋๊ธ**: {comment}\n**๋ต๊ธ**: {reply}")
|
153 |
+
await thread.send(embed=embed)
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
discord_client = MyClient(intents=intents)
|