Spaces:
Runtime error
Runtime error
seawolf2357
commited on
Commit
β’
b4bdc7a
1
Parent(s):
4f7204d
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
-
import requests
|
6 |
import asyncio
|
|
|
7 |
from googleapiclient.discovery import build
|
8 |
from dotenv import load_dotenv
|
9 |
|
@@ -23,17 +22,12 @@ intents.guild_messages = True
|
|
23 |
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
24 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
25 |
|
26 |
-
# Pexels API ν€ μ€μ
|
27 |
-
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
28 |
-
PEXELS_VIDEO_API_URL = "https://api.pexels.com/videos/search"
|
29 |
-
|
30 |
# YouTube API ν€ μ€μ
|
31 |
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
32 |
youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
|
33 |
|
34 |
# νΉμ μ±λ ID
|
35 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
36 |
-
YOUTUBE_MUSIC_CHANNEL_ID = os.getenv("YOUTUBE_MUSIC_CHANNEL_ID")
|
37 |
|
38 |
# λν νμ€ν 리λ₯Ό μ μ₯ν μ μ λ³μ
|
39 |
conversation_history = []
|
@@ -49,7 +43,7 @@ class MyClient(discord.Client):
|
|
49 |
# λ΄μ΄ μμλ λ μλ΄ λ©μμ§λ₯Ό μ μ‘
|
50 |
channel = self.get_channel(SPECIFIC_CHANNEL_ID)
|
51 |
if channel:
|
52 |
-
await channel.send("
|
53 |
|
54 |
async def on_message(self, message):
|
55 |
if message.author == self.user:
|
@@ -60,18 +54,16 @@ class MyClient(discord.Client):
|
|
60 |
return
|
61 |
self.is_processing = True
|
62 |
try:
|
63 |
-
#
|
64 |
keywords = await extract_keywords(message)
|
65 |
if keywords:
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
music_urls = await search_music_on_youtube(keywords)
|
70 |
-
if video_urls or music_urls:
|
71 |
# μμ²μμμ μ°λ λ μμ± λ° κ²°κ³Ό μ μ‘
|
72 |
-
await create_thread_and_send_results(message, keywords,
|
73 |
else:
|
74 |
-
await message.channel.send(f"**{keywords}**μ λν
|
75 |
else:
|
76 |
await message.channel.send("ν€μλλ₯Ό μΆμΆν μ μμ΅λλ€.")
|
77 |
finally:
|
@@ -104,46 +96,35 @@ async def extract_keywords(message):
|
|
104 |
logging.debug(f'Extracted keywords: {keywords}')
|
105 |
return keywords
|
106 |
|
107 |
-
async def
|
108 |
-
headers = {
|
109 |
-
"Authorization": PEXELS_API_KEY
|
110 |
-
}
|
111 |
-
params = {
|
112 |
-
"query": keywords,
|
113 |
-
"per_page": 10 # μ΅λ 10κ° λΉλμ€λ₯Ό κ°μ Έμ΅λλ€
|
114 |
-
}
|
115 |
-
response = requests.get(PEXELS_VIDEO_API_URL, headers=headers, params=params)
|
116 |
-
if response.status_code == 200:
|
117 |
-
data = response.json()
|
118 |
-
return [video['video_files'][0]['link'] for video in data['videos']]
|
119 |
-
return None
|
120 |
-
|
121 |
-
async def search_music_on_youtube(keywords):
|
122 |
response = youtube_service.search().list(
|
123 |
q=keywords,
|
124 |
part='snippet',
|
125 |
type='video',
|
126 |
-
|
127 |
-
|
128 |
).execute()
|
129 |
-
|
|
|
130 |
for item in response.get('items', []):
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
135 |
# μ°λ λ μμ±
|
136 |
-
thread = await message.channel.create_thread(name=f"{message.author.name}μ κ²μ κ²°κ³Ό", message=message)
|
137 |
-
if
|
138 |
-
message_content = f"**{keywords}**μ λν
|
139 |
-
await thread.send(message_content)
|
140 |
-
for url in video_urls:
|
141 |
-
await thread.send(url)
|
142 |
-
if music_urls:
|
143 |
-
message_content = f"**{keywords}**μ λν μμ
{len(music_urls)}κ°λ₯Ό μ°Ύμμ΅λλ€:"
|
144 |
await thread.send(message_content)
|
145 |
-
for
|
146 |
-
|
|
|
|
|
147 |
|
148 |
if __name__ == "__main__":
|
149 |
discord_client = MyClient(intents=intents)
|
|
|
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 |
|
|
|
22 |
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
23 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
|
24 |
|
|
|
|
|
|
|
|
|
25 |
# YouTube API ν€ μ€μ
|
26 |
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
27 |
youtube_service = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
|
28 |
|
29 |
# νΉμ μ±λ ID
|
30 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
|
31 |
|
32 |
# λν νμ€ν 리λ₯Ό μ μ₯ν μ μ λ³μ
|
33 |
conversation_history = []
|
|
|
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 |
return
|
55 |
self.is_processing = True
|
56 |
try:
|
57 |
+
# μ£Όμ ν€μλ μΆμΆ
|
58 |
keywords = await extract_keywords(message)
|
59 |
if keywords:
|
60 |
+
# YouTube APIλ‘ μ΅μ νΈλ λ λ° μΈκΈ° λΉλμ€ κ²μ
|
61 |
+
video_details = await search_trending_videos(keywords)
|
62 |
+
if video_details:
|
|
|
|
|
63 |
# μμ²μμμ μ°λ λ μμ± λ° κ²°κ³Ό μ μ‘
|
64 |
+
await create_thread_and_send_results(message, keywords, video_details)
|
65 |
else:
|
66 |
+
await message.channel.send(f"**{keywords}**μ λν μ΅μ νΈλ λ λΉλμ€λ₯Ό μ°Ύμ μ μμ΅λλ€.")
|
67 |
else:
|
68 |
await message.channel.send("ν€μλλ₯Ό μΆμΆν μ μμ΅λλ€.")
|
69 |
finally:
|
|
|
96 |
logging.debug(f'Extracted keywords: {keywords}')
|
97 |
return keywords
|
98 |
|
99 |
+
async def search_trending_videos(keywords):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
response = youtube_service.search().list(
|
101 |
q=keywords,
|
102 |
part='snippet',
|
103 |
type='video',
|
104 |
+
order='viewCount', # μΈκΈ° μλ λΉλμ€ κΈ°μ€μΌλ‘ μ λ ¬
|
105 |
+
maxResults=5 # μνλ κ²°κ³Ό μ
|
106 |
).execute()
|
107 |
+
|
108 |
+
video_details = []
|
109 |
for item in response.get('items', []):
|
110 |
+
video_details.append({
|
111 |
+
'title': item['snippet']['title'],
|
112 |
+
'url': f"https://www.youtube.com/watch?v={item['id']['videoId']}",
|
113 |
+
'description': item['snippet']['description'],
|
114 |
+
'thumbnail': item['snippet']['thumbnails']['high']['url']
|
115 |
+
})
|
116 |
+
return video_details
|
117 |
+
|
118 |
+
async def create_thread_and_send_results(message, keywords, video_details):
|
119 |
# μ°λ λ μμ±
|
120 |
+
thread = await message.channel.create_thread(name=f"{message.author.name}μ νΈλ λ κ²μ κ²°κ³Ό", message=message)
|
121 |
+
if video_details:
|
122 |
+
message_content = f"**{keywords}**μ λν μ΅μ νΈλ λ λΉλμ€ {len(video_details)}κ°λ₯Ό μ°Ύμμ΅λλ€:"
|
|
|
|
|
|
|
|
|
|
|
123 |
await thread.send(message_content)
|
124 |
+
for video in video_details:
|
125 |
+
embed = discord.Embed(title=video['title'], description=video['description'], url=video['url'])
|
126 |
+
embed.set_thumbnail(url=video['thumbnail'])
|
127 |
+
await thread.send(embed=embed)
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
discord_client = MyClient(intents=intents)
|