Chris Addis commited on
Commit ·
86cec04
1
Parent(s): 4c4fdd7
add authentication
Browse files
app.py
CHANGED
|
@@ -20,26 +20,11 @@ from library.utils_prompt import *
|
|
| 20 |
authorized_users_str = os.environ.get("AUTHORIZED_USER_IDS", "")
|
| 21 |
AUTHORIZED_USER_IDS = set(authorized_users_str.split(',') if authorized_users_str and authorized_users_str.strip() else [])
|
| 22 |
|
| 23 |
-
# Check if current user is authorized
|
| 24 |
-
def check_authorization(profile: gr.OAuthProfile | None):
|
| 25 |
-
if profile is None:
|
| 26 |
-
return False #"Not logged in. Please Login for full access."
|
| 27 |
-
|
| 28 |
-
if profile.username in AUTHORIZED_USER_IDS:
|
| 29 |
-
return True
|
| 30 |
-
else:
|
| 31 |
-
return False
|
| 32 |
|
| 33 |
#Api sdk
|
| 34 |
OR = OpenRouterAPI()
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
PREFERENCES_FILE = "data/user_preferences.csv"
|
| 38 |
-
|
| 39 |
-
# Ensure directory exists
|
| 40 |
-
os.makedirs(os.path.dirname(PREFERENCES_FILE), exist_ok=True)
|
| 41 |
-
|
| 42 |
-
# Define model pricing information (approximate costs per 100 image API calls)
|
| 43 |
MODEL_PRICING = {
|
| 44 |
"google/gemini-2.0-flash-001": "$0.03",
|
| 45 |
"gpt-4.1-mini": "$0.07",
|
|
@@ -50,9 +35,28 @@ MODEL_PRICING = {
|
|
| 50 |
"gpt-4.1-nano": "$0.02",
|
| 51 |
"openai/chatgpt-4o-latest": "$0.75",
|
| 52 |
"meta-llama/llama-4-maverick": "$0.04",
|
| 53 |
-
"meta-llama/llama-4-maverick:free": "
|
| 54 |
}
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def get_sys_prompt(length="medium", photograph=False):
|
| 57 |
extra_prompt = ""
|
| 58 |
|
|
@@ -97,6 +101,18 @@ def get_base_filename(filepath):
|
|
| 97 |
filename = os.path.splitext(basename)[0]
|
| 98 |
return filename
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Define the Gradio interface
|
| 101 |
def create_demo():
|
| 102 |
custom_css = """
|
|
@@ -131,23 +147,6 @@ def create_demo():
|
|
| 131 |
"""
|
| 132 |
# --- Pass css to gr.Blocks ---
|
| 133 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
| 134 |
-
# Store authentication state
|
| 135 |
-
is_authenticated = gr.State(False)
|
| 136 |
-
|
| 137 |
-
# Initialize authentication on app load
|
| 138 |
-
def app_load():
|
| 139 |
-
# Get the OAuth profile (will be None if not logged in)
|
| 140 |
-
profile = getattr(gr.context.get('user', None), 'profile', None)
|
| 141 |
-
# Use the check_authorization function
|
| 142 |
-
return check_authorization(profile)
|
| 143 |
-
|
| 144 |
-
# Update authentication state on load
|
| 145 |
-
demo.load(
|
| 146 |
-
fn=app_load,
|
| 147 |
-
inputs=None,
|
| 148 |
-
outputs=[is_authenticated]
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=3):
|
| 153 |
gr.Markdown("# MATCHA: Museum Alt-Text for Cultural Heritage with AI 🍵 🌿")
|
|
@@ -155,23 +154,16 @@ def create_demo():
|
|
| 155 |
gr.Markdown("Developed by the Natural History Museum in Partnership with National Museums Liverpool. Funded by the DCMS Pilot Scheme")
|
| 156 |
with gr.Column(scale=1):
|
| 157 |
with gr.Row():
|
| 158 |
-
gr.Image("images/nhm_logo.png", show_label=False, height=
|
| 159 |
interactive=False, show_download_button=False,
|
| 160 |
show_share_button=False, show_fullscreen_button=False,
|
| 161 |
container=False, elem_id="nhm-logo")
|
| 162 |
-
gr.Image("images/nml_logo.png", show_label=False, height=
|
| 163 |
interactive=False, show_download_button=False,
|
| 164 |
show_share_button=False, show_fullscreen_button=False,
|
| 165 |
container=False, elem_id="nml-logo")
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
show_all_models_state = gr.State(False)
|
| 169 |
-
|
| 170 |
-
# Define model lists
|
| 171 |
-
free_model = [
|
| 172 |
-
("Llama 4 Maverick (Free)", "meta-llama/llama-4-maverick:free")
|
| 173 |
-
]
|
| 174 |
-
|
| 175 |
preferred_models = [
|
| 176 |
("Gemini 2.0 Flash (cheap)", "google/gemini-2.0-flash-001"),
|
| 177 |
("GPT-4.1 Mini", "gpt-4.1-mini"),
|
|
@@ -180,107 +172,52 @@ def create_demo():
|
|
| 180 |
("Gemini 2.5 Pro", "google/gemini-2.5-pro-preview-03-25"),
|
| 181 |
("Gemini 2.5 Flash Thinking (Recommended)", "google/gemini-2.5-flash-preview:thinking")
|
| 182 |
]
|
| 183 |
-
|
| 184 |
additional_models = [
|
| 185 |
("GPT-4.1 Nano", "gpt-4.1-nano"),
|
| 186 |
("ChatGPT Latest", "openai/chatgpt-4o-latest"),
|
| 187 |
("Llama 4 Maverick", "meta-llama/llama-4-maverick")
|
| 188 |
]
|
| 189 |
-
|
| 190 |
-
# Calculate all models
|
| 191 |
all_models_list = preferred_models + additional_models
|
| 192 |
-
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
else:
|
| 198 |
-
return gr.Dropdown(choices=free_model, value="meta-llama/llama-4-maverick:free")
|
| 199 |
-
|
| 200 |
with gr.Row():
|
| 201 |
-
# Left column: Controls and uploads
|
| 202 |
with gr.Column(scale=1):
|
| 203 |
-
upload_button = gr.UploadButton(
|
| 204 |
-
"Click to Upload Images",
|
| 205 |
-
file_types=["image"],
|
| 206 |
-
file_count="multiple"
|
| 207 |
-
)
|
| 208 |
|
| 209 |
-
# Model dropdown - initially empty, will be populated based on auth status
|
| 210 |
model_choice = gr.Dropdown(
|
| 211 |
-
choices=
|
|
|
|
| 212 |
label="Select Model",
|
| 213 |
-
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
-
# Update the model dropdown when authentication state changes
|
| 217 |
-
is_authenticated.change(
|
| 218 |
-
fn=update_model_dropdown,
|
| 219 |
-
inputs=[is_authenticated],
|
| 220 |
-
outputs=[model_choice]
|
| 221 |
-
)
|
| 222 |
-
|
| 223 |
-
# Also update model dropdown on initial load
|
| 224 |
-
demo.load(
|
| 225 |
-
fn=update_model_dropdown,
|
| 226 |
-
inputs=[is_authenticated],
|
| 227 |
-
outputs=[model_choice]
|
| 228 |
)
|
| 229 |
|
| 230 |
length_choice = gr.Radio(
|
| 231 |
choices=["short", "medium", "long"],
|
| 232 |
-
label="Response Length",
|
| 233 |
-
value="medium",
|
| 234 |
info="Short: max 130 chars | Medium: 250-300 chars | Long: max 450 chars"
|
| 235 |
)
|
| 236 |
|
| 237 |
-
#
|
| 238 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 239 |
-
|
| 240 |
label="Show Additional Models",
|
| 241 |
-
value=False,
|
| 242 |
-
info="Display additional model options in the dropdown above"
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
# Update checkbox interactivity based on auth status
|
| 246 |
-
def update_checkbox_interactive(is_auth):
|
| 247 |
-
return gr.Checkbox(
|
| 248 |
-
label="Show Additional Models",
|
| 249 |
-
value=False,
|
| 250 |
-
info="Display additional model options in the dropdown above",
|
| 251 |
-
interactive=is_auth
|
| 252 |
-
)
|
| 253 |
-
|
| 254 |
-
is_authenticated.change(
|
| 255 |
-
fn=update_checkbox_interactive,
|
| 256 |
-
inputs=[is_authenticated],
|
| 257 |
-
outputs=[show_all_models]
|
| 258 |
-
)
|
| 259 |
-
|
| 260 |
-
# Also update checkbox interactivity on initial load
|
| 261 |
-
demo.load(
|
| 262 |
-
fn=update_checkbox_interactive,
|
| 263 |
-
inputs=[is_authenticated],
|
| 264 |
-
outputs=[show_all_models]
|
| 265 |
)
|
| 266 |
-
|
| 267 |
content_type = gr.Radio(
|
| 268 |
choices=["Museum Object", "Photography"],
|
| 269 |
-
label="Content Type",
|
| 270 |
-
value="Museum Object"
|
| 271 |
)
|
| 272 |
|
| 273 |
-
|
| 274 |
-
model_info = gr.Markdown(
|
| 275 |
-
"**Select a model to see information**",
|
| 276 |
-
elem_id="model-info-display"
|
| 277 |
-
)
|
| 278 |
|
| 279 |
gr.Markdown("### Uploaded Images")
|
| 280 |
-
input_gallery = gr.Gallery(
|
| 281 |
-
label="Uploaded Image Previews", columns=3, height=150,
|
| 282 |
-
object_fit="contain", show_label=False
|
| 283 |
-
)
|
| 284 |
analyze_button = gr.Button("Generate Alt-Text", variant="primary", size="lg")
|
| 285 |
image_state = gr.State([])
|
| 286 |
filename_state = gr.State([])
|
|
@@ -288,141 +225,95 @@ def create_demo():
|
|
| 288 |
|
| 289 |
# Right column: Display area
|
| 290 |
with gr.Column(scale=2):
|
|
|
|
| 291 |
current_image = gr.Image(
|
| 292 |
-
label="Current Image",
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
show_fullscreen_button=True,
|
| 296 |
-
show_download_button=False,
|
| 297 |
-
show_share_button=False,
|
| 298 |
-
show_label=False
|
| 299 |
)
|
| 300 |
-
|
| 301 |
with gr.Row():
|
| 302 |
prev_button = gr.Button("← Previous", size="sm")
|
| 303 |
image_counter = gr.Markdown("0 of 0", elem_id="image-counter")
|
| 304 |
next_button = gr.Button("Next →", size="sm")
|
| 305 |
-
|
| 306 |
gr.Markdown("### Generated Alt-text")
|
| 307 |
analysis_text = gr.Textbox(
|
| 308 |
-
label="Generated Text",
|
| 309 |
-
value="Upload images and click 'Generate Alt-Text'.",
|
| 310 |
lines=6, max_lines=10, interactive=True, show_label=False
|
| 311 |
)
|
| 312 |
current_index = gr.State(0)
|
| 313 |
all_images = gr.State([])
|
| 314 |
all_results = gr.State([])
|
| 315 |
-
|
| 316 |
-
# Handle checkbox change to update model dropdown
|
| 317 |
-
def toggle_models(show_all, current_model, is_auth):
|
| 318 |
-
if not is_auth:
|
| 319 |
-
# Non-authenticated users only get free model
|
| 320 |
-
return gr.Dropdown(choices=free_model, value="meta-llama/llama-4-maverick:free")
|
| 321 |
-
|
| 322 |
-
# For authenticated users, existing logic
|
| 323 |
-
preferred_choices = list(preferred_models)
|
| 324 |
-
all_choices = list(all_models_list)
|
| 325 |
-
|
| 326 |
-
if show_all:
|
| 327 |
-
return gr.Dropdown(choices=all_choices, value=current_model)
|
| 328 |
-
else:
|
| 329 |
-
preferred_values = [value for _, value in preferred_choices]
|
| 330 |
-
|
| 331 |
-
if current_model in preferred_values:
|
| 332 |
-
return gr.Dropdown(choices=preferred_choices, value=current_model)
|
| 333 |
-
else:
|
| 334 |
-
return gr.Dropdown(choices=preferred_choices, value="google/gemini-2.0-flash-001")
|
| 335 |
|
| 336 |
-
#
|
| 337 |
-
def
|
| 338 |
-
|
| 339 |
-
combined_models = preferred_models + additional_models + free_model
|
| 340 |
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
model_choice.change(
|
| 364 |
-
fn=update_model_info,
|
| 365 |
-
inputs=[model_choice],
|
| 366 |
-
outputs=[model_info]
|
| 367 |
)
|
| 368 |
|
| 369 |
-
#
|
| 370 |
-
def
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
if
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
-
|
| 380 |
-
fn=
|
| 381 |
-
inputs=[
|
| 382 |
-
outputs=[
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
)
|
| 385 |
|
| 386 |
-
# Analyze images
|
| 387 |
-
def analyze_images(image_paths, model_choice, length_choice, filenames, content_type_choice):
|
| 388 |
-
if not image_paths:
|
| 389 |
-
return [], [], 0, None, "0 of 0", "No images uploaded to analyze.", None
|
| 390 |
-
|
| 391 |
-
is_photography = content_type_choice == "Photography"
|
| 392 |
-
sys_prompt = get_sys_prompt(length_choice, photograph=is_photography)
|
| 393 |
-
image_results = []
|
| 394 |
-
analysis_progress = gr.Progress(track_tqdm=True)
|
| 395 |
-
|
| 396 |
-
for i, image_path in enumerate(analysis_progress.tqdm(image_paths, desc="Analyzing Images")):
|
| 397 |
-
image_id = filenames[i] if i < len(filenames) and filenames[i] else f"Image_{i+1}_{os.path.basename(image_path)}"
|
| 398 |
-
try:
|
| 399 |
-
img = Image.open(image_path)
|
| 400 |
-
prompt0 = prompt_new()
|
| 401 |
-
model_name = model_choice
|
| 402 |
-
client_to_use = OR # Default client
|
| 403 |
-
|
| 404 |
-
result = client_to_use.generate_caption(
|
| 405 |
-
img, model=model_name, max_image_size=512,
|
| 406 |
-
prompt=prompt0, prompt_dev=sys_prompt, temperature=1
|
| 407 |
-
)
|
| 408 |
-
image_results.append({"image_id": image_id, "content": result.strip()})
|
| 409 |
-
except FileNotFoundError:
|
| 410 |
-
error_message = f"Error: File not found at path '{image_path}'"
|
| 411 |
-
print(error_message)
|
| 412 |
-
image_results.append({"image_id": image_id, "content": error_message})
|
| 413 |
-
except Exception as e:
|
| 414 |
-
error_message = f"Error processing {image_id}: {str(e)}"
|
| 415 |
-
print(error_message)
|
| 416 |
-
image_results.append({"image_id": image_id, "content": error_message})
|
| 417 |
-
|
| 418 |
-
csv_path = create_csv_file_simple(image_results)
|
| 419 |
-
initial_image = image_paths[0] if image_paths else None
|
| 420 |
-
initial_counter = f"1 of {len(image_paths)}" if image_paths else "0 of 0"
|
| 421 |
-
initial_text = image_results[0]["content"] if image_results else "Analysis complete, but no results generated."
|
| 422 |
-
|
| 423 |
-
return (image_paths, image_results, 0, initial_image, initial_counter,
|
| 424 |
-
initial_text, csv_path)
|
| 425 |
-
|
| 426 |
# Navigate previous
|
| 427 |
def go_to_prev(current_idx, images, results):
|
| 428 |
if not images or not results or len(images) == 0:
|
|
@@ -482,4 +373,5 @@ def create_demo():
|
|
| 482 |
# Launch the app
|
| 483 |
if __name__ == "__main__":
|
| 484 |
app = create_demo()
|
| 485 |
-
|
|
|
|
|
|
| 20 |
authorized_users_str = os.environ.get("AUTHORIZED_USER_IDS", "")
|
| 21 |
AUTHORIZED_USER_IDS = set(authorized_users_str.split(',') if authorized_users_str and authorized_users_str.strip() else [])
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
#Api sdk
|
| 25 |
OR = OpenRouterAPI()
|
| 26 |
|
| 27 |
+
# At the top of your script, modify MODEL_PRICING:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
MODEL_PRICING = {
|
| 29 |
"google/gemini-2.0-flash-001": "$0.03",
|
| 30 |
"gpt-4.1-mini": "$0.07",
|
|
|
|
| 35 |
"gpt-4.1-nano": "$0.02",
|
| 36 |
"openai/chatgpt-4o-latest": "$0.75",
|
| 37 |
"meta-llama/llama-4-maverick": "$0.04",
|
| 38 |
+
"meta-llama/llama-4-maverick:free": "$0.00" # Added for the free tier
|
| 39 |
}
|
| 40 |
|
| 41 |
+
# Define choices and default for unauthenticated users
|
| 42 |
+
UNAUTHENTICATED_MODEL_CHOICES = [("Llama 4 Maverick (Free Tier)", "meta-llama/llama-4-maverick:free")]
|
| 43 |
+
UNAUTHENTICATED_DEFAULT_MODEL_ID = "meta-llama/llama-4-maverick:free"
|
| 44 |
+
|
| 45 |
+
# Your existing default for authenticated users
|
| 46 |
+
AUTHENTICATED_DEFAULT_MODEL_ID = "google/gemini-2.0-flash-001"
|
| 47 |
+
|
| 48 |
+
# New function to check if current user is authorized
|
| 49 |
+
def check_authorization(profile: gr.OAuthProfile | None):
|
| 50 |
+
if not AUTHORIZED_USER_IDS: # If the set is empty, no one is authorized by this list
|
| 51 |
+
return False
|
| 52 |
+
if profile is None:
|
| 53 |
+
return False #"Not logged in. Please Login for full access."
|
| 54 |
+
|
| 55 |
+
if profile.username in AUTHORIZED_USER_IDS:
|
| 56 |
+
return True
|
| 57 |
+
else:
|
| 58 |
+
return False
|
| 59 |
+
|
| 60 |
def get_sys_prompt(length="medium", photograph=False):
|
| 61 |
extra_prompt = ""
|
| 62 |
|
|
|
|
| 101 |
filename = os.path.splitext(basename)[0]
|
| 102 |
return filename
|
| 103 |
|
| 104 |
+
# Helper function to generate model info markdown
|
| 105 |
+
def get_model_info_markdown(model_id, is_authorized):
|
| 106 |
+
models_to_search = all_models_list if is_authorized else UNAUTHENTICATED_MODEL_CHOICES
|
| 107 |
+
model_name = "Unknown Model"
|
| 108 |
+
for name, value in models_to_search:
|
| 109 |
+
if value == model_id:
|
| 110 |
+
model_name = name
|
| 111 |
+
break
|
| 112 |
+
cost = MODEL_PRICING.get(model_id, "N/A")
|
| 113 |
+
return f"""**Current Model**: {model_name}
|
| 114 |
+
**Estimated cost per 100 Images**: {cost}"""
|
| 115 |
+
|
| 116 |
# Define the Gradio interface
|
| 117 |
def create_demo():
|
| 118 |
custom_css = """
|
|
|
|
| 147 |
"""
|
| 148 |
# --- Pass css to gr.Blocks ---
|
| 149 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
with gr.Row():
|
| 151 |
with gr.Column(scale=3):
|
| 152 |
gr.Markdown("# MATCHA: Museum Alt-Text for Cultural Heritage with AI 🍵 🌿")
|
|
|
|
| 154 |
gr.Markdown("Developed by the Natural History Museum in Partnership with National Museums Liverpool. Funded by the DCMS Pilot Scheme")
|
| 155 |
with gr.Column(scale=1):
|
| 156 |
with gr.Row():
|
| 157 |
+
gr.Image("images/nhm_logo.png", show_label=False, height=100,
|
| 158 |
interactive=False, show_download_button=False,
|
| 159 |
show_share_button=False, show_fullscreen_button=False,
|
| 160 |
container=False, elem_id="nhm-logo")
|
| 161 |
+
gr.Image("images/nml_logo.png", show_label=False, height=100,
|
| 162 |
interactive=False, show_download_button=False,
|
| 163 |
show_share_button=False, show_fullscreen_button=False,
|
| 164 |
container=False, elem_id="nml-logo")
|
| 165 |
|
| 166 |
+
# Define model lists (these will be used by the auth logic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
preferred_models = [
|
| 168 |
("Gemini 2.0 Flash (cheap)", "google/gemini-2.0-flash-001"),
|
| 169 |
("GPT-4.1 Mini", "gpt-4.1-mini"),
|
|
|
|
| 172 |
("Gemini 2.5 Pro", "google/gemini-2.5-pro-preview-03-25"),
|
| 173 |
("Gemini 2.5 Flash Thinking (Recommended)", "google/gemini-2.5-flash-preview:thinking")
|
| 174 |
]
|
|
|
|
| 175 |
additional_models = [
|
| 176 |
("GPT-4.1 Nano", "gpt-4.1-nano"),
|
| 177 |
("ChatGPT Latest", "openai/chatgpt-4o-latest"),
|
| 178 |
("Llama 4 Maverick", "meta-llama/llama-4-maverick")
|
| 179 |
]
|
|
|
|
|
|
|
| 180 |
all_models_list = preferred_models + additional_models
|
| 181 |
+
|
| 182 |
+
# State variables
|
| 183 |
+
auth_status_state = gr.State(False) # True if authorized
|
| 184 |
+
user_show_all_models_preference_state = gr.State(False) # User's preference for showing all models
|
| 185 |
+
|
|
|
|
|
|
|
|
|
|
| 186 |
with gr.Row():
|
|
|
|
| 187 |
with gr.Column(scale=1):
|
| 188 |
+
upload_button = gr.UploadButton("Click to Upload Images", file_types=["image"], file_count="multiple")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
|
|
|
| 190 |
model_choice = gr.Dropdown(
|
| 191 |
+
choices=UNAUTHENTICATED_MODEL_CHOICES, # Initial default for unauth
|
| 192 |
+
value=UNAUTHENTICATED_DEFAULT_MODEL_ID,
|
| 193 |
label="Select Model",
|
| 194 |
+
interactive=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
length_choice = gr.Radio(
|
| 198 |
choices=["short", "medium", "long"],
|
| 199 |
+
label="Response Length", value="medium",
|
|
|
|
| 200 |
info="Short: max 130 chars | Medium: 250-300 chars | Long: max 450 chars"
|
| 201 |
)
|
| 202 |
|
| 203 |
+
# Accordion for advanced settings - visibility controlled by auth
|
| 204 |
+
with gr.Accordion("Advanced Settings", open=False, visible=False) as advanced_settings_accordion:
|
| 205 |
+
show_all_models_checkbox = gr.Checkbox(
|
| 206 |
label="Show Additional Models",
|
| 207 |
+
value=False, # Will be updated by auth logic
|
| 208 |
+
info="Display additional model options in the dropdown above",
|
| 209 |
+
interactive=False # Will be updated by auth logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
)
|
|
|
|
| 211 |
content_type = gr.Radio(
|
| 212 |
choices=["Museum Object", "Photography"],
|
| 213 |
+
label="Content Type", value="Museum Object"
|
|
|
|
| 214 |
)
|
| 215 |
|
| 216 |
+
initial_model_info = get_model_info_markdown(UNAUTHENTICATED_DEFAULT_MODEL_ID, False)
|
| 217 |
+
model_info = gr.Markdown(initial_model_info, elem_id="model-info-display")
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
gr.Markdown("### Uploaded Images")
|
| 220 |
+
input_gallery = gr.Gallery(label="Uploaded Image Previews", columns=3, height=150, object_fit="contain", show_label=False)
|
|
|
|
|
|
|
|
|
|
| 221 |
analyze_button = gr.Button("Generate Alt-Text", variant="primary", size="lg")
|
| 222 |
image_state = gr.State([])
|
| 223 |
filename_state = gr.State([])
|
|
|
|
| 225 |
|
| 226 |
# Right column: Display area
|
| 227 |
with gr.Column(scale=2):
|
| 228 |
+
# right column UI: current_image, navigation, analysis_text
|
| 229 |
current_image = gr.Image(
|
| 230 |
+
label="Current Image", type="filepath", elem_id="current-image-display",
|
| 231 |
+
show_fullscreen_button=True, show_download_button=False,
|
| 232 |
+
show_share_button=False, show_label=False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
)
|
|
|
|
| 234 |
with gr.Row():
|
| 235 |
prev_button = gr.Button("← Previous", size="sm")
|
| 236 |
image_counter = gr.Markdown("0 of 0", elem_id="image-counter")
|
| 237 |
next_button = gr.Button("Next →", size="sm")
|
|
|
|
| 238 |
gr.Markdown("### Generated Alt-text")
|
| 239 |
analysis_text = gr.Textbox(
|
| 240 |
+
label="Generated Text", value="Upload images and click 'Generate Alt-Text'.",
|
|
|
|
| 241 |
lines=6, max_lines=10, interactive=True, show_label=False
|
| 242 |
)
|
| 243 |
current_index = gr.State(0)
|
| 244 |
all_images = gr.State([])
|
| 245 |
all_results = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
# Function to update UI based on authentication status (called on demo.load)
|
| 248 |
+
def handle_auth_change_and_ui_setup(profile: gr.OAuthProfile | None, current_user_pref_show_all: bool):
|
| 249 |
+
is_authorized = check_authorization(profile)
|
|
|
|
| 250 |
|
| 251 |
+
if is_authorized:
|
| 252 |
+
# Authorized User
|
| 253 |
+
current_choices = list(all_models_list) if current_user_pref_show_all else list(preferred_models)
|
| 254 |
+
current_value = AUTHENTICATED_DEFAULT_MODEL_ID # Reset to default for authed user
|
| 255 |
+
# You could add logic here to try and preserve model_choice.value if it's valid in current_choices
|
| 256 |
+
|
| 257 |
+
model_dropdown_update = gr.Dropdown.update(choices=current_choices, value=current_value, label="Select Model", interactive=True)
|
| 258 |
+
accordion_update = gr.Accordion.update(visible=True)
|
| 259 |
+
model_info_text_update = get_model_info_markdown(current_value, True)
|
| 260 |
+
show_all_checkbox_update = gr.Checkbox.update(value=current_user_pref_show_all, interactive=True)
|
| 261 |
+
else:
|
| 262 |
+
# Unauthenticated User
|
| 263 |
+
model_dropdown_update = gr.Dropdown.update(choices=UNAUTHENTICATED_MODEL_CHOICES, value=UNAUTHENTICATED_DEFAULT_MODEL_ID, label="Select Model (Free Tier)", interactive=True)
|
| 264 |
+
accordion_update = gr.Accordion.update(visible=False)
|
| 265 |
+
model_info_text_update = get_model_info_markdown(UNAUTHENTICATED_DEFAULT_MODEL_ID, False)
|
| 266 |
+
show_all_checkbox_update = gr.Checkbox.update(value=False, interactive=False)
|
| 267 |
|
| 268 |
+
return is_authorized, model_dropdown_update, accordion_update, model_info_text_update, show_all_checkbox_update
|
| 269 |
+
|
| 270 |
+
demo.load(
|
| 271 |
+
fn=handle_auth_change_and_ui_setup,
|
| 272 |
+
inputs=[user_show_all_models_preference_state], # Pass the state that holds user's preference
|
| 273 |
+
outputs=[
|
| 274 |
+
auth_status_state,
|
| 275 |
+
model_choice,
|
| 276 |
+
advanced_settings_accordion,
|
| 277 |
+
model_info,
|
| 278 |
+
show_all_models_checkbox
|
| 279 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
)
|
| 281 |
|
| 282 |
+
# Function to update UI based on authentication status (called on demo.load)
|
| 283 |
+
def handle_auth_change_and_ui_setup(profile: gr.OAuthProfile | None, current_user_pref_show_all: bool):
|
| 284 |
+
is_authorized = check_authorization(profile)
|
| 285 |
+
|
| 286 |
+
if is_authorized:
|
| 287 |
+
# Authorized User
|
| 288 |
+
current_choices = list(all_models_list) if current_user_pref_show_all else list(preferred_models)
|
| 289 |
+
current_value = AUTHENTICATED_DEFAULT_MODEL_ID # Reset to default for authed user
|
| 290 |
+
# You could add logic here to try and preserve model_choice.value if it's valid in current_choices
|
| 291 |
+
|
| 292 |
+
model_dropdown_update = gr.Dropdown.update(choices=current_choices, value=current_value, label="Select Model", interactive=True)
|
| 293 |
+
accordion_update = gr.Accordion.update(visible=True)
|
| 294 |
+
model_info_text_update = get_model_info_markdown(current_value, True)
|
| 295 |
+
show_all_checkbox_update = gr.Checkbox.update(value=current_user_pref_show_all, interactive=True)
|
| 296 |
+
else:
|
| 297 |
+
# Unauthenticated User
|
| 298 |
+
model_dropdown_update = gr.Dropdown.update(choices=UNAUTHENTICATED_MODEL_CHOICES, value=UNAUTHENTICATED_DEFAULT_MODEL_ID, label="Select Model (Free Tier)", interactive=True)
|
| 299 |
+
accordion_update = gr.Accordion.update(visible=False)
|
| 300 |
+
model_info_text_update = get_model_info_markdown(UNAUTHENTICATED_DEFAULT_MODEL_ID, False)
|
| 301 |
+
show_all_checkbox_update = gr.Checkbox.update(value=False, interactive=False)
|
| 302 |
+
|
| 303 |
+
return is_authorized, model_dropdown_update, accordion_update, model_info_text_update, show_all_checkbox_update
|
| 304 |
|
| 305 |
+
demo.load(
|
| 306 |
+
fn=handle_auth_change_and_ui_setup,
|
| 307 |
+
inputs=[user_show_all_models_preference_state], # Pass the state that holds user's preference
|
| 308 |
+
outputs=[
|
| 309 |
+
auth_status_state,
|
| 310 |
+
model_choice,
|
| 311 |
+
advanced_settings_accordion,
|
| 312 |
+
model_info,
|
| 313 |
+
show_all_models_checkbox
|
| 314 |
+
]
|
| 315 |
)
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
# Navigate previous
|
| 318 |
def go_to_prev(current_idx, images, results):
|
| 319 |
if not images or not results or len(images) == 0:
|
|
|
|
| 373 |
# Launch the app
|
| 374 |
if __name__ == "__main__":
|
| 375 |
app = create_demo()
|
| 376 |
+
auth_list_for_gradio = [(user, "password") for user in AUTHORIZED_USER_IDS]
|
| 377 |
+
app.launch(auth=auth_list_for_gradio)
|