from app import create_app
from app.ml.naive_bayes_classifier import NaiveBayesSpamClassifier
from app.models import SpamTrainingSample


def main():
    app = create_app()
    with app.app_context():
        rows = SpamTrainingSample.query.filter_by(is_active=True).order_by(SpamTrainingSample.id.asc()).all()
        samples = [{"text": row.text, "label": row.label} for row in rows]
        clf = NaiveBayesSpamClassifier(app.config["NB_MODEL_PATH"])
        meta = clf.train(samples)
        print(f"模型训练完成: {meta.get('version')} 样本数={meta.get('sample_count')}")


if __name__ == "__main__":
    main()
