FLUX server (imagewright.flux_server)¶
imagewright.flux_server
¶
FLUX.2 Klein image server: the /v1/images/generations contract over an in-process model.
A FastAPI app around one long-lived :class:~imagewright.client.ImageSynthesizer
in mflux-local mode, so the model stays loaded between requests. It
answers the same routes, request fields and response bodies as gutenberg_kg's
serve/image_server.py, so a client of that server can point here unchanged.
Run it with imagewright serve. It needs the serve and flux extras;
nothing at module scope imports either, so this module imports on a core install.
Environment variables¶
FluxServer.from_env reads these.
IMAGE_MODEL HF repo for Flux2Klein. Fallback: GUTENKG_IMAGE_MODEL. Default: mlx-community/flux2-klein-4b-4bit IMAGE_STEPS Default inference steps (default: 4) IMAGE_OUTPUT_DIR Where response_format=filepath images go (default: imagewright_images in the system temp directory) IMAGE_PRELOAD 1/true/yes loads the model before the server accepts requests IMAGE_IDLE_UNLOAD_S Unload FLUX after this many idle seconds, so a small Mac gets its 12-15 GB back; the next render reloads it. 0 (default) keeps it loaded.
FluxServer(*, model_name=_DEFAULT_MODEL, steps=4, output_dir=None, preload=False, idle_unload_s=0)
¶
The model, the render lock and the idle clock behind the FLUX app.
One render runs at a time, and the model is never unloaded in the middle of
one: :meth:render and :meth:maybe_unload share one lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
HF repo for Flux2Klein. |
_DEFAULT_MODEL
|
steps
|
int
|
Inference steps when a request does not set them. |
4
|
output_dir
|
Path | None
|
Where |
None
|
preload
|
bool
|
Load the model when the app starts, before it accepts requests. |
False
|
idle_unload_s
|
int
|
Unload the model after this many idle seconds; 0 never does. |
0
|
Source code in src/imagewright/flux_server.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
from_env()
classmethod
¶
Build a server from the environment variables in the module docstring.
Returns:
| Type | Description |
|---|---|
FluxServer
|
The configured server. |
Source code in src/imagewright/flux_server.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
load()
¶
Load the model now, under the render lock.
Source code in src/imagewright/flux_server.py
109 110 111 112 | |
maybe_unload(now=None)
¶
Unload the model if it has been idle for idle_unload_s seconds.
Skipped while a render holds the lock; the watcher tries again later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
now
|
float | None
|
The current |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/imagewright/flux_server.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
render(prompt, size, seed, steps)
¶
Render one image under the lock and note when it finished.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Text description of the image. |
required |
size
|
str
|
|
required |
seed
|
int | None
|
Random seed, or None for a random one. |
required |
steps
|
int | None
|
Inference steps; None or 0 uses the server default. |
required |
Returns:
| Type | Description |
|---|---|
Image
|
The rendered image. |
Source code in src/imagewright/flux_server.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
watch_idle(stop, interval=None)
¶
Check for an idle model every few seconds until stop is set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop
|
Event
|
Set it to end the loop. |
required |
interval
|
float | None
|
Seconds between checks. Default: a quarter of
|
None
|
Source code in src/imagewright/flux_server.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
create_app(server=None, *, watch_interval=None)
¶
Build the FastAPI app.
On startup the app loads the model when server.preload is set, and starts
the idle watcher when server.idle_unload_s is above 0; shutdown stops it.
app.state.server is server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FluxServer | None
|
The model and its settings. Default: :meth: |
None
|
watch_interval
|
float | None
|
Seconds between idle checks (for tests); see
:meth: |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A |
Source code in src/imagewright/flux_server.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 | |
require_extras()
¶
Check that the FLUX server can run here.
Raises:
| Type | Description |
|---|---|
MissingExtraError
|
Naming |
Source code in src/imagewright/flux_server.py
52 53 54 55 56 57 | |