multimodalart HF Staff commited on
Commit
a118bb2
·
verified ·
1 Parent(s): 77b1187

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from google import genai
 
4
  import os
5
  from typing import Optional, List, Tuple, Union
6
  from huggingface_hub import whoami
@@ -126,7 +127,7 @@ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
126
  if not GOOGLE_API_KEY:
127
  raise ValueError("GOOGLE_API_KEY environment variable not set.")
128
  client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
129
- GEMINI_MODEL_NAME = 'gemini-2.5-flash-image-preview'
130
 
131
  def verify_pro_status(token: Optional[Union[gr.OAuthToken, str]]) -> bool:
132
  """Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
@@ -250,7 +251,7 @@ def _generate_video_segment(input_image_path: str, output_image_path: str, promp
250
  )
251
  return result[0]["video"]
252
 
253
- def unified_image_generator(prompt: str, images: Optional[List[str]], previous_video_path: Optional[str], last_frame_path: Optional[str], manual_token: str, oauth_token: Optional[gr.OAuthToken]) -> tuple:
254
  if not (verify_pro_status(oauth_token) or verify_pro_status(manual_token)):
255
  raise gr.Error("Access Denied.")
256
 
@@ -265,7 +266,25 @@ def unified_image_generator(prompt: str, images: Optional[List[str]], previous_v
265
  try:
266
  contents = [Image.open(image_path[0]) for image_path in images] if images else []
267
  contents.append(prompt)
268
- response = client.models.generate_content(model=GEMINI_MODEL_NAME, contents=contents)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
  image_data = _extract_image_data_from_response(response)
270
  if not image_data: raise gr.Error("No image data in response")
271
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
@@ -339,6 +358,12 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
339
  with gr.Column(scale=1):
340
  image_input_gallery = gr.Gallery(label="Upload one or more images here. Leave empty for text-to-image", file_types=["image"], height="auto")
341
  prompt_input = gr.Textbox(label="Prompt", placeholder="Turns this photo into a masterpiece")
 
 
 
 
 
 
342
  generate_button = gr.Button("Generate", variant="primary")
343
  with gr.Column(scale=1):
344
  output_image = gr.Image(label="Output", interactive=False, elem_id="output", type="filepath")
@@ -357,7 +382,7 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
357
  gr.on(
358
  triggers=[generate_button.click, prompt_input.submit],
359
  fn=unified_image_generator,
360
- inputs=[prompt_input, image_input_gallery, previous_video_state, last_frame_of_video_state, manual_token],
361
  outputs=[output_image, create_video_button, extend_video_button, video_group],
362
  api_name=False
363
  )
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from google import genai
4
+ from google.genai import types
5
  import os
6
  from typing import Optional, List, Tuple, Union
7
  from huggingface_hub import whoami
 
127
  if not GOOGLE_API_KEY:
128
  raise ValueError("GOOGLE_API_KEY environment variable not set.")
129
  client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
130
+ GEMINI_MODEL_NAME = 'gemini-2.5-flash-image'
131
 
132
  def verify_pro_status(token: Optional[Union[gr.OAuthToken, str]]) -> bool:
133
  """Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
 
251
  )
252
  return result[0]["video"]
253
 
254
+ def unified_image_generator(prompt: str, images: Optional[List[str]], previous_video_path: Optional[str], last_frame_path: Optional[str], aspect_ratio: str, manual_token: str, oauth_token: Optional[gr.OAuthToken]) -> tuple:
255
  if not (verify_pro_status(oauth_token) or verify_pro_status(manual_token)):
256
  raise gr.Error("Access Denied.")
257
 
 
266
  try:
267
  contents = [Image.open(image_path[0]) for image_path in images] if images else []
268
  contents.append(prompt)
269
+
270
+ # Create config with aspect ratio (omit image_config if Auto is selected)
271
+ if aspect_ratio == "Auto":
272
+ generate_content_config = types.GenerateContentConfig(
273
+ response_modalities=["IMAGE", "TEXT"],
274
+ )
275
+ else:
276
+ generate_content_config = types.GenerateContentConfig(
277
+ response_modalities=["IMAGE", "TEXT"],
278
+ image_config=types.ImageConfig(
279
+ aspect_ratio=aspect_ratio,
280
+ ),
281
+ )
282
+
283
+ response = client.models.generate_content(
284
+ model=GEMINI_MODEL_NAME,
285
+ contents=contents,
286
+ config=generate_content_config
287
+ )
288
  image_data = _extract_image_data_from_response(response)
289
  if not image_data: raise gr.Error("No image data in response")
290
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
 
358
  with gr.Column(scale=1):
359
  image_input_gallery = gr.Gallery(label="Upload one or more images here. Leave empty for text-to-image", file_types=["image"], height="auto")
360
  prompt_input = gr.Textbox(label="Prompt", placeholder="Turns this photo into a masterpiece")
361
+ aspect_ratio_dropdown = gr.Dropdown(
362
+ label="Aspect Ratio",
363
+ choices=["Auto", "1:1", "9:16", "16:9", "3:4", "4:3", "3:2", "2:3", "5:4", "4:5", "21:9"],
364
+ value="Auto",
365
+ interactive=True
366
+ )
367
  generate_button = gr.Button("Generate", variant="primary")
368
  with gr.Column(scale=1):
369
  output_image = gr.Image(label="Output", interactive=False, elem_id="output", type="filepath")
 
382
  gr.on(
383
  triggers=[generate_button.click, prompt_input.submit],
384
  fn=unified_image_generator,
385
+ inputs=[prompt_input, image_input_gallery, previous_video_state, last_frame_of_video_state, aspect_ratio_dropdown, manual_token],
386
  outputs=[output_image, create_video_button, extend_video_button, video_group],
387
  api_name=False
388
  )