Download scripts/train_viral_potential.py from Fred808/Insta-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/Fred808/Insta-AI/resolve/main/scripts/train_viral_potential.py
- Command line
-
hf download hf://spaces/Fred808/Insta-AI/scripts/train_viral_potential.py
-
curl -L -o train_viral_potential.py https://huggingface.co/spaces/Fred808/Insta-AI/resolve/main/scripts/train_viral_potential.py
1.16 kB
| 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") | |