Skip to content

Commit

Permalink
fix(diffusers): correctly parse height and width request without para…
Browse files Browse the repository at this point in the history
…metrization (#4082)

* fix(diffusers): allow to specify width and height without enable-parameters

Let's simplify usage by not gating width and height by parameters

Signed-off-by: Ettore Di Giacinto <[email protected]>

* chore: use sane defaults

Signed-off-by: Ettore Di Giacinto <[email protected]>

---------

Signed-off-by: Ettore Di Giacinto <[email protected]>
  • Loading branch information
mudler authored Nov 6, 2024
1 parent b59e167 commit b425a87
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions backend/python/diffusers/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,6 @@ def GenerateImage(self, request, context):
# create a dictionary of values for the parameters
options = {
"negative_prompt": request.negative_prompt,
"width": request.width,
"height": request.height,
"num_inference_steps": steps,
}

Expand All @@ -428,13 +426,13 @@ def GenerateImage(self, request, context):
keys = options.keys()

if request.EnableParameters != "":
keys = request.EnableParameters.split(",")
keys = [key.strip() for key in request.EnableParameters.split(",")]

if request.EnableParameters == "none":
keys = []

# create a dictionary of parameters by using the keys from EnableParameters and the values from defaults
kwargs = {key: options[key] for key in keys}
kwargs = {key: options.get(key) for key in keys if key in options}

# Set seed
if request.seed > 0:
Expand All @@ -445,6 +443,12 @@ def GenerateImage(self, request, context):
if self.PipelineType == "FluxPipeline":
kwargs["max_sequence_length"] = 256

if request.width:
kwargs["width"] = request.width

if request.height:
kwargs["height"] = request.height

if self.PipelineType == "FluxTransformer2DModel":
kwargs["output_type"] = "pil"
kwargs["generator"] = torch.Generator("cpu").manual_seed(0)
Expand All @@ -464,6 +468,7 @@ def GenerateImage(self, request, context):
export_to_video(video_frames, request.dst)
return backend_pb2.Result(message="Media generated successfully", success=True)

print(f"Generating image with {kwargs=}", file=sys.stderr)
image = {}
if COMPEL:
conditioning, pooled = self.compel.build_conditioning_tensor(prompt)
Expand Down
5 changes: 5 additions & 0 deletions core/http/endpoints/openai/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func ImageEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, appCon
config.Backend = model.StableDiffusionBackend
}

if !strings.Contains(input.Size, "x") {
input.Size = "512x512"
log.Warn().Msgf("Invalid size, using default 512x512")
}

sizeParts := strings.Split(input.Size, "x")
if len(sizeParts) != 2 {
return fmt.Errorf("invalid value for 'size'")
Expand Down

0 comments on commit b425a87

Please sign in to comment.