keeperballon commited on
Commit
8f82122
·
verified ·
1 Parent(s): 4559ffe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -27
app.py CHANGED
@@ -131,10 +131,12 @@ for category, models in MODEL_CATEGORIES.items():
131
  ALL_MODELS.extend(models)
132
 
133
 
134
- # Helper function to get model info display
135
  def get_model_info(model_name):
136
  """Extract and format model information for display"""
137
  parts = model_name.split('/')
 
 
 
138
  org = parts[0]
139
  model = parts[1]
140
 
@@ -146,6 +148,20 @@ def get_model_info(model_name):
146
  return f"**Organization:** {org}\n**Model:** {model}\n**Size:** {size}"
147
 
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  def filter_models(search_term):
150
  """Filter models based on search term across all categories"""
151
  if not search_term:
@@ -164,18 +180,48 @@ def update_model_display(search_term=""):
164
  """Update the model selection UI based on search term"""
165
  filtered_categories = filter_models(search_term)
166
 
167
- # Create HTML for model display
168
- html = "<div style='max-height: 400px; overflow-y: auto;'>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
 
170
  for category, models in filtered_categories.items():
171
  html += f"<h3>{category}</h3><div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 10px;'>"
172
 
173
  for model in models:
174
  model_short = model.split('/')[-1]
 
175
  html += f"""
176
- <div class='model-card' onclick='selectAndUpdateModel("{model}")'
177
  style='border: 1px solid #ddd; border-radius: 8px; padding: 12px; cursor: pointer; transition: all 0.2s;
178
- background: linear-gradient(145deg, #f0f0f0, #ffffff); box-shadow: 0 4px 6px rgba(0,0,0,0.1);'>
 
179
  <div style='font-weight: bold; margin-bottom: 6px; color: #1a73e8;'>{model_short}</div>
180
  <div style='font-size: 0.8em; color: #666;'>{model.split('/')[0]}</div>
181
  </div>
@@ -185,25 +231,7 @@ def update_model_display(search_term=""):
185
  if not filtered_categories:
186
  html += "<p>No models found matching your search.</p>"
187
 
188
- html += """</div>
189
- <script>
190
- function selectAndUpdateModel(model) {
191
- // Set the value in the input field
192
- const input = document.getElementById('custom-model-input');
193
- input.value = model;
194
-
195
- // Create and dispatch a change event to trigger Gradio's handlers
196
- const event = new Event('change', { bubbles: true });
197
- input.dispatchEvent(event);
198
-
199
- // Also trigger the input event for good measure
200
- const inputEvent = new Event('input', { bubbles: true });
201
- input.dispatchEvent(inputEvent);
202
-
203
- console.log('Model selected:', model);
204
- }
205
- </script>"""
206
-
207
  return html
208
 
209
 
@@ -325,7 +353,7 @@ with gr.Blocks(css=custom_css, title=APP_TITLE, theme=gr.themes.Soft()) as demo:
325
 
326
  # Model information display
327
  gr.HTML("<h4>Current Model Info</h4>")
328
- model_info_display = gr.Markdown(get_model_info("Qwen/Qwen3-32B")) # Updated to match default
329
  gr.HTML("</div>")
330
 
331
  with gr.Column(scale=3):
@@ -412,6 +440,9 @@ with gr.Blocks(css=custom_css, title=APP_TITLE, theme=gr.themes.Soft()) as demo:
412
  </div>
413
  """)
414
 
 
 
 
415
  # Set up event handlers
416
  msg.submit(
417
  fn=respond,
@@ -446,9 +477,16 @@ with gr.Blocks(css=custom_css, title=APP_TITLE, theme=gr.themes.Soft()) as demo:
446
 
447
  # Update model info when selection changes
448
  custom_model_box.change(
449
- fn=lambda x: get_model_info(x),
 
 
 
 
 
 
 
450
  inputs=custom_model_box,
451
- outputs=model_info_display
452
  )
453
 
454
  if __name__ == "__main__":
 
131
  ALL_MODELS.extend(models)
132
 
133
 
 
134
  def get_model_info(model_name):
135
  """Extract and format model information for display"""
136
  parts = model_name.split('/')
137
+ if len(parts) != 2:
138
+ return f"**Model:** {model_name}\n**Format:** Unknown"
139
+
140
  org = parts[0]
141
  model = parts[1]
142
 
 
148
  return f"**Organization:** {org}\n**Model:** {model}\n**Size:** {size}"
149
 
150
 
151
+ def set_model_and_update_info(model_name):
152
+ """Set the selected model and update the model info display"""
153
+ # This function is called when a model is selected (either clicked or via API)
154
+ try:
155
+ # Get model info
156
+ model_info = get_model_info(model_name)
157
+
158
+ # Return both the model name and the model info
159
+ return model_name, model_info
160
+ except Exception as e:
161
+ print(f"Error in set_model_and_update_info: {e}")
162
+ return model_name, f"**Error loading model info**: {str(e)}"
163
+
164
+
165
  def filter_models(search_term):
166
  """Filter models based on search term across all categories"""
167
  if not search_term:
 
180
  """Update the model selection UI based on search term"""
181
  filtered_categories = filter_models(search_term)
182
 
183
+ # Create HTML for model display with a more direct approach
184
+ html = """
185
+ <div style='max-height: 400px; overflow-y: auto;'>
186
+ <script>
187
+ // Direct model selection function - more reliable
188
+ function selectModel(modelName) {
189
+ // Get the textbox element by its ID
190
+ const modelInput = document.getElementById('custom-model-input');
191
+ if (modelInput) {
192
+ // Set the value
193
+ modelInput.value = modelName;
194
+
195
+ // Create and dispatch change event
196
+ const event = new Event('change', { bubbles: true });
197
+ modelInput.dispatchEvent(event);
198
+
199
+ // Look for the hidden trigger button and click it
200
+ const triggerBtn = document.querySelector('button[value="Select Model"]');
201
+ if (triggerBtn) {
202
+ triggerBtn.click();
203
+ }
204
+
205
+ console.log('Selected model:', modelName);
206
+ } else {
207
+ console.error('Model input element not found');
208
+ }
209
+ }
210
+ </script>
211
+ """
212
 
213
+ # Add models by category
214
  for category, models in filtered_categories.items():
215
  html += f"<h3>{category}</h3><div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 10px;'>"
216
 
217
  for model in models:
218
  model_short = model.split('/')[-1]
219
+ escaped_model = model.replace("'", "\\'").replace('"', '\\"')
220
  html += f"""
221
+ <div class='model-card'
222
  style='border: 1px solid #ddd; border-radius: 8px; padding: 12px; cursor: pointer; transition: all 0.2s;
223
+ background: linear-gradient(145deg, #f0f0f0, #ffffff); box-shadow: 0 4px 6px rgba(0,0,0,0.1);'
224
+ onclick="selectModel('{escaped_model}')">
225
  <div style='font-weight: bold; margin-bottom: 6px; color: #1a73e8;'>{model_short}</div>
226
  <div style='font-size: 0.8em; color: #666;'>{model.split('/')[0]}</div>
227
  </div>
 
231
  if not filtered_categories:
232
  html += "<p>No models found matching your search.</p>"
233
 
234
+ html += "</div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  return html
236
 
237
 
 
353
 
354
  # Model information display
355
  gr.HTML("<h4>Current Model Info</h4>")
356
+ model_info_display = gr.Markdown(get_model_info("Qwen/Qwen3-32B"))
357
  gr.HTML("</div>")
358
 
359
  with gr.Column(scale=3):
 
440
  </div>
441
  """)
442
 
443
+ # Add a hidden button to trigger model selection via JavaScript
444
+ trigger_model_selection = gr.Button("Select Model", visible=False)
445
+
446
  # Set up event handlers
447
  msg.submit(
448
  fn=respond,
 
477
 
478
  # Update model info when selection changes
479
  custom_model_box.change(
480
+ fn=set_model_and_update_info,
481
+ inputs=custom_model_box,
482
+ outputs=[custom_model_box, model_info_display]
483
+ )
484
+
485
+ # Connect the hidden trigger button to update model info
486
+ trigger_model_selection.click(
487
+ fn=set_model_and_update_info,
488
  inputs=custom_model_box,
489
+ outputs=[custom_model_box, model_info_display]
490
  )
491
 
492
  if __name__ == "__main__":