Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,14 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ----------------------------------------------------
|
| 7 |
# Imports
|
| 8 |
# ----------------------------------------------------
|
| 9 |
from searchschool import (
|
| 10 |
DEFAULT_STATE_KEY,
|
| 11 |
-
load_master_for_state,
|
| 12 |
-
update_blocks,
|
| 13 |
search_candidates,
|
| 14 |
on_search_web,
|
| 15 |
)
|
|
@@ -28,16 +28,20 @@ if not ADMIN_PASSWORD:
|
|
| 28 |
# ----------------------------------------------------
|
| 29 |
# Load states ONCE
|
| 30 |
# ----------------------------------------------------
|
| 31 |
-
|
|
|
|
| 32 |
path = hf_hub_download(
|
| 33 |
repo_id=HF_SCHOOLS_DATASET,
|
| 34 |
repo_type="dataset",
|
| 35 |
-
filename=
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
STATE_CHOICES = load_state_choices_once()
|
| 41 |
|
| 42 |
# ----------------------------------------------------
|
| 43 |
# UI RENDER HELPERS
|
|
@@ -82,6 +86,27 @@ def render_results_md(df, show_score=True):
|
|
| 82 |
return md
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
|
|
@@ -163,17 +188,18 @@ with gr.Blocks(title="State School Fuzzy Matcher") as demo:
|
|
| 163 |
|
| 164 |
# ================= Wiring =================
|
| 165 |
state_dd.change(
|
| 166 |
-
fn=
|
| 167 |
inputs=state_dd,
|
| 168 |
outputs=[district_dd, block_dd],
|
| 169 |
)
|
| 170 |
-
|
| 171 |
district_dd.change(
|
| 172 |
-
fn=
|
| 173 |
-
inputs=district_dd,
|
| 174 |
outputs=block_dd,
|
| 175 |
)
|
| 176 |
|
|
|
|
| 177 |
search_btn.click(
|
| 178 |
fn=rapid_search_and_render,
|
| 179 |
inputs=[school_input, state_dd, district_dd, block_dd],
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
+
import json
|
| 6 |
+
from functools import lru_cache
|
| 7 |
|
| 8 |
# ----------------------------------------------------
|
| 9 |
# Imports
|
| 10 |
# ----------------------------------------------------
|
| 11 |
from searchschool import (
|
| 12 |
DEFAULT_STATE_KEY,
|
|
|
|
|
|
|
| 13 |
search_candidates,
|
| 14 |
on_search_web,
|
| 15 |
)
|
|
|
|
| 28 |
# ----------------------------------------------------
|
| 29 |
# Load states ONCE
|
| 30 |
# ----------------------------------------------------
|
| 31 |
+
@lru_cache(maxsize=1)
|
| 32 |
+
def load_state_hierarchy():
|
| 33 |
path = hf_hub_download(
|
| 34 |
repo_id=HF_SCHOOLS_DATASET,
|
| 35 |
repo_type="dataset",
|
| 36 |
+
filename="state_district_block.json",
|
| 37 |
)
|
| 38 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 39 |
+
return json.load(f)
|
| 40 |
+
|
| 41 |
+
STATE_HIER = load_state_hierarchy()
|
| 42 |
+
STATE_CHOICES = sorted(STATE_HIER.keys())
|
| 43 |
+
|
| 44 |
|
|
|
|
| 45 |
|
| 46 |
# ----------------------------------------------------
|
| 47 |
# UI RENDER HELPERS
|
|
|
|
| 86 |
return md
|
| 87 |
|
| 88 |
|
| 89 |
+
def on_state_change(state):
|
| 90 |
+
if not state:
|
| 91 |
+
return (
|
| 92 |
+
gr.update(choices=[], value=None),
|
| 93 |
+
gr.update(choices=[], value=None),
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
districts = sorted(STATE_HIER.get(state, {}).keys())
|
| 97 |
+
|
| 98 |
+
return (
|
| 99 |
+
gr.update(choices=districts, value=None),
|
| 100 |
+
gr.update(choices=[], value=None),
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
def on_district_change(state, district):
|
| 104 |
+
if not state or not district:
|
| 105 |
+
return gr.update(choices=[], value=None)
|
| 106 |
+
|
| 107 |
+
blocks = STATE_HIER.get(state, {}).get(district, [])
|
| 108 |
+
|
| 109 |
+
return gr.update(choices=blocks, value=None)
|
| 110 |
|
| 111 |
|
| 112 |
|
|
|
|
| 188 |
|
| 189 |
# ================= Wiring =================
|
| 190 |
state_dd.change(
|
| 191 |
+
fn=on_state_change,
|
| 192 |
inputs=state_dd,
|
| 193 |
outputs=[district_dd, block_dd],
|
| 194 |
)
|
| 195 |
+
|
| 196 |
district_dd.change(
|
| 197 |
+
fn=on_district_change,
|
| 198 |
+
inputs=[state_dd, district_dd],
|
| 199 |
outputs=block_dd,
|
| 200 |
)
|
| 201 |
|
| 202 |
+
|
| 203 |
search_btn.click(
|
| 204 |
fn=rapid_search_and_render,
|
| 205 |
inputs=[school_input, state_dd, district_dd, block_dd],
|