@@ -91,10 +91,19 @@ def to_mapping_value(value) -> struct_pb2.Struct:
91
91
# We got a dict (or something dict-like); convert it.
92
92
return struct_pb2 .Struct (fields = {k : to_value (v ) for k , v in value .items ()})
93
93
94
- ASPECT_RATIOS = ["1:1" , "9:16" , "16:9" , "4:3" , "3:4" ]
95
- OUTPUT_MIME_TYPES = ["image/png" , "image/jpeg" ]
96
- SAFETY_FILTER_LEVELS = ["block_most" , "block_some" , "block_few" , "block_fewest" ]
97
- PERSON_GENERATIONS = ["dont_allow" , "allow_adult" , "allow_all" ]
94
+
95
+ AspectRatio = Literal ["1:1" , "9:16" , "16:9" , "4:3" , "3:4" ]
96
+ ASPECT_RATIOS = AspectRatio .__args__
97
+
98
+ OutputMimeType = Literal ["image/png" , "image/jpeg" ]
99
+ OUTPUT_MIME_TYPES = OutputMimeType .__args__
100
+
101
+ SafetyFilterLevel = Literal ["block_most" , "block_some" , "block_few" , "block_fewest" ]
102
+ SAFETY_FILTER_LEVELS = SafetyFilterLevel .__args__
103
+
104
+ PersonGeneration = Literal ["dont_allow" , "allow_adult" , "allow_all" ]
105
+ PERSON_GENERATIONS = PersonGeneration .__args__
106
+
98
107
99
108
class Image :
100
109
"""Image."""
@@ -226,15 +235,13 @@ def _generate_images(
226
235
number_of_images : int = 1 ,
227
236
width : Optional [int ] = None ,
228
237
height : Optional [int ] = None ,
229
- aspect_ratio : Optional [Literal [ * ASPECT_RATIOS ] ] = None ,
238
+ aspect_ratio : Optional [AspectRatio ] = None ,
230
239
guidance_scale : Optional [float ] = None ,
231
- output_mime_type : Optional [Literal [ * OUTPUT_MIME_TYPES ] ] = None ,
240
+ output_mime_type : Optional [OutputMimeType ] = None ,
232
241
compression_quality : Optional [float ] = None ,
233
242
language : Optional [str ] = None ,
234
- safety_filter_level : Optional [
235
- Literal [* SAFETY_FILTER_LEVELS ]
236
- ] = None ,
237
- person_generation : Optional [Literal [* PERSON_GENERATIONS ]] = None ,
243
+ safety_filter_level : Optional [SafetyFilterLevel ] = None ,
244
+ person_generation : Optional [PersonGeneration ] = None ,
238
245
) -> "ImageGenerationResponse" :
239
246
"""Generates images from text prompt.
240
247
@@ -292,7 +299,7 @@ def _generate_images(
292
299
max_size = max (width or 0 , height or 0 ) or None
293
300
if aspect_ratio is not None :
294
301
if aspect_ratio not in ASPECT_RATIOS :
295
- raise ValueError (f' aspect_ratio not in { ASPECT_RATIOS } ' )
302
+ raise ValueError (f" aspect_ratio not in { ASPECT_RATIOS } " )
296
303
parameters ["aspectRatio" ] = aspect_ratio
297
304
elif max_size :
298
305
# Note: The size needs to be a string
@@ -316,7 +323,7 @@ def _generate_images(
316
323
parameters ["outputOptions" ] = {}
317
324
if output_mime_type is not None :
318
325
if output_mime_type not in OUTPUT_MIME_TYPES :
319
- raise ValueError (f' output_mime_type not in { OUTPUT_MIME_TYPES } ' )
326
+ raise ValueError (f" output_mime_type not in { OUTPUT_MIME_TYPES } " )
320
327
parameters ["outputOptions" ]["mimeType" ] = output_mime_type
321
328
shared_generation_parameters ["mime_type" ] = output_mime_type
322
329
@@ -326,7 +333,7 @@ def _generate_images(
326
333
327
334
if safety_filter_level is not None :
328
335
if safety_filter_level not in SAFETY_FILTER_LEVELS :
329
- raise ValueError (f' safety_filter_level not in { SAFETY_FILTER_LEVELS } ' )
336
+ raise ValueError (f" safety_filter_level not in { SAFETY_FILTER_LEVELS } " )
330
337
parameters ["safetySetting" ] = safety_filter_level
331
338
shared_generation_parameters ["safety_filter_level" ] = safety_filter_level
332
339
@@ -361,13 +368,13 @@ def generate_images(
361
368
* ,
362
369
negative_prompt : Optional [str ] = None ,
363
370
number_of_images : int = 1 ,
364
- aspect_ratio : Optional [Literal [ "1:1" , "9:16" , "16:9" , "4:3" , "3:4" ] ] = None ,
371
+ aspect_ratio : Optional [AspectRatio ] = None ,
365
372
guidance_scale : Optional [float ] = None ,
366
373
language : Optional [str ] = None ,
367
374
safety_filter_level : Optional [
368
- Literal [ "block_most" , "block_some" , "block_few" , "block_fewest" ]
375
+ SafetyFilterLevel
369
376
] = None ,
370
- person_generation : Optional [Literal [ "dont_allow" , "allow_adult" , "allow_all" ] ] = None ,
377
+ person_generation : Optional [PersonGeneration ] = None ,
371
378
) -> "ImageGenerationResponse" :
372
379
"""Generates images from text prompt.
373
380
0 commit comments