Rainbowdesign commited on
Commit
2eec318
·
verified ·
1 Parent(s): dbc45a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -70
app.py CHANGED
@@ -70,7 +70,7 @@ def flatten_models(model_tree):
70
 
71
 
72
  # -----------------------------
73
- # Add a new model
74
  # -----------------------------
75
  def add_model_box(
76
  category,
@@ -179,21 +179,17 @@ def respond(
179
  def build_model_tree(
180
  models,
181
  active_model_state,
182
- current_model_label,
183
- info_markdowns,
184
- use_buttons
185
  ):
186
  """
187
  models: merged models dict (Category -> Family -> Model -> meta)
188
  active_model_state: gr.State storing current active full key
189
  current_model_label: gr.Markdown for 'Current model: ...'
190
- info_markdowns: dict full_key -> gr.Markdown (Model Info)
191
- use_buttons: dict full_key -> gr.Button (Use this model)
192
  """
193
-
194
  flat = flatten_models(models)
195
 
196
- # We’ll build the UI, and inside each loop create small closures
 
197
  for category, families in models.items():
198
  with gr.Accordion(category, open=False):
199
  for family, model_dict in families.items():
@@ -202,69 +198,61 @@ def build_model_tree(
202
  emoji = meta.get("emoji", "✨")
203
  full_key = f"{category} / {family} / {model_name}"
204
 
205
- # Model button row
206
- model_button = gr.Button(f"{emoji} {model_name}", size="sm")
207
-
208
- # Model info area
209
- info_md = gr.Markdown(visible=False)
210
- info_markdowns[full_key] = info_md
211
-
212
- # Use button
213
- use_btn = gr.Button("Use this model", visible=False, size="sm")
214
- use_buttons[full_key] = use_btn
215
-
216
- # ---- Click model button → show this info, hide others
217
- def show_info(clicked_key, fk=full_key):
218
- models_local = merge_models()
219
- flat_local = flatten_models(models_local)
220
-
221
- updates = {}
222
- for key, (meta_loc, _, _, _) in flat_local.items():
223
- md_out = info_markdowns.get(key)
224
- btn_out = use_buttons.get(key)
225
- if md_out is None or btn_out is None:
226
- continue
227
-
228
- if key == fk:
229
  text = (
230
  f"**Model ID:** `{meta_loc['id']}` \n"
231
  f"**Description:** {meta_loc['description']} \n"
232
  f"[Model card]({meta_loc['link']})"
233
  )
234
- updates[md_out] = gr.Markdown.update(value=text, visible=True)
235
- updates[btn_out] = gr.Button.update(visible=True)
236
- else:
237
- updates[md_out] = gr.Markdown.update(visible=False)
238
- updates[btn_out] = gr.Button.update(visible=False)
239
- return updates
240
-
241
- model_button.click(
242
- show_info,
243
- inputs=active_model_state,
244
- outputs=list(info_markdowns.values()) + list(use_buttons.values()),
245
- )
246
-
247
- # ---- Click "Use this model" → set active model + label
248
- def use_model(fk=full_key):
249
- models_local = merge_models()
250
- flat_local = flatten_models(models_local)
251
- meta_loc_tuple = flat_local.get(fk)
252
-
253
- if not meta_loc_tuple:
254
- return fk, gr.Markdown.update(
255
- value="**Current model:** _none selected_"
256
  )
257
 
258
- meta_loc, _, _, mname = meta_loc_tuple
259
- emoji_local = meta_loc.get("emoji", "✨")
260
- label_text = f"**Current model:** {emoji_local} {mname}"
261
- return fk, gr.Markdown.update(value=label_text)
262
-
263
- use_btn.click(
264
- use_model,
265
- inputs=None,
266
- outputs=[active_model_state, current_model_label],
267
- )
 
 
 
 
 
 
 
 
 
 
 
268
 
269
 
270
  # -----------------------------
@@ -330,10 +318,6 @@ with gr.Blocks() as demo:
330
  # Current model label under the Add box
331
  current_model_label = gr.Markdown("**Current model:** _none selected_")
332
 
333
- # Structures to hold info areas
334
- info_markdowns = {}
335
- use_buttons = {}
336
-
337
  gr.Markdown("### Models")
338
 
339
  # Build nested accordions for models
@@ -341,8 +325,6 @@ with gr.Blocks() as demo:
341
  models_tree,
342
  active_model_state=active_model_key,
343
  current_model_label=current_model_label,
344
- info_markdowns=info_markdowns,
345
- use_buttons=use_buttons,
346
  )
347
 
348
  # Main chat interface
 
70
 
71
 
72
  # -----------------------------
73
+ # Add a new model (from the box)
74
  # -----------------------------
75
  def add_model_box(
76
  category,
 
179
  def build_model_tree(
180
  models,
181
  active_model_state,
182
+ current_model_label
 
 
183
  ):
184
  """
185
  models: merged models dict (Category -> Family -> Model -> meta)
186
  active_model_state: gr.State storing current active full key
187
  current_model_label: gr.Markdown for 'Current model: ...'
 
 
188
  """
 
189
  flat = flatten_models(models)
190
 
191
+ # We will keep references to inner model-info components
192
+ # to wire callbacks in a simpler way.
193
  for category, families in models.items():
194
  with gr.Accordion(category, open=False):
195
  for family, model_dict in families.items():
 
198
  emoji = meta.get("emoji", "✨")
199
  full_key = f"{category} / {family} / {model_name}"
200
 
201
+ # Model accordion
202
+ with gr.Accordion(f"{emoji} {model_name}", open=False):
203
+ # Nested Model Info accordion
204
+ with gr.Accordion("Model Info", open=False):
205
+ # Filled dynamically via a function
206
+ info_md = gr.Markdown("")
207
+ use_btn = gr.Button("Use this model", size="sm")
208
+
209
+ # Function to populate Model Info
210
+ def load_model_info(fk=full_key):
211
+ models_local = merge_models()
212
+ flat_local = flatten_models(models_local)
213
+ meta_loc_tuple = flat_local.get(fk)
214
+ if not meta_loc_tuple:
215
+ return gr.Markdown.update(
216
+ value="No info available for this model."
217
+ )
218
+ meta_loc, _, _, _ = meta_loc_tuple
 
 
 
 
 
 
219
  text = (
220
  f"**Model ID:** `{meta_loc['id']}` \n"
221
  f"**Description:** {meta_loc['description']} \n"
222
  f"[Model card]({meta_loc['link']})"
223
  )
224
+ return gr.Markdown.update(value=text)
225
+
226
+ # Populate Model Info when this accordion is opened
227
+ # Gradio doesn't have a direct 'on open' event,
228
+ # so we just pre-fill it once on load:
229
+ info_md.load(
230
+ load_model_info,
231
+ inputs=None,
232
+ outputs=info_md,
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  )
234
 
235
+ # Function to use this model
236
+ def use_model(fk=full_key):
237
+ models_local = merge_models()
238
+ flat_local = flatten_models(models_local)
239
+ meta_loc_tuple = flat_local.get(fk)
240
+
241
+ if not meta_loc_tuple:
242
+ return fk, gr.Markdown.update(
243
+ value="**Current model:** _none selected_"
244
+ )
245
+
246
+ meta_loc, _, _, mname = meta_loc_tuple
247
+ emoji_local = meta_loc.get("emoji", "✨")
248
+ label_text = f"**Current model:** {emoji_local} {mname}"
249
+ return fk, gr.Markdown.update(value=label_text)
250
+
251
+ use_btn.click(
252
+ use_model,
253
+ inputs=None,
254
+ outputs=[active_model_state, current_model_label],
255
+ )
256
 
257
 
258
  # -----------------------------
 
318
  # Current model label under the Add box
319
  current_model_label = gr.Markdown("**Current model:** _none selected_")
320
 
 
 
 
 
321
  gr.Markdown("### Models")
322
 
323
  # Build nested accordions for models
 
325
  models_tree,
326
  active_model_state=active_model_key,
327
  current_model_label=current_model_label,
 
 
328
  )
329
 
330
  # Main chat interface