Chris Addis commited on
Commit
5a20aa7
·
1 Parent(s): 86cec04

add authentication

Browse files
Files changed (1) hide show
  1. app.py +91 -30
app.py CHANGED
@@ -279,40 +279,101 @@ def create_demo():
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):
 
279
  ]
280
  )
281
 
282
+ # Handle "Show Additional Models" checkbox change
283
+ def persist_preference_and_toggle_models_visibility(show_all_checkbox_val: bool, current_model_id: str, is_authorized: bool):
284
+ if not is_authorized: # Should not be triggered if checkbox is not interactive
285
+ mdl_choice_update = gr.Dropdown.update(choices=UNAUTHENTICATED_MODEL_CHOICES, value=UNAUTHENTICATED_DEFAULT_MODEL_ID)
286
+ return show_all_checkbox_val, mdl_choice_update # Persist the (likely False) checkbox value
 
 
 
 
287
 
288
+ # Authorized user
289
+ new_choices = list(all_models_list) if show_all_checkbox_val else list(preferred_models)
 
 
 
 
 
 
 
 
290
 
291
+ new_model_value = current_model_id
292
+ if not any(val == current_model_id for _, val in new_choices): # If current model not in new list
293
+ new_model_value = AUTHENTICATED_DEFAULT_MODEL_ID # Revert to default
294
+ if not any(val == new_model_value for _, val in new_choices): # If default not in new list (edge case)
295
+ new_model_value = new_choices[0][1] if new_choices else None
296
 
297
+
298
+ mdl_choice_update = gr.Dropdown.update(choices=new_choices, value=new_model_value)
299
+ return show_all_checkbox_val, mdl_choice_update # Return new preference and dropdown update
300
+
301
+ show_all_models_checkbox.change(
302
+ fn=persist_preference_and_toggle_models_visibility,
303
+ inputs=[show_all_models_checkbox, model_choice, auth_status_state],
304
+ outputs=[user_show_all_models_preference_state, model_choice]
305
+ )
306
+
307
+ # Update model info when model selection changes
308
+ def update_model_info_display(selected_model_id: str, is_authorized: bool):
309
+ return get_model_info_markdown(selected_model_id, is_authorized)
310
+
311
+ model_choice.change(
312
+ fn=update_model_info_display,
313
+ inputs=[model_choice, auth_status_state],
314
+ outputs=[model_info]
315
  )
316
+
317
+ # Handle file uploads
318
+ def handle_upload(files, current_paths, current_filenames):
319
+ # ... (your existing function, no changes needed here)
320
+ file_paths = []
321
+ file_names = []
322
+ if files:
323
+ for file in files:
324
+ file_paths.append(file.name)
325
+ file_names.append(get_base_filename(file.name))
326
+ return file_paths, file_paths, file_names, 0, None, "0 of 0", "Upload images and click 'Generate Alt-Text'."
327
+
328
+
329
+ upload_button.upload(
330
+ fn=handle_upload,
331
+ inputs=[upload_button, image_state, filename_state],
332
+ outputs=[input_gallery, image_state, filename_state,
333
+ current_index, current_image, image_counter, analysis_text]
334
+ )
335
+
336
+ # Analyze images
337
+ def analyze_images(image_paths, current_model_choice_id, length_choice, filenames, content_type_choice):
338
+ # Note: current_model_choice_id will be correctly set based on auth status by the UI
339
+ if not image_paths:
340
+ return [], [], 0, None, "0 of 0", "No images uploaded to analyze.", None
341
+
342
+ is_photography = content_type_choice == "Photography"
343
+ sys_prompt = get_sys_prompt(length_choice, photograph=is_photography)
344
+ image_results = []
345
+ analysis_progress = gr.Progress(track_tqdm=True)
346
+
347
+ for i, image_path in enumerate(analysis_progress.tqdm(image_paths, desc="Analyzing Images")):
348
+ image_id = filenames[i] if i < len(filenames) and filenames[i] else f"Image_{i+1}_{os.path.basename(image_path)}"
349
+ try:
350
+ img = Image.open(image_path)
351
+ prompt0 = prompt_new() # Assuming prompt_new() is defined in your utils
352
+ model_name = current_model_choice_id
353
+ client_to_use = OR # Ensure OR can handle "meta-llama/llama-4-maverick:free"
354
+ # Or add logic here: if model_name == UNAUTHENTICATED_DEFAULT_MODEL_ID: client_to_use = DifferentClient()
355
+
356
+ result = client_to_use.generate_caption(
357
+ img, model=model_name, max_image_size=512,
358
+ prompt=prompt0, prompt_dev=sys_prompt, temperature=1
359
+ )
360
+ image_results.append({"image_id": image_id, "content": result.strip()})
361
+ except FileNotFoundError:
362
+ error_message = f"Error: File not found at path '{image_path}'"
363
+ print(error_message)
364
+ image_results.append({"image_id": image_id, "content": error_message})
365
+ except Exception as e:
366
+ error_message = f"Error processing {image_id}: {str(e)}"
367
+ print(error_message)
368
+ image_results.append({"image_id": image_id, "content": error_message})
369
+
370
+ csv_path = create_csv_file_simple(image_results)
371
+ initial_image = image_paths[0] if image_paths else None
372
+ initial_counter = f"1 of {len(image_paths)}" if image_paths else "0 of 0"
373
+ initial_text = image_results[0]["content"] if image_results else "Analysis complete, but no results generated."
374
+
375
+ return (image_paths, image_results, 0, initial_image, initial_counter,
376
+ initial_text, csv_path)
377
 
378
  # Navigate previous
379
  def go_to_prev(current_idx, images, results):