Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,30 @@ import json
|
|
| 8 |
from tavily import TavilyClient
|
| 9 |
|
| 10 |
KYS_SAMPLE = "https://kys.udiseplus.gov.in/webapp/api/search-schools?searchType=3&searchParam={udise}"
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def call_kys_by_udise(udise_code):
|
|
@@ -66,41 +89,37 @@ def json_to_table(obj):
|
|
| 66 |
|
| 67 |
def to_table_from_kys(kys_json):
|
| 68 |
"""
|
| 69 |
-
Convert KYS JSON
|
| 70 |
-
|
| 71 |
"""
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if not content:
|
| 77 |
-
# fallback if structure slightly differs
|
| 78 |
-
content = kys_json.get("content") or []
|
| 79 |
-
|
| 80 |
-
if not isinstance(content, list) or not content:
|
| 81 |
return pd.DataFrame()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
"
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
"
|
| 91 |
-
"District": rec.get("districtName"),
|
| 92 |
-
"Management Type": rec.get("schMgmtType")
|
| 93 |
})
|
| 94 |
-
|
| 95 |
-
# Step 3: Convert to DataFrame
|
| 96 |
-
return pd.DataFrame(filtered)
|
| 97 |
-
|
| 98 |
except Exception as e:
|
| 99 |
-
print("
|
| 100 |
return pd.DataFrame()
|
| 101 |
|
| 102 |
|
| 103 |
-
|
| 104 |
def search_workflow(school_name, state_name, search_key, use_search, use_kys):
|
| 105 |
out = {"kys": None, "search": None, "suggestions": []}
|
| 106 |
payload_text = f"{school_name or ''} {state_name or ''} UDISE code".strip()
|
|
@@ -124,16 +143,18 @@ def search_workflow(school_name, state_name, search_key, use_search, use_kys):
|
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown(
|
| 126 |
"""
|
| 127 |
-
#
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
Enter a school name and select the state; the app calls Search
|
| 130 |
with the combined query `<school> <state> UDISE code`, then optionally calls KYS.
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
-
inp = gr.Textbox(label="School name", placeholder="e.g. GOVT SEC SCHOOL DARLONG", lines=1)
|
| 136 |
-
state_dropdown = gr.Dropdown(choices=STATES, label="State", value=STATES[0] if STATES else "", interactive=True)
|
| 137 |
|
| 138 |
search_key = gr.Textbox(label="Search API Key (required)", placeholder="api-key...", lines=1)
|
| 139 |
save_key_toggle = gr.Checkbox(value=False, label="Save key in session (keeps key between interactions)")
|
|
@@ -145,7 +166,10 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
|
|
| 145 |
|
| 146 |
run = gr.Button("Search")
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
search_table = gr.DataFrame(headers=None, label="Search results (table)")
|
| 150 |
|
| 151 |
gr.Markdown("### UDISE candidates found in Search results")
|
|
@@ -153,7 +177,7 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
|
|
| 153 |
udise_input = gr.Textbox(label="UDISE to lookup (editable)", placeholder="Pick a candidate or type a UDISE code...", lines=1)
|
| 154 |
lookup_btn = gr.Button("Lookup UDISE (Call KYS)")
|
| 155 |
|
| 156 |
-
kys_output_json = gr.JSON(label="KYS Raw Output")
|
| 157 |
kys_table = gr.DataFrame(headers=None, label="KYS results (table)")
|
| 158 |
|
| 159 |
saved_key_state = gr.State("")
|
|
@@ -170,9 +194,10 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
|
|
| 170 |
if save_key:
|
| 171 |
new_saved_key = key or saved_key
|
| 172 |
textbox_value = new_saved_key
|
| 173 |
-
return
|
|
|
|
| 174 |
|
| 175 |
-
run.click(on_run, inputs=[inp, state_dropdown, search_key, save_key_toggle, use_search, use_kys, saved_key_state], outputs=[output_json, search_table, suggestions_dropdown, saved_key_state, search_key])
|
| 176 |
|
| 177 |
def on_select_suggestion(choice):
|
| 178 |
return choice or ""
|
|
@@ -195,6 +220,12 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
|
|
| 195 |
|
| 196 |
clear_key_btn.click(on_clear_key, inputs=[saved_key_state], outputs=[saved_key_state, search_key])
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
gr.Markdown(
|
| 199 |
"""
|
| 200 |
---
|
|
|
|
| 8 |
from tavily import TavilyClient
|
| 9 |
|
| 10 |
KYS_SAMPLE = "https://kys.udiseplus.gov.in/webapp/api/search-schools?searchType=3&searchParam={udise}"
|
| 11 |
+
states = [
|
| 12 |
+
"Arunachal_pradesh",
|
| 13 |
+
"Assam",
|
| 14 |
+
"Bihar",
|
| 15 |
+
"Chhattisgarh",
|
| 16 |
+
"Delhi",
|
| 17 |
+
"Jharkhand",
|
| 18 |
+
"Karnataka",
|
| 19 |
+
"Madhya pradesh",
|
| 20 |
+
"Manipur",
|
| 21 |
+
"Meghalaya",
|
| 22 |
+
"Mizoram",
|
| 23 |
+
"Nagaland",
|
| 24 |
+
"Odisha",
|
| 25 |
+
"Puducherry",
|
| 26 |
+
"Rajasthan",
|
| 27 |
+
"Sikkim",
|
| 28 |
+
"Tamil nadu",
|
| 29 |
+
"Telangana",
|
| 30 |
+
"Tripura",
|
| 31 |
+
"Uttar pradesh",
|
| 32 |
+
"Uttarakhand"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
|
| 36 |
|
| 37 |
def call_kys_by_udise(udise_code):
|
|
|
|
| 89 |
|
| 90 |
def to_table_from_kys(kys_json):
|
| 91 |
"""
|
| 92 |
+
Convert KYS JSON wrapper into a simplified pandas DataFrame showing only
|
| 93 |
+
selected fields from the `content` list.
|
| 94 |
"""
|
| 95 |
try:
|
| 96 |
+
content = None
|
| 97 |
+
if isinstance(kys_json, dict):
|
| 98 |
+
inner = kys_json.get("data") if kys_json.get("data") is not None else None
|
| 99 |
+
if isinstance(inner, dict) and isinstance(inner.get("content"), list):
|
| 100 |
+
content = inner.get("content")
|
| 101 |
+
elif isinstance(inner, dict) and isinstance(inner.get("data"), dict) and isinstance(inner.get("data").get("content"), list):
|
| 102 |
+
content = inner.get("data").get("content")
|
| 103 |
+
elif isinstance(kys_json.get("content"), list):
|
| 104 |
+
content = kys_json.get("content")
|
| 105 |
if not content:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return pd.DataFrame()
|
| 107 |
+
rows = []
|
| 108 |
+
for r in content:
|
| 109 |
+
rows.append({
|
| 110 |
+
"School Name": r.get("schoolName"),
|
| 111 |
+
"School ID": r.get("schoolId"),
|
| 112 |
+
"Pincode": r.get("pincode"),
|
| 113 |
+
"State": r.get("stateName"),
|
| 114 |
+
"District": r.get("districtName"),
|
| 115 |
+
"Management Type": r.get("schMgmtType")
|
|
|
|
|
|
|
| 116 |
})
|
| 117 |
+
return pd.DataFrame(rows)
|
|
|
|
|
|
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
+
print("to_table_from_kys error:", e)
|
| 120 |
return pd.DataFrame()
|
| 121 |
|
| 122 |
|
|
|
|
| 123 |
def search_workflow(school_name, state_name, search_key, use_search, use_kys):
|
| 124 |
out = {"kys": None, "search": None, "suggestions": []}
|
| 125 |
payload_text = f"{school_name or ''} {state_name or ''} UDISE code".strip()
|
|
|
|
| 143 |
with gr.Blocks() as demo:
|
| 144 |
gr.Markdown(
|
| 145 |
"""
|
| 146 |
+
# Search + KYS Lookup (Hugging Face Space)
|
| 147 |
+
|
| 148 |
+
This version uses the Search SDK internally. Provide your API key in the textbox.
|
| 149 |
|
| 150 |
+
Enter a school name (or UDISE code) and select the state; the app calls Search SDK
|
| 151 |
with the combined query `<school> <state> UDISE code`, then optionally calls KYS.
|
| 152 |
"""
|
| 153 |
)
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
+
inp = gr.Textbox(label="School name or UDISE code", placeholder="e.g. GOVT SEC SCHOOL DARLONG or 12345678901", lines=1)
|
| 157 |
+
state_dropdown = gr.Dropdown(choices=STATES, label="State", value=STATES[0] if STATES else "", interactive=True, allow_custom_value=True)
|
| 158 |
|
| 159 |
search_key = gr.Textbox(label="Search API Key (required)", placeholder="api-key...", lines=1)
|
| 160 |
save_key_toggle = gr.Checkbox(value=False, label="Save key in session (keeps key between interactions)")
|
|
|
|
| 166 |
|
| 167 |
run = gr.Button("Search")
|
| 168 |
|
| 169 |
+
# By default hide raw JSON outputs; users can toggle visibility with `show_raw_checkbox`
|
| 170 |
+
show_raw_checkbox = gr.Checkbox(value=False, label="Show raw JSON outputs")
|
| 171 |
+
|
| 172 |
+
output_json = gr.JSON(label="Raw Search Output (JSON)", visible=False)
|
| 173 |
search_table = gr.DataFrame(headers=None, label="Search results (table)")
|
| 174 |
|
| 175 |
gr.Markdown("### UDISE candidates found in Search results")
|
|
|
|
| 177 |
udise_input = gr.Textbox(label="UDISE to lookup (editable)", placeholder="Pick a candidate or type a UDISE code...", lines=1)
|
| 178 |
lookup_btn = gr.Button("Lookup UDISE (Call KYS)")
|
| 179 |
|
| 180 |
+
kys_output_json = gr.JSON(label="KYS Raw Output", visible=False)
|
| 181 |
kys_table = gr.DataFrame(headers=None, label="KYS results (table)")
|
| 182 |
|
| 183 |
saved_key_state = gr.State("")
|
|
|
|
| 194 |
if save_key:
|
| 195 |
new_saved_key = key or saved_key
|
| 196 |
textbox_value = new_saved_key
|
| 197 |
+
# return raw JSON objects (even if hidden) and the table + suggestions
|
| 198 |
+
return res.get("search"), tbl, suggestions, new_saved_key, textbox_value, res.get("kys")
|
| 199 |
|
| 200 |
+
run.click(on_run, inputs=[inp, state_dropdown, search_key, save_key_toggle, use_search, use_kys, saved_key_state], outputs=[output_json, search_table, suggestions_dropdown, saved_key_state, search_key, kys_output_json])
|
| 201 |
|
| 202 |
def on_select_suggestion(choice):
|
| 203 |
return choice or ""
|
|
|
|
| 220 |
|
| 221 |
clear_key_btn.click(on_clear_key, inputs=[saved_key_state], outputs=[saved_key_state, search_key])
|
| 222 |
|
| 223 |
+
# Toggle visibility handler for raw JSON outputs
|
| 224 |
+
def toggle_raw(visible: bool):
|
| 225 |
+
return gr.update(visible=visible), gr.update(visible=visible)
|
| 226 |
+
|
| 227 |
+
show_raw_checkbox.change(toggle_raw, inputs=[show_raw_checkbox], outputs=[output_json, kys_output_json])
|
| 228 |
+
|
| 229 |
gr.Markdown(
|
| 230 |
"""
|
| 231 |
---
|