Ardaosta Claude Sonnet 4.6 commited on
Commit
fbcaabd
·
1 Parent(s): c8ac6b3

Add Gemini Flash image generation as a layer source

Browse files

New /generate endpoint takes a text prompt, calls Gemini 3.1 Flash image
preview (mirrors the PS Designs Studio client setup), caches the result with
an image_token and a source data URL so it slots into the existing layer
pipeline (mask extraction, crop, position, polarity, etc.). Plasma-friendly
style toggle auto-appends a suffix to steer Gemini toward high-contrast
silhouettes that subject-extract cleanly.

UI: prompt box + Generate button below the upload dropzone in the Layers
panel. Enter submits.

Requires GEMINI_API_KEY env var or HF Space secret.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (5) hide show
  1. app.py +89 -0
  2. requirements.txt +1 -0
  3. static/app.js +73 -0
  4. static/index.html +8 -0
  5. static/style.css +28 -0
app.py CHANGED
@@ -9,6 +9,7 @@ from __future__ import annotations
9
  import base64
10
  import io
11
  import math
 
12
  import uuid
13
  from collections import OrderedDict
14
  from pathlib import Path
@@ -435,6 +436,94 @@ def root():
435
  return send_from_directory(str(STATIC_DIR), "index.html")
436
 
437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  @app.post("/upload")
439
  def upload():
440
  if "image" not in request.files:
 
9
  import base64
10
  import io
11
  import math
12
+ import os
13
  import uuid
14
  from collections import OrderedDict
15
  from pathlib import Path
 
436
  return send_from_directory(str(STATIC_DIR), "index.html")
437
 
438
 
439
+ # --- Gemini image generation ---
440
+
441
+ GEMINI_FLASH_MODEL = "gemini-3.1-flash-image-preview" # "Nano Banana 2"
442
+ PLASMA_STYLE_SUFFIX = (
443
+ " High-contrast black silhouette on a pure white background. "
444
+ "Bold simple shapes, clean outlines, no shading or gradients, no text. "
445
+ "Designed to be cut from steel on a plasma table."
446
+ )
447
+
448
+ _gemini_client = None
449
+
450
+
451
+ def get_gemini_client():
452
+ global _gemini_client
453
+ if _gemini_client is None:
454
+ api_key = os.environ.get("GEMINI_API_KEY")
455
+ if not api_key:
456
+ raise RuntimeError(
457
+ "GEMINI_API_KEY is not set. On HF Spaces, add it under "
458
+ "Settings → Variables and secrets. Locally, set it in your shell."
459
+ )
460
+ from google import genai
461
+ _gemini_client = genai.Client(api_key=api_key)
462
+ return _gemini_client
463
+
464
+
465
+ def _gemini_generate_image(prompt: str, size: str = "1K") -> Image.Image:
466
+ from google.genai import types
467
+ client = get_gemini_client()
468
+ resp = client.models.generate_content(
469
+ model=GEMINI_FLASH_MODEL,
470
+ contents=[prompt],
471
+ config=types.GenerateContentConfig(
472
+ image_config=types.ImageConfig(image_size=size),
473
+ ),
474
+ )
475
+ # Find the first image part in the response
476
+ for cand in (resp.candidates or []):
477
+ for part in (cand.content.parts or []):
478
+ inline = getattr(part, "inline_data", None)
479
+ if inline and inline.data:
480
+ return Image.open(io.BytesIO(inline.data)).convert("RGBA")
481
+ raise RuntimeError("Gemini returned no image data")
482
+
483
+
484
+ @app.post("/generate")
485
+ def generate():
486
+ data = request.get_json(force=True) or {}
487
+ prompt = (data.get("prompt") or "").strip()
488
+ if not prompt:
489
+ return jsonify({"error": "prompt required"}), 400
490
+ plasma_style = bool(data.get("plasma_style", True))
491
+ size = data.get("size", "1K")
492
+ if size not in ("1K", "2K", "4K"):
493
+ size = "1K"
494
+ full_prompt = prompt + (PLASMA_STYLE_SUFFIX if plasma_style else "")
495
+ try:
496
+ img = _gemini_generate_image(full_prompt, size=size)
497
+ except RuntimeError as e:
498
+ return jsonify({"error": str(e)}), 503
499
+ except Exception as e:
500
+ return jsonify({"error": f"generation failed: {e}"}), 502
501
+ # Downscale to fit our pipeline expectations
502
+ if max(img.size) > MAX_IMAGE_DIM:
503
+ scale = MAX_IMAGE_DIM / max(img.size)
504
+ img = img.resize((int(img.width * scale), int(img.height * scale)), Image.LANCZOS)
505
+ token = _cache_image(img)
506
+ thumb = img.copy()
507
+ thumb.thumbnail((160, 160), Image.LANCZOS)
508
+ buf = io.BytesIO()
509
+ thumb.convert("RGB").save(buf, format="JPEG", quality=80)
510
+ thumb_b64 = base64.b64encode(buf.getvalue()).decode("ascii")
511
+ # Echo the source PNG as a base64 data URL so the frontend can use it for
512
+ # crop modal display (same as if the user had uploaded the file directly).
513
+ src_buf = io.BytesIO()
514
+ img.save(src_buf, format="PNG")
515
+ src_b64 = base64.b64encode(src_buf.getvalue()).decode("ascii")
516
+ return jsonify({
517
+ "image_token": token,
518
+ "width": img.width,
519
+ "height": img.height,
520
+ "thumbnail": f"data:image/jpeg;base64,{thumb_b64}",
521
+ "source_data_url": f"data:image/png;base64,{src_b64}",
522
+ "name": prompt[:40] + ("…" if len(prompt) > 40 else ""),
523
+ "used_prompt": full_prompt,
524
+ })
525
+
526
+
527
  @app.post("/upload")
