Chris Addis commited on
Commit
44c4cc1
·
1 Parent(s): 2c8a1cc

Advanced settings

Browse files
Files changed (1) hide show
  1. app.py +72 -66
app.py CHANGED
@@ -39,17 +39,36 @@ os.makedirs(os.path.dirname(PREFERENCES_FILE), exist_ok=True)
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", "speed": "Fast"},
43
- "gpt-4.1-mini": {"cost": "$2.00", "speed": "Medium"},
44
- "gpt-4.1": {"cost": "$5.00", "speed": "Medium-Slow"},
45
- "anthropic/claude-3.7-sonnet": {"cost": "$4.00", "speed": "Medium"},
46
- "google/gemini-2.5-pro-preview-03-25": {"cost": "$1.50", "speed": "Medium"},
47
- "google/gemini-2.5-flash-preview:thinking": {"cost": "$0.75", "speed": "Fast"},
48
- "gpt-4.1-nano": {"cost": "$1.00", "speed": "Fast"},
49
- "openai/chatgpt-4o-latest": {"cost": "$4.50", "speed": "Medium-Slow"},
50
- "meta-llama/llama-4-maverick": {"cost": "$0.25", "speed": "Medium"}
51
  }
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  def get_sys_prompt(length="medium", photograph=False):
54
 
55
  extra_prompt = ""
@@ -95,6 +114,36 @@ def get_base_filename(filepath):
95
  filename = os.path.splitext(basename)[0]
96
  return filename
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  # Define the Gradio interface
99
  def create_demo():
100
  # --- Reintroduce CSS ---
@@ -146,30 +195,11 @@ def create_demo():
146
  file_count="multiple"
147
  )
148
 
149
- # Define preferred and additional models
150
- preferred_models = [
151
- ("Gemini 2.0 Flash (default)", "google/gemini-2.0-flash-001"),
152
- ("GPT-4.1 Mini", "gpt-4.1-mini"),
153
- ("GPT-4.1", "gpt-4.1"),
154
- ("Claude 3.7 Sonnet", "anthropic/claude-3.7-sonnet"),
155
- ("Gemini 2.5 Pro", "google/gemini-2.5-pro-preview-03-25"),
156
- ("Gemini 2.5 Flash Thinking", "google/gemini-2.5-flash-preview:thinking")
157
- ]
158
-
159
- additional_models = [
160
- ("GPT-4.1 Nano", "gpt-4.1-nano"),
161
- ("ChatGPT Latest", "openai/chatgpt-4o-latest"),
162
- ("Llama 4 Maverick", "meta-llama/llama-4-maverick")
163
- ]
164
-
165
- # Default model
166
- default_model_internal_value = "google/gemini-2.0-flash-001"
167
-
168
  # Model dropdown (will be updated dynamically)
169
  model_choice = gr.Dropdown(
170
- choices=preferred_models,
171
  label="Select Model",
172
- value=default_model_internal_value,
173
  visible=True
174
  )
175
 
@@ -194,11 +224,12 @@ def create_demo():
194
  value="Museum Object"
195
  )
196
 
197
- # Display current model info
 
 
 
198
  model_info = gr.Markdown(
199
- """**Current Model**: Gemini 2.0 Flash
200
- **Cost per 100 Images**: $0.50
201
- **Speed**: Fast""",
202
  label="Model Information"
203
  )
204
 
@@ -239,36 +270,6 @@ def create_demo():
239
  all_images = gr.State([])
240
  all_results = gr.State([])
241
 
242
- # --- Functions for model selection and display ---
243
- # Update dropdown based on checkbox
244
- def update_model_dropdown(show_all, current_model):
245
- if show_all:
246
- # Combine preferred and additional models
247
- all_models = preferred_models + additional_models
248
- return gr.Dropdown.update(choices=all_models, value=current_model)
249
- else:
250
- # Only show preferred models
251
- # If current model is not in preferred, switch to default
252
- current_model_in_preferred = any(model[1] == current_model for model in preferred_models)
253
- if not current_model_in_preferred:
254
- return gr.Dropdown.update(choices=preferred_models, value=default_model_internal_value)
255
- return gr.Dropdown.update(choices=preferred_models, value=current_model)
256
-
257
- # Update model info display
258
- def update_model_info(model_id):
259
- # Find the display name for the model ID
260
- model_name = next((name for name, id in preferred_models + additional_models if id == model_id), "Unknown Model")
261
-
262
- # Get pricing info
263
- pricing_info = MODEL_PRICING.get(model_id, {"cost": "Unknown", "speed": "Unknown"})
264
-
265
- # Create markdown text
266
- info_text = f"""**Current Model**: {model_name}
267
- **Cost per 100 Images**: {pricing_info['cost']}
268
- **Speed**: {pricing_info['speed']}"""
269
-
270
- return info_text
271
-
272
  # Connect checkbox to update dropdown
273
  show_all_models.change(
274
  fn=update_model_dropdown,
@@ -316,7 +317,13 @@ def create_demo():
316
  img = Image.open(image_path)
317
  prompt0 = prompt_new()
318
  model_name = model_choice
319
- result = OR.generate_caption(
 
 
 
 
 
 
320
  img, model=model_name, max_image_size=512,
321
  prompt=prompt0, prompt_dev=sys_prompt, temperature=1
322
  )
@@ -396,6 +403,5 @@ def create_demo():
396
 
397
  # Launch the app
398
  if __name__ == "__main__":
399
-
400
  app = create_demo()
401
  app.launch()
 
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 = ""
 
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 ---
 
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
 
 
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
 
 
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,
 
317
  img = Image.open(image_path)
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,
328
  prompt=prompt0, prompt_dev=sys_prompt, temperature=1
329
  )
 
403
 
404
  # Launch the app
405
  if __name__ == "__main__":
 
406
  app = create_demo()
407
  app.launch()