File size: 2,421 Bytes
9df97a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Simple smoke test: load baseline model and run a prediction on a sample pair.

Run from repository root:
    /Users/elhadjibassirousy/Desktop/AI-Talent-Finder/.venv/bin/python backend/scripts/smoke_test_predict.py

This script avoids starting the HTTP server and directly loads the saved model
and its TF-IDF + SVD meta to compute a prediction on a synthetic example.
"""
import sys
from pathlib import Path
import joblib
import numpy as np

script_dir = Path(__file__).resolve().parent
repo_root = script_dir.parent.parent
if str(repo_root / 'backend') not in sys.path:
    sys.path.insert(0, str(repo_root / 'backend'))

from app.services.feature_engineering import PairFeatureMeta, build_pair_features


def main():
    model_path = repo_root / 'models' / 'baseline_model.joblib'
    if not model_path.exists():
        print('Baseline model not found. Expected at', model_path)
        sys.exit(3)

    try:
        bundle = joblib.load(model_path)
    except Exception as e:
        print('Failed to load joblib model:', e)
        sys.exit(4)

    model = bundle.get('model')
    meta = bundle.get('meta') or {}

    if isinstance(meta, PairFeatureMeta):
        feature_meta = meta
    else:
        tf = meta.get('tfidf') or meta.get('tf')
        svd = meta.get('svd')
        if tf is None or svd is None:
            print('Model meta missing tfidf/tf or svd objects')
            sys.exit(5)
        feature_meta = PairFeatureMeta(tfidf=tf, svd=svd)

    cand_text = 'Jean Dupont; Python; SQL; Docker; 5 years experience as backend developer'
    job_text = 'Senior Python Backend Developer. Required: Python, SQL, Docker, microservices.'

    try:
        X = build_pair_features(cand_text, job_text, feature_meta)
    except Exception as e:
        print('Feature building failed:', e)
        sys.exit(6)

    try:
        prob = None
        try:
            prob = model.predict_proba(X)[:,1][0]
        except Exception:
            try:
                prob = model.decision_function(X)[0]
                prob = 1/(1+np.exp(-prob))
            except Exception:
                prob = float(model.predict(X)[0])

        print('Prediction probability (0-1):', prob)
        print('Prediction percent:', float(max(0, min(100, prob*100))))
    except Exception as e:
        print('Model prediction failed:', e)
        sys.exit(7)

    print('Smoke test OK')


if __name__ == '__main__':
    main()