Skip to content

Commit 7194141

Browse files
committed
feat(google_gemini.py): add aspect ratio and resolution enum options for image generation
1 parent 1e1767d commit 7194141

1 file changed

Lines changed: 39 additions & 25 deletions

File tree

pipelines/google/google_gemini.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@
6262
from open_webui.models.users import UserModel, Users
6363
from starlette.datastructures import Headers
6464

65+
ASPECT_RATIO_OPTIONS: List[str] = [
66+
"default",
67+
"1:1",
68+
"2:3",
69+
"3:2",
70+
"3:4",
71+
"4:3",
72+
"4:5",
73+
"5:4",
74+
"9:16",
75+
"16:9",
76+
"21:9",
77+
]
78+
79+
RESOLUTION_OPTIONS: List[str] = [
80+
"default",
81+
"1K",
82+
"2K",
83+
"4K",
84+
]
85+
6586

6687
# Simplified encryption implementation with automatic handling
6788
class EncryptedStr(str):
@@ -150,11 +171,13 @@ class Pipe:
150171
class UserValves(BaseModel):
151172
IMAGE_GENERATION_ASPECT_RATIO: str = Field(
152173
default=os.getenv("GOOGLE_IMAGE_GENERATION_ASPECT_RATIO", "default"),
153-
description="Default aspect ratio for image generation. Valid values: '1:1', '2:3', '3:2', '3:4', '4:3', '4:5', '5:4', '9:16', '16:9', '21:9'",
174+
description="Default aspect ratio for image generation.",
175+
json_schema_extra={"enum": ASPECT_RATIO_OPTIONS},
154176
)
155177
IMAGE_GENERATION_RESOLUTION: str = Field(
156178
default=os.getenv("GOOGLE_IMAGE_GENERATION_RESOLUTION", "default"),
157-
description="Default resolution for image generation. Valid values: '1K', '2K', '4K'",
179+
description="Default resolution for image generation.",
180+
json_schema_extra={"enum": RESOLUTION_OPTIONS},
158181
)
159182

160183
# Configuration valves for the pipeline
@@ -236,11 +259,13 @@ class Valves(BaseModel):
236259
# Image Processing Configuration
237260
IMAGE_GENERATION_ASPECT_RATIO: str = Field(
238261
default=os.getenv("GOOGLE_IMAGE_GENERATION_ASPECT_RATIO", "1:1"),
239-
description="Default aspect ratio for image generation. Valid values: '1:1', '2:3', '3:2', '3:4', '4:3', '4:5', '5:4', '9:16', '16:9', '21:9'",
262+
description="Default aspect ratio for image generation.",
263+
json_schema_extra={"enum": ASPECT_RATIO_OPTIONS},
240264
)
241265
IMAGE_GENERATION_RESOLUTION: str = Field(
242266
default=os.getenv("GOOGLE_IMAGE_GENERATION_RESOLUTION", "2K"),
243-
description="Default resolution for image generation. Valid values: '1K', '2K', '4K'",
267+
description="Default resolution for image generation.",
268+
json_schema_extra={"enum": RESOLUTION_OPTIONS},
244269
)
245270
IMAGE_MAX_SIZE_MB: float = Field(
246271
default=float(os.getenv("GOOGLE_IMAGE_MAX_SIZE_MB", "15.0")),
@@ -920,24 +945,13 @@ def _validate_aspect_ratio(self, aspect_ratio: str) -> Optional[str]:
920945
Returns:
921946
Validated aspect ratio string or None if invalid
922947
"""
923-
if not aspect_ratio:
948+
if not aspect_ratio or aspect_ratio == "default":
949+
self.log.debug("Using default aspect ratio (None)")
924950
return None
925951

926-
# Valid aspect ratios according to Google's API
927-
valid_ratios = [
928-
"1:1",
929-
"2:3",
930-
"3:2",
931-
"3:4",
932-
"4:3",
933-
"4:5",
934-
"5:4",
935-
"9:16",
936-
"16:9",
937-
"21:9",
938-
]
939-
940952
normalized = aspect_ratio.strip()
953+
valid_ratios = [r for r in ASPECT_RATIO_OPTIONS if r != "default"]
954+
941955
if normalized in valid_ratios:
942956
return normalized
943957

@@ -957,13 +971,13 @@ def _validate_resolution(self, resolution: str) -> Optional[str]:
957971
Returns:
958972
Validated resolution string or None if invalid
959973
"""
960-
if not resolution:
974+
if not resolution or resolution.lower() == "default":
975+
self.log.debug("Using default resolution (None)")
961976
return None
962977

963-
# Valid resolutions according to Google's API
964-
valid_resolutions = ["1K", "2K", "4K"]
965-
966978
normalized = resolution.strip().upper()
979+
valid_resolutions = [r for r in RESOLUTION_OPTIONS if r.lower() != "default"]
980+
967981
if normalized in valid_resolutions:
968982
return normalized
969983

@@ -1696,7 +1710,7 @@ def _configure_generation(
16961710
(
16971711
user_aspect_ratio
16981712
if user_aspect_ratio
1699-
else self.user_valves.IMAGE_GENERATION_ASPECT_RATIO
1713+
else self.valves.IMAGE_GENERATION_ASPECT_RATIO
17001714
),
17011715
)
17021716

@@ -1712,7 +1726,7 @@ def _configure_generation(
17121726
(
17131727
user_resolution
17141728
if user_resolution
1715-
else self.user_valves.IMAGE_GENERATION_RESOLUTION
1729+
else self.valves.IMAGE_GENERATION_RESOLUTION
17161730
),
17171731
)
17181732

0 commit comments

Comments
 (0)