Client (imagewright.client)¶
imagewright.client
¶
ImageSynthesizer: image generation across mflux-local, mflux-serve and OpenAI.
Only httpx and pillow are imported here at module scope. The model stacks
(mflux, mlx, openai) are imported inside the method that needs them, so a
core install imports this module, and a backend whose extra is missing raises
:class:MissingExtraError naming the extra to install.
ImageSynthesizer(config)
¶
Unified image generation across mflux-local, mflux-serve and OpenAI.
All generate* methods return a PIL Image; callers that need a base64
string (e.g. HTTP wire responses) should use generate_b64(). A
per-instance mflux model cache avoids reloading across calls to the same
synthesizer; :meth:unload_model releases it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ImageConfig
|
Backend configuration produced by |
required |
Source code in src/imagewright/client.py
135 136 137 138 139 140 141 142 | |
model_loaded
property
¶
True while an in-process Flux2Klein model is held in memory.
generate(prompt, *, aspect_ratio='3:2', seed=None, steps=None, model=None, size=None)
¶
Generate an image and return a PIL Image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Text description of the scene to generate. |
required |
aspect_ratio
|
str
|
One of 1:1, 3:2, 2:3, 16:9, 9:16, 4:3, 3:4. |
'3:2'
|
seed
|
int | None
|
Random seed for reproducibility (random int if omitted). |
None
|
steps
|
int | None
|
Override inference steps (mflux backends only; ignored for OpenAI). |
None
|
model
|
str | None
|
Override the configured model for this single call. |
None
|
size
|
str | None
|
Explicit |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
PIL Image. |
Raises:
| Type | Description |
|---|---|
MissingExtraError
|
When the backend's extra is not installed. |
Source code in src/imagewright/client.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
generate_b64(prompt, *, aspect_ratio='3:2', seed=None, steps=None, model=None, size=None)
¶
Generate an image and return it as a base64-encoded PNG string.
Convenience wrapper around generate() for HTTP wire responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Text description of the scene to generate. |
required |
aspect_ratio
|
str
|
One of 1:1, 3:2, 2:3, 16:9, 9:16, 4:3, 3:4. |
'3:2'
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
steps
|
int | None
|
Override inference steps (mflux backends only). |
None
|
model
|
str | None
|
Override the configured model for this single call. |
None
|
size
|
str | None
|
Explicit |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Base64-encoded PNG string. |
Source code in src/imagewright/client.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
load_model()
¶
Load the in-process Flux2Klein model now instead of on the first render.
An image server calls this to preload. A no-op when the configured model is already loaded.
Raises:
| Type | Description |
|---|---|
MissingExtraError
|
When the |
Source code in src/imagewright/client.py
172 173 174 175 176 177 178 179 180 | |
unload_model()
¶
Drop the cached Flux2Klein model and return its memory to the system.
An image server calls this after its idle timeout, so a small Mac gets FLUX's 12-15 GB back. The next in-process generation loads the model again.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/imagewright/client.py
187 188 189 190 191 192 193 194 195 | |
MissingExtraError
¶
Bases: ImportError
A backend needs an optional extra that is not installed.
Subclasses ImportError so existing except ImportError handlers still
catch it; the message names the pip install command that fixes it.