File size: 1,432 Bytes
eb7a003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

pipeline.py - Main training pipeline, run this to retrain the model

"""

import subprocess as sub
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent

try :
    import nbformat
    from nbconvert.preprocessors import ExecutePreprocessor
except ModuleNotFoundError :
    print("Please Install nbconvert using pip or uv")
    nbformat = None
    ExecutePreprocessor = None

def run_notebook(notebook_path: str, kernel_name: str = "python3") -> None:
    if nbformat is None or ExecutePreprocessor is None:
        print(f"Skipping {notebook_path} (nbformat/nbconvert not installed)")
        return

    print(f"Starting: {notebook_path}")

    with open(notebook_path, "r", encoding="utf-8") as f:
        nb = nbformat.read(f, as_version=4)
        
    ep = ExecutePreprocessor(timeout=600, kernel_name=kernel_name)

    ep.preprocess(nb, {"metadata": {"path": "./"}})

    with open(notebook_path, "w", encoding="utf-8") as f:
        nbformat.write(nb, f)

    print(f"Finished: {notebook_path}\n")
    
def run_scripts(script_path: str, shell_name: str = "python") -> None:
    print(f"Running scripts from {script_path}")

    sub.run([f"{shell_name}", f"{script_path}"], cwd=REPO_ROOT, check=True)

run_notebook("notebooks/01_EDA.ipynb")
run_notebook("notebooks/02_Data_Engineering.ipynb")
run_scripts("model/prep/data_prep.py")
run_scripts("model/Model.py")