Prince012 commited on
Commit
556524e
·
1 Parent(s): 7fa2eda

fix: use importlib to load api_client directly from file

Browse files
app/pages/compare.py CHANGED
@@ -1,55 +1,28 @@
1
- import sys
2
  from pathlib import Path
3
 
4
- sys.path.insert(0, str(Path(__file__).parent.parent))
 
 
 
 
 
5
 
6
  import pandas as pd
7
  import streamlit as st
8
 
9
- from app.api_client import api_predict, require_api
10
-
11
  require_api()
12
 
13
  st.title("Compare Models")
14
  st.caption("Side-by-side comparison of all trained models on CLINC150 test set.")
15
 
16
  RESULTS = {
17
- "logreg": {
18
- "accuracy": 0.8058,
19
- "macro_f1": 0.8416,
20
- "weighted_f1": 0.7985,
21
- "latency_p50_ms": 0.12,
22
- },
23
- "svm": {
24
- "accuracy": 0.8002,
25
- "macro_f1": 0.8474,
26
- "weighted_f1": 0.7795,
27
- "latency_p50_ms": 0.14,
28
- },
29
- "textcnn": {
30
- "accuracy": 0.7740,
31
- "macro_f1": 0.8244,
32
- "weighted_f1": 0.7646,
33
- "latency_p50_ms": 97.6,
34
- },
35
- "rnn": {
36
- "accuracy": 0.1818,
37
- "macro_f1": 0.0020,
38
- "weighted_f1": 0.0560,
39
- "latency_p50_ms": 124.5,
40
- },
41
- "lstm": {
42
- "accuracy": 0.7669,
43
- "macro_f1": 0.8113,
44
- "weighted_f1": 0.7555,
45
- "latency_p50_ms": 249.2,
46
- },
47
- "distilbert": {
48
- "accuracy": 0.8765,
49
- "macro_f1": 0.9019,
50
- "weighted_f1": 0.8722,
51
- "latency_p50_ms": 9.6,
52
- },
53
  }
54
 
55
  df = pd.DataFrame(RESULTS).T
@@ -57,14 +30,12 @@ df.index.name = "model"
57
 
58
  st.subheader("Metrics Table")
59
  st.dataframe(
60
- df.style.format(
61
- {
62
- "accuracy": "{:.2%}",
63
- "macro_f1": "{:.2%}",
64
- "weighted_f1": "{:.2%}",
65
- "latency_p50_ms": "{:.2f} ms",
66
- }
67
- ).background_gradient(subset=["accuracy", "macro_f1", "weighted_f1"], cmap="Greens"),
68
  use_container_width=True,
69
  )
70
 
@@ -97,4 +68,4 @@ if st.button("Compare", type="primary", disabled=not (text and selected_models))
97
  st.metric("Confidence", f"{result['confidence']:.2%}")
98
  st.metric("Latency", f"{result['latency_ms']:.1f} ms")
99
  if result["is_oos"]:
100
- st.warning("flagged OOS")
 
1
+ import importlib.util
2
  from pathlib import Path
3
 
4
+ _client_path = Path(__file__).parent.parent / "api_client.py"
5
+ _spec = importlib.util.spec_from_file_location("api_client", _client_path)
6
+ _module = importlib.util.module_from_spec(_spec)
7
+ _spec.loader.exec_module(_module)
8
+ api_predict = _module.api_predict
9
+ require_api = _module.require_api
10
 
11
  import pandas as pd
12
  import streamlit as st
13
 
 
 
14
  require_api()
15
 
16
  st.title("Compare Models")
17
  st.caption("Side-by-side comparison of all trained models on CLINC150 test set.")
18
 
19
  RESULTS = {
20
+ "logreg": {"accuracy": 0.8058, "macro_f1": 0.8416, "weighted_f1": 0.7985, "latency_p50_ms": 0.12},
21
+ "svm": {"accuracy": 0.8002, "macro_f1": 0.8474, "weighted_f1": 0.7795, "latency_p50_ms": 0.14},
22
+ "textcnn": {"accuracy": 0.7740, "macro_f1": 0.8244, "weighted_f1": 0.7646, "latency_p50_ms": 97.6},
23
+ "rnn": {"accuracy": 0.1818, "macro_f1": 0.0020, "weighted_f1": 0.0560, "latency_p50_ms": 124.5},
24
+ "lstm": {"accuracy": 0.7669, "macro_f1": 0.8113, "weighted_f1": 0.7555, "latency_p50_ms": 249.2},
25
+ "distilbert": {"accuracy": 0.8765, "macro_f1": 0.9019, "weighted_f1": 0.8722, "latency_p50_ms": 9.6},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  }
27
 
28
  df = pd.DataFrame(RESULTS).T
 
30
 
31
  st.subheader("Metrics Table")
