Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,15 +8,19 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
8 |
from sklearn.cluster import KMeans
|
9 |
from datetime import datetime, timedelta
|
10 |
import os
|
11 |
-
from
|
|
|
|
|
|
|
12 |
|
13 |
def create_client(model_name):
|
14 |
-
|
|
|
15 |
|
16 |
client = create_client("CohereForAI/c4ai-command-r-plus")
|
17 |
|
18 |
-
def get_video_stats(
|
19 |
-
youtube = build("youtube", "v3", developerKey=
|
20 |
video_response = youtube.videos().list(
|
21 |
part="snippet,statistics",
|
22 |
id=video_id
|
@@ -40,8 +44,8 @@ def get_video_stats(api_key, video_id):
|
|
40 |
"Comment Count": comment_count
|
41 |
}
|
42 |
|
43 |
-
def get_channel_stats(
|
44 |
-
youtube = build("youtube", "v3", developerKey=
|
45 |
channel_response = youtube.channels().list(
|
46 |
part="statistics",
|
47 |
id=channel_id
|
@@ -55,8 +59,8 @@ def get_channel_stats(api_key, channel_id):
|
|
55 |
|
56 |
return subscriber_count
|
57 |
|
58 |
-
def get_video_data(
|
59 |
-
youtube = build("youtube", "v3", developerKey=
|
60 |
video_ids = []
|
61 |
next_page_token = None
|
62 |
|
@@ -82,9 +86,9 @@ def get_video_data(api_key, query, max_results, published_after, published_befor
|
|
82 |
|
83 |
video_stats = []
|
84 |
for video_id in video_ids:
|
85 |
-
stats = get_video_stats(
|
86 |
channel_id = stats["Channel ID"]
|
87 |
-
subscriber_count = get_channel_stats(
|
88 |
stats["Subscriber Count"] = subscriber_count
|
89 |
video_stats.append(stats)
|
90 |
|
@@ -130,14 +134,11 @@ def analyze_titles(video_stats_df, n_clusters=5):
|
|
130 |
return cluster_summary_df
|
131 |
|
132 |
def summarize_cluster(cluster_text, cluster_num):
|
133 |
-
response = client
|
134 |
-
|
135 |
-
max_tokens=500
|
136 |
-
)
|
137 |
-
summary = response['choices'][0]['text'].strip()
|
138 |
return summary
|
139 |
|
140 |
-
def main(
|
141 |
if query:
|
142 |
# ๊ธฐ๊ฐ ์ค์
|
143 |
now = datetime.utcnow()
|
@@ -151,7 +152,7 @@ def main(api_key, query, max_results, period, page, n_clusters=5):
|
|
151 |
else:
|
152 |
published_after = (now - timedelta(days=30)).isoformat("T") + "Z" # ๊ธฐ๋ณธ๊ฐ 1๊ฐ์
|
153 |
|
154 |
-
video_stats_df = get_video_data(
|
155 |
|
156 |
if page == "Video Ranking":
|
157 |
video_stats_df, fig, csv_download_link = visualize_video_ranking(video_stats_df)
|
@@ -164,7 +165,6 @@ def main(api_key, query, max_results, period, page, n_clusters=5):
|
|
164 |
iface = gr.Interface(
|
165 |
fn=main,
|
166 |
inputs=[
|
167 |
-
gr.components.Textbox(label="YouTube API Key๋ฅผ ์
๋ ฅํ์ธ์", type="password"),
|
168 |
gr.components.Textbox(label="๊ฒ์ ์ฟผ๋ฆฌ"),
|
169 |
gr.components.Slider(minimum=1, maximum=1000, value=5, label="์ต๋ ๊ฒฐ๊ณผ ์"),
|
170 |
gr.components.Dropdown(["1์ฃผ์ผ", "1๊ฐ์", "3๊ฐ์"], label="๊ธฐ๊ฐ"),
|
|
|
8 |
from sklearn.cluster import KMeans
|
9 |
from datetime import datetime, timedelta
|
10 |
import os
|
11 |
+
from huggingface_hub import InferenceApi # Hugging Face Hub API ์ฌ์ฉ
|
12 |
+
|
13 |
+
# ์ฌ๊ธฐ์ YouTube API ํค๋ฅผ ์
๋ ฅํ์ธ์
|
14 |
+
YOUTUBE_API_KEY = "YOUR_YOUTUBE_API_KEY"
|
15 |
|
16 |
def create_client(model_name):
|
17 |
+
token = os.getenv("HF_TOKEN")
|
18 |
+
return InferenceApi(repo_id=model_name, token=token)
|
19 |
|
20 |
client = create_client("CohereForAI/c4ai-command-r-plus")
|
21 |
|
22 |
+
def get_video_stats(video_id):
|
23 |
+
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
24 |
video_response = youtube.videos().list(
|
25 |
part="snippet,statistics",
|
26 |
id=video_id
|
|
|
44 |
"Comment Count": comment_count
|
45 |
}
|
46 |
|
47 |
+
def get_channel_stats(channel_id):
|
48 |
+
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
49 |
channel_response = youtube.channels().list(
|
50 |
part="statistics",
|
51 |
id=channel_id
|
|
|
59 |
|
60 |
return subscriber_count
|
61 |
|
62 |
+
def get_video_data(query, max_results, published_after, published_before):
|
63 |
+
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
64 |
video_ids = []
|
65 |
next_page_token = None
|
66 |
|
|
|
86 |
|
87 |
video_stats = []
|
88 |
for video_id in video_ids:
|
89 |
+
stats = get_video_stats(video_id)
|
90 |
channel_id = stats["Channel ID"]
|
91 |
+
subscriber_count = get_channel_stats(channel_id)
|
92 |
stats["Subscriber Count"] = subscriber_count
|
93 |
video_stats.append(stats)
|
94 |
|
|
|
134 |
return cluster_summary_df
|
135 |
|
136 |
def summarize_cluster(cluster_text, cluster_num):
|
137 |
+
response = client(inputs=cluster_text)
|
138 |
+
summary = response[0]["generated_text"].strip()
|
|
|
|
|
|
|
139 |
return summary
|
140 |
|
141 |
+
def main(query, max_results, period, page, n_clusters=5):
|
142 |
if query:
|
143 |
# ๊ธฐ๊ฐ ์ค์
|
144 |
now = datetime.utcnow()
|
|
|
152 |
else:
|
153 |
published_after = (now - timedelta(days=30)).isoformat("T") + "Z" # ๊ธฐ๋ณธ๊ฐ 1๊ฐ์
|
154 |
|
155 |
+
video_stats_df = get_video_data(query, max_results, published_after, published_before)
|
156 |
|
157 |
if page == "Video Ranking":
|
158 |
video_stats_df, fig, csv_download_link = visualize_video_ranking(video_stats_df)
|
|
|
165 |
iface = gr.Interface(
|
166 |
fn=main,
|
167 |
inputs=[
|
|
|
168 |
gr.components.Textbox(label="๊ฒ์ ์ฟผ๋ฆฌ"),
|
169 |
gr.components.Slider(minimum=1, maximum=1000, value=5, label="์ต๋ ๊ฒฐ๊ณผ ์"),
|
170 |
gr.components.Dropdown(["1์ฃผ์ผ", "1๊ฐ์", "3๊ฐ์"], label="๊ธฐ๊ฐ"),
|