KevinIsInCoding Claude Sonnet 4.6 commited on
Commit
f726933
·
1 Parent(s): cbc4a98

fix(app): handle missing ChromaDB collection gracefully on startup

Browse files

Space crashes if pipeline data hasn't been uploaded. Wrap collection
load in try/except; show a clear message in UI instead of crashing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +15 -2
app.py CHANGED
@@ -37,12 +37,19 @@ def _load_trials() -> list[dict]:
37
  return [json.loads(line) for line in f if line.strip()]
38
 
39
 
40
- _collection = load_collection(CHROMA_DIR, CHROMA_COLLECTION)
 
 
 
 
 
 
 
41
  _graph = _load_graph()
42
  _trials = _load_trials()
43
  _client = anthropic.Anthropic()
44
 
45
- _n_chunks = _collection.count()
46
  _n_trials = len(_trials)
47
  _kg_nodes = _graph.number_of_nodes() if _graph else 0
48
 
@@ -64,6 +71,12 @@ def respond(message: str, history: list[dict]):
64
  yield history, gr.update(value="", interactive=True)
65
  return
66
 
 
 
 
 
 
 
67
  history = history + [{"role": "user", "content": message}]
68
  history = history + [{"role": "assistant", "content": ""}]
69
  yield history, gr.update(value="", interactive=False)
 
37
  return [json.loads(line) for line in f if line.strip()]
38
 
39
 
40
+ def _load_collection():
41
+ try:
42
+ return load_collection(CHROMA_DIR, CHROMA_COLLECTION)
43
+ except Exception:
44
+ _logger.warning("ChromaDB collection not found — running in demo mode (no data)")
45
+ return None
46
+
47
+ _collection = _load_collection()
48
  _graph = _load_graph()
49
  _trials = _load_trials()
50
  _client = anthropic.Anthropic()
51
 
52
+ _n_chunks = _collection.count() if _collection else 0
53
  _n_trials = len(_trials)
54
  _kg_nodes = _graph.number_of_nodes() if _graph else 0
55
 
 
71
  yield history, gr.update(value="", interactive=True)
72
  return
73
 
74
+ if _collection is None:
75
+ history = history + [{"role": "user", "content": message}]
76
+ history = history + [{"role": "assistant", "content": "⚠️ The knowledge base has not been loaded yet. The pipeline data (ChromaDB index, knowledge graph, papers) needs to be uploaded to this Space. Please contact the Space administrator."}]
77
+ yield history, gr.update(value="", interactive=True)
78
+ return
79
+
80
  history = history + [{"role": "user", "content": message}]
81
  history = history + [{"role": "assistant", "content": ""}]
82
  yield history, gr.update(value="", interactive=False)