File size: 3,614 Bytes
308b9ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d1382d
308b9ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from pathlib import Path
import gradio as gr
import pandas as pd
import os

# ---------- Paths ----------
from .config import build_paths, UI_EXAMPLES

HERE = Path(__file__).resolve()
SRC_DIR = HERE.parents[1]

p = build_paths(SRC_DIR)

MODEL_DIR   = p["MODEL_DIR"]
MODEL_PATH  = p["MODEL_PATH"]
FEATURE_SCALER_PATH = p.get("FEATURE_SCALER_PATH")
TARGET_SCALER_PATH  = p.get("TARGET_SCALER_PATH")
ENCODER_PATH = p["ENCODER_PATH"]
SCHEMA_PATH = p["SCHEMA_PATH"]
LOGS_DIR    = p["LOGS_DIR"]; LOGS_DIR.mkdir(parents=True, exist_ok=True)
DB_PATH     = p["DB_PATH"]
REPORT_PATH = p["REPORT_PATH"]

# ---------- Load model & schema ----------
from .model_loader import load_model_and_schema, load_optional_joblib

model, schema, TARGET_NAME, FEATURES, INTERNAL_EXPECTED = load_model_and_schema(
    MODEL_PATH, SCHEMA_PATH
)
fx_scaler = load_optional_joblib(FEATURE_SCALER_PATH)
y_scaler  = load_optional_joblib(TARGET_SCALER_PATH)
encoder   = load_optional_joblib(ENCODER_PATH)
UI_FEATURE_NAMES = [f["name"] for f in FEATURES]

# ---------- Helpers ----------
from .helpers.log_utils import log_prediction
from .helpers.predict_utils import predict_single
from .helpers.schema_utils import get_bounds
from .helpers.report_utils import read_model_report, report_summary_df, report_metrics_df
from .helpers.sqlite_utils import load_val_subset

# ---------- UI ----------
def build_app():
    app_title   = f"TrAIn.me — (v5.0-minimal)"
    app_desc_ml = "Personalize your experience"
    app_desc_ex = "Choose your training program"
    # app_desc_dl = "Generate your personalized exercise"
    app_desc_dl_exec = "Execution generator"

    from .pages.ml_tab import render_ml_tab
    # from .pages.exercices_tab import render_list_of_exercices
    # from .pages.dl_tab import render_dl_tab
    from .pages.dl_execution_tab import render_dl_execution_tab
    from .config import UI_EXAMPLES

    with gr.Blocks(title=app_title) as demo:
        gr.Markdown(f"# {app_title}\n{app_desc_ml} / {app_desc_dl_exec}")
        with gr.Tabs():
            # Onglet 1 : ML
            level_out, wf_out, wt_out = render_ml_tab(
                app_desc_ml=app_desc_ml,
                feature_specs=FEATURES,
                ui_feature_names=UI_FEATURE_NAMES,
                internal_expected=INTERNAL_EXPECTED,
                target_name=TARGET_NAME,
                schema=schema,
                ui_examples=UI_EXAMPLES,
                db_path=DB_PATH,
                model=model,
                logs_dir=LOGS_DIR,
                model_path=MODEL_PATH,
                feature_scaler=fx_scaler,
                target_scaler=y_scaler,
                encoder=encoder,
                report_path=REPORT_PATH,
                on_load=demo.load,
            )

            # # Onglet 2 : liste des programmes
            # selected_program_state, goal_state = render_list_of_exercices(
            #     app_desc_ex=app_desc_ex,
            #     level_out=level_out,
            # )

            # # Onglet 3 : DL – programme complet
            # render_dl_tab(
            #     app_desc_dl=app_desc_dl,
            #     level_out=level_out,
            #     wf_comp=wf_out,
            #     wt_comp=wt_out,
            #     selected_program_df=selected_program_state,
            #     goal_state=goal_state,
            # )

            # Onglet 4 : DL – Execution generator
            render_dl_execution_tab(
                app_desc_dl_exec=app_desc_dl_exec
            )

    return demo


if __name__ == "__main__":
    build_app().launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))