32
  st.dataframe(
33
+ df.style.format({
34
+ "accuracy": "{:.2%}",
35
+ "macro_f1": "{:.2%}",
36
+ "weighted_f1": "{:.2%}",
37
+ "latency_p50_ms": "{:.2f} ms",
38
+ }).background_gradient(subset=["accuracy", "macro_f1", "weighted_f1"], cmap="Greens"),
 
 
39
  use_container_width=True,
40
  )
41
 
 
68
  st.metric("Confidence", f"{result['confidence']:.2%}")
69
  st.metric("Latency", f"{result['latency_ms']:.1f} ms")
70
  if result["is_oos"]:
71
+ st.warning("flagged OOS")
app/pages/monitoring.py CHANGED
@@ -1,12 +1,17 @@
1
- import sys
2
  from pathlib import Path
3
 
4
- sys.path.insert(0, str(Path(__file__).parent.parent))
 
 
 
 
 
 
 
5
 
6
  import streamlit as st
7
 
8
- from app.api_client import api_ab_stats, api_drift, api_reset_monitoring, require_api
9
-
10
  require_api()
11
 
12
  st.title("Monitoring Dashboard")
@@ -56,19 +61,13 @@ if st.button("Run drift check"):
56
  drift = api_drift()
57
 
58
  col1, col2, col3 = st.columns(3)
59
- col1.metric(
60
- "Drifted Columns",
61
- int(drift["drift_summary"].get("drifted_columns_count", 0)),
62
- )
63
  col2.metric(
64
  "Confidence Drop",
65
  f"{drift['confidence_drift']['confidence_drop']:.2%}",
66
  delta=f"{-drift['confidence_drift']['confidence_drop']:.2%}",
67
  )
68
- col3.metric(
69
- "OOS Rate Increase",
70
- f"{drift['oos_rate_drift']['oos_rate_increase']:.2%}",
71
- )
72
 
73
  if drift["confidence_drift"]["is_degraded"]:
74
  st.error("Confidence has degraded significantly vs reference traffic.")
@@ -87,4 +86,4 @@ if st.button("Run drift check"):
87
  st.warning(
88
  "No reference/current data available yet. Run `verify_phase11.py` first to "
89
  f"generate baseline monitoring data. ({e})"
90
- )
 
1
+ import importlib.util
2
  from pathlib import Path
3
 
4
+ _client_path = Path(__file__).parent.parent / "api_client.py"
5
+ _spec = importlib.util.spec_from_file_location("api_client", _client_path)
6
+ _module = importlib.util.module_from_spec(_spec)
7
+ _spec.loader.exec_module(_module)
8
+ api_ab_stats = _module.api_ab_stats
9
+ api_drift = _module.api_drift
10
+ api_reset_monitoring = _module.api_reset_monitoring
11
+ require_api = _module.require_api
12
 
13
  import streamlit as st
14
 
 
 
15
  require_api()
16
 
17
  st.title("Monitoring Dashboard")
 
61
  drift = api_drift()
62
 
63
  col1, col2, col3 = st.columns(3)
64
+ col1.metric("Drifted Columns", int(drift["drift_summary"].get("drifted_columns_count", 0)))
 
 
 
65
  col2.metric(
66
  "Confidence Drop",
67
  f"{drift['confidence_drift']['confidence_drop']:.2%}",
68
  delta=f"{-drift['confidence_drift']['confidence_drop']:.2%}",
69
  )
70
+ col3.metric("OOS Rate Increase", f"{drift['oos_rate_drift']['oos_rate_increase']:.2%}")
 
 
 
71
 
72
  if drift["confidence_drift"]["is_degraded"]:
73
  st.error("Confidence has degraded significantly vs reference traffic.")
 
86
  st.warning(
87
  "No reference/current data available yet. Run `verify_phase11.py` first to "
88
  f"generate baseline monitoring data. ({e})"
89
+ )
app/pages/predict.py CHANGED
@@ -1,13 +1,18 @@
 
1
  import sys
2
  from pathlib import Path
3
 
4
- sys.path.insert(0, str(Path(__file__).parent.parent.parent))
 
 
 
 
 
 
5
 
6
  import pandas as pd
7
  import streamlit as st
8
 
9
- from app.api_client import api_predict, require_api
10
-
11
  require_api()
12
 
13
  st.title("Predict Intent")
 
1
+ import importlib.util
2
  import sys
3
  from pathlib import Path
4
 
5
+ # load api_client directly from file to avoid 'app' package conflict
6
+ _client_path = Path(__file__).parent.parent / "api_client.py"
7
+ _spec = importlib.util.spec_from_file_location("api_client", _client_path)
8
+ _module = importlib.util.module_from_spec(_spec)
9
+ _spec.loader.exec_module(_module)
10
+ api_predict = _module.api_predict
11
+ require_api = _module.require_api
12
 
13
  import pandas as pd
14
  import streamlit as st
15
 
 
 
16
  require_api()
17
 
18
  st.title("Predict Intent")