Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +78 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from googleapiclient.discovery import build
|
| 4 |
+
import re
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# ⛔ Replace with your actual API key
|
| 8 |
+
YOUTUBE_API_KEY = "AIzaSyBHwvf0dEMzAkrHol7FBoWu_1cnwGDMAvA"
|
| 9 |
+
|
| 10 |
+
# Setup: Load Hugging Face sentiment analysis pipeline
|
| 11 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 12 |
+
|
| 13 |
+
# Function to extract video ID from YouTube URL
|
| 14 |
+
def extract_video_id(url):
|
| 15 |
+
patterns = [
|
| 16 |
+
r"(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([^&\n?#]+)",
|
| 17 |
+
r"youtube\.com\/shorts\/([^&\n?#]+)"
|
| 18 |
+
]
|
| 19 |
+
for pattern in patterns:
|
| 20 |
+
match = re.search(pattern, url)
|
| 21 |
+
if match:
|
| 22 |
+
return match.group(1)
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
# Function to fetch comments using YouTube API
|
| 26 |
+
def fetch_comments(video_url, max_results=10):
|
| 27 |
+
video_id = extract_video_id(video_url)
|
| 28 |
+
if not video_id:
|
| 29 |
+
return pd.DataFrame({"error": ["Invalid YouTube URL"]})
|
| 30 |
+
|
| 31 |
+
youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 32 |
+
request = youtube.commentThreads().list(
|
| 33 |
+
part="snippet",
|
| 34 |
+
videoId=video_id,
|
| 35 |
+
maxResults=max_results,
|
| 36 |
+
textFormat="plainText"
|
| 37 |
+
)
|
| 38 |
+
comments = []
|
| 39 |
+
try:
|
| 40 |
+
response = request.execute()
|
| 41 |
+
for item in response["items"]:
|
| 42 |
+
comment = item["snippet"]["topLevelComment"]["snippet"]["textDisplay"]
|
| 43 |
+
comments.append(comment)
|
| 44 |
+
return pd.DataFrame({"Comment": comments})
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return pd.DataFrame({"error": [str(e)]})
|
| 47 |
+
|
| 48 |
+
# Main analysis function
|
| 49 |
+
def analyze_video(video_url, max_comments=10):
|
| 50 |
+
df = fetch_comments(video_url, max_comments)
|
| 51 |
+
if "error" in df.columns:
|
| 52 |
+
return df.to_string(index=False)
|
| 53 |
+
|
| 54 |
+
results = []
|
| 55 |
+
for comment in df["Comment"]:
|
| 56 |
+
result = sentiment_pipeline(comment[:512])[0]
|
| 57 |
+
results.append({
|
| 58 |
+
"Comment": comment,
|
| 59 |
+
"Sentiment": result["label"],
|
| 60 |
+
"Score": round(result["score"], 3)
|
| 61 |
+
})
|
| 62 |
+
result_df = pd.DataFrame(results)
|
| 63 |
+
return result_df
|
| 64 |
+
|
| 65 |
+
# Gradio UI
|
| 66 |
+
with gr.Blocks(title="YouTube Comment Sentiment Analyzer") as demo:
|
| 67 |
+
gr.Markdown("# 📊 YouTube Comment Sentiment Analyzer")
|
| 68 |
+
|
| 69 |
+
video_url = gr.Textbox(label="📺 YouTube Video URL", placeholder="Paste the video link here")
|
| 70 |
+
max_comments = gr.Slider(1, 100, value=10, step=1, label="Number of Comments")
|
| 71 |
+
|
| 72 |
+
btn = gr.Button("Analyze")
|
| 73 |
+
|
| 74 |
+
output = gr.Dataframe(label="Sentiment Analysis Result", interactive=False)
|
| 75 |
+
|
| 76 |
+
btn.click(fn=analyze_video, inputs=[video_url, max_comments], outputs=output)
|
| 77 |
+
|
| 78 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.10.0
|
| 2 |
+
google-api-python-client==2.115.0
|
| 3 |
+
transformers==4.38.2
|
| 4 |
+
torch
|
| 5 |
+
pandas
|