File size: 3,302 Bytes
7ff2308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d298cff
7ff2308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

import os

os.system("python3 -m pip install --upgrade pip")
os.system("pip install httpx==0.24.1")
os.system("pip uninstall -y gradio")
os.system("pip install gradio==3.1.4")

import gradio as gr
import hopsworks
import joblib
import pandas as pd
from googleapiclient.discovery import build
import re

hopsworks_key = "LR2zRcmisfNRQu0h.Hk1RWXOxv3HzMk54dE7iYDFMawiK6PYxb42sjHx8iQsc7D0h6Fsy76Ult5OJFmSi"

youtube = build(
    'youtube',
    'v3',
    developerKey="AIzaSyAOsM68BSlRzcCReBf1Houhoe9zvTAaNFU"
)

project = hopsworks.login(api_key_value=hopsworks_key)
fs = project.get_feature_store()


mr = project.get_model_registry()
model = mr.get_model("comments_model", version=2)
model_dir = model.download()
model = joblib.load(model_dir + "/comments_model.pkl")
vectorizer = joblib.load(model_dir + "/vectorizer.pkl")
print("Model downloaded")

def get_video_id(video_link):

    # Define a regular expression pattern to match YouTube video URLs
    pattern = (
        r'(?:https?://)?(?:www\.)?'
        '(?:youtube\.com/.*?[?&]v=|youtu\.be/|youtube\.com/embed/|youtube\.com/v/|youtube\.com/e/|youtube\.com/user/[^/]+/u/0/|www\.youtube\.com/user/[^/]+/u/0/|youtube\.com/s[^/]+/|www\.youtube\.com/s[^/]+/|youtube\.com/channel/|youtube\.com/c/|youtube\.com/user/[^/]+/|youtube\.com/user/[^/]+/live/|twitch\.tv/)'
        '([^"&?/ ]{11})'
    )

    # Use re.search to find the video ID in the URL
    match = re.search(pattern, video_link)

    # If a match is found, return the video ID; otherwise, return None
    return match.group(1) if match else None


def sentiment(video_link):
    print("Calling function")
    video_id = get_video_id(video_link)
    request = youtube.commentThreads().list(
    part="snippet",
    videoId=video_id,
    maxResults=100
)
    response = request.execute()

    comments = []
    for item in response['items']:
        comment = item['snippet']['topLevelComment']['snippet']
        comment_text = ''.join(e for e in comment['textDisplay'] if (e.isalnum() or e.isspace()))
        comments.append([comment_text])

    df = pd.DataFrame(comments, columns=['comment'])
    df = df.dropna(subset=['comment'])
    comments_features = vectorizer.transform(df['comment'])
    predictions = model.predict(comments_features)
    positive_count = sum(predictions > 0)
    negative_count = sum(predictions < 0)
    total_count = len(predictions)
    positive_percentage = (positive_count / total_count) * 100
    negative_percentage = (negative_count / total_count) * 100
    return positive_count, negative_count, f"{positive_percentage:.2f}%", f"{negative_percentage:.2f}%"
        
demo = gr.Interface(
    fn=sentiment,
    title="YouTube comment sentiment analysis",
    description="Experiment with YouTube comments to predict the YouTube video sentiments.",
    allow_flagging="never",
    inputs=gr.Textbox(type="text", label="input YouTube video link",variable="video_link"),
    outputs=[
        gr.Number(label="The number of positive comments", default=0), 
        gr.Number(label="The number of negative comments", default=0),
        gr.Textbox(label="Percentage of positive comments", name="positive_percentage"),
        gr.Textbox(label="Percentage of negative comments", name="negative_percentage"),
    ],
)

demo.launch(debug=True, share=True)