Spaces:
Runtime error
Runtime error
Commit ·
979c758
1
Parent(s): 68aaa09
🚧 download_youtube_comments
Browse files- app.py +2 -0
- requirements.txt +1 -0
- tools.py +28 -0
app.py
CHANGED
|
@@ -25,6 +25,7 @@ from tools import (
|
|
| 25 |
fetch_file,
|
| 26 |
normalize_place_name,
|
| 27 |
is_drink,
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
# (Keep Constants as is)
|
|
@@ -90,6 +91,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 90 |
fetch_file,
|
| 91 |
normalize_place_name,
|
| 92 |
is_drink,
|
|
|
|
| 93 |
],
|
| 94 |
verbosity_level=2,
|
| 95 |
additional_authorized_imports=authorized_imports,
|
|
|
|
| 25 |
fetch_file,
|
| 26 |
normalize_place_name,
|
| 27 |
is_drink,
|
| 28 |
+
download_youtube_comments,
|
| 29 |
)
|
| 30 |
|
| 31 |
# (Keep Constants as is)
|
|
|
|
| 91 |
fetch_file,
|
| 92 |
normalize_place_name,
|
| 93 |
is_drink,
|
| 94 |
+
download_youtube_comments,
|
| 95 |
],
|
| 96 |
verbosity_level=2,
|
| 97 |
additional_authorized_imports=authorized_imports,
|
requirements.txt
CHANGED
|
@@ -6,3 +6,4 @@ smolagents[toolkit]
|
|
| 6 |
torch
|
| 7 |
transformers
|
| 8 |
wikipedia-api
|
|
|
|
|
|
| 6 |
torch
|
| 7 |
transformers
|
| 8 |
wikipedia-api
|
| 9 |
+
youtube-comment-downloader
|
tools.py
CHANGED
|
@@ -7,6 +7,10 @@ import spaces
|
|
| 7 |
from transformers import pipeline
|
| 8 |
from pdfminer.high_level import extract_text
|
| 9 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
@tool
|
|
@@ -353,3 +357,27 @@ def is_drink(item_name: str) -> bool:
|
|
| 353 |
"""
|
| 354 |
drinks = ["soda", "cola", "coffee", "tea", "juice", "milkshake", "water"]
|
| 355 |
return item_name.lower() in drinks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
from pdfminer.high_level import extract_text
|
| 9 |
import os
|
| 10 |
+
from youtube_comment_downloader import (
|
| 11 |
+
YoutubeCommentDownloader,
|
| 12 |
+
SORT_BY_POPULAR,
|
| 13 |
+
)
|
| 14 |
|
| 15 |
|
| 16 |
@tool
|
|
|
|
| 357 |
"""
|
| 358 |
drinks = ["soda", "cola", "coffee", "tea", "juice", "milkshake", "water"]
|
| 359 |
return item_name.lower() in drinks
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
@tool
|
| 363 |
+
def download_youtube_comments(url: str, max_comments: int = 100) -> list[str]:
|
| 364 |
+
"""
|
| 365 |
+
Download up to `max_comments` popular comments from a YouTube video.
|
| 366 |
+
|
| 367 |
+
Args:
|
| 368 |
+
url (str): Full URL of a YouTube video (e.g. "https://www.youtube.com/watch?v=abc123").
|
| 369 |
+
max_comments (int): Maximum number of comments to retrieve (default is 100).
|
| 370 |
+
|
| 371 |
+
Returns:
|
| 372 |
+
list[str]: List of plain-text comment strings. Returns an empty list if the video
|
| 373 |
+
can't be accessed or no comments are found.
|
| 374 |
+
"""
|
| 375 |
+
downloader = YoutubeCommentDownloader()
|
| 376 |
+
comments = []
|
| 377 |
+
for comment in downloader.get_comments_from_url(
|
| 378 |
+
url, sort_by=SORT_BY_POPULAR
|
| 379 |
+
):
|
| 380 |
+
comments.append(comment["text"])
|
| 381 |
+
if len(comments) >= max_comments:
|
| 382 |
+
break
|
| 383 |
+
return comments
|