Predict Emotions for Trending Youtube Videos

Predict Emotions for Trending Youtube Videos

How I solved the problem of Predicting Emotions of a trending youtube video using MindsDB

ยท

3 min read

Introduction

With the immense growth of video-sharing platforms like YouTube, understanding the emotional engagement of the audience has become increasingly important for content creators and marketers.

Predicting the emotional response to videos can help creators/companies make data-driven decisions and enhance their content strategy.

In this article, we will explore how to predict emotions for trending YouTube videos using machine learning techniques. We will leverage the power of In-Database natural language processing (NLP) to extract features from video titles and descriptions and use them to train a machine-learning model to predict the emotions associated with the videos with the help of MindsDB Hugging face model

Design & Implementation

The first step is to extract the data from the google youtube API. Below is the function which does that.

def get_trending_videos(region_code):
    # Define the resource we want to get (trending videos in this case)
    resource = youtube.videos().list(
        part='snippet,statistics',
        chart='mostPopular',
        regionCode=region_code,
        maxResults=50
    )

    videos = []
    try:
        # Execute the request and extract the required information from the API response
        response = resource.execute()
        for video in response['items']:
            try:
                # Get the video details
                trending_date = video['snippet']['publishedAt']
                title = video['snippet']['title']
                channelTitle = video['snippet']['channelTitle']
                view_count = video['statistics'].get('viewCount', 0)
                likes = video['statistics'].get('likeCount', 0)
                dislikes = video['statistics'].get('dislikeCount', 0)

                if langid.classify(title)[0] == 'en':
                    # Add the video details to the list of videos
                    videos.append({
                        'Trending Date': trending_date,
                        'text': title,
                        'Channel Title': channelTitle,
                        'Views': view_count,
                        'Likes': likes,
                        'Dislikes': dislikes
                    })

            except KeyError as e:
                print(f"Skipping video with missing field: {str(e)}")
                continue

    except HttpError as e:
        print(f"An HTTP error {e.resp.status} occurred: {e.content}")
        return None

    return videos

And the next step is to create the below ML model in MindsDB.

CREATE MODEL mindsdb.hf_emotions_6
PREDICT PRED
USING
engine = 'huggingface',
task = 'text-classification',
model_name = 'j-hartmann/emotion-english-distilroberta-base',
input_column = 'text';

Once the model is created, you can query the status of the model using the below command.

SELECT *
FROM mindsdb.models 
WHERE name = 'hf_emotions_6';

Now use the below Python function to pass your data & query the predictions

def predict_from_mindsdb(df: pd.DataFrame):
    server=mdb.connect(login=MDB_EMAIL,password=MDB_PWD)
    model=server.get_project('mindsdb').get_model(MODEL_NAME)
    pred_df = pd.DataFrame(columns=['text'])
    pred_df['text'] = df['text']
    try: 
        ret_df = model.predict(pred_df)
    except Exception as e:
        print('Not able to generate predictions at the moment')
    return ret_df

The entire code is listed in this repository here, and the project is live here

Working Screenshots

The project is live and hosted in the following link: https://bala-ceg-trending-videos-emotio-youtube-trending-mindsdb-5la4kh.streamlit.app/

Conclusion

In conclusion, predicting emotions for trending YouTube videos is a challenging task, but with the help of the MindsDB machine learning model, we can make predictions with a reasonable level of accuracy. This project has demonstrated the potential of using machine learning techniques to analyze and classify emotions associated with YouTube videos, which can have significant applications in the areas of marketing, advertising, and content creation.

If you want to contribute to this project, you are most welcome! And finally, a big thank you for taking the time to read my article. Please support me by liking the article and sharing the article if you like it, and follow me for more related articles.

#mindsdb @mindsdb @hashnode #machinelearning #AI #datascience #mindsdbhackathon

Did you find this article valuable?

Support Balaji Seetharaman by becoming a sponsor. Any amount is appreciated!

ย