gkdivya commited on
Commit
686aead
·
verified ·
1 Parent(s): 06d5eae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -5,11 +5,12 @@ import gradio as gr
5
  import requests
6
  import pandas as pd
7
  import json
8
- from tavily import TavilyClient
9
  import re
 
10
 
11
  KYS_SAMPLE = "https://kys.udiseplus.gov.in/webapp/api/search-schools?searchType=3&searchParam={udise}"
12
 
 
13
  # List of valid state codes for UDISE (first 2 digits of UDISE code)
14
  VALID_UDISE_STATE_CODES = [
15
  '01', # Jammu & Kashmir
@@ -51,7 +52,6 @@ VALID_UDISE_STATE_CODES = [
51
  '37' # Ladakh (added in newer datasets)
52
  ]
53
 
54
-
55
  def is_valid_udise(code):
56
  """Check if a string is a valid UDISE code."""
57
  if not (code and code.isdigit() and len(code) == 11):
@@ -200,7 +200,7 @@ def to_table_from_kys(kys_json):
200
  return pd.DataFrame()
201
 
202
 
203
- def search_workflow(school_name, state_name, search_key, use_search, use_kys):
204
  out = {"kys": None, "search": None, "suggestions": [], "first_candidate": None}
205
  payload_text = f"{school_name or ''} {state_name or ''} UDISE code".strip()
206
 
@@ -237,14 +237,7 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
237
  state_dropdown = gr.Dropdown(choices=STATES, label="State", value=STATES[0] if STATES else "", interactive=True, allow_custom_value=True)
238
 
239
  search_key = gr.Textbox(label="Search API Key (required)", placeholder="api-key...", lines=1)
240
- save_key_toggle = gr.Checkbox(value=False, label="Save key in session (keeps key between interactions)")
241
- clear_key_btn = gr.Button("Clear saved key")
242
-
243
- with gr.Row():
244
- use_search = gr.Checkbox(value=True, label="Call Search API")
245
- use_kys = gr.Checkbox(value=True, label="Call KYS by UDISE")
246
-
247
- run = gr.Button("Search")
248
 
249
  # By default hide raw JSON outputs; users can toggle visibility with `show_raw_checkbox`
250
  show_raw_checkbox = gr.Checkbox(value=False, label="Show raw JSON outputs")
@@ -262,47 +255,55 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
262
 
263
  saved_key_state = gr.State("")
264
 
265
- def on_run(school, state, key, save_key, use_s, use_k, saved_key):
 
266
  effective_key = saved_key if saved_key else key
267
- res = search_workflow(school, state, effective_key, use_s, use_k)
 
268
  tbl = pd.DataFrame()
269
  if res.get("search") and res["search"].get("ok"):
270
  tbl = json_to_table(res["search"]["data"])
 
 
271
  suggestions = res.get("suggestions", [])
272
- new_saved_key = saved_key
273
- textbox_value = key
274
- if save_key:
275
- new_saved_key = key or saved_key
276
- textbox_value = new_saved_key
 
277
  # Return the first candidate along with other values
278
  return (
279
  res.get("search"), # output_json
280
  tbl, # search_table
281
- suggestions, # suggestions_dropdown
282
  res.get("first_candidate", ""), # This will update udise_input
283
  new_saved_key, # saved_key_state
284
- textbox_value, # search_key
285
  res.get("kys") # kys_output_json
286
  )
287
 
288
  run.click(
289
  on_run,
290
- inputs=[inp, state_dropdown, search_key, save_key_toggle, use_search, use_kys, saved_key_state],
291
  outputs=[
292
  output_json,
293
  search_table,
294
  suggestions_dropdown,
295
  udise_input, # This will be updated with first_candidate
296
  saved_key_state,
297
- search_key,
298
  kys_output_json
299
  ]
300
  )
301
 
302
  def on_select_suggestion(choice):
303
- return choice or ""
 
304
 
305
- suggestions_dropdown.change(on_select_suggestion, inputs=[suggestions_dropdown], outputs=[udise_input])
 
 
 
 
306
 
307
  def on_lookup_udise(udise_code):
308
  if not udise_code or not udise_code.strip().isdigit():
@@ -315,11 +316,6 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
315
 
316
  lookup_btn.click(on_lookup_udise, inputs=[udise_input], outputs=[kys_output_json, kys_table])
317
 
318
- def on_clear_key(_):
319
- return "", ""
320
-
321
- clear_key_btn.click(on_clear_key, inputs=[saved_key_state], outputs=[saved_key_state, search_key])
322
-
323
  # Toggle visibility handler for raw JSON outputs
324
  def toggle_raw(visible: bool):
325
  return gr.update(visible=visible), gr.update(visible=visible)
@@ -330,9 +326,8 @@ with the combined query `<school> <state> UDISE code`, then optionally calls KYS
330
  """
331
  ---
332
  **Notes:**
333
- - The 'Save key in session' toggle keeps the key active for the current session.
334
- - The key is stored only in-memory (not written to disk) and is not logged.
335
- - Use 'Clear saved key' to remove it from the session.
336
  """
337
  )
