Update app.py
Browse files
app.py
CHANGED
@@ -379,6 +379,13 @@ def get_model_info(model_choice):
|
|
379 |
return model_id_value, ctx_size
|
380 |
return None, 0
|
381 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
def call_openrouter_api(payload):
|
383 |
"""Make a call to OpenRouter API with error handling"""
|
384 |
try:
|
@@ -711,8 +718,10 @@ def create_app():
|
|
711 |
value=ALL_MODELS[0][0],
|
712 |
label="Model",
|
713 |
elem_id="model-choice",
|
714 |
-
|
|
|
715 |
)
|
|
|
716 |
context_display = gr.Textbox(
|
717 |
value=update_context_display(ALL_MODELS[0][0]),
|
718 |
label="Context",
|
@@ -730,10 +739,15 @@ def create_app():
|
|
730 |
|
731 |
# Create a container for the category models dropdown
|
732 |
with gr.Column(visible=True, elem_id="category-models-container") as category_models_container:
|
|
|
|
|
|
|
|
|
733 |
category_models = gr.Dropdown(
|
734 |
-
[
|
735 |
label="Models in Category",
|
736 |
-
value=
|
|
|
737 |
allow_custom_value=True
|
738 |
)
|
739 |
|
@@ -929,9 +943,9 @@ def create_app():
|
|
929 |
|
930 |
# Update model list when category changes
|
931 |
model_categories.change(
|
932 |
-
fn=
|
933 |
inputs=model_categories,
|
934 |
-
outputs=
|
935 |
)
|
936 |
|
937 |
# Update main model choice when category model is selected
|
@@ -940,7 +954,55 @@ def create_app():
|
|
940 |
inputs=category_models,
|
941 |
outputs=model_choice
|
942 |
)
|
943 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
944 |
# Process uploaded images
|
945 |
image_upload_btn.upload(
|
946 |
fn=lambda files: files,
|
@@ -1007,6 +1069,9 @@ def create_app():
|
|
1007 |
|
1008 |
return demo
|
1009 |
|
|
|
|
|
|
|
1010 |
# Launch the app
|
1011 |
if __name__ == "__main__":
|
1012 |
# Check API key before starting
|
|
|
379 |
return model_id_value, ctx_size
|
380 |
return None, 0
|
381 |
|
382 |
+
def get_models_for_category(category):
|
383 |
+
"""Get model list for a specific category"""
|
384 |
+
for cat in MODELS:
|
385 |
+
if cat["category"] == category:
|
386 |
+
return [model[0] for model in cat["models"]]
|
387 |
+
return []
|
388 |
+
|
389 |
def call_openrouter_api(payload):
|
390 |
"""Make a call to OpenRouter API with error handling"""
|
391 |
try:
|
|
|
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 |
+
|
725 |
context_display = gr.Textbox(
|
726 |
value=update_context_display(ALL_MODELS[0][0]),
|
727 |
label="Context",
|
|
|
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 |
|
|
|
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
|
|
|
954 |
inputs=category_models,
|
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,
|
|
|
1069 |
|
1070 |
return demo
|
1071 |
|
1072 |
+
|
1073 |
+
|
1074 |
+
|
1075 |
# Launch the app
|
1076 |
if __name__ == "__main__":
|
1077 |
# Check API key before starting
|