AIRider commited on
Commit
b6549ee
ยท
verified ยท
1 Parent(s): a629f01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
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 transformers import InferenceClient # ๊ฐ€์ •: transformers ๋ชจ๋“ˆ ์‚ฌ์šฉ
 
 
 
12
 
13
  def create_client(model_name):
14
- return InferenceClient(model_name, token=os.getenv("HF_TOKEN"))
 
15
 
16
  client = create_client("CohereForAI/c4ai-command-r-plus")
17
 
18
- def get_video_stats(api_key, video_id):
19
- youtube = build("youtube", "v3", developerKey=api_key)
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(api_key, channel_id):
44
- youtube = build("youtube", "v3", developerKey=api_key)
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(api_key, query, max_results, published_after, published_before):
59
- youtube = build("youtube", "v3", developerKey=api_key)
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(api_key, video_id)
86
  channel_id = stats["Channel ID"]
87
- subscriber_count = get_channel_stats(api_key, channel_id)
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.generate(
134
- prompt=f"๋‹ค์Œ ๋™์˜์ƒ์„ ๋ถ„์„ํ•˜์—ฌ ์š”์•ฝํ•˜๊ณ , 500์ž ์ด๋‚ด๋กœ ๋™์˜์ƒ์˜ ํŠน์ง• ๋ฐ ์ธ๊ธฐ ์š”์ธ์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”: {cluster_text}",
135
- max_tokens=500
136
- )
137
- summary = response['choices'][0]['text'].strip()
138
  return summary
139
 
140
- def main(api_key, query, max_results, period, page, n_clusters=5):
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(api_key, query, max_results, published_after, published_before)
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="๊ธฐ๊ฐ„"),