338
 
 
5
  import requests
6
  import pandas as pd
7
  import json
 
8
  import re
9
+ from tavily import TavilyClient
10
 
11
  KYS_SAMPLE = "https://kys.udiseplus.gov.in/webapp/api/search-schools?searchType=3&searchParam={udise}"
12
 
13
+ # List of valid state codes for UDISE (first 2 digits of UDISE code)
14
  # List of valid state codes for UDISE (first 2 digits of UDISE code)
15
  VALID_UDISE_STATE_CODES = [
16
  '01', # Jammu & Kashmir
 
52
  '37' # Ladakh (added in newer datasets)
53
  ]
54
 
 
55
  def is_valid_udise(code):
56
  """Check if a string is a valid UDISE code."""
57
  if not (code and code.isdigit() and len(code) == 11):
 
200
  return pd.DataFrame()
201
 
202
 
203
+ def search_workflow(school_name, state_name, search_key, use_search=True, use_kys=True):
204
  out = {"kys": None, "search": None, "suggestions": [], "first_candidate": None}
205
  payload_text = f"{school_name or ''} {state_name or ''} UDISE code".strip()
206
 
 
237
  state_dropdown = gr.Dropdown(choices=STATES, label="State", value=STATES[0] if STATES else "", interactive=True, allow_custom_value=True)
238
 
239
  search_key = gr.Textbox(label="Search API Key (required)", placeholder="api-key...", lines=1)
240
+ run = gr.Button("Search", variant="primary")
 
 
 
 
 
 
 
241
 
242
  # By default hide raw JSON outputs; users can toggle visibility with `show_raw_checkbox`
243
  show_raw_checkbox = gr.Checkbox(value=False, label="Show raw JSON outputs")
 
255
 
256
  saved_key_state = gr.State("")
257
 
258
+ def on_run(school, state, key, saved_key):
259
+ # Always use the saved key if it exists, otherwise use the provided key
260
  effective_key = saved_key if saved_key else key
261
+ # Always enable both search and KYS by default
262
+ res = search_workflow(school, state, effective_key, use_search=True, use_kys=True)
263
  tbl = pd.DataFrame()
264
  if res.get("search") and res["search"].get("ok"):
265
  tbl = json_to_table(res["search"]["data"])
266
+
267
+ # Ensure suggestions is always a list and handle empty state
268
  suggestions = res.get("suggestions", [])
269
+ if not suggestions:
270
+ suggestions = ["No UDISE codes found"]
271
+
272
+ # Always save the key to state if a new one is provided
273
+ new_saved_key = key or saved_key
274
+
275
  # Return the first candidate along with other values
276
  return (
277
  res.get("search"), # output_json
278
  tbl, # search_table
279
+ {"choices": suggestions, "__type__": "update"}, # Update dropdown choices
280
  res.get("first_candidate", ""), # This will update udise_input
281
  new_saved_key, # saved_key_state
 
282
  res.get("kys") # kys_output_json
283
  )
284
 
285
  run.click(
286
  on_run,
287
+ inputs=[inp, state_dropdown, search_key, saved_key_state],
288
  outputs=[
289
  output_json,
290
  search_table,
291
  suggestions_dropdown,
292
  udise_input, # This will be updated with first_candidate
293
  saved_key_state,
 
294
  kys_output_json
295
  ]
296
  )
297
 
298
  def on_select_suggestion(choice):
299
+ # Only update if a valid choice is selected (not the placeholder)
300
+ return choice if choice and choice != "No UDISE codes found" else ""
301
 
302
+ suggestions_dropdown.change(
303
+ on_select_suggestion,
304
+ inputs=[suggestions_dropdown],
305
+ outputs=[udise_input]
306
+ )
307
 
308
  def on_lookup_udise(udise_code):
309
  if not udise_code or not udise_code.strip().isdigit():
 
316
 
317
  lookup_btn.click(on_lookup_udise, inputs=[udise_input], outputs=[kys_output_json, kys_table])
318
 
 
 
 
 
 
319
  # Toggle visibility handler for raw JSON outputs
320
  def toggle_raw(visible: bool):
321
  return gr.update(visible=visible), gr.update(visible=visible)
 
326
  """
327
  ---
328
  **Notes:**
329
+ - Your API key is stored in-memory for the current session only.
330
+ - It is not written to disk or logged.
 
331
  """
332
  )
333