Deadmon commited on
Commit
400d329
·
verified ·
1 Parent(s): 11695a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -73,7 +73,8 @@ else:
73
  INITIALIZATION_SUCCESS = False
74
 
75
  # --- 2. The Core Image Generation Function ---
76
- def generate_image(prompt: str, negative_prompt: str, seed: int):
 
77
  """Generates an image using the Vertex AI Imagen model."""
78
  if not INITIALIZATION_SUCCESS:
79
  # Provide the clear, generic error message in the UI if initialization failed.
@@ -81,14 +82,19 @@ def generate_image(prompt: str, negative_prompt: str, seed: int):
81
 
82
  # <<< START: PRIVACY UPDATE >>>
83
  # Do not log the user's prompt content.
84
- print("Received new image generation request.")
85
  # <<< END: PRIVACY UPDATE >>>
86
 
87
  try:
88
  images = generation_model.generate_images(
89
- prompt=prompt, number_of_images=1, aspect_ratio="1:1",
90
- negative_prompt=negative_prompt, add_watermark=False,
91
- safety_filter_level="block_few", person_generation="allow_adult",
 
 
 
 
 
92
  seed=seed
93
  )
94
  print("Image generation successful for the request.")
@@ -102,6 +108,9 @@ def generate_image(prompt: str, negative_prompt: str, seed: int):
102
  # <<< END: SECURITY UPDATE >>>
103
 
104
  # --- 3. Gradio User Interface Definition ---
 
 
 
105
  with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 960px !important}") as demo:
106
  gr.Markdown("# 🎨 Vertex AI Imagen 4.0 Image Generator")
107
  gr.Markdown("Generate high-quality images with Google's latest Imagen 4.0 model.")
@@ -110,6 +119,15 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 960px
110
  with gr.Column(scale=2):
111
  prompt_input = gr.Textbox(label="Prompt", placeholder="A photorealistic image of a futuristic city...", lines=3)
112
  negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="text, watermark, blurry...", lines=2)
 
 
 
 
 
 
 
 
 
113
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=99999, step=1, randomize=True, info="Controls randomness.")
114
  submit_button = gr.Button("Generate Image", variant="primary")
115
  with gr.Column(scale=1):
@@ -125,7 +143,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 960px
125
 
126
  submit_button.click(
127
  fn=generate_image,
128
- inputs=[prompt_input, negative_prompt_input, seed_input],
 
129
  outputs=image_output
130
  )
131
 
 
73
  INITIALIZATION_SUCCESS = False
74
 
75
  # --- 2. The Core Image Generation Function ---
76
+ # UPDATE: Added 'aspect_ratio' parameter to the function signature
77
+ def generate_image(prompt: str, negative_prompt: str, seed: int, aspect_ratio: str):
78
  """Generates an image using the Vertex AI Imagen model."""
79
  if not INITIALIZATION_SUCCESS:
80
  # Provide the clear, generic error message in the UI if initialization failed.
 
82
 
83
  # <<< START: PRIVACY UPDATE >>>
84
  # Do not log the user's prompt content.
85
+ print(f"Received new image generation request with aspect ratio: {aspect_ratio}.")
86
  # <<< END: PRIVACY UPDATE >>>
87
 
88
  try:
89
  images = generation_model.generate_images(
90
+ prompt=prompt,
91
+ number_of_images=1,
92
+ # UPDATE: Use the aspect_ratio from the UI instead of a hardcoded value
93
+ aspect_ratio=aspect_ratio,
94
+ negative_prompt=negative_prompt,
95
+ add_watermark=False,
96
+ safety_filter_level="block_few",
97
+ person_generation="allow_adult",
98
  seed=seed
99
  )
100
  print("Image generation successful for the request.")
 
108
  # <<< END: SECURITY UPDATE >>>
109
 
110
  # --- 3. Gradio User Interface Definition ---
111
+ # UPDATE: Added a list of all supported aspect ratios for Imagen 4
112
+ IMAGEN4_ASPECT_RATIOS = ["1:1", "16:9", "9:16", "4:3", "3:4"]
113
+
114
  with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 960px !important}") as demo:
115
  gr.Markdown("# 🎨 Vertex AI Imagen 4.0 Image Generator")
116
  gr.Markdown("Generate high-quality images with Google's latest Imagen 4.0 model.")
 
119
  with gr.Column(scale=2):
120
  prompt_input = gr.Textbox(label="Prompt", placeholder="A photorealistic image of a futuristic city...", lines=3)
121
  negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="text, watermark, blurry...", lines=2)
122
+
123
+ # UPDATE: Added a radio button selector for the aspect ratio
124
+ aspect_ratio_input = gr.Radio(
125
+ choices=IMAGEN4_ASPECT_RATIOS,
126
+ label="Aspect Ratio",
127
+ value="1:1",
128
+ info="Select the output image dimensions (Square, Landscape, Portrait)."
129
+ )
130
+
131
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=99999, step=1, randomize=True, info="Controls randomness.")
132
  submit_button = gr.Button("Generate Image", variant="primary")
133
  with gr.Column(scale=1):
 
143
 
144
  submit_button.click(
145
  fn=generate_image,
146
+ # UPDATE: Added aspect_ratio_input to the list of inputs
147
+ inputs=[prompt_input, negative_prompt_input, seed_input, aspect_ratio_input],
148
  outputs=image_output
149
  )
150