Skip to content

Commit ecf3fa2

Browse files
committed
refac
1 parent 4aacaeb commit ecf3fa2

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

backend/open_webui/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,6 +3450,12 @@ class BannerModel(BaseModel):
34503450
os.getenv("IMAGE_GENERATION_MODEL", ""),
34513451
)
34523452

3453+
# Regex pattern for models that support IMAGE_SIZE = "auto".
3454+
IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN = os.getenv("IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN", "^gpt-image")
3455+
3456+
# Regex pattern for models that return URLs instead of base64 data.
3457+
IMAGE_URL_RESPONSE_MODELS_REGEX_PATTERN = os.getenv("IMAGE_URL_RESPONSE_MODELS_REGEX_PATTERN", "^gpt-image")
3458+
34533459
IMAGE_SIZE = PersistentConfig(
34543460
"IMAGE_SIZE", "image_generation.size", os.getenv("IMAGE_SIZE", "512x512")
34553461
)

backend/open_webui/routers/images.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from fastapi import APIRouter, Depends, HTTPException, Request, UploadFile
1515
from fastapi.responses import FileResponse
1616

17-
from open_webui.config import CACHE_DIR
17+
from open_webui.config import CACHE_DIR, IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN, IMAGE_URL_RESPONSE_MODELS_REGEX_PATTERN
1818
from open_webui.constants import ERROR_MESSAGES
1919
from open_webui.retrieval.web.utils import validate_url
2020
from open_webui.env import ENABLE_FORWARD_USER_INFO_HEADERS
@@ -201,12 +201,12 @@ async def update_config(
201201
set_image_model(request, form_data.IMAGE_GENERATION_MODEL)
202202
if (
203203
form_data.IMAGE_SIZE == "auto"
204-
and not form_data.IMAGE_GENERATION_MODEL.startswith("gpt-image")
204+
and not re.match(IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN, form_data.IMAGE_GENERATION_MODEL)
205205
):
206206
raise HTTPException(
207207
status_code=400,
208208
detail=ERROR_MESSAGES.INCORRECT_FORMAT(
209-
" (auto is only allowed with gpt-image models)."
209+
f" (auto is only allowed with models matching {IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN})."
210210
),
211211
)
212212

@@ -610,9 +610,7 @@ async def image_generations(
610610
),
611611
**(
612612
{}
613-
if request.app.state.config.IMAGE_GENERATION_MODEL.startswith(
614-
"gpt-image"
615-
)
613+
if re.match(IMAGE_URL_RESPONSE_MODELS_REGEX_PATTERN, request.app.state.config.IMAGE_GENERATION_MODEL)
616614
else {"response_format": "b64_json"}
617615
),
618616
**(
@@ -949,7 +947,7 @@ def get_image_file_item(base64_string, param_name="image"):
949947
**({"size": size} if size else {}),
950948
**(
951949
{}
952-
if request.app.state.config.IMAGE_EDIT_MODEL.startswith("gpt-image")
950+
if re.match(IMAGE_URL_RESPONSE_MODELS_REGEX_PATTERN, request.app.state.config.IMAGE_EDIT_MODEL)
953951
else {"response_format": "b64_json"}
954952
),
955953
}

0 commit comments

Comments
 (0)