sharanyaswarup commited on
Commit
49f8c4f
Β·
1 Parent(s): 4e68f40

Fix HF hang

Browse files
Files changed (2) hide show
  1. app.py +17 -10
  2. hf_reader.py +4 -0
app.py CHANGED
@@ -249,13 +249,9 @@ details summary {
249
  """
250
 
251
  # ─── Data Initialization ──────────────────────────────────────────────────────
252
- try:
253
- STATE_HIER = get_state_hierarchy()
254
- STATE_CHOICES = sorted(STATE_HIER.keys())
255
- except Exception as e:
256
- print(f"Warning: Could not load state hierarchy on startup: {e}")
257
- STATE_HIER = {}
258
- STATE_CHOICES = []
259
 
260
  # ─── HTML builders ────────────────────────────────────────────────────────────
261
  def build_results_heading_html(results: list[dict], udise_code: str) -> str:
@@ -523,13 +519,24 @@ with gr.Blocks(title="School Name Resolver") as app:
523
 
524
  refresh_cache_btn.click(fn=on_refresh_caches, outputs=[cache_status, state_dd])
525
 
 
 
 
 
 
 
 
 
 
 
 
 
526
  # ─── Launch ───────────────────────────────────────────────────────────────────
527
  if __name__ == "__main__":
528
- port = int(os.getenv("PORT", 7862))
529
  app.launch(
530
  server_name="0.0.0.0",
531
  server_port=port,
532
  theme=THEME,
533
- css=CSS,
534
- inbrowser=True,
535
  )
 
249
  """
250
 
251
  # ─── Data Initialization ──────────────────────────────────────────────────────
252
+ # We now defer the heavy loading until the UI is booted so HuggingFace Spaces doesn't time out.
253
+ STATE_HIER = {}
254
+ STATE_CHOICES = []
 
 
 
 
255
 
256
  # ─── HTML builders ────────────────────────────────────────────────────────────
257
  def build_results_heading_html(results: list[dict], udise_code: str) -> str:
 
519
 
520
  refresh_cache_btn.click(fn=on_refresh_caches, outputs=[cache_status, state_dd])
521
 
522
+ def load_initial_data():
523
+ try:
524
+ global STATE_HIER, STATE_CHOICES
525
+ STATE_HIER = get_state_hierarchy()
526
+ STATE_CHOICES = sorted(STATE_HIER.keys())
527
+ return gr.update(choices=STATE_CHOICES, interactive=True, info="Select State")
528
+ except Exception as e:
529
+ print(f"Failed to load data on boot: {e}")
530
+ return gr.update(info="Failed to load data")
531
+
532
+ app.load(fn=load_initial_data, outputs=[state_dd])
533
+
534
  # ─── Launch ───────────────────────────────────────────────────────────────────
535
  if __name__ == "__main__":
536
+ port = int(os.getenv("PORT", 7860))
537
  app.launch(
538
  server_name="0.0.0.0",
539
  server_port=port,
540
  theme=THEME,
541
+ css=CSS
 
542
  )
hf_reader.py CHANGED
@@ -102,9 +102,13 @@ def _ensure_loaded():
102
  """Load all caches if they haven't been loaded yet."""
103
  global _old_master_df, _mapped_masters, _cache_loaded
104
  if not _cache_loaded:
 
105
  _old_master_df = _load_old_master()
 
106
  _mapped_masters = _load_mapped_masters()
 
107
  _cache_loaded = True
 
108
 
109
 
110
  def get_state_hierarchy() -> dict:
 
102
  """Load all caches if they haven't been loaded yet."""
103
  global _old_master_df, _mapped_masters, _cache_loaded
104
  if not _cache_loaded:
105
+ print("[hf_reader] Initializing caches... This may take a few minutes if downloading from HuggingFace.")
106
  _old_master_df = _load_old_master()
107
+ print("[hf_reader] Old Master loaded.")
108
  _mapped_masters = _load_mapped_masters()
109
+ print(f"[hf_reader] Mapped Masters loaded: {len(_mapped_masters)} files.")
110
  _cache_loaded = True
111
+ print("[hf_reader] All caches fully loaded and ready.")
112
 
113
 
114
  def get_state_hierarchy() -> dict: