File size: 1,160 Bytes
40fb94f
 
 
 
 
 
827161e
 
40fb94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
827161e
 
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
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import joblib
from utils.preprocessing import preprocess_data
from huggingface_hub import HfApi, login
import os

def train_viral_potential():
    """Train the viral potential prediction model."""
    # Load data
    df = pd.read_json("data/raw/engagement_metrics.json")
    df = preprocess_data(df)

    # Train viral potential model
    viral_threshold = df['engagement_rate'].quantile(0.9)
    df['viral'] = df['engagement_rate'].apply(lambda x: 1 if x >= viral_threshold else 0)

    X = df[['caption_length', 'hashtag_count', 'sentiment']]
    y = df['viral']
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    viral_model = RandomForestClassifier(random_state=42)
    viral_model.fit(X_train, y_train)
    y_pred = viral_model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    print(f"Viral Potential Model Accuracy: {accuracy:.4f}")

    # Save the model locally
    joblib.dump(viral_model, "viral_potential_model.pkl")