Image generation¶
gutenberg_kg.image_gen
¶
Image generation — local MLX or remote mflux-serve.
Two code paths: generate() — local Flux2Klein via mflux (Apple Silicon only) generate_via_server() — HTTP call to a running mflux-serve instance generate_auto() — server if GUTENKG_IMAGE_ENDPOINT / server_url set, else local fallback
Environment variables¶
GUTENKG_IMAGE_MODEL HuggingFace repo or mflux model name (default: mlx-community/flux2-klein-4b-4bit) GUTENKG_IMAGE_STEPS Inference steps (default: 4) GUTENKG_IMAGE_SIZE Default output size WIDTHxHEIGHT (default: 1536x1024) GUTENKG_IMAGE_ENDPOINT Base URL of a running mflux-serve instance (default: empty — use local generation) Example: http://localhost:8090 (mflux-server default)
discover_image_endpoint(candidates=DEFAULT_IMAGE_ENDPOINTS, timeout=1.5)
¶
Find a local image server that is actually up.
Tries /health first and falls back to /v1/models. Both routes are
served by gutenkg-image-server and gutenkg-sdxl-server, but /health
is the newer one, and an already-running server — or a third-party
mflux-serve — will only answer the model listing. Falling back costs one
extra request against a port that is live either way.
A connection refused, a timeout, or any non-200 all mean "not this one".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
tuple[str, ...]
|
Base URLs to try, in order. |
DEFAULT_IMAGE_ENDPOINTS
|
timeout
|
float
|
Per-candidate timeout in seconds. |
1.5
|
Returns:
| Type | Description |
|---|---|
str | None
|
The first responding base URL, or None if none answered. |
Source code in src/gutenberg_kg/image_gen.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
generate(prompt, *, size=None, seed=None, output_path=None, model_name=None, steps=None)
¶
Generate an image locally via Flux2Klein (Apple Silicon / mflux).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Text description of the image to generate. |
required |
size
|
str | None
|
Output size |
None
|
seed
|
int | None
|
Random seed for reproducibility (random if omitted). |
None
|
output_path
|
str | Path | None
|
If given, save the PNG here in addition to returning it. |
None
|
model_name
|
str | None
|
Override the HF model repo (default: mlx-community/flux2-klein-4b-4bit). |
None
|
steps
|
int | None
|
Override inference steps (default: 4). |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
PIL Image. |
Source code in src/gutenberg_kg/image_gen.py
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 | |
generate_auto(prompt, *, server_url=None, size=None, seed=None, steps=None, model_name=None)
¶
Generate an image, preferring a remote server and falling back to local mflux.
Resolution order: 1. server_url argument (non-empty string) 2. GUTENKG_IMAGE_ENDPOINT environment variable 3. Local generate() — requires mflux on Apple Silicon
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Text description of the image to generate. |
required |
server_url
|
str | None
|
Override server URL; pass None to use env var or local. |
None
|
size
|
str | None
|
Output size |
None
|
seed
|
int | None
|
Optional integer seed for reproducibility. |
None
|
steps
|
int | None
|
Override inference steps. |
None
|
model_name
|
str | None
|
Local model override (ignored when using server). |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
PIL Image. |
Source code in src/gutenberg_kg/image_gen.py
267 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 | |
generate_via_server(prompt, *, server_url, size=None, seed=None, steps=None)
¶
Generate an image by calling a running mflux-serve HTTP server.
Requires only httpx + pillow — safe to call from Linux containers or any environment without mflux installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Text description of the image to generate. |
required |
server_url
|
str
|
Base URL of the mflux-serve instance, e.g. http://localhost:8088. |
required |
size
|
str | None
|
Output size |
None
|
seed
|
int | None
|
Optional integer seed for reproducibility. |
None
|
steps
|
int | None
|
Override inference steps (default: GUTENKG_IMAGE_STEPS or 4). |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
PIL Image decoded from the server response. |
Source code in src/gutenberg_kg/image_gen.py
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 | |
vlm_rewrite(corpus_text, *, base_url=None, model=None)
¶
Rewrite corpus prose into a visual image-generation prompt via a local VLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus_text
|
str
|
Historical text to rewrite as a visual scene description. |
required |
base_url
|
str | None
|
OpenAI-compatible endpoint URL. Falls back to GUTENKG_VLM_ENDPOINT or http://localhost:8080/v1. |
None
|
model
|
str | None
|
Model ID. Falls back to GUTENKG_VLM_MODEL or the Qwen3-30B default. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str, str | None]
|
(prompt, error) — prompt is the rewritten text (or corpus_text on failure); error is None on success or a short message describing the failure. |
Source code in src/gutenberg_kg/image_gen.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |