Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from googleapiclient.discovery import build
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Setup Sentiment Analysis
|
| 8 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 9 |
+
|
| 10 |
+
# YouTube API Key
|
| 11 |
+
YOUTUBE_API_KEY = "AIzaSyBuNxsm0LnHF0OkbYgMSNHnwu8iVUVi5gc"
|
| 12 |
+
|
| 13 |
+
def extract_video_id(url):
|
| 14 |
+
match = re.search(r"(?:v=|youtu\.be/)([a-zA-Z0-9_-]{11})", url)
|
| 15 |
+
return match.group(1) if match else None
|
| 16 |
+
|
| 17 |
+
def get_youtube_comments(video_url, max_results=20):
|
| 18 |
+
video_id = extract_video_id(video_url)
|
| 19 |
+
if not video_id:
|
| 20 |
+
return None, "Invalid YouTube URL."
|
| 21 |
+
|
| 22 |
+
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 23 |
+
request = youtube.commentThreads().list(
|
| 24 |
+
part="snippet",
|
| 25 |
+
videoId=video_id,
|
| 26 |
+
maxResults=max_results,
|
| 27 |
+
order="relevance",
|
| 28 |
+
textFormat="plainText"
|
| 29 |
+
)
|
| 30 |
+
response = request.execute()
|
| 31 |
+
|
| 32 |
+
comments_data = []
|
| 33 |
+
for item in response["items"]:
|
| 34 |
+
comment_text = item["snippet"]["topLevelComment"]["snippet"]["textDisplay"]
|
| 35 |
+
like_count = item["snippet"]["topLevelComment"]["snippet"].get("likeCount", 0)
|
| 36 |
+
comments_data.append((comment_text, like_count))
|
| 37 |
+
|
| 38 |
+
return comments_data, None
|
| 39 |
+
|
| 40 |
+
def analyze_comments(video_url):
|
| 41 |
+
comments_data, error = get_youtube_comments(video_url)
|
| 42 |
+
if error:
|
| 43 |
+
return error
|
| 44 |
+
|
| 45 |
+
table_md = "| Comment | Sentiment | Likes |\n|---|---|---|\n"
|
| 46 |
+
summary_data = {
|
| 47 |
+
"positive": 0,
|
| 48 |
+
"neutral": 0,
|
| 49 |
+
"negative": 0,
|
| 50 |
+
"max_likes": -1,
|
| 51 |
+
"top_comment": "",
|
| 52 |
+
"top_sentiment": "",
|
| 53 |
+
"most_positive": "",
|
| 54 |
+
"most_negative": ""
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
for comment, likes in comments_data:
|
| 58 |
+
result = sentiment_analyzer(comment)[0]
|
| 59 |
+
sentiment = result["label"]
|
| 60 |
+
|
| 61 |
+
if sentiment == "POSITIVE":
|
| 62 |
+
summary_data["positive"] += 1
|
| 63 |
+
elif sentiment == "NEGATIVE":
|
| 64 |
+
summary_data["negative"] += 1
|
| 65 |
+
else:
|
| 66 |
+
summary_data["neutral"] += 1
|
| 67 |
+
|
| 68 |
+
table_md += f"| {comment} | {sentiment} | {likes} |\n"
|
| 69 |
+
|
| 70 |
+
if likes > summary_data["max_likes"]:
|
| 71 |
+
summary_data["max_likes"] = likes
|
| 72 |
+
summary_data["top_comment"] = comment
|
| 73 |
+
summary_data["top_sentiment"] = sentiment
|
| 74 |
+
|
| 75 |
+
if sentiment == "POSITIVE":
|
| 76 |
+
summary_data["most_positive"] = comment
|
| 77 |
+
if sentiment == "NEGATIVE":
|
| 78 |
+
summary_data["most_negative"] = comment
|
| 79 |
+
|
| 80 |
+
summary = (
|
| 81 |
+
f"\n\n### Summary:\n"
|
| 82 |
+
f"- Most liked comment: \"{summary_data['top_comment']}\" ({summary_data['max_likes']} likes, {summary_data['top_sentiment']})\n"
|
| 83 |
+
f"- Most positive comment: \"{summary_data['most_positive']}\"\n"
|
| 84 |
+
f"- Most negative comment: \"{summary_data['most_negative']}\"\n"
|
| 85 |
+
f"- Sentiment Count: {summary_data['positive']} Positive, {summary_data['neutral']} Neutral, {summary_data['negative']} Negative\n"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return table_md + summary
|
| 89 |
+
|
| 90 |
+
interface = gr.Interface(
|
| 91 |
+
fn=analyze_comments,
|
| 92 |
+
inputs=gr.Textbox(label="Enter the Youtube Link:"),
|
| 93 |
+
outputs=gr.Markdown(),
|
| 94 |
+
title="YouTube Comment Sentiment Analyzer",
|
| 95 |
+
description="Paste a YouTube video link to analyze top comments for sentiment (Positive, Negative, Neutral)."
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
interface.launch()
|