528
  def upload():
529
  if "image" not in request.files:
requirements.txt CHANGED
@@ -6,3 +6,4 @@ ezdxf>=1.3
6
  potracer>=0.0.4
7
  rembg>=2.0
8
  onnxruntime>=1.17
 
 
6
  potracer>=0.0.4
7
  rembg>=2.0
8
  onnxruntime>=1.17
9
+ google-genai>=1.0
static/app.js CHANGED
@@ -58,6 +58,9 @@ const els = {
58
  cropApplyBtn: $("cropApplyBtn"),
59
  cropCancelBtn: $("cropCancelBtn"),
60
  cropClearBtn: $("cropClearBtn"),
 
 
 
61
  composeBtn: $("composeBtn"),
62
  exportSvgBtn: $("exportSvgBtn"),
63
  exportDxfBtn: $("exportDxfBtn"),
@@ -82,6 +85,72 @@ function selectedLayer() {
82
  return state.layers.find((l) => l.id === state.selectedLayerId) || null;
83
  }
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  async function addLayerFromUpload(file) {
86
  setStatus(`Uploading ${file.name}…`);
87
  const fd = new FormData();
@@ -751,6 +820,10 @@ els.showWarnings.addEventListener("change", () => {
751
  });
752
 
753
  els.composeBtn.addEventListener("click", runCompose);
 
 
 
 
754
  els.exportSvgBtn.addEventListener("click", () => exportFile("/export/svg", "plasma-design.svg"));
755
  els.exportDxfBtn.addEventListener("click", () => exportFile("/export/dxf", "plasma-design.dxf"));
756
 
 
58
  cropApplyBtn: $("cropApplyBtn"),
59
  cropCancelBtn: $("cropCancelBtn"),
60
  cropClearBtn: $("cropClearBtn"),
61
+ genPrompt: $("genPrompt"),
62
+ genPlasmaStyle: $("genPlasmaStyle"),
63
+ genBtn: $("genBtn"),
64
  composeBtn: $("composeBtn"),
65
  exportSvgBtn: $("exportSvgBtn"),
66
  exportDxfBtn: $("exportDxfBtn"),
 
85
  return state.layers.find((l) => l.id === state.selectedLayerId) || null;
86
  }
87
 
88
+ async function addLayerFromGenerated(data, prompt) {
89
+ // The /generate endpoint already cached the image server-side and returned
90
+ // an image_token. Build a layer entry that mirrors what addLayerFromUpload
91
+ // produces, using the returned source_data_url so the crop modal works.
92
+ const defaultPolarity = "subtract";
93
+ const layer = {
94
+ id: newLayerId(),
95
+ name: data.name || (prompt.slice(0, 40)),
96
+ imageToken: data.image_token,
97
+ sourceUrl: data.source_data_url, // data URL of the full source PNG
98
+ originalWidth: data.width,
99
+ originalHeight: data.height,
100
+ sourceWidth: data.width,
101
+ sourceHeight: data.height,
102
+ thumbnail: data.thumbnail,
103
+ maskMode: "subject", // Plasma-friendly silhouettes work great with rembg
104
+ maskParams: { threshold: 128, blur: 0, invert: false, alpha_cutoff: 50, crop: null },
105
+ polarity: defaultPolarity,
106
+ maskPng: null,
107
+ visible: true,
108
+ xIn: state.panelWidthIn / 2 + state.layers.length * 2,
109
+ yIn: state.panelHeightIn / 2 + state.layers.length * 2,
110
+ widthIn: Math.min(state.panelWidthIn, state.panelHeightIn) * 0.55,
111
+ aspect: data.width / Math.max(data.height, 1),
112
+ };
113
+ state.layers.push(layer);
114
+ state.selectedLayerId = layer.id;
115
+ await refreshLayerMask(layer);
116
+ renderLayerList();
117
+ renderEditor();
118
+ renderStageLayers();
119
+ els.composeBtn.disabled = false;
120
+ }
121
+
122
+ async function runGenerate() {
123
+ const prompt = els.genPrompt.value.trim();
124
+ if (!prompt) return;
125
+ const box = els.genBtn.closest(".generate-box");
126
+ box.classList.add("busy");
127
+ setStatus(`Generating "${prompt}"…`);
128
+ try {
129
+ const res = await fetch("/generate", {
130
+ method: "POST",
131
+ headers: { "Content-Type": "application/json" },
132
+ body: JSON.stringify({
133
+ prompt,
134
+ plasma_style: els.genPlasmaStyle.checked,
135
+ size: "1K",
136
+ }),
137
+ });
138
+ if (!res.ok) {
139
+ const err = await res.json().catch(() => ({}));
140
+ setStatus(err.error || `Generate failed (${res.status})`, true);
141
+ return;
142
+ }
143
+ const data = await res.json();
144
+ await addLayerFromGenerated(data, prompt);
145
+ setStatus(`Added "${data.name}" — extracting subject…`);
146
+ els.genPrompt.value = "";
147
+ } catch (err) {
148
+ setStatus(err.message || String(err), true);
149
+ } finally {
150
+ box.classList.remove("busy");
151
+ }
152
+ }
153
+
154
  async function addLayerFromUpload(file) {
155
  setStatus(`Uploading ${file.name}…`);
156
  const fd = new FormData();
 
820
  });
821
 
822
  els.composeBtn.addEventListener("click", runCompose);
823
+ els.genBtn.addEventListener("click", runGenerate);
824
+ els.genPrompt.addEventListener("keydown", (e) => {
825
+ if (e.key === "Enter") runGenerate();
826
+ });
827
  els.exportSvgBtn.addEventListener("click", () => exportFile("/export/svg", "plasma-design.svg"));
828
  els.exportDxfBtn.addEventListener("click", () => exportFile("/export/dxf", "plasma-design.dxf"));
829
 
static/index.html CHANGED
@@ -25,6 +25,14 @@
25
  <span id="dropzoneLabel">+ Add layer (drop image or click)</span>
26
  </label>
27
 
 
 
 
 
 
 
 
 
28
  <section id="layerEditor" class="layer-editor hidden">
29
  <h2>Edit layer <span id="editorName" class="muted-inline"></span></h2>
30
  <div class="row">
 
25
  <span id="dropzoneLabel">+ Add layer (drop image or click)</span>
26
  </label>
27
 
28
+ <div class="generate-box">
29
+ <input type="text" id="genPrompt" placeholder="…or describe what to cut: &quot;wolf howling at the moon&quot;" autocomplete="off" />
30
+ <div class="generate-row">
31
+ <label class="checkbox"><input type="checkbox" id="genPlasmaStyle" checked /> Plasma-friendly style</label>
32
+ <button type="button" id="genBtn" class="primary">Generate</button>
33
+ </div>
34
+ </div>
35
+
36
  <section id="layerEditor" class="layer-editor hidden">
37
  <h2>Edit layer <span id="editorName" class="muted-inline"></span></h2>
38
  <div class="row">
static/style.css CHANGED
@@ -195,6 +195,34 @@ button.primary:hover:not(:disabled) {
195
  border-style: solid;
196
  }
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  .layer-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
199
  .layer-row {
200
  display: flex; align-items: center; gap: 8px;
 
195
  border-style: solid;
196
  }
197
 
198
+ .generate-box {
199
+ margin-top: 8px;
200
+ padding: 10px;
201
+ background: var(--panel-2);
202
+ border: 1px solid var(--line);
203
+ border-radius: 3px;
204
+ display: flex;
205
+ flex-direction: column;
206
+ gap: 8px;
207
+ }
208
+ .generate-box input[type="text"] {
209
+ background: #0a0a0a;
210
+ color: var(--ink);
211
+ border: 1px solid var(--line);
212
+ border-radius: 3px;
213
+ padding: 7px 9px;
214
+ font-size: 12px;
215
+ width: 100%;
216
+ }
217
+ .generate-box input[type="text"]:focus { outline: none; border-color: var(--accent); }
218
+ .generate-row {
219
+ display: flex; align-items: center; gap: 8px;
220
+ }
221
+ .generate-row .checkbox { flex: 1; font-size: 11px; color: var(--muted); }
222
+ .generate-row .checkbox label { font-size: 11px; gap: 6px; }
223
+ .generate-row button { padding: 7px 14px; }
224
+ .generate-box.busy { opacity: 0.6; pointer-events: none; }
225
+
226
  .layer-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
227
  .layer-row {
228
  display: flex; align-items: center; gap: 8px;