Chris Addis commited on
Commit
7aa90ae
·
1 Parent(s): 44c4cc1

Advanced settings

Browse files
Files changed (1) hide show
  1. app.py +73 -82
app.py CHANGED
@@ -28,7 +28,6 @@ from library.utils_html import *
28
  from library.utils_prompt import *
29
 
30
  OR = OpenRouterAPI()
31
- gemini = OpenRouterAPI(api_key = os.getenv("GEMINI_API_KEY"),base_url="https://generativelanguage.googleapis.com/v1beta/openai/")
32
 
33
  # Path for storing user preferences
34
  PREFERENCES_FILE = "data/user_preferences.csv"
@@ -37,45 +36,24 @@ PREFERENCES_FILE = "data/user_preferences.csv"
37
  os.makedirs(os.path.dirname(PREFERENCES_FILE), exist_ok=True)
38
 
39
  # Define model pricing information (approximate costs per 100 image API calls)
40
- # These are placeholder values - replace with actual pricing
41
  MODEL_PRICING = {
42
- "google/gemini-2.0-flash-001": {"cost": "$0.50"},
43
- "gpt-4.1-mini": {"cost": "$2.00"},
44
- "gpt-4.1": {"cost": "$5.00"},
45
- "anthropic/claude-3.7-sonnet": {"cost": "$4.00"},
46
- "google/gemini-2.5-pro-preview-03-25": {"cost": "$1.50"},
47
- "google/gemini-2.5-flash-preview:thinking": {"cost": "$0.75"},
48
- "gpt-4.1-nano": {"cost": "$1.00"},
49
- "openai/chatgpt-4o-latest": {"cost": "$4.50"},
50
- "meta-llama/llama-4-maverick": {"cost": "$0.25"}
51
  }
52
 
53
- # Define model lists globally to avoid scope issues
54
- PREFERRED_MODELS = [
55
- ("Gemini 2.0 Flash (default)", "google/gemini-2.0-flash-001"),
56
- ("GPT-4.1 Mini", "gpt-4.1-mini"),
57
- ("GPT-4.1", "gpt-4.1"),
58
- ("Claude 3.7 Sonnet", "anthropic/claude-3.7-sonnet"),
59
- ("Gemini 2.5 Pro", "google/gemini-2.5-pro-preview-03-25"),
60
- ("Gemini 2.5 Flash Thinking", "google/gemini-2.5-flash-preview:thinking")
61
- ]
62
-
63
- ADDITIONAL_MODELS = [
64
- ("GPT-4.1 Nano", "gpt-4.1-nano"),
65
- ("ChatGPT Latest", "openai/chatgpt-4o-latest"),
66
- ("Llama 4 Maverick", "meta-llama/llama-4-maverick")
67
- ]
68
-
69
- # Default model
70
- DEFAULT_MODEL = "google/gemini-2.0-flash-001"
71
-
72
  def get_sys_prompt(length="medium", photograph=False):
73
-
74
  extra_prompt = ""
75
 
76
  if photograph:
77
  object_type = "wildlife photography"
78
- extra_prompt = " Do not guess the exact species of the animals in the photograph - simply use a broader grouping to make less errors."
79
  else:
80
  object_type = "museum objects"
81
 
@@ -114,36 +92,6 @@ def get_base_filename(filepath):
114
  filename = os.path.splitext(basename)[0]
115
  return filename
116
 
117
- # Functions for model selection outside the create_demo scope to avoid errors
118
- def update_model_dropdown(show_all, current_model):
119
- """Update the model dropdown based on the checkbox state"""
120
- if show_all:
121
- # Combine preferred and additional models
122
- all_models = PREFERRED_MODELS + ADDITIONAL_MODELS
123
- return gr.Dropdown.update(choices=all_models, value=current_model)
124
- else:
125
- # Only show preferred models
126
- # If current model is not in preferred, switch to default
127
- current_model_in_preferred = any(model[1] == current_model for model in PREFERRED_MODELS)
128
- if not current_model_in_preferred:
129
- return gr.Dropdown.update(choices=PREFERRED_MODELS, value=DEFAULT_MODEL)
130
- return gr.Dropdown.update(choices=PREFERRED_MODELS, value=current_model)
131
-
132
- def update_model_info(model_id):
133
- """Update the model information display"""
134
- # Find the display name for the model ID
135
- all_models = PREFERRED_MODELS + ADDITIONAL_MODELS
136
- model_name = next((name for name, id in all_models if id == model_id), "Unknown Model")
137
-
138
- # Get pricing info
139
- pricing_info = MODEL_PRICING.get(model_id, {"cost": "Unknown"})
140
-
141
- # Create markdown text - removed speed information
142
- info_text = f"""**Current Model**: {model_name}
143
- **Cost per 100 Images**: {pricing_info['cost']}"""
144
-
145
- return info_text
146
-
147
  # Define the Gradio interface
148
  def create_demo():
149
  # --- Reintroduce CSS ---
@@ -186,6 +134,31 @@ def create_demo():
186
  show_share_button=False, show_fullscreen_button=False,
187
  container=False, elem_id="nml-logo")
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  with gr.Row():
190
  # Left column: Controls and uploads
191
  with gr.Column(scale=1):
@@ -195,12 +168,11 @@ def create_demo():
195
  file_count="multiple"
196
  )
197
 
198
- # Model dropdown (will be updated dynamically)
199
  model_choice = gr.Dropdown(
200
- choices=PREFERRED_MODELS,
201
  label="Select Model",
202
- value=DEFAULT_MODEL,
203
- visible=True
204
  )
205
 
206
  length_choice = gr.Radio(
@@ -224,13 +196,10 @@ def create_demo():
224
  value="Museum Object"
225
  )
226
 
227
- # Display current model info - initial value
228
- initial_model_info = f"""**Current Model**: Gemini 2.0 Flash (default)
229
- **Cost per 100 Images**: {MODEL_PRICING[DEFAULT_MODEL]["cost"]}"""
230
-
231
  model_info = gr.Markdown(
232
- initial_model_info,
233
- label="Model Information"
234
  )
235
 
236
  gr.Markdown("### Uploaded Images")
@@ -248,7 +217,7 @@ def create_demo():
248
  current_image = gr.Image(
249
  label="Current Image",
250
  type="filepath",
251
- elem_id="current-image-display", # ADDED - for CSS targeting
252
  show_fullscreen_button=True,
253
  show_download_button=False,
254
  show_share_button=False,
@@ -269,15 +238,41 @@ def create_demo():
269
  current_index = gr.State(0)
270
  all_images = gr.State([])
271
  all_results = gr.State([])
272
-
273
- # Connect checkbox to update dropdown
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  show_all_models.change(
275
- fn=update_model_dropdown,
276
- inputs=[show_all_models, model_choice],
277
  outputs=[model_choice]
278
  )
279
 
280
- # Connect model dropdown to update info display
281
  model_choice.change(
282
  fn=update_model_info,
283
  inputs=[model_choice],
@@ -318,10 +313,6 @@ def create_demo():
318
  prompt0 = prompt_new()
319
  model_name = model_choice
320
  client_to_use = OR # Default client
321
- # Add logic here if you need to switch between OR and gemini clients based on model_name
322
- # Example:
323
- # if model_name.startswith("google/gemini") and gemini:
324
- # client_to_use = gemini
325
 
326
  result = client_to_use.generate_caption(
327
  img, model=model_name, max_image_size=512,
 
28
  from library.utils_prompt import *
29
 
30
  OR = OpenRouterAPI()
 
31
 
32
  # Path for storing user preferences
33
  PREFERENCES_FILE = "data/user_preferences.csv"
 
36
  os.makedirs(os.path.dirname(PREFERENCES_FILE), exist_ok=True)
37
 
38
  # Define model pricing information (approximate costs per 100 image API calls)
 
39
  MODEL_PRICING = {
40
+ "google/gemini-2.0-flash-001": "$0.50",
41
+ "gpt-4.1-mini": "$2.00",
42
+ "gpt-4.1": "$5.00",
43
+ "anthropic/claude-3.7-sonnet": "$4.00",
44
+ "google/gemini-2.5-pro-preview-03-25": "$1.50",
45
+ "google/gemini-2.5-flash-preview:thinking": "$0.75",
46
+ "gpt-4.1-nano": "$1.00",
47
+ "openai/chatgpt-4o-latest": "$4.50",
48
+ "meta-llama/llama-4-maverick": "$0.25"
49
  }
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def get_sys_prompt(length="medium", photograph=False):
 
52
  extra_prompt = ""
53
 
54
  if photograph:
55
  object_type = "wildlife photography"
56
+ extra_prompt = " Do not guess the exact species of the animals in the photograph unless you are certain - simply use a broader terms e.g. the genus or family to make less errors, "
57
  else:
58
  object_type = "museum objects"
59
 
 
92
  filename = os.path.splitext(basename)[0]
93
  return filename
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Define the Gradio interface
96
  def create_demo():
97
  # --- Reintroduce CSS ---
 
134
  show_share_button=False, show_fullscreen_button=False,
135
  container=False, elem_id="nml-logo")
136
 
137
+ # Store model choices and state
138
+ show_all_models_state = gr.State(False)
139
+
140
+ # Define preferred and additional models directly in the function
141
+ preferred_models = [
142
+ ("Gemini 2.0 Flash (default)", "google/gemini-2.0-flash-001"),
143
+ ("GPT-4.1 Mini", "gpt-4.1-mini"),
144
+ ("GPT-4.1", "gpt-4.1"),
145
+ ("Claude 3.7 Sonnet", "anthropic/claude-3.7-sonnet"),
146
+ ("Gemini 2.5 Pro", "google/gemini-2.5-pro-preview-03-25"),
147
+ ("Gemini 2.5 Flash Thinking", "google/gemini-2.5-flash-preview:thinking")
148
+ ]
149
+
150
+ additional_models = [
151
+ ("GPT-4.1 Nano", "gpt-4.1-nano"),
152
+ ("ChatGPT Latest", "openai/chatgpt-4o-latest"),
153
+ ("Llama 4 Maverick", "meta-llama/llama-4-maverick")
154
+ ]
155
+
156
+ # Calculate all models once
157
+ all_models = preferred_models + additional_models
158
+
159
+ # Default model value
160
+ default_model = "google/gemini-2.0-flash-001"
161
+
162
  with gr.Row():
163
  # Left column: Controls and uploads
164
  with gr.Column(scale=1):
 
168
  file_count="multiple"
169
  )
170
 
171
+ # Model dropdown
172
  model_choice = gr.Dropdown(
173
+ choices=preferred_models,
174
  label="Select Model",
175
+ value=default_model
 
176
  )
177
 
178
  length_choice = gr.Radio(
 
196
  value="Museum Object"
197
  )
198
 
199
+ # Display current model info
 
 
 
200
  model_info = gr.Markdown(
201
+ f"""**Current Model**: Gemini 2.0 Flash (default)
202
+ **Cost per 100 Images**: {MODEL_PRICING[default_model]}"""
203
  )
204
 
205
  gr.Markdown("### Uploaded Images")
 
217
  current_image = gr.Image(
218
  label="Current Image",
219
  type="filepath",
220
+ elem_id="current-image-display",
221
  show_fullscreen_button=True,
222
  show_download_button=False,
223
  show_share_button=False,
 
238
  current_index = gr.State(0)
239
  all_images = gr.State([])
240
  all_results = gr.State([])
241
+
242
+ # Define functions within the demo function to avoid scope issues
243
+
244
+ # Handle checkbox change to update model dropdown
245
+ def toggle_models(show_all):
246
+ if show_all:
247
+ return gr.Dropdown.update(choices=all_models)
248
+ else:
249
+ return gr.Dropdown.update(choices=preferred_models)
250
+
251
+ # Update model info when model selection changes
252
+ def update_model_info(model_value):
253
+ # Find display name
254
+ for name, value in all_models:
255
+ if value == model_value:
256
+ model_name = name
257
+ break
258
+ else:
259
+ model_name = "Unknown Model"
260
+
261
+ # Get cost
262
+ cost = MODEL_PRICING.get(model_value, "Unknown")
263
+
264
+ # Create markdown
265
+ return f"""**Current Model**: {model_name}
266
+ **Cost per 100 Images**: {cost}"""
267
+
268
+ # Connect checkbox to toggle model choices
269
  show_all_models.change(
270
+ fn=toggle_models,
271
+ inputs=[show_all_models],
272
  outputs=[model_choice]
273
  )
274
 
275
+ # Connect model selection to update info
276
  model_choice.change(
277
  fn=update_model_info,
278
  inputs=[model_choice],
 
313
  prompt0 = prompt_new()
314
  model_name = model_choice
315
  client_to_use = OR # Default client
 
 
 
 
316
 
317
  result = client_to_use.generate_caption(
318
  img, model=model_name, max_image_size=512,