File size: 1,376 Bytes
79cb04a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
One-time training script for the RetentionCurvePredictor.

Usage:
    python scripts/train_retention_model.py

Steps:
    1. Builds retention_dataset.json if it doesn't exist
    2. Trains the RetentionCurvePredictor
    3. Prints train/val MAE per timepoint
    4. Saves model to viral_script_engine/retention/model.joblib
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from viral_script_engine.retention.training_data.build_dataset import build, _OUTPUT_PATH
from viral_script_engine.retention.curve_predictor import RetentionCurvePredictor

_CULTURAL_KB_PATH = str(
    Path(__file__).parent.parent / "viral_script_engine" / "data" / "cultural_kb.json"
)


def main():
    # Step 1: build dataset if missing
    if not _OUTPUT_PATH.exists():
        print("Building retention dataset...")
        out = build()
        print(f"  Dataset created: {out}")
    else:
        print(f"Dataset already exists: {_OUTPUT_PATH}")

    # Step 2: train the predictor
    print("\nTraining RetentionCurvePredictor...")
    predictor = RetentionCurvePredictor()
    result = predictor.train(
        dataset_path=str(_OUTPUT_PATH),
        cultural_kb_path=_CULTURAL_KB_PATH,
    )

    avg_mae = result["avg_mae"]
    print(f"\nRetention model trained. Avg MAE: {avg_mae:.4f}. Model saved.")


if __name__ == "__main__":
    main()