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")