Chris Addis commited on
Commit ·
2c8a1cc
1
Parent(s): 12bfe0c
Advanced settings
Browse files
app.py
CHANGED
|
@@ -36,6 +36,20 @@ PREFERENCES_FILE = "data/user_preferences.csv"
|
|
| 36 |
# Ensure directory exists
|
| 37 |
os.makedirs(os.path.dirname(PREFERENCES_FILE), exist_ok=True)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def get_sys_prompt(length="medium", photograph=False):
|
| 40 |
|
| 41 |
extra_prompt = ""
|
|
@@ -107,7 +121,21 @@ def create_demo():
|
|
| 107 |
"""
|
| 108 |
# --- Pass css to gr.Blocks ---
|
| 109 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
# Left column: Controls and uploads
|
|
@@ -118,8 +146,8 @@ def create_demo():
|
|
| 118 |
file_count="multiple"
|
| 119 |
)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
("Gemini 2.0 Flash (default)", "google/gemini-2.0-flash-001"),
|
| 124 |
("GPT-4.1 Mini", "gpt-4.1-mini"),
|
| 125 |
("GPT-4.1", "gpt-4.1"),
|
|
@@ -128,30 +156,36 @@ def create_demo():
|
|
| 128 |
("Gemini 2.5 Flash Thinking", "google/gemini-2.5-flash-preview:thinking")
|
| 129 |
]
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
default_model_internal_value = "google/gemini-2.0-flash-001"
|
|
|
|
|
|
|
| 132 |
model_choice = gr.Dropdown(
|
| 133 |
-
choices=
|
| 134 |
-
|
|
|
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
length_choice = gr.Radio(
|
| 138 |
-
choices=["short", "medium", "long"],
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
# Advanced settings accordion
|
| 143 |
with gr.Accordion("Advanced Settings", open=False):
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
("Llama 4 Maverick", "meta-llama/llama-4-maverick")
|
| 149 |
-
]
|
| 150 |
-
|
| 151 |
-
advanced_model_choice = gr.Dropdown(
|
| 152 |
-
choices=advanced_model_choices,
|
| 153 |
-
label="Additional Models",
|
| 154 |
-
value=None
|
| 155 |
)
|
| 156 |
|
| 157 |
content_type = gr.Radio(
|
|
@@ -159,8 +193,15 @@ def create_demo():
|
|
| 159 |
label="Content Type",
|
| 160 |
value="Museum Object"
|
| 161 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
# [rest of the interface remains the same]
|
| 164 |
gr.Markdown("### Uploaded Images")
|
| 165 |
input_gallery = gr.Gallery(
|
| 166 |
label="Uploaded Image Previews", columns=3, height=150,
|
|
@@ -198,7 +239,50 @@ def create_demo():
|
|
| 198 |
all_images = gr.State([])
|
| 199 |
all_results = gr.State([])
|
| 200 |
|
| 201 |
-
# --- Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
# Handle file uploads
|
| 203 |
def handle_upload(files, current_paths, current_filenames):
|
| 204 |
file_paths = []
|
|
@@ -217,13 +301,10 @@ def create_demo():
|
|
| 217 |
)
|
| 218 |
|
| 219 |
# Analyze images
|
| 220 |
-
def analyze_images(image_paths,
|
| 221 |
if not image_paths:
|
| 222 |
return [], [], 0, None, "0 of 0", "No images uploaded to analyze.", None
|
| 223 |
|
| 224 |
-
# Determine which model to use (advanced model takes precedence if selected)
|
| 225 |
-
model_choice = adv_model if adv_model else main_model
|
| 226 |
-
|
| 227 |
is_photography = content_type_choice == "Photography"
|
| 228 |
sys_prompt = get_sys_prompt(length_choice, photograph=is_photography)
|
| 229 |
image_results = []
|
|
@@ -278,7 +359,7 @@ def create_demo():
|
|
| 278 |
# Connect analyze button
|
| 279 |
analyze_button.click(
|
| 280 |
fn=analyze_images,
|
| 281 |
-
inputs=[image_state, model_choice,
|
| 282 |
outputs=[all_images, all_results, current_index, current_image, image_counter,
|
| 283 |
analysis_text, csv_download]
|
| 284 |
)
|
|
|
|
| 36 |
# Ensure directory exists
|
| 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", "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 = ""
|
|
|
|
| 121 |
"""
|
| 122 |
# --- Pass css to gr.Blocks ---
|
| 123 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
| 124 |
+
with gr.Row():
|
| 125 |
+
with gr.Column(scale=3):
|
| 126 |
+
gr.Markdown("# MATCHA: Museum Alt-Text for Cultural Heritage with AI 🍵 🌿")
|
| 127 |
+
gr.Markdown("Upload one or more images to generate accessible alternative text (designed to meet WCAG Guidelines)")
|
| 128 |
+
gr.Markdown("Developed by the Natural History Museum in Partnership with National Museums Liverpool. Funded by the DCMS Pilot Scheme")
|
| 129 |
+
with gr.Column(scale=1):
|
| 130 |
+
with gr.Row():
|
| 131 |
+
gr.Image("images/nhm_logo.png", show_label=False, height=120,
|
| 132 |
+
interactive=False, show_download_button=False,
|
| 133 |
+
show_share_button=False, show_fullscreen_button=False,
|
| 134 |
+
container=False, elem_id="nhm-logo")
|
| 135 |
+
gr.Image("images/nml_logo.png", show_label=False, height=120,
|
| 136 |
+
interactive=False, show_download_button=False,
|
| 137 |
+
show_share_button=False, show_fullscreen_button=False,
|
| 138 |
+
container=False, elem_id="nml-logo")
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
# Left column: Controls and uploads
|
|
|
|
| 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"),
|
|
|
|
| 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 |
|
| 176 |
length_choice = gr.Radio(
|
| 177 |
+
choices=["short", "medium", "long"],
|
| 178 |
+
label="Response Length",
|
| 179 |
+
value="medium",
|
| 180 |
+
info="Short: max 130 chars | Medium: 250-300 chars | Long: max 450 chars"
|
| 181 |
)
|
| 182 |
|
| 183 |
# Advanced settings accordion
|
| 184 |
with gr.Accordion("Advanced Settings", open=False):
|
| 185 |
+
show_all_models = gr.Checkbox(
|
| 186 |
+
label="Show Additional Models",
|
| 187 |
+
value=False,
|
| 188 |
+
info="Display additional model options in the dropdown above"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
)
|
| 190 |
|
| 191 |
content_type = gr.Radio(
|
|
|
|
| 193 |
label="Content Type",
|
| 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 |
|
|
|
|
| 205 |
gr.Markdown("### Uploaded Images")
|
| 206 |
input_gallery = gr.Gallery(
|
| 207 |
label="Uploaded Image Previews", columns=3, height=150,
|
|
|
|
| 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,
|
| 275 |
+
inputs=[show_all_models, model_choice],
|
| 276 |
+
outputs=[model_choice]
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# Connect model dropdown to update info display
|
| 280 |
+
model_choice.change(
|
| 281 |
+
fn=update_model_info,
|
| 282 |
+
inputs=[model_choice],
|
| 283 |
+
outputs=[model_info]
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
# Handle file uploads
|
| 287 |
def handle_upload(files, current_paths, current_filenames):
|
| 288 |
file_paths = []
|
|
|
|
| 301 |
)
|
| 302 |
|
| 303 |
# Analyze images
|
| 304 |
+
def analyze_images(image_paths, model_choice, length_choice, filenames, content_type_choice):
|
| 305 |
if not image_paths:
|
| 306 |
return [], [], 0, None, "0 of 0", "No images uploaded to analyze.", None
|
| 307 |
|
|
|
|
|
|
|
|
|
|
| 308 |
is_photography = content_type_choice == "Photography"
|
| 309 |
sys_prompt = get_sys_prompt(length_choice, photograph=is_photography)
|
| 310 |
image_results = []
|
|
|
|
| 359 |
# Connect analyze button
|
| 360 |
analyze_button.click(
|
| 361 |
fn=analyze_images,
|
| 362 |
+
inputs=[image_state, model_choice, length_choice, filename_state, content_type],
|
| 363 |
outputs=[all_images, all_results, current_index, current_image, image_counter,
|
| 364 |
analysis_text, csv_download]
|
| 365 |
)
|