multimodalart HF Staff commited on
Commit
7f686f5
·
verified ·
1 Parent(s): ebbfe5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -34
app.py CHANGED
@@ -299,19 +299,22 @@ def _generate_video_segment(input_image_path: str, output_image_path: str, promp
299
  )
300
  return result[0]["video"]
301
 
302
- def unified_image_generator(prompt: str, images: Optional[List[str]], previous_video_path: Optional[str], last_frame_path: Optional[str], aspect_ratio: str, model_selection: str, manual_token: str, oauth_token: Optional[gr.OAuthToken]) -> tuple:
303
  if not (verify_pro_status(oauth_token) or verify_pro_status(manual_token)):
304
  raise gr.Error("Access Denied.")
305
 
306
  # Determine if using PRO model based on radio selection
307
  use_pro_model = (model_selection == "Nano Banana PRO")
308
 
 
 
 
309
  # Check rate limit
310
  username = get_username(oauth_token) or get_username(manual_token)
311
  if not username:
312
  raise gr.Error("Could not identify user.")
313
 
314
- can_generate = check_and_update_usage(username, use_pro_model)
315
 
316
  if not can_generate:
317
  # Check if user has quota on the other model
@@ -320,10 +323,16 @@ def unified_image_generator(prompt: str, images: Optional[List[str]], previous_v
320
  model_name = "Nano Banana PRO" if use_pro_model else "Nano Banana"
321
  other_model_name = "Nano Banana" if use_pro_model else "Nano Banana PRO"
322
 
 
 
 
 
 
 
323
  if remaining_other > 0:
324
- gr.Info(f"You've reached your daily limit for {model_name} ({limit_current} images). You still have {remaining_other} generations left with {other_model_name}!")
325
 
326
- raise gr.Error(f"This demo is made for interactive generations, not automated workflows. You have generated {limit_current} images with {model_name} today, come back tomorrow for more.")
327
 
328
  try:
329
  contents = [Image.open(image_path[0]) for image_path in images] if images else []
@@ -332,18 +341,38 @@ def unified_image_generator(prompt: str, images: Optional[List[str]], previous_v
332
  # Select model based on radio selection
333
  model_name = GEMINI_PRO_MODEL_NAME if use_pro_model else GEMINI_MODEL_NAME
334
 
335
- # Create config with aspect ratio
336
- if aspect_ratio == "Auto":
337
- generate_content_config = types.GenerateContentConfig(
338
- response_modalities=["IMAGE", "TEXT"],
339
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  else:
341
- generate_content_config = types.GenerateContentConfig(
342
- response_modalities=["IMAGE", "TEXT"],
343
- image_config=types.ImageConfig(
344
- aspect_ratio=aspect_ratio,
345
- ),
346
- )
 
 
 
 
 
 
347
 
348
  response = client.models.generate_content(
349
  model=model_name,
@@ -428,25 +457,25 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
428
  model_radio = gr.Radio(
429
  choices=["Nano Banana", "Nano Banana PRO"],
430
  value="Nano Banana",
431
- label="Model Selection",
432
- info="PRO: Higher quality, slower generation (50/day) | Standard: Faster generation (75/day)"
433
- )
434
-
435
- # Resolution selection for PRO model
436
- resolution_dropdown = gr.Dropdown(
437
- label="Resolution (Nano Banana PRO only)",
438
- choices=["1K (1 credit)", "2K (2 credits)", "4K (4 credits)"],
439
- value="1K (1 credit)",
440
- interactive=True,
441
- visible=False
442
  )
443
 
444
- aspect_ratio_dropdown = gr.Dropdown(
445
- label="Aspect Ratio",
446
- choices=["Auto", "1:1", "9:16", "16:9", "3:4", "4:3", "3:2", "2:3", "5:4", "4:5", "21:9"],
447
- value="Auto",
448
- interactive=True
449
- )
 
 
 
 
 
 
 
 
 
 
 
450
 
451
  generate_button = gr.Button("Generate", variant="primary")
452
  with gr.Column(scale=1):
@@ -463,10 +492,20 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
463
 
464
  login_button = gr.LoginButton()
465
 
 
 
 
 
 
 
 
 
 
 
466
  gr.on(
467
  triggers=[generate_button.click, prompt_input.submit],
468
  fn=unified_image_generator,
469
- inputs=[prompt_input, image_input_gallery, previous_video_state, last_frame_of_video_state, aspect_ratio_dropdown, model_radio, manual_token],
470
  outputs=[output_image, create_video_button, extend_video_button, video_group],
471
  api_name=False
472
  )
@@ -513,4 +552,4 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
513
  demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
514
 
515
  if __name__ == "__main__":
516
- demo.queue(max_size=None, default_concurrency_limit=None).launch(show_error=True)
 
299
  )
300
  return result[0]["video"]
301
 
302
+ def unified_image_generator(prompt: str, images: Optional[List[str]], previous_video_path: Optional[str], last_frame_path: Optional[str], aspect_ratio: str, model_selection: str, resolution: str, manual_token: str, oauth_token: Optional[gr.OAuthToken]) -> tuple:
303
  if not (verify_pro_status(oauth_token) or verify_pro_status(manual_token)):
304
  raise gr.Error("Access Denied.")
305
 
306
  # Determine if using PRO model based on radio selection
307
  use_pro_model = (model_selection == "Nano Banana PRO")
308
 
309
+ # Calculate credit cost based on resolution (only for PRO model)
310
+ credits_to_use = get_credit_cost(resolution) if use_pro_model else 1
311
+
312
  # Check rate limit
313
  username = get_username(oauth_token) or get_username(manual_token)
314
  if not username:
315
  raise gr.Error("Could not identify user.")
316
 
317
+ can_generate = check_and_update_usage(username, use_pro_model, credits_to_use)
318
 
319
  if not can_generate:
320
  # Check if user has quota on the other model
 
323
  model_name = "Nano Banana PRO" if use_pro_model else "Nano Banana"
324
  other_model_name = "Nano Banana" if use_pro_model else "Nano Banana PRO"
325
 
326
+ # Get remaining credits for current model
327
+ remaining_current = get_remaining_generations(username, use_pro_model)
328
+
329
+ if use_pro_model and remaining_current > 0 and remaining_current < credits_to_use:
330
+ gr.Info(f"You need {credits_to_use} credits for {get_resolution_value(resolution)} but only have {remaining_current} credits remaining. Try a lower resolution or use Nano Banana.")
331
+
332
  if remaining_other > 0:
333
+ gr.Info(f"You've reached your daily limit for {model_name}. You still have {remaining_other} generations left with {other_model_name}!")
334
 
335
+ raise gr.Error(f"Insufficient credits. You need {credits_to_use} credits for this generation.")
336
 
337
  try:
338
  contents = [Image.open(image_path[0]) for image_path in images] if images else []
 
341
  # Select model based on radio selection
342
  model_name = GEMINI_PRO_MODEL_NAME if use_pro_model else GEMINI_MODEL_NAME
343
 
344
+ # Create config with aspect ratio and resolution (for PRO model)
345
+ if use_pro_model:
346
+ # PRO model: use both aspect_ratio and image_size
347
+ resolution_value = get_resolution_value(resolution)
348
+ if aspect_ratio == "Auto":
349
+ generate_content_config = types.GenerateContentConfig(
350
+ response_modalities=["IMAGE", "TEXT"],
351
+ image_config=types.ImageConfig(
352
+ image_size=resolution_value,
353
+ ),
354
+ )
355
+ else:
356
+ generate_content_config = types.GenerateContentConfig(
357
+ response_modalities=["IMAGE", "TEXT"],
358
+ image_config=types.ImageConfig(
359
+ aspect_ratio=aspect_ratio,
360
+ image_size=resolution_value,
361
+ ),
362
+ )
363
  else:
364
+ # Standard model: only aspect_ratio
365
+ if aspect_ratio == "Auto":
366
+ generate_content_config = types.GenerateContentConfig(
367
+ response_modalities=["IMAGE", "TEXT"],
368
+ )
369
+ else:
370
+ generate_content_config = types.GenerateContentConfig(
371
+ response_modalities=["IMAGE", "TEXT"],
372
+ image_config=types.ImageConfig(
373
+ aspect_ratio=aspect_ratio,
374
+ ),
375
+ )
376
 
377
  response = client.models.generate_content(
378
  model=model_name,
 
457
  model_radio = gr.Radio(
458
  choices=["Nano Banana", "Nano Banana PRO"],
459
  value="Nano Banana",
 
 
 
 
 
 
 
 
 
 
 
460
  )
461
 
462
+ with gr.Row():
463
+ aspect_ratio_dropdown = gr.Dropdown(
464
+ label="Aspect Ratio",
465
+ choices=["Auto", "1:1", "9:16", "16:9", "3:4", "4:3", "3:2", "2:3", "5:4", "4:5", "21:9"],
466
+ value="Auto",
467
+ interactive=True
468
+ )
469
+
470
+ resolution_dropdown = gr.Dropdown(
471
+ label="Resolution (Nano Banana PRO only)",
472
+ choices=["1K", "2K", "4K"],
473
+ value="1K",
474
+ interactive=True,
475
+ visible=False
476
+ )
477
+
478
+
479
 
480
  generate_button = gr.Button("Generate", variant="primary")
481
  with gr.Column(scale=1):
 
492
 
493
  login_button = gr.LoginButton()
494
 
495
+ # Show/hide resolution dropdown based on model selection
496
+ def update_resolution_visibility(model_selection):
497
+ return gr.update(visible=(model_selection == "Nano Banana PRO"))
498
+
499
+ model_radio.change(
500
+ fn=update_resolution_visibility,
501
+ inputs=[model_radio],
502
+ outputs=[resolution_dropdown]
503
+ )
504
+
505
  gr.on(
506
  triggers=[generate_button.click, prompt_input.submit],
507
  fn=unified_image_generator,
508
+ inputs=[prompt_input, image_input_gallery, previous_video_state, last_frame_of_video_state, aspect_ratio_dropdown, model_radio, resolution_dropdown, manual_token],
509
  outputs=[output_image, create_video_button, extend_video_button, video_group],
510
  api_name=False
511
  )
 
552
  demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
553
 
554
  if __name__ == "__main__":
555
+ demo.queue(max_size=None, default_concurrency_limit=None).launch(show_error=True)