Slaiwala commited on
Commit
ffc3583
·
verified ·
1 Parent(s): cd28972

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -24
app.py CHANGED
@@ -1,12 +1,11 @@
1
  # app.py — Spine Coder (Chatbot + Feedback + Session Logs) — Gradio 4.x
2
  # ------------------------------------------------------------------------------
3
- # FINAL-v2.1 UI build:
4
- # - Bulletproof core loader (module + direct file path) to avoid stale caches
5
- # - Startup PROBE print to logs + in-UI Diagnostics/Probe button
6
- # - Clean CPT table (no per-row modifier columns)
7
- # - Case Modifiers panel (-22, -50, -53, -80, -82, ...)
8
- # - Build tag + Core file path in UI chips and logs
9
- # - Structured logs/export, modern Gradio usage
10
  # ------------------------------------------------------------------------------
11
 
12
  import os
@@ -21,14 +20,30 @@ import pandas as pd
21
  import gradio as gr
22
 
23
  # ==== Bulletproof Core Import ==================================================
24
- import importlib, importlib.util, importlib.machinery, sys as __sys, os as __os, inspect as __inspect
25
 
26
  __sys.path.insert(0, __os.path.abspath(".")) # ensure repo root is on sys.path
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def _load_core_from_file(path: str):
29
  """Load spine_coder_core.py directly from a given path (bypass module cache)."""
30
- if not __os.path.isabs(path):
31
- path = __os.path.abspath(path)
32
  if not __os.path.exists(path):
33
  return None
34
  name = "spine_coder_core_dynamic"
@@ -39,35 +54,54 @@ def _load_core_from_file(path: str):
39
  return importlib.reload(mod)
40
 
41
  def _force_import_core():
42
- """Try package modules first; then known file paths. Reload to bust caches."""
43
- # 1) Try module layouts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  for modname in [
45
  "spine_coder.spine_coder.spine_coder_core",
46
  "spine_coder.spine_coder_core",
47
  ]:
48
  try:
49
  mod = importlib.import_module(modname)
50
- return importlib.reload(mod)
 
 
 
51
  except Exception:
52
  pass
53
- # 2) Try likely file paths (repo variants)
54
- for rel in [
55
- "spine_coder/spine_coder/spine_coder_core.py",
56
- "spine_coder/spine_coder_core.py",
57
- "spine_coder_core.py",
58
- ]:
59
- mod = _load_core_from_file(rel)
60
- if mod:
61
- return mod
62
  raise ImportError("Unable to locate spine_coder_core.py via modules or file paths.")
63
 
64
  _core = _force_import_core()
65
  suggest_with_cpt_billing = _core.suggest_with_cpt_billing
66
 
67
  try:
68
- print("[CORE] loaded from:", __inspect.getsourcefile(suggest_with_cpt_billing))
 
69
  except Exception:
70
- pass
71
 
72
  # ---- One-time startup probe (prints to Space logs) ----------------------------
73
  try:
 
1
  # app.py — Spine Coder (Chatbot + Feedback + Session Logs) — Gradio 4.x
2
  # ------------------------------------------------------------------------------
3
+ # FINAL-v2.1 UI build (loader hardened):
4
+ # - Purge module caches + prefer explicit file via SPINE_CORE_PATH
5
+ # - Fallbacks: local files package modules
6
+ # - SHA checksum + active source printed; startup probe; in-UI Diagnostics
7
+ # - Clean CPT table (no per-row modifier columns) + Case Modifiers panel
8
+ # - Build/Core chips, structured logs/export, modern Gradio
 
9
  # ------------------------------------------------------------------------------
10
 
11
  import os
 
20
  import gradio as gr
21
 
22
  # ==== Bulletproof Core Import ==================================================
23
+ import importlib, importlib.util, importlib.machinery, sys as __sys, os as __os, inspect as __inspect, hashlib
24
 
25
  __sys.path.insert(0, __os.path.abspath(".")) # ensure repo root is on sys.path
26
 
27
+ def _purge_spine_modules():
28
+ """Remove any cached spine_coder modules so we truly reload the file we want."""
29
+ for k in list(__sys.modules):
30
+ if k == "spine_coder" or k.startswith("spine_coder."):
31
+ __sys.modules.pop(k, None)
32
+ importlib.invalidate_caches()
33
+
34
+ def _sha256(path: str) -> str:
35
+ try:
36
+ h = hashlib.sha256()
37
+ with open(path, "rb") as f:
38
+ for chunk in iter(lambda: f.read(8192), b""):
39
+ h.update(chunk)
40
+ return h.hexdigest()[:12]
41
+ except Exception:
42
+ return "unknown"
43
+
44
  def _load_core_from_file(path: str):
45
  """Load spine_coder_core.py directly from a given path (bypass module cache)."""
46
+ path = __os.path.abspath(path)
 
47
  if not __os.path.exists(path):
48
  return None
49
  name = "spine_coder_core_dynamic"
 
54
  return importlib.reload(mod)
55
 
56
  def _force_import_core():
57
+ """Order: purge caches SPINE_CORE_PATH local files package modules."""
58
+ _purge_spine_modules()
59
+
60
+ # 1) Explicit path override (set in Space secrets/vars)
61
+ forced = __os.environ.get("SPINE_CORE_PATH")
62
+ if forced and __os.path.exists(forced):
63
+ mod = _load_core_from_file(forced)
64
+ if mod:
65
+ print("[CORE] forced path:", forced, "sha:", _sha256(forced))
66
+ return mod
67
+
68
+ # 2) Try likely local file locations (edited copies near app)
69
+ for rel in [
70
+ "spine_coder_core.py",
71
+ "spine_coder/spine_coder_core.py",
72
+ "spine_coder/spine_coder/spine_coder_core.py", # package-style tree
73
+ ]:
74
+ p = __os.path.abspath(rel)
75
+ if __os.path.exists(p):
76
+ mod = _load_core_from_file(p)
77
+ if mod:
78
+ print("[CORE] loaded file:", p, "sha:", _sha256(p))
79
+ return mod
80
+
81
+ # 3) Fall back to package modules (may be stale)
82
  for modname in [
83
  "spine_coder.spine_coder.spine_coder_core",
84
  "spine_coder.spine_coder_core",
85
  ]:
86
  try:
87
  mod = importlib.import_module(modname)
88
+ mod = importlib.reload(mod)
89
+ src = __inspect.getsourcefile(mod.suggest_with_cpt_billing) or "unknown"
90
+ print("[CORE] loaded module:", modname, "from:", src)
91
+ return mod
92
  except Exception:
93
  pass
94
+
 
 
 
 
 
 
 
 
95
  raise ImportError("Unable to locate spine_coder_core.py via modules or file paths.")
96
 
97
  _core = _force_import_core()
98
  suggest_with_cpt_billing = _core.suggest_with_cpt_billing
99
 
100
  try:
101
+ _active_src = __inspect.getsourcefile(suggest_with_cpt_billing) or "unknown"
102
+ print("[CORE] active source:", _active_src)
103
  except Exception:
104
+ _active_src = "unknown"
105
 
106
  # ---- One-time startup probe (prints to Space logs) ----------------------------
107
  try: