cstr commited on
Commit
1b53c0d
·
verified ·
1 Parent(s): 82b8835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -67
app.py CHANGED
@@ -203,6 +203,13 @@ def update_model_info(model_name):
203
  """
204
  return "<p>Model information not available</p>"
205
 
 
 
 
 
 
 
 
206
  def update_category_models_ui(category):
207
  """Completely regenerate the models dropdown based on selected category"""
208
  for cat in MODELS:
@@ -434,7 +441,7 @@ def extract_ai_response(result):
434
  return f"Error: {str(e)}"
435
 
436
  # streaming code:
437
- def streaming_handler(response, chatbot, message_idx):
438
  try:
439
  # First add the user message if needed
440
  if len(chatbot) == message_idx:
@@ -563,7 +570,7 @@ def ask_ai(message, history, model_choice, temperature, max_tokens, top_p,
563
 
564
  # Set up generator for streaming updates
565
  def streaming_generator():
566
- for updated_history in streaming_handler(response, chat_history, len(chat_history) - 1):
567
  yield updated_history
568
 
569
  return streaming_generator()
@@ -713,12 +720,13 @@ def create_app():
713
  )
714
 
715
  with gr.Row(elem_classes="model-selection-row"):
 
 
716
  model_choice = gr.Dropdown(
717
  [model[0] for model in ALL_MODELS],
718
  value=ALL_MODELS[0][0],
719
  label="Model",
720
  elem_id="model-choice",
721
- elem_classes="model-choice",
722
  allow_custom_value=True
723
  )
724
 
@@ -732,24 +740,18 @@ def create_app():
732
  # Model category selection
733
  with gr.Accordion("Browse by Category", open=False):
734
  model_categories = gr.Dropdown(
735
- [category["category"] for category in MODELS],
736
  label="Categories",
737
  value=MODELS[0]["category"]
738
  )
739
 
740
- # Create a container for the category models dropdown
741
- with gr.Column(visible=True, elem_id="category-models-container") as category_models_container:
742
- # Create a hidden text component to store model choices as JSON
743
- category_model_choices = gr.Text(visible=False)
744
-
745
- # Create the dropdown with no initial choices
746
- category_models = gr.Dropdown(
747
- [],
748
- label="Models in Category",
749
- value=None,
750
- elem_classes="category-models",
751
- allow_custom_value=True
752
- )
753
 
754
  with gr.Accordion("Generation Parameters", open=False):
755
  with gr.Group(elem_classes="parameter-grid"):
@@ -943,9 +945,9 @@ def create_app():
943
 
944
  # Update model list when category changes
945
  model_categories.change(
946
- fn=lambda cat: json.dumps(get_models_for_category(cat)),
947
  inputs=model_categories,
948
- outputs=category_model_choices
949
  )
950
 
951
  # Update main model choice when category model is selected
@@ -955,54 +957,6 @@ def create_app():
955
  outputs=model_choice
956
  )
957
 
958
- category_model_choices.change(
959
- fn=None,
960
- inputs=None,
961
- outputs=None,
962
- _js="""
963
- function(choices_json) {
964
- // Parse JSON string to array
965
- const choices = JSON.parse(choices_json);
966
-
967
- // Find the dropdown element
968
- const dropdown = document.querySelector('.category-models select');
969
-
970
- // Clear existing options
971
- dropdown.innerHTML = '';
972
-
973
- // Add new options
974
- choices.forEach(model => {
975
- const option = document.createElement('option');
976
- option.value = model;
977
- option.textContent = model;
978
- dropdown.appendChild(option);
979
- });
980
-
981
- // Set the first option as selected if available
982
- if (choices.length > 0) {
983
- dropdown.value = choices[0];
984
-
985
- // Update the main model dropdown
986
- const mainDropdown = document.querySelector('.model-choice select');
987
- mainDropdown.value = choices[0];
988
-
989
- // Trigger change events
990
- dropdown.dispatchEvent(new Event('change', { bubbles: true }));
991
- mainDropdown.dispatchEvent(new Event('change', { bubbles: true }));
992
- }
993
- }
994
- """
995
- )
996
-
997
- # Function to initialize the category models dropdown
998
- def init_category_models():
999
- initial_category = MODELS[0]["category"]
1000
- initial_models = get_models_for_category(initial_category)
1001
- return json.dumps(initial_models)
1002
-
1003
- # Set initial choices for category models dropdown
1004
- category_model_choices.value = init_category_models()
1005
-
1006
  # Process uploaded images
1007
  image_upload_btn.upload(
1008
  fn=lambda files: files,
 
203
  """
204
  return "<p>Model information not available</p>"
205
 
206
+ def update_category_dropdown(category):
207
+ """Update the category model dropdown when a category is selected"""
208
+ models = get_models_for_category(category)
209
+ if not models:
210
+ return [], None
211
+ return models, models[0]
212
+
213
  def update_category_models_ui(category):
214
  """Completely regenerate the models dropdown based on selected category"""
215
  for cat in MODELS:
 
441
  return f"Error: {str(e)}"
442
 
443
  # streaming code:
444
+ def streaming_handler(response, chatbot, message_idx, message):
445
  try:
446
  # First add the user message if needed
447
  if len(chatbot) == message_idx:
 
570
 
571
  # Set up generator for streaming updates
572
  def streaming_generator():
573
+ for updated_history in streaming_handler(response, chat_history, len(chat_history) - 1, message):
574
  yield updated_history
575
 
576
  return streaming_generator()
 
720
  )
721
 
722
  with gr.Row(elem_classes="model-selection-row"):
723
+
724
+ # Main model dropdown
725
  model_choice = gr.Dropdown(
726
  [model[0] for model in ALL_MODELS],
727
  value=ALL_MODELS[0][0],
728
  label="Model",
729
  elem_id="model-choice",
 
730
  allow_custom_value=True
731
  )
732
 
 
740
  # Model category selection
741
  with gr.Accordion("Browse by Category", open=False):
742
  model_categories = gr.Dropdown(
743
+ [model["category"] for model in MODELS],
744
  label="Categories",
745
  value=MODELS[0]["category"]
746
  )
747
 
748
+ # Models in category dropdown
749
+ category_models = gr.Dropdown(
750
+ get_models_for_category(MODELS[0]["category"]),
751
+ label="Models in Category",
752
+ value=get_models_for_category(MODELS[0]["category"])[0] if get_models_for_category(MODELS[0]["category"]) else None,
753
+ allow_custom_value=True
754
+ )
 
 
 
 
 
 
755
 
756
  with gr.Accordion("Generation Parameters", open=False):
757
  with gr.Group(elem_classes="parameter-grid"):
 
945
 
946
  # Update model list when category changes
947
  model_categories.change(
948
+ fn=update_category_dropdown,
949
  inputs=model_categories,
950
+ outputs=[category_models, category_models]
951
  )
952
 
953
  # Update main model choice when category model is selected
 
957
  outputs=model_choice
958
  )
959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960
  # Process uploaded images
961
  image_upload_btn.upload(
962
  fn=lambda files: files,