TechTush commited on
Commit
a7bac0c
·
verified ·
1 Parent(s): 2e2ea5e

Rangrez dashboard

Browse files
Dockerfile CHANGED
@@ -7,6 +7,7 @@ COPY pyproject.toml uv.lock README.md ./
7
  COPY src ./src
8
  RUN uv sync --frozen --no-dev && chmod -R a+rX /app
9
  ENV RANGREZ_HOST=0.0.0.0 RANGREZ_PORT_HTTP=7860 RANGREZ_RECORD=0 RANGREZ_STEP_DELAY=1.2 \
 
10
  HOME=/tmp PYTHONUNBUFFERED=1
11
  EXPOSE 7860
12
  CMD ["/app/.venv/bin/rangrez-lab"]
 
7
  COPY src ./src
8
  RUN uv sync --frozen --no-dev && chmod -R a+rX /app
9
  ENV RANGREZ_HOST=0.0.0.0 RANGREZ_PORT_HTTP=7860 RANGREZ_RECORD=0 RANGREZ_STEP_DELAY=1.2 \
10
+ RANGREZ_MEMORY=/tmp/lab/memory.json \
11
  HOME=/tmp PYTHONUNBUFFERED=1
12
  EXPOSE 7860
13
  CMD ["/app/.venv/bin/rangrez-lab"]
README.md CHANGED
@@ -21,6 +21,10 @@ Without the live passcode it runs the offline policy and SAM masks cached for ea
21
  With it, hosted **SAM 3.1** measures every photo and **Muse Wonder** runs the campaign,
22
  and you can read a target colour from your own photo ("match this kurta").
23
 
 
 
 
 
24
  One lab, shared by everyone viewing: if a campaign is running, you are watching it.
25
 
26
  Code, hardware, firmware and evaluation: https://github.com/TusharTechs/rangrez
 
21
  With it, hosted **SAM 3.1** measures every photo and **Muse Wonder** runs the campaign,
22
  and you can read a target colour from your own photo ("match this kurta").
23
 
24
+ **Photograph your outfit:** SAM 3.1 reads its colour (a few free reads a day per visitor) and
25
+ Rangrez gives you a recipe to dye a matching cloth at home, in drops of blue, red and yellow
26
+ household colour.
27
+
28
  One lab, shared by everyone viewing: if a campaign is running, you are watching it.
29
 
30
  Code, hardware, firmware and evaluation: https://github.com/TusharTechs/rangrez
src/rangrez/agent.py CHANGED
@@ -33,9 +33,13 @@ You control the lab only through tools. Rules:
33
  - Before every run_shot, call suggest_recipes. You may pick a candidate or blend candidates, but explain why.
34
  - After each shot with a usable measurement, call fit_model so the dye model learns this dye lot.
35
  - For the first dip, if a glimmer candidate is offered, use it: Glimmer learned this dye lot from the lab's own dips.
 
 
36
  - Far from the target, trust model_inverse. Once the best dip is within about dE00 6, prefer colourist_correction:
37
  the model always predicts a perfect match but cannot capture everything about this dye lot (saturation, lot strength),
38
  while the correction is learned from real dips.
 
 
39
  - If a shot is flagged REJECT, do not trust its colour; re-run or change something and say what.
40
  - Write one short notebook entry (write_note) per shot: hypothesis, what you changed, what you expect.
41
  - If dips at the concentration cap still miss, the shade is outside this dye set's gamut: say so and report the closest dip.
@@ -73,7 +77,10 @@ def _out_of_gamut(c: Campaign) -> bool:
73
  if len(recent) < OUT_OF_GAMUT_STREAK:
74
  return False
75
  cap = 0.98 * c.rig.limits.max_conc_g_per_l
76
- return all(max(s.recipe.conc) >= cap and s.delta_e > c.tolerance for s in recent)
 
 
 
77
 
78
 
79
  def dispatch(c: Campaign, name: str, args: dict) -> dict:
@@ -108,6 +115,13 @@ def dispatch(c: Campaign, name: str, args: dict) -> dict:
108
 
109
  def run_with_wonder(c: Campaign, log: CallLog | None = None, max_turns: int = 60) -> None:
110
  """Drive the campaign with Muse Wonder over the Meta Model API."""
 
 
 
 
 
 
 
111
  from openai import OpenAI
112
 
113
  log = log or CallLog()
@@ -149,6 +163,13 @@ def run_with_wonder(c: Campaign, log: CallLog | None = None, max_turns: int = 60
149
 
150
  def run_deterministic(c: Campaign, on_step: Callable[[], None] | None = None) -> None:
151
  """Offline policy with the same tools and rules: used in tests and replay mode."""
 
 
 
 
 
 
 
152
  while c.status == "running" and len(c.shots) < c.max_shots:
153
  if c.shots:
154
  dispatch(c, "fit_model", {})
@@ -160,8 +181,12 @@ def run_deterministic(c: Campaign, on_step: Callable[[], None] | None = None) ->
160
  near = c.best is not None and c.best.delta_e < CORRECTION_ZONE_DE00
161
  correction = [x for x in cands if x["method"] == "colourist_correction"]
162
  first = [x for x in cands if x["method"] == "glimmer"]
 
 
163
  if first:
164
  pick = first[0]
 
 
165
  else:
166
  pick = correction[0] if near and correction else min(cands, key=lambda x: x["predicted_delta_e00"])
167
  dispatch(c, "write_note", {"text": f"Shot {len(c.shots) + 1}: {pick['method']} predicts dE00 {pick['predicted_delta_e00']}."})
 
33
  - Before every run_shot, call suggest_recipes. You may pick a candidate or blend candidates, but explain why.
34
  - After each shot with a usable measurement, call fit_model so the dye model learns this dye lot.
35
  - For the first dip, if a glimmer candidate is offered, use it: Glimmer learned this dye lot from the lab's own dips.
36
+ Otherwise, if lab_memory is offered and its remembered dip was within about dE00 6 of the target, use it:
37
+ it corrects a real dip of this very dye lot.
38
  - Far from the target, trust model_inverse. Once the best dip is within about dE00 6, prefer colourist_correction:
39
  the model always predicts a perfect match but cannot capture everything about this dye lot (saturation, lot strength),
40
  while the correction is learned from real dips.
41
+ - dyer_notes in the state are what the person at the bench said they see (e.g. "patchy", "darker than the
42
+ customer's cloth"). Take them seriously, but measurements decide; say how a note changed your plan.
43
  - If a shot is flagged REJECT, do not trust its colour; re-run or change something and say what.
44
  - Write one short notebook entry (write_note) per shot: hypothesis, what you changed, what you expect.
45
  - If dips at the concentration cap still miss, the shade is outside this dye set's gamut: say so and report the closest dip.
 
77
  if len(recent) < OUT_OF_GAMUT_STREAK:
78
  return False
79
  cap = 0.98 * c.rig.limits.max_conc_g_per_l
80
+ at_cap = all(max(s.recipe.conc) >= cap and s.delta_e > c.tolerance for s in recent)
81
+ # Still closing in at the cap (a shade on the edge of the gamut) is not out of reach yet.
82
+ improving = recent[-1].delta_e < recent[-2].delta_e - MIN_IMPROVEMENT_DE00
83
+ return at_cap and not improving
84
 
85
 
86
  def dispatch(c: Campaign, name: str, args: dict) -> dict:
 
115
 
116
  def run_with_wonder(c: Campaign, log: CallLog | None = None, max_turns: int = 60) -> None:
117
  """Drive the campaign with Muse Wonder over the Meta Model API."""
118
+ try:
119
+ _wonder_loop(c, log, max_turns)
120
+ finally:
121
+ c.close()
122
+
123
+
124
+ def _wonder_loop(c: Campaign, log: CallLog | None, max_turns: int) -> None:
125
  from openai import OpenAI
126
 
127
  log = log or CallLog()
 
163
 
164
  def run_deterministic(c: Campaign, on_step: Callable[[], None] | None = None) -> None:
165
  """Offline policy with the same tools and rules: used in tests and replay mode."""
166
+ try:
167
+ _deterministic_loop(c, on_step)
168
+ finally:
169
+ c.close()
170
+
171
+
172
+ def _deterministic_loop(c: Campaign, on_step: Callable[[], None] | None) -> None:
173
  while c.status == "running" and len(c.shots) < c.max_shots:
174
  if c.shots:
175
  dispatch(c, "fit_model", {})
 
181
  near = c.best is not None and c.best.delta_e < CORRECTION_ZONE_DE00
182
  correction = [x for x in cands if x["method"] == "colourist_correction"]
183
  first = [x for x in cands if x["method"] == "glimmer"]
184
+ remembered = [x for x in cands if x["method"] == "lab_memory"
185
+ and c.memory_source_de is not None and c.memory_source_de < CORRECTION_ZONE_DE00]
186
  if first:
187
  pick = first[0]
188
+ elif remembered:
189
+ pick = remembered[0]
190
  else:
191
  pick = correction[0] if near and correction else min(cands, key=lambda x: x["predicted_delta_e00"])
192
  dispatch(c, "write_note", {"text": f"Shot {len(c.shots) + 1}: {pick['method']} predicts dE00 {pick['predicted_delta_e00']}."})
src/rangrez/campaign.py CHANGED
@@ -16,6 +16,7 @@ import numpy as np
16
  from . import glimmer
17
  from .colour import delta_e_2000, lab_to_hex
18
  from .km import DyeModel, fit_gains, solve_recipe
 
19
  from .profiles import PROFILE
20
  from .rig import DYES, UNIT, GateError, Recipe, Rig, check_recipe, execute
21
  from .vision import Measurement, Segmenter, measure, overlay
@@ -48,6 +49,16 @@ class Campaign:
48
  listeners: list = field(default_factory=list)
49
  status: str = "running"
50
  side_readings: list = field(default_factory=list) # the customer's cloth, read beside each strip
 
 
 
 
 
 
 
 
 
 
51
 
52
  # -- events ------------------------------------------------------------
53
  def emit(self, kind: str, **data) -> None:
@@ -78,6 +89,7 @@ class Campaign:
78
  "history": [self._shot_summary(s) for s in self.shots],
79
  "model_gains": dict(zip(DYES, np.round(self.model.gains, 3).tolist())),
80
  "stock_ml": dict(zip(DYES, np.round(self.rig.inventory.stock_ml, 1).tolist())),
 
81
  "status": self.status,
82
  }
83
 
@@ -95,8 +107,10 @@ class Campaign:
95
 
96
  # -- tools -----------------------------------------------------------------
97
  def fit_model(self) -> dict:
98
- """Refit per-dye uptake to every valid shot so far."""
99
  data = [(s.recipe.as_array(), s.measurement.lab) for s in self.shots if s.measurement.ok]
 
 
100
  before = self.model.gains.copy()
101
  self.model = fit_gains(self.model, data)
102
  errs = [float(delta_e_2000(self.model.predict_lab(c), lab)) for c, lab in data]
@@ -118,8 +132,15 @@ class Campaign:
118
  g = glimmer.first_guess(self.target_lab, log=getattr(self.segmenter, "log", None))
119
  if g is not None:
120
  d = float(delta_e_2000(self.model.predict_lab(g), self.target_lab))
121
- cands.append(("glimmer", g, d, "Muse Glimmer fine-tuned on this lab's own dips: its first recipes "
122
- "land closer than the supplier model's (the predicted dE00 shown is the supplier model's opinion)."))
 
 
 
 
 
 
 
123
  best = self.best
124
  if best is not None:
125
  step = self._correction_step(best)
@@ -165,6 +186,8 @@ class Campaign:
165
  if m.ok and m.target_lab is not None:
166
  self._side_by_side(m)
167
  de = float(delta_e_2000(m.lab, self.target_lab)) if m.ok else float("nan")
 
 
168
  shot = Shot(len(self.shots) + 1, recipe, m, de, rationale, result.image_jpeg, overlay(result.image_jpeg, m))
169
  self.shots.append(shot)
170
  self.emit("shot", index=shot.index, recipe=recipe.describe(), lab=m.lab.tolist(),
@@ -197,6 +220,18 @@ class Campaign:
197
  for s in self.shots: # earlier dips are judged against the better target too
198
  s.delta_e = float(delta_e_2000(s.measurement.lab, self.target_lab)) if s.measurement.ok else float("nan")
199
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  def declare_out_of_gamut(self) -> dict:
201
  best = self.best
202
  self.status = "out_of_gamut"
@@ -233,9 +268,12 @@ class Campaign:
233
  return np.stack(cols, axis=1)
234
 
235
  def _correction_step(self, best: Shot) -> np.ndarray | None:
236
- c0 = best.recipe.as_array()
 
 
 
237
  J = self._jac(c0)
238
- err = self.target_lab - best.measurement.lab
239
  # Damped least squares; weight lightness less, as CIEDE2000 does.
240
  Wt = np.diag([0.6, 1.0, 1.0])
241
  A = Wt @ J
 
16
  from . import glimmer
17
  from .colour import delta_e_2000, lab_to_hex
18
  from .km import DyeModel, fit_gains, solve_recipe
19
+ from .memory import LabMemory
20
  from .profiles import PROFILE
21
  from .rig import DYES, UNIT, GateError, Recipe, Rig, check_recipe, execute
22
  from .vision import Measurement, Segmenter, measure, overlay
 
49
  listeners: list = field(default_factory=list)
50
  status: str = "running"
51
  side_readings: list = field(default_factory=list) # the customer's cloth, read beside each strip
52
+ memory: LabMemory | None = None # every dip this lab has made with this dye lot
53
+ campaign_id: str = field(default_factory=lambda: f"c{int(time.time() * 1000)}")
54
+ memory_source_de: float | None = None # how far the remembered dip offered first was from the target
55
+
56
+ def __post_init__(self) -> None:
57
+ if self.memory is not None and self.memory.dips:
58
+ self.model = self.memory.warm_model(self.model)
59
+ self.note(f"Lab memory: {len(self.memory.dips)} dips from {len(self.memory.campaigns)} earlier campaigns "
60
+ f"with this dye lot. Starting from the uptake they showed: "
61
+ f"{dict(zip(DYES, np.round(self.model.gains, 2).tolist()))}.", who="lab")
62
 
63
  # -- events ------------------------------------------------------------
64
  def emit(self, kind: str, **data) -> None:
 
89
  "history": [self._shot_summary(s) for s in self.shots],
90
  "model_gains": dict(zip(DYES, np.round(self.model.gains, 3).tolist())),
91
  "stock_ml": dict(zip(DYES, np.round(self.rig.inventory.stock_ml, 1).tolist())),
92
+ "dyer_notes": [n["text"] for n in self.notebook if n["who"] == "dyer"][-5:],
93
  "status": self.status,
94
  }
95
 
 
107
 
108
  # -- tools -----------------------------------------------------------------
109
  def fit_model(self) -> dict:
110
+ """Refit per-dye uptake to every valid shot so far (and every remembered dip of this lot)."""
111
  data = [(s.recipe.as_array(), s.measurement.lab) for s in self.shots if s.measurement.ok]
112
+ if self.memory is not None: # it already holds this campaign's dips
113
+ data = self.memory.recent()
114
  before = self.model.gains.copy()
115
  self.model = fit_gains(self.model, data)
116
  errs = [float(delta_e_2000(self.model.predict_lab(c), lab)) for c, lab in data]
 
132
  g = glimmer.first_guess(self.target_lab, log=getattr(self.segmenter, "log", None))
133
  if g is not None:
134
  d = float(delta_e_2000(self.model.predict_lab(g), self.target_lab))
135
+ cands.append(("glimmer", g, d, ("Muse Glimmer fine-tuned on this lab's own dips: its first recipes "
136
+ "land closer than the supplier model's (the predicted dE00 shown is the supplier model's opinion).")))
137
+ if not self.shots and self.memory is not None and (near := self.memory.nearest(self.target_lab)):
138
+ c0, lab0, d0 = near
139
+ step = self._correct(c0, lab0)
140
+ pred = float(delta_e_2000(lab0 + self._jac(c0) @ (step - c0), self.target_lab))
141
+ self.memory_source_de = d0
142
+ cands.append(("lab_memory", step, pred, (f"The most similar dip this lab has made (dE00 {d0:.1f} from the target), "
143
+ "corrected towards it with the refitted model's sensitivity.")))
144
  best = self.best
145
  if best is not None:
146
  step = self._correction_step(best)
 
186
  if m.ok and m.target_lab is not None:
187
  self._side_by_side(m)
188
  de = float(delta_e_2000(m.lab, self.target_lab)) if m.ok else float("nan")
189
+ if m.ok and self.memory is not None:
190
+ self.memory.record_dip(recipe.as_array(), m.lab, self.campaign_id)
191
  shot = Shot(len(self.shots) + 1, recipe, m, de, rationale, result.image_jpeg, overlay(result.image_jpeg, m))
192
  self.shots.append(shot)
193
  self.emit("shot", index=shot.index, recipe=recipe.describe(), lab=m.lab.tolist(),
 
220
  for s in self.shots: # earlier dips are judged against the better target too
221
  s.delta_e = float(delta_e_2000(s.measurement.lab, self.target_lab)) if s.measurement.ok else float("nan")
222
 
223
+ def close(self) -> None:
224
+ """Remember how this campaign went (the night shift's chart reads these)."""
225
+ if self.memory is None:
226
+ return
227
+ ok = [s for s in self.shots if s.measurement.ok]
228
+ self.memory.record_campaign({
229
+ "campaign": self.campaign_id, "target_lab": [round(float(v), 2) for v in self.target_lab],
230
+ "status": self.status, "dips": len(self.shots),
231
+ "first_de00": round(ok[0].delta_e, 2) if ok else None,
232
+ "best_de00": round(self.best.delta_e, 2) if self.best else None,
233
+ })
234
+
235
  def declare_out_of_gamut(self) -> dict:
236
  best = self.best
237
  self.status = "out_of_gamut"
 
268
  return np.stack(cols, axis=1)
269
 
270
  def _correction_step(self, best: Shot) -> np.ndarray | None:
271
+ return self._correct(best.recipe.as_array(), best.measurement.lab)
272
+
273
+ def _correct(self, c0: np.ndarray, lab: np.ndarray) -> np.ndarray:
274
+ """Move recipe c0, which dyed `lab`, towards the target."""
275
  J = self._jac(c0)
276
+ err = self.target_lab - lab
277
  # Damped least squares; weight lightness less, as CIEDE2000 does.
278
  Wt = np.diag([0.6, 1.0, 1.0])
279
  A = Wt @ J
src/rangrez/glimmer.py CHANGED
@@ -6,7 +6,8 @@ camera, from every recipe the lab dipped and the colour SAM 3.1 measured on the
6
 
7
  GLIMMER_MODEL=accounts/<account>/models/<model>#accounts/<account>/deployments/<deployment>
8
 
9
- and the first dip of every campaign gets a "glimmer" candidate.
 
10
  """
11
 
12
  from __future__ import annotations
@@ -20,7 +21,7 @@ import numpy as np
20
  from .colour import lab_to_hex
21
  from .profiles import PROFILE
22
 
23
- INFERENCE_URL = "https://api.fireworks.ai/inference/v1"
24
  SYSTEM = (
25
  f"You are Glimmer, the colourist of a small cotton dye lab. Dyes: {', '.join(PROFILE.names)} "
26
  f"({PROFILE.description}). Given a target colour measured in CIELAB (D65), reply with only the "
@@ -45,8 +46,15 @@ def parse(text: str) -> np.ndarray | None:
45
  return None
46
 
47
 
 
 
 
 
 
 
 
48
  def configured() -> bool:
49
- return bool(os.environ.get("GLIMMER_MODEL") and os.environ.get("FIREWORKS_API_KEY"))
50
 
51
 
52
  def first_guess(target_lab, log=None, model: str | None = None) -> np.ndarray | None:
@@ -58,7 +66,8 @@ def first_guess(target_lab, log=None, model: str | None = None) -> np.ndarray |
58
  model = model or os.environ.get("GLIMMER_MODEL")
59
  if not model:
60
  return None
61
- client = OpenAI(base_url=INFERENCE_URL, api_key=os.environ["FIREWORKS_API_KEY"].strip(), timeout=60, max_retries=1)
 
62
  started = time.monotonic()
63
  try:
64
  r = client.chat.completions.create(model=model, temperature=0, max_tokens=400, messages=[
 
6
 
7
  GLIMMER_MODEL=accounts/<account>/models/<model>#accounts/<account>/deployments/<deployment>
8
 
9
+ or serve the published weights on Modal (eval/glimmer_serve.py) and set GLIMMER_BASE_URL and
10
+ GLIMMER_API_KEY. Either way the first dip of every campaign gets a "glimmer" candidate.
11
  """
12
 
13
  from __future__ import annotations
 
21
  from .colour import lab_to_hex
22
  from .profiles import PROFILE
23
 
24
+ FIREWORKS_URL = "https://api.fireworks.ai/inference/v1"
25
  SYSTEM = (
26
  f"You are Glimmer, the colourist of a small cotton dye lab. Dyes: {', '.join(PROFILE.names)} "
27
  f"({PROFILE.description}). Given a target colour measured in CIELAB (D65), reply with only the "
 
46
  return None
47
 
48
 
49
+ def _endpoint() -> tuple[str, str]:
50
+ """Where Glimmer runs: Fireworks by default, or any OpenAI-style endpoint (eval/glimmer_serve.py on Modal)."""
51
+ url = os.environ.get("GLIMMER_BASE_URL") or FIREWORKS_URL
52
+ key = os.environ.get("GLIMMER_API_KEY") if os.environ.get("GLIMMER_BASE_URL") else os.environ.get("FIREWORKS_API_KEY")
53
+ return url, (key or "").strip()
54
+
55
+
56
  def configured() -> bool:
57
+ return bool(os.environ.get("GLIMMER_MODEL") and _endpoint()[1])
58
 
59
 
60
  def first_guess(target_lab, log=None, model: str | None = None) -> np.ndarray | None:
 
66
  model = model or os.environ.get("GLIMMER_MODEL")
67
  if not model:
68
  return None
69
+ url, key = _endpoint()
70
+ client = OpenAI(base_url=url, api_key=key, timeout=120, max_retries=1)
71
  started = time.monotonic()
72
  try:
73
  r = client.chat.completions.create(model=model, temperature=0, max_tokens=400, messages=[
src/rangrez/home.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A first home recipe for any colour: drops of blue, red and yellow household colour.
2
+
3
+ The public demo's take-home: photograph your outfit, SAM 3.1 reads its colour, and the household
4
+ dye model turns it into drops for a 4-teaspoon bath. It is a first-dip estimate (inks and food
5
+ colours vary by brand); assisted mode then corrects it dip by dip.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import itertools
11
+ import math
12
+
13
+ import numpy as np
14
+
15
+ from .colour import delta_e_2000, lab_to_hex
16
+ from .km import solve_recipe
17
+ from .profiles import PROFILES
18
+
19
+ HOME = PROFILES["home"]
20
+ REACHABLE_DE00 = 3.0 # the model's own predicted error above which it says it can't get there
21
+
22
+
23
+ def home_recipe(target_lab) -> dict:
24
+ model = HOME.nominal()
25
+ target = np.asarray(target_lab, dtype=float)
26
+ conc, _ = solve_recipe(model, target, max_conc=HOME.max_conc) # drops per 20 mL
27
+ best = None
28
+ # Pale shades need less than a drop: a bigger bath (8 or 16 teaspoons) gives finer steps.
29
+ for scale in (1, 2, 4):
30
+ options = itertools.product(*[sorted({math.floor(c * scale), math.ceil(c * scale)}) for c in conc])
31
+ for o in options:
32
+ d = np.array(o, dtype=float)
33
+ lab = model.predict_lab(d / scale)
34
+ de = float(delta_e_2000(lab, target))
35
+ if best is None or de < best[0] - 1e-9:
36
+ best = (de, scale, d, lab)
37
+ if best[0] <= 1.0:
38
+ break
39
+ de, scale, d, lab = best
40
+ return {
41
+ "drops": {name: int(n) for name, n in zip(HOME.names, d)},
42
+ "water_ml": 20 * scale,
43
+ "teaspoons": 4 * scale,
44
+ "predicted_de00": round(de, 1),
45
+ "reachable": bool(de <= REACHABLE_DE00),
46
+ "closest_hex": lab_to_hex(lab),
47
+ }
src/rangrez/memory.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Lab memory: every dip this lab has made with this dye lot, kept across campaigns.
2
+
3
+ A colourist who has dyed a hundred shades with this lot does not start the next one from the
4
+ supplier's sheet. Neither does Rangrez: each campaign starts from a dye model fitted to every dip
5
+ remembered, and is offered the most similar dip ever made, corrected towards the new target.
6
+ The memory is a JSON file per dye set and lot (RANGREZ_MEMORY to move it, "off" to disable).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import os
13
+ import time
14
+ from pathlib import Path
15
+
16
+ import numpy as np
17
+
18
+ from .colour import delta_e_2000
19
+ from .km import DyeModel, fit_gains
20
+ from .profiles import PROFILE
21
+
22
+ RECENT_DIPS = 200 # a dye lot drifts slowly: fit to the most recent dips
23
+
24
+
25
+ def default_path() -> Path | None:
26
+ raw = os.environ.get("RANGREZ_MEMORY", f"lab/memory_{PROFILE.key}.json")
27
+ return None if raw.lower() == "off" else Path(raw)
28
+
29
+
30
+ class LabMemory:
31
+ def __init__(self, path: Path | None = None) -> None:
32
+ self.path = path
33
+ self.data = {"dye_set": PROFILE.key, "dips": [], "campaigns": []}
34
+ if path is not None and path.exists():
35
+ self.data = json.loads(path.read_text())
36
+
37
+ @property
38
+ def dips(self) -> list[dict]:
39
+ return self.data["dips"]
40
+
41
+ @property
42
+ def campaigns(self) -> list[dict]:
43
+ return self.data["campaigns"]
44
+
45
+ def record_dip(self, conc, lab, campaign: str) -> None:
46
+ self.dips.append({"conc": [round(float(v), 4) for v in conc], "lab": [round(float(v), 3) for v in lab],
47
+ "campaign": campaign, "at": time.time()})
48
+ self.save()
49
+
50
+ def record_campaign(self, summary: dict) -> None:
51
+ self.campaigns.append({**summary, "at": time.time()})
52
+ self.save()
53
+
54
+ def save(self) -> None:
55
+ if self.path is None:
56
+ return
57
+ self.path.parent.mkdir(parents=True, exist_ok=True)
58
+ tmp = self.path.with_suffix(".tmp")
59
+ tmp.write_text(json.dumps(self.data))
60
+ tmp.replace(self.path)
61
+
62
+ def recent(self, n: int = RECENT_DIPS) -> list[tuple[np.ndarray, np.ndarray]]:
63
+ return [(np.array(d["conc"]), np.array(d["lab"])) for d in self.dips[-n:]]
64
+
65
+ def warm_model(self, nominal: DyeModel) -> DyeModel:
66
+ """The dye model fitted to what this lot actually did, not what the supplier says."""
67
+ data = self.recent()
68
+ return fit_gains(nominal, data) if len(data) >= 3 else nominal.copy()
69
+
70
+ def nearest(self, target_lab) -> tuple[np.ndarray, np.ndarray, float] | None:
71
+ """The remembered dip closest in colour to target_lab: (recipe, measured Lab, dE00)."""
72
+ if not self.dips:
73
+ return None
74
+ labs = np.array([d["lab"] for d in self.dips])
75
+ des = delta_e_2000(labs, np.broadcast_to(np.asarray(target_lab, dtype=float), labs.shape))
76
+ i = int(np.argmin(des))
77
+ return np.array(self.dips[i]["conc"]), labs[i], float(des[i])
src/rangrez/preview.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Bulk preview: the customer's garment shown in the shade the lab actually dyed.
2
+
3
+ Muse Image recolours the garment in the customer's photo (keeping its folds, buttons and light).
4
+ Image models are not colour instruments, so Rangrez then measures the preview the same way it
5
+ measures a dip (SAM 3.1 + the paper or card), nudges the garment's pixels so the measurement
6
+ matches the dip, and reports the residual. The preview is for the eye; the dip is the evidence.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import base64
12
+ import io
13
+ import os
14
+ import time
15
+
16
+ import numpy as np
17
+ from PIL import Image
18
+
19
+ from .colour import (
20
+ delta_e_2000,
21
+ lab_to_hex,
22
+ lab_to_srgb,
23
+ linear_to_srgb,
24
+ srgb_to_linear,
25
+ )
26
+ from .vision import ApiCall, MuseSamSegmenter, decode, read_object, target_from_photo
27
+
28
+ IMAGE_MODEL = "muse-image-1.0"
29
+
30
+
31
+ def _edit(photo: bytes, concept: str, hex_colour: str, log) -> bytes:
32
+ from openai import OpenAI
33
+
34
+ client = OpenAI(base_url="https://api.meta.ai/v1", api_key=os.environ["MODEL_API_KEY"], timeout=240, max_retries=1)
35
+ started = time.monotonic()
36
+ try:
37
+ r = client.images.edit(model=IMAGE_MODEL, image=("garment.jpg", photo, "image/jpeg"), prompt=(
38
+ f"Recolour only the {concept} to a solid {hex_colour}, matte dyed cotton. Keep its folds, seams, buttons, "
39
+ "shading and everything else in the photo exactly as it is."))
40
+ except Exception as exc:
41
+ log.add(ApiCall(IMAGE_MODEL, "bulk preview", time.monotonic() - started, False, str(exc)[:200]))
42
+ raise RuntimeError(f"Muse Image could not make the preview: {exc}") from exc
43
+ log.add(ApiCall(IMAGE_MODEL, "bulk preview", time.monotonic() - started, True))
44
+ img = Image.open(io.BytesIO(base64.b64decode(r.data[0].b64_json))).convert("RGB")
45
+ buf = io.BytesIO()
46
+ img.save(buf, format="JPEG", quality=92)
47
+ return buf.getvalue()
48
+
49
+
50
+ def bulk_preview(photo: bytes, concept: str, dip_lab, log, segmenter=None) -> dict:
51
+ """{"jpeg", "measured_hex", "raw_de00", "de00"}: the corrected preview and how close it is to the dip."""
52
+ dip_lab = np.asarray(dip_lab, dtype=float)
53
+ edited = _edit(photo, concept, lab_to_hex(dip_lab), log)
54
+ seg = segmenter or MuseSamSegmenter(log)
55
+ raw_lab, _, mask = read_object(edited, concept, seg) # the same garment the measurement chose
56
+ raw_de = float(delta_e_2000(raw_lab, dip_lab))
57
+
58
+ # Nudge the garment's pixels, per channel in linear light, so it measures as the dip.
59
+ _, srgb = decode(edited)
60
+ lin = srgb_to_linear(srgb)
61
+ gain = srgb_to_linear(lab_to_srgb(dip_lab)) / np.maximum(srgb_to_linear(lab_to_srgb(raw_lab)), 1e-4)
62
+ lin[mask] = np.clip(lin[mask] * np.clip(gain, 0.25, 4.0), 0, 1)
63
+ buf = io.BytesIO()
64
+ Image.fromarray((linear_to_srgb(lin) * 255).round().astype(np.uint8)).save(buf, format="JPEG", quality=92)
65
+ corrected = buf.getvalue()
66
+ final_lab, _ = target_from_photo(corrected, concept, seg)
67
+ return {"jpeg": corrected, "measured_hex": lab_to_hex(final_lab), "raw_de00": round(raw_de, 2),
68
+ "de00": round(float(delta_e_2000(final_lab, dip_lab)), 2)}
src/rangrez/report.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The lab-dip report: what a dye house sends a buyer to approve a shade.
2
+
3
+ One self-contained HTML page (prints to PDF): target against the approved dip, the colour
4
+ difference split the way spectrophotometer QC reports do (lightness, chroma, hue, with plain
5
+ words), a pass or fail against the tolerance, the recipe scaled for production, every dip with
6
+ its photo and SAM's masks, and a line for the buyer's signature.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import base64
12
+ import html
13
+ import math
14
+ import time
15
+
16
+ import numpy as np
17
+
18
+ from .colour import delta_e_2000, lab_to_hex
19
+ from .profiles import PROFILE
20
+ from .rig import DYES, UNIT
21
+
22
+ LIQUOR_RATIO = 10 # litres of dye bath per kg of cotton (1:10), typical for exhaust dyeing
23
+
24
+
25
+ def difference(target, sample) -> dict:
26
+ """CIELAB difference of sample from target: dL*, dC*, dH* and how a colourist would say it."""
27
+ L1, a1, b1 = (float(v) for v in target)
28
+ L2, a2, b2 = (float(v) for v in sample)
29
+ C1, C2 = math.hypot(a1, b1), math.hypot(a2, b2)
30
+ dL, dC, da, db = L2 - L1, C2 - C1, a2 - a1, b2 - b1
31
+ dH2 = max(da * da + db * db - dC * dC, 0.0)
32
+ h1 = math.atan2(b1, a1)
33
+ sign = 1.0 if (a1 * db - b1 * da) >= 0 else -1.0 # counter-clockwise on the hue circle
34
+ dH = sign * math.sqrt(dH2)
35
+
36
+ def size(x: float) -> str:
37
+ x = abs(x)
38
+ return "" if x < 0.5 else "slightly " if x < 1.5 else "" if x < 3 else "much "
39
+
40
+ words = []
41
+ if abs(dL) >= 0.5:
42
+ words.append(f"{size(dL)}{'lighter' if dL > 0 else 'darker'}")
43
+ if abs(dC) >= 0.5:
44
+ words.append(f"{size(dC)}{'brighter' if dC > 0 else 'duller'}")
45
+ if abs(dH) >= 0.5:
46
+ hue_dir = math.degrees(h1 + (math.pi / 2 if dH > 0 else -math.pi / 2)) % 360
47
+ towards = "redder" if hue_dir < 45 or hue_dir >= 315 else "yellower" if hue_dir < 135 else "greener" if hue_dir < 225 else "bluer"
48
+ words.append(f"{size(dH)}{towards}")
49
+ return {"dE00": float(delta_e_2000(np.array(sample), np.array(target))), "dL": dL, "dC": dC, "dH": dH,
50
+ "words": ", ".join(words) or "no visible difference"}
51
+
52
+
53
+ def _img(jpeg: bytes, width: int) -> str:
54
+ return f'<img src="data:image/jpeg;base64,{base64.b64encode(jpeg).decode()}" width="{width}" alt="">'
55
+
56
+
57
+ def render(campaign, target_photo: bytes | None = None, agent: str = "", api_calls: int | None = None,
58
+ preview: dict | None = None) -> str:
59
+ c = campaign
60
+ best = c.best
61
+ tol = c.tolerance
62
+ passed = best is not None and best.delta_e <= tol
63
+ diff = difference(c.target_lab, best.measurement.lab) if best else None
64
+ when = time.strftime("%d %b %Y, %H:%M")
65
+ ref = f"RZ-{c.campaign_id[-8:].upper()}"
66
+ rows = []
67
+ for s in c.shots:
68
+ d = difference(c.target_lab, s.measurement.lab) if s.measurement.ok else None
69
+ recipe = " · ".join(f"{v:g}" for v in s.recipe.conc)
70
+ rows.append(f"""<tr><td>{s.index}</td><td>{_img(s.overlay_jpeg, 150)}</td>
71
+ <td><span class="sw" style="background:{lab_to_hex(s.measurement.lab)}"></span>{lab_to_hex(s.measurement.lab)}</td>
72
+ <td>{recipe}</td><td class="num">{"rejected" if d is None else f"{s.delta_e:.2f}"}</td>
73
+ <td>{"" if d is None else html.escape(d["words"])}</td></tr>""")
74
+ if best is not None:
75
+ bath = best.recipe.as_array() / PROFILE.stock_per_l * 1000 # mL of stock per litre of bath
76
+ per_kg = bath * LIQUOR_RATIO
77
+ prod = "".join(f"<tr><td>{html.escape(n)}</td><td class='num'>{v:g} {UNIT}</td><td class='num'>{m:.0f} mL</td></tr>"
78
+ for n, v, m in zip(DYES, best.recipe.conc, per_kg))
79
+ verdict = ("PASS" if passed else "NOT YET") if best else "NO DIP"
80
+ flags = sorted({f for s in c.shots for f in s.measurement.flags})
81
+ notes = "".join(f"<li>{html.escape(f)}</li>" for f in flags) or "<li>No measurement warnings.</li>"
82
+ return f"""<!doctype html><html lang="en"><head><meta charset="utf-8">
83
+ <meta name="viewport" content="width=device-width, initial-scale=1"><title>Lab-dip report {ref}</title>
84
+ <style>
85
+ :root {{ --ink:#1b1b1d; --muted:#6b6862; --line:#e4e1dc; --pass:#1f7a4d; --fail:#a8452a; }}
86
+ body {{ font: 15px/1.45 -apple-system, "Segoe UI", Arial, sans-serif; color:var(--ink); background:#fff; max-width: 960px; margin: 0 auto; padding: 32px 20px; }}
87
+ h1 {{ font-size: 26px; margin: 0; }} h2 {{ font-size: 17px; margin: 28px 0 10px; border-bottom: 1px solid var(--line); padding-bottom: 6px; }}
88
+ .meta {{ color: var(--muted); }} .grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; }}
89
+ .chip {{ height: 120px; border-radius: 10px; border: 1px solid var(--line); }}
90
+ .verdict {{ font-size: 28px; font-weight: 700; color: {"var(--pass)" if passed else "var(--fail)"}; }}
91
+ table {{ border-collapse: collapse; width: 100%; }} td, th {{ border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: middle; }}
92
+ th {{ color: var(--muted); font-weight: 600; }} .num {{ text-align: right; font-variant-numeric: tabular-nums; }}
93
+ .sw {{ display:inline-block; width: 16px; height: 16px; border-radius: 4px; margin-right: 6px; vertical-align: -3px; border: 1px solid var(--line); }}
94
+ img {{ border-radius: 6px; max-width: 100%; height: auto; }} .sign {{ display:flex; gap: 40px; margin-top: 40px; }} .sign div {{ flex:1; border-top: 1px solid var(--ink); padding-top: 6px; color: var(--muted); }}
95
+ @media print {{ body {{ padding: 0; }} }}
96
+ </style></head><body>
97
+ <h1>Lab-dip report</h1>
98
+ <div class="meta">Rangrez self-driving dye lab · ref {ref} · {when} · {html.escape(PROFILE.description)}</div>
99
+
100
+ <h2>Shade</h2>
101
+ <div class="grid">
102
+ <div><div class="chip" style="background:{lab_to_hex(c.target_lab)}"></div>
103
+ <b>Target</b> {lab_to_hex(c.target_lab)} · L*a*b* {", ".join(f"{v:.1f}" for v in c.target_lab)}
104
+ {("<div>" + _img(target_photo, 300) + "</div>") if target_photo else ""}</div>
105
+ <div><div class="chip" style="background:{lab_to_hex(best.measurement.lab) if best else "#fff"}"></div>
106
+ <b>Submitted dip {best.index if best else ""}</b> {lab_to_hex(best.measurement.lab) if best else ""}
107
+ {("· L*a*b* " + ", ".join(f"{v:.1f}" for v in best.measurement.lab)) if best else ""}</div>
108
+ <div><div class="verdict">{verdict}</div>
109
+ {f'<div>ΔE00 <b>{diff["dE00"]:.2f}</b> against a tolerance of {tol}</div><div>ΔL* {diff["dL"]:+.2f} · ΔC* {diff["dC"]:+.2f} · ΔH* {diff["dH"]:+.2f}</div><div>The dip is <b>{html.escape(diff["words"])}</b> than the target.</div>' if diff else ""}</div>
110
+ </div>
111
+
112
+ {f'''<h2>Recipe</h2>
113
+ <table><tr><th>Dye</th><th class="num">Bath concentration</th><th class="num">Stock for 1 kg cotton (1:{LIQUOR_RATIO})</th></tr>{prod}</table>
114
+ <p class="meta">Dip {best.recipe.dip_seconds:.0f} s. Stock at {PROFILE.stock_per_l:g} {UNIT if UNIT != "drops" else "drops"} per litre. Scale-up assumes the same liquor ratio, temperature and fixation as the lab dip; confirm with a bulk trial.</p>''' if best else ""}
115
+
116
+ {f"""<h2>Bulk preview</h2>
117
+ <div class="grid"><div>{_img(preview["jpeg"], 420)}</div>
118
+ <div class="meta">The customer's photo, recoloured by Muse Image to dip {preview["dip"]}'s shade. An image model is not a colour
119
+ instrument: its first attempt measured ΔE00 {preview["raw_de00"]} from the dip, so Rangrez measured it with SAM 3.1 and corrected the
120
+ garment's pixels; it now measures {preview["measured_hex"]}, ΔE00 {preview["de00"]} from the dip. For the eye only: the dip is the evidence.</div></div>""" if preview else ""}
121
+
122
+ <h2>Dips</h2>
123
+ <table><tr><th>#</th><th>Photo, SAM 3.1 masks</th><th>Measured</th><th>Recipe ({" · ".join(DYES)}, {UNIT})</th><th class="num">ΔE00</th><th>Against the target</th></tr>
124
+ {"".join(rows)}</table>
125
+
126
+ <h2>How it was measured</h2>
127
+ <ul class="meta">
128
+ <li>Each dip is dried and photographed on white paper; SAM 3.1 segments the strip and the 24-patch colour card, and a colour-correction matrix turns pixels into CIELAB (D65). Uneven light is evened out using the paper.</li>
129
+ <li>Colour difference is CIEDE2000; ΔL*, ΔC*, ΔH* are CIELAB components (positive: lighter, brighter, counter-clockwise in hue).</li>
130
+ <li>Campaign run by {html.escape(agent or "Rangrez")}{f", {api_calls} live Meta API calls" if api_calls is not None else ""}; {len(c.shots)} dips; status {html.escape(c.status)}.</li>
131
+ {notes}
132
+ </ul>
133
+
134
+ <div class="sign"><div>Dyer</div><div>Buyer approval</div><div>Date</div></div>
135
+ </body></html>"""
src/rangrez/server.py CHANGED
@@ -17,13 +17,14 @@ from pathlib import Path
17
 
18
  import numpy as np
19
  from dotenv import load_dotenv
20
- from fastapi import FastAPI, File, Form, HTTPException, UploadFile
21
- from fastapi.responses import FileResponse, Response, StreamingResponse
22
  from pydantic import BaseModel
23
 
24
  from .agent import WONDER_MODEL, run_deterministic, run_with_wonder
25
  from .campaign import Campaign
26
  from .colour import hex_to_lab, lab_to_hex
 
27
  from .photoreal import scene_names
28
  from .recorder import Recorder
29
  from .rig import DYES, UNIT
@@ -48,6 +49,9 @@ class Lab:
48
  self.campaign: Campaign | None = None
49
  self.thread: threading.Thread | None = None
50
  self.rec = Recorder()
 
 
 
51
  self.log.listeners.append(lambda call: self.publish({
52
  "kind": "api", "model": call.model, "purpose": call.purpose,
53
  "seconds": round(call.seconds, 2), "ok": call.ok, "detail": call.detail, "at": time.time(),
@@ -115,6 +119,15 @@ class Lab:
115
  self.rec.save(f"dip{s.index:02d}_sam.jpg", s.overlay_jpeg)
116
  if event.get("kind") == "status" and event.get("status") not in ("running", "waiting"):
117
  self.rec.save("state.json", self.campaign.state())
 
 
 
 
 
 
 
 
 
118
 
119
  def subscribe(self) -> queue.Queue:
120
  """A new page (a reload, the phone joining) first gets the campaign so far."""
@@ -123,6 +136,7 @@ class Lab:
123
  q.put({"kind": "reset"})
124
  for event in self.history:
125
  q.put(event)
 
126
  self.subscribers.append(q)
127
  return q
128
 
@@ -133,8 +147,11 @@ class Lab:
133
  with self.lock: # keep only the API calls made since the last start (e.g. reading the target photo)
134
  self.history = [e for e in self.history if e.get("kind") == "api" and e.get("at", 0) >= self.since]
135
  self.since = time.time()
 
136
  rig = self.make_rig(None, live)
137
- c = Campaign(target_lab, rig, self.make_segmenter(rig, live), tolerance=tolerance)
 
 
138
  self.rec.begin(type(rig).__name__, {
139
  "rig": type(rig).__name__, "scene": getattr(rig, "scene", None), "dyes": DYES, "unit": UNIT,
140
  "agent": WONDER_MODEL if live else "offline policy", "tolerance": tolerance,
@@ -148,7 +165,8 @@ class Lab:
148
  self.campaign = c
149
  self.publish({"kind": "start", "target_lab": target_lab.tolist(), "target_hex": lab_to_hex(target_lab),
150
  "tolerance": tolerance, "agent": WONDER_MODEL if live else "offline policy",
151
- "rig": type(rig).__name__, "at": time.time()})
 
152
  delay = float(os.environ.get("RANGREZ_STEP_DELAY", "1.5"))
153
 
154
  def work() -> None:
@@ -188,6 +206,39 @@ def _json_safe(value):
188
  LAB = Lab()
189
 
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  class StartBody(BaseModel):
192
  hex: str | None = None
193
  lab: list[float] | None = None
@@ -204,6 +255,7 @@ def index() -> FileResponse:
204
  def config() -> dict:
205
  return {"online": LAB.online, "agent": WONDER_MODEL if LAB.online else "offline policy",
206
  "passcode": bool(os.environ.get("RANGREZ_LIVE_PASSCODE")) and LAB.key_available,
 
207
  "rig": os.environ.get("RANGREZ_RIG", "sim"), "dyes": DYES, "unit": UNIT}
208
 
209
 
@@ -223,18 +275,28 @@ def start(body: StartBody) -> dict:
223
 
224
 
225
  @app.post("/api/target-from-photo")
226
- async def target_photo(photo: UploadFile = File(...), concept: str = Form("fabric"), passcode: str = Form("")) -> dict:
 
 
 
227
  jpeg = await photo.read()
 
 
 
228
  try:
229
  from .assisted import normalise_photo
230
 
231
  photo = normalise_photo(jpeg)
232
- lab, calibration = target_from_photo(photo, concept, LAB.make_segmenter(live=LAB.live(passcode)))
233
  except (RuntimeError, ValueError) as exc:
234
  raise HTTPException(422, str(exc))
235
- result = {"lab": [round(v, 2) for v in lab], "hex": lab_to_hex(lab), "calibration": calibration, "concept": concept}
 
236
  LAB.rec.pending = [] # only the latest target photo goes with the next campaign
237
  LAB.rec.stash("target.jpg", photo)
 
 
 
238
  LAB.rec.stash("target.json", result)
239
  return result
240
 
@@ -263,6 +325,74 @@ def assist_webcam() -> dict:
263
  return {"ok": True}
264
 
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  @app.post("/api/stop")
267
  def stop() -> dict:
268
  c = LAB.campaign
@@ -281,6 +411,13 @@ def state() -> dict:
281
  return LAB.campaign.state() if LAB.campaign else {"status": "idle"}
282
 
283
 
 
 
 
 
 
 
 
284
  @app.get("/api/shot/{index}.jpg")
285
  def shot_image(index: int, raw: bool = False) -> Response:
286
  c = LAB.campaign
 
17
 
18
  import numpy as np
19
  from dotenv import load_dotenv
20
+ from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
21
+ from fastapi.responses import FileResponse, HTMLResponse, Response, StreamingResponse
22
  from pydantic import BaseModel
23
 
24
  from .agent import WONDER_MODEL, run_deterministic, run_with_wonder
25
  from .campaign import Campaign
26
  from .colour import hex_to_lab, lab_to_hex
27
+ from .memory import LabMemory, default_path
28
  from .photoreal import scene_names
29
  from .recorder import Recorder
30
  from .rig import DYES, UNIT
 
49
  self.campaign: Campaign | None = None
50
  self.thread: threading.Thread | None = None
51
  self.rec = Recorder()
52
+ self.target_photo: bytes | None = None # the customer's photo the current target came from
53
+ self.target_concept = "garment"
54
+ self.preview: dict | None = None # Muse Image's bulk preview, measured and corrected
55
  self.log.listeners.append(lambda call: self.publish({
56
  "kind": "api", "model": call.model, "purpose": call.purpose,
57
  "seconds": round(call.seconds, 2), "ok": call.ok, "detail": call.detail, "at": time.time(),
 
119
  self.rec.save(f"dip{s.index:02d}_sam.jpg", s.overlay_jpeg)
120
  if event.get("kind") == "status" and event.get("status") not in ("running", "waiting"):
121
  self.rec.save("state.json", self.campaign.state())
122
+ self.rec.save("report.html", self.report())
123
+
124
+ def report(self) -> str:
125
+ from .report import render
126
+
127
+ c = self.campaign
128
+ calls = sum(1 for e in self.history if e.get("kind") == "api")
129
+ agent = next((e.get("agent") for e in self.history if e.get("kind") == "start"), "")
130
+ return render(c, target_photo=self.target_photo, agent=agent, api_calls=calls, preview=self.preview)
131
 
132
  def subscribe(self) -> queue.Queue:
133
  """A new page (a reload, the phone joining) first gets the campaign so far."""
 
136
  q.put({"kind": "reset"})
137
  for event in self.history:
138
  q.put(event)
139
+ q.put({"kind": "replayed"}) # live events from here on (the page may act on them, e.g. speak)
140
  self.subscribers.append(q)
141
  return q
142
 
 
147
  with self.lock: # keep only the API calls made since the last start (e.g. reading the target photo)
148
  self.history = [e for e in self.history if e.get("kind") == "api" and e.get("at", 0) >= self.since]
149
  self.since = time.time()
150
+ self.preview = None # a new campaign, a new best dip
151
  rig = self.make_rig(None, live)
152
+ path = default_path()
153
+ memory = LabMemory(path) if path is not None else None # re-read: another process may have added dips
154
+ c = Campaign(target_lab, rig, self.make_segmenter(rig, live), tolerance=tolerance, memory=memory)
155
  self.rec.begin(type(rig).__name__, {
156
  "rig": type(rig).__name__, "scene": getattr(rig, "scene", None), "dyes": DYES, "unit": UNIT,
157
  "agent": WONDER_MODEL if live else "offline policy", "tolerance": tolerance,
 
165
  self.campaign = c
166
  self.publish({"kind": "start", "target_lab": target_lab.tolist(), "target_hex": lab_to_hex(target_lab),
167
  "tolerance": tolerance, "agent": WONDER_MODEL if live else "offline policy",
168
+ "rig": type(rig).__name__, "memory_dips": len(memory.dips) if memory else 0,
169
+ "memory_campaigns": len(memory.campaigns) if memory else 0, "at": time.time()})
170
  delay = float(os.environ.get("RANGREZ_STEP_DELAY", "1.5"))
171
 
172
  def work() -> None:
 
206
  LAB = Lab()
207
 
208
 
209
+ class PublicQuota:
210
+ """Free colour reads on a public demo: a few per visitor and a cap per day, so it can't run up a bill."""
211
+
212
+ def __init__(self) -> None:
213
+ self.per_day = int(os.environ.get("RANGREZ_PUBLIC_READS_PER_DAY", "0"))
214
+ self.per_visitor = int(os.environ.get("RANGREZ_PUBLIC_READS_PER_VISITOR", "5"))
215
+ self.day, self.total, self.by_ip = "", 0, {}
216
+ self.lock = threading.Lock()
217
+
218
+ def allow(self, ip: str) -> bool:
219
+ with self.lock:
220
+ today = time.strftime("%Y-%m-%d")
221
+ if today != self.day:
222
+ self.day, self.total, self.by_ip = today, 0, {}
223
+ if self.total >= self.per_day or self.by_ip.get(ip, 0) >= self.per_visitor:
224
+ return False
225
+ self.total += 1
226
+ self.by_ip[ip] = self.by_ip.get(ip, 0) + 1
227
+ return True
228
+
229
+ def left(self) -> int:
230
+ with self.lock:
231
+ return max(0, self.per_day - (self.total if self.day == time.strftime("%Y-%m-%d") else 0))
232
+
233
+
234
+ QUOTA = PublicQuota()
235
+
236
+
237
+ def client_ip(request: Request) -> str:
238
+ forwarded = request.headers.get("x-forwarded-for", "")
239
+ return forwarded.split(",")[0].strip() or (request.client.host if request.client else "?")
240
+
241
+
242
  class StartBody(BaseModel):
243
  hex: str | None = None
244
  lab: list[float] | None = None
 
255
  def config() -> dict:
256
  return {"online": LAB.online, "agent": WONDER_MODEL if LAB.online else "offline policy",
257
  "passcode": bool(os.environ.get("RANGREZ_LIVE_PASSCODE")) and LAB.key_available,
258
+ "public_reads_left": QUOTA.left() if LAB.key_available else 0,
259
  "rig": os.environ.get("RANGREZ_RIG", "sim"), "dyes": DYES, "unit": UNIT}
260
 
261
 
 
275
 
276
 
277
  @app.post("/api/target-from-photo")
278
+ async def target_photo(request: Request, photo: UploadFile = File(...), concept: str = Form("fabric"),
279
+ passcode: str = Form("")) -> dict:
280
+ from .home import home_recipe
281
+
282
  jpeg = await photo.read()
283
+ live = LAB.live(passcode) or (LAB.key_available and QUOTA.allow(client_ip(request)))
284
+ if not live and LAB.key_available:
285
+ raise HTTPException(429, "today's free colour reads are used up: try again tomorrow, or enter the live passcode")
286
  try:
287
  from .assisted import normalise_photo
288
 
289
  photo = normalise_photo(jpeg)
290
+ lab, calibration = await asyncio.to_thread(target_from_photo, photo, concept, LAB.make_segmenter(live=live))
291
  except (RuntimeError, ValueError) as exc:
292
  raise HTTPException(422, str(exc))
293
+ result = {"lab": [round(v, 2) for v in lab], "hex": lab_to_hex(lab), "calibration": calibration, "concept": concept,
294
+ "home_recipe": home_recipe(lab)}
295
  LAB.rec.pending = [] # only the latest target photo goes with the next campaign
296
  LAB.rec.stash("target.jpg", photo)
297
+ LAB.target_photo = photo
298
+ LAB.target_concept = concept
299
+ LAB.preview = None
300
  LAB.rec.stash("target.json", result)
301
  return result
302
 
 
325
  return {"ok": True}
326
 
327
 
328
+ @app.post("/api/voice")
329
+ async def voice(audio: UploadFile = File(...), passcode: str = Form("")) -> dict:
330
+ """One spoken turn from the hands-free dashboard: transcribe it, then do at most one safe thing."""
331
+ from .voice import intent, transcribe
332
+
333
+ if not LAB.live(passcode):
334
+ raise HTTPException(403, "voice needs the Meta models: set MODEL_API_KEY, or enter the live passcode")
335
+ wav = await audio.read()
336
+ try:
337
+ text = await asyncio.to_thread(transcribe, wav, LAB.log)
338
+ except RuntimeError as exc:
339
+ raise HTTPException(502, str(exc))
340
+ act = intent(text)
341
+ c, result = LAB.campaign, ""
342
+ rig = getattr(c, "rig", None)
343
+ waiting = getattr(rig, "waiting", None)
344
+ if act == "photo":
345
+ if waiting and hasattr(rig, "capture"):
346
+ try:
347
+ rig.provide(await asyncio.to_thread(rig.capture))
348
+ result = "Photo taken. Measuring."
349
+ except (RuntimeError, ValueError) as exc:
350
+ result = f"Could not take the photo ({exc}). Send one from your phone."
351
+ else:
352
+ result = "Nothing to photograph right now."
353
+ elif act == "stop":
354
+ stop()
355
+ result = "Stopped."
356
+ elif act == "repeat":
357
+ result = speakable(waiting) if waiting else "No dip is waiting."
358
+ elif act == "note" and c is not None:
359
+ c.note(text, who="dyer")
360
+ result = "Noted for the colourist."
361
+ return {"transcript": text, "intent": act, "result": result}
362
+
363
+
364
+ def speakable(w: dict) -> str:
365
+ amounts = ", ".join(f"{v} {w['unit']} of {d}" for d, v in w["amounts"].items() if float(v) > 0)
366
+ return f"Dip {w['index']}. {amounts}. Dip for {w['dip_seconds']} seconds. Say done when the strip is dry and on the paper."
367
+
368
+
369
+ @app.post("/api/preview")
370
+ async def preview(passcode: str = Form("")) -> dict:
371
+ """The customer's garment in the best dip's shade: Muse Image recolours, SAM 3.1 checks and corrects."""
372
+ from .preview import bulk_preview
373
+
374
+ c = LAB.campaign
375
+ if c is None or c.best is None or LAB.target_photo is None:
376
+ raise HTTPException(409, "needs a campaign started from a customer's photo, with at least one dip")
377
+ if not LAB.live(passcode):
378
+ raise HTTPException(403, "the preview needs the Meta models: set MODEL_API_KEY, or enter the live passcode")
379
+ try:
380
+ p = await asyncio.to_thread(bulk_preview, LAB.target_photo, LAB.target_concept, c.best.measurement.lab, LAB.log)
381
+ except RuntimeError as exc:
382
+ raise HTTPException(502, str(exc))
383
+ LAB.preview = {**p, "dip": c.best.index}
384
+ LAB.rec.save("preview.jpg", p["jpeg"])
385
+ LAB.rec.save("report.html", LAB.report())
386
+ return {"dip": c.best.index, "measured_hex": p["measured_hex"], "raw_de00": p["raw_de00"], "de00": p["de00"]}
387
+
388
+
389
+ @app.get("/api/preview.jpg")
390
+ def preview_image() -> Response:
391
+ if not LAB.preview:
392
+ raise HTTPException(404)
393
+ return Response(LAB.preview["jpeg"], media_type="image/jpeg")
394
+
395
+
396
  @app.post("/api/stop")
397
  def stop() -> dict:
398
  c = LAB.campaign
 
411
  return LAB.campaign.state() if LAB.campaign else {"status": "idle"}
412
 
413
 
414
+ @app.get("/api/report.html")
415
+ def report() -> HTMLResponse:
416
+ if LAB.campaign is None or not LAB.campaign.shots:
417
+ raise HTTPException(404, "no dips yet")
418
+ return HTMLResponse(LAB.report())
419
+
420
+
421
  @app.get("/api/shot/{index}.jpg")
422
  def shot_image(index: int, raw: bool = False) -> Response:
423
  c = LAB.campaign
src/rangrez/static/index.html CHANGED
@@ -39,11 +39,13 @@
39
  .de b { font-size: 44px; font-variant-numeric: tabular-nums; line-height: 1; }
40
  .de small { color: var(--mute); margin-top: 6px; text-align: center; }
41
  .de.pass b { color: var(--ok); }
 
42
  #frame { width: 100%; border-radius: 8px; border: 1px solid var(--line); background: #000; aspect-ratio: 16/9; object-fit: contain; }
43
  svg { width: 100%; height: 170px; display: block; }
44
  table { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
45
  td, th { padding: 5px 4px; border-bottom: 1px solid var(--line); text-align: left; }
46
  th { color: var(--mute); font-weight: 500; }
 
47
  td .sw { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: -3px; margin-right: 6px; border: 1px solid #0006; }
48
  .feed { max-height: 280px; overflow: auto; font-size: 12.5px; }
49
  .feed div { padding: 6px 0; border-bottom: 1px dashed var(--line); }
@@ -73,6 +75,7 @@
73
  <span class="spacer"></span>
74
  <span class="pill" id="agent">agent: …</span>
75
  <span class="pill" id="rig">rig: …</span>
 
76
  <span class="pill" id="status">idle</span>
77
  </header>
78
 
@@ -90,6 +93,7 @@
90
  <div style="height:8px"></div>
91
  <button class="ghost" id="usePhoto">Read colour with SAM 3.1</button>
92
  <p class="hint" id="photoNote"></p>
 
93
  <label for="tol">Pass tolerance (CIEDE2000)</label>
94
  <input type="number" id="tol" value="1.5" step="0.1" min="0.5" max="5">
95
  <div style="height:12px"></div>
@@ -101,6 +105,9 @@
101
  <button id="start">Start campaign</button>
102
  <div style="height:8px"></div>
103
  <button class="ghost" id="stop">Stop</button>
 
 
 
104
  </section>
105
  <section>
106
  <h2>Meta API calls</h2>
@@ -126,6 +133,10 @@
126
  <div class="de" id="deBox"><b id="de">—</b><small id="deNote">CIEDE2000</small></div>
127
  <div class="chip" id="dipChip"><span id="dipLabel">no dip yet</span></div>
128
  </div>
 
 
 
 
129
  </section>
130
  <section>
131
  <h2>Camera — SAM 3.1 masks: card (yellow), strip (cyan)</h2>
@@ -169,6 +180,7 @@ fetch("/api/config").then((r) => r.json()).then((c) => {
169
  $("rig").textContent = "rig: " + ({ sim: "simulator", serial: "hardware", assisted: "assisted (your hands)" }[c.rig] || c.rig);
170
  $("dyeHead").textContent = c.dyes.join(" · ") + " " + c.unit;
171
  $("liveBox").hidden = !c.passcode;
 
172
  });
173
 
174
  function setStatus(s) {
@@ -208,12 +220,16 @@ function drawChart() {
208
  const es = new EventSource("/api/events");
209
  es.onmessage = (msg) => {
210
  const e = JSON.parse(msg.data);
 
211
  if (e.kind === "reset") { // the server replays this campaign from the start
 
212
  shots = [];
213
  for (const id of ["shots", "notebook", "api"]) $(id).innerHTML = "";
 
214
  $("assist").hidden = true; $("frame").hidden = true; drawChart(); setStatus("idle");
215
  } else if (e.kind === "start") {
216
  $("agent").textContent = "agent: " + e.agent;
 
217
  shots = []; tolerance = e.tolerance;
218
  $("shots").innerHTML = ""; $("notebook").innerHTML = ""; drawChart();
219
  $("targetChip").style.background = e.target_hex;
@@ -222,6 +238,7 @@ es.onmessage = (msg) => {
222
  $("de").textContent = "—"; $("deBox").classList.remove("pass");
223
  setStatus("running");
224
  } else if (e.kind === "assist") {
 
225
  $("assist").hidden = false;
226
  $("assistTitle").textContent = `Your turn: dip ${e.index}`;
227
  $("amounts").innerHTML = Object.entries(e.amounts).map(([d, v]) => `<div class="amount"><b>${v}</b><small>${e.unit} ${esc(d)}</small></div>`).join("");
@@ -256,6 +273,8 @@ es.onmessage = (msg) => {
256
  feed("notebook", `<span class="tool">model refit</span> <span class="muted">uptake ${esc(JSON.stringify(e.gains_after))}, fit error ${e.model_fit_error_de00}</span>`);
257
  } else if (e.kind === "status") {
258
  setStatus(e.status);
 
 
259
  if (!["running", "waiting"].includes(e.status)) $("assist").hidden = true;
260
  if (e.reason) feed("notebook", `<span class="gate">${esc(e.status)}: ${esc(e.reason)}</span>`);
261
  } else if (e.kind === "api") {
@@ -275,7 +294,15 @@ $("usePhoto").onclick = async () => {
275
  const r = await fetch("/api/target-from-photo", { method: "POST", body: form });
276
  const j = await r.json();
277
  if (!r.ok) { $("photoNote").textContent = j.detail || "Could not read the photo."; return; }
278
- $("picker").value = j.hex; $("targetChip").style.background = j.hex;
 
 
 
 
 
 
 
 
279
  $("photoNote").textContent = {
280
  card: `Calibrated against the colour card: ${j.hex}`,
281
  paper: `Balanced against the white paper: ${j.hex}. Photograph every dip in the same spot and light.`,
@@ -303,6 +330,76 @@ $("sendPhoto").onclick = () => {
303
  };
304
  $("useWebcam").onclick = () => sendAssist("/api/assist/webcam");
305
  $("stop").onclick = () => fetch("/api/stop", { method: "POST" });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  drawChart();
307
  </script>
308
  </body>
 
39
  .de b { font-size: 44px; font-variant-numeric: tabular-nums; line-height: 1; }
40
  .de small { color: var(--mute); margin-top: 6px; text-align: center; }
41
  .de.pass b { color: var(--ok); }
42
+ #previewImg { width: 100%; border-radius: 8px; border: 1px solid var(--line); margin-top: 8px; }
43
  #frame { width: 100%; border-radius: 8px; border: 1px solid var(--line); background: #000; aspect-ratio: 16/9; object-fit: contain; }
44
  svg { width: 100%; height: 170px; display: block; }
45
  table { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
46
  td, th { padding: 5px 4px; border-bottom: 1px solid var(--line); text-align: left; }
47
  th { color: var(--mute); font-weight: 500; }
48
+ .hint .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -2px; margin: 0 4px; border: 1px solid #0006; }
49
  td .sw { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: -3px; margin-right: 6px; border: 1px solid #0006; }
50
  .feed { max-height: 280px; overflow: auto; font-size: 12.5px; }
51
  .feed div { padding: 6px 0; border-bottom: 1px dashed var(--line); }
 
75
  <span class="spacer"></span>
76
  <span class="pill" id="agent">agent: …</span>
77
  <span class="pill" id="rig">rig: …</span>
78
+ <span class="pill" id="memory">lab memory</span>
79
  <span class="pill" id="status">idle</span>
80
  </header>
81
 
 
93
  <div style="height:8px"></div>
94
  <button class="ghost" id="usePhoto">Read colour with SAM 3.1</button>
95
  <p class="hint" id="photoNote"></p>
96
+ <p class="hint" id="homeRecipe" hidden></p>
97
  <label for="tol">Pass tolerance (CIEDE2000)</label>
98
  <input type="number" id="tol" value="1.5" step="0.1" min="0.5" max="5">
99
  <div style="height:12px"></div>
 
105
  <button id="start">Start campaign</button>
106
  <div style="height:8px"></div>
107
  <button class="ghost" id="stop">Stop</button>
108
+ <div style="height:8px"></div>
109
+ <button class="ghost" id="handsFree" aria-pressed="false">Hands-free voice: off</button>
110
+ <p class="hint" id="voiceNote">Say "done" when the strip is dry, "repeat", or "stop". Anything else goes to the colourist's notebook.</p>
111
  </section>
112
  <section>
113
  <h2>Meta API calls</h2>
 
133
  <div class="de" id="deBox"><b id="de">—</b><small id="deNote">CIEDE2000</small></div>
134
  <div class="chip" id="dipChip"><span id="dipLabel">no dip yet</span></div>
135
  </div>
136
+ <p class="hint"><a id="reportLink" href="/api/report.html" target="_blank" rel="noopener" hidden>Lab-dip report for the buyer ↗</a></p>
137
+ <button class="ghost" id="previewBtn" hidden>Preview the customer's garment in this shade (Muse Image)</button>
138
+ <p class="hint" id="previewNote"></p>
139
+ <img id="previewImg" hidden alt="The customer's garment recoloured to the dip's shade">
140
  </section>
141
  <section>
142
  <h2>Camera — SAM 3.1 masks: card (yellow), strip (cyan)</h2>
 
180
  $("rig").textContent = "rig: " + ({ sim: "simulator", serial: "hardware", assisted: "assisted (your hands)" }[c.rig] || c.rig);
181
  $("dyeHead").textContent = c.dyes.join(" · ") + " " + c.unit;
182
  $("liveBox").hidden = !c.passcode;
183
+ if (c.public_reads_left > 0) $("photoNote").textContent = "Try it: photograph any outfit and get its colour, plus a recipe to dye it at home.";
184
  });
185
 
186
  function setStatus(s) {
 
220
  const es = new EventSource("/api/events");
221
  es.onmessage = (msg) => {
222
  const e = JSON.parse(msg.data);
223
+ if (e.kind === "replayed") { replaying = false; return; }
224
  if (e.kind === "reset") { // the server replays this campaign from the start
225
+ replaying = true;
226
  shots = [];
227
  for (const id of ["shots", "notebook", "api"]) $(id).innerHTML = "";
228
+ $("reportLink").hidden = true; $("previewBtn").hidden = true; $("previewImg").hidden = true; $("previewNote").textContent = "";
229
  $("assist").hidden = true; $("frame").hidden = true; drawChart(); setStatus("idle");
230
  } else if (e.kind === "start") {
231
  $("agent").textContent = "agent: " + e.agent;
232
+ $("memory").textContent = `lab memory: ${e.memory_dips || 0} dips · ${e.memory_campaigns || 0} campaigns`;
233
  shots = []; tolerance = e.tolerance;
234
  $("shots").innerHTML = ""; $("notebook").innerHTML = ""; drawChart();
235
  $("targetChip").style.background = e.target_hex;
 
238
  $("de").textContent = "—"; $("deBox").classList.remove("pass");
239
  setStatus("running");
240
  } else if (e.kind === "assist") {
241
+ if (handsFree && !replaying) say(`Dip ${e.index}. ` + Object.entries(e.amounts).filter(([, v]) => +v > 0).map(([d, v]) => `${v} ${e.unit} of ${d}`).join(", ") + `. Dip for ${e.dip_seconds} seconds. Say done when the strip is dry and on the paper.`);
242
  $("assist").hidden = false;
243
  $("assistTitle").textContent = `Your turn: dip ${e.index}`;
244
  $("amounts").innerHTML = Object.entries(e.amounts).map(([d, v]) => `<div class="amount"><b>${v}</b><small>${e.unit} ${esc(d)}</small></div>`).join("");
 
273
  feed("notebook", `<span class="tool">model refit</span> <span class="muted">uptake ${esc(JSON.stringify(e.gains_after))}, fit error ${e.model_fit_error_de00}</span>`);
274
  } else if (e.kind === "status") {
275
  setStatus(e.status);
276
+ $("reportLink").hidden = ["running", "waiting"].includes(e.status) || !shots.length;
277
+ $("previewBtn").hidden = $("reportLink").hidden || !targetFromPhoto;
278
  if (!["running", "waiting"].includes(e.status)) $("assist").hidden = true;
279
  if (e.reason) feed("notebook", `<span class="gate">${esc(e.status)}: ${esc(e.reason)}</span>`);
280
  } else if (e.kind === "api") {
 
294
  const r = await fetch("/api/target-from-photo", { method: "POST", body: form });
295
  const j = await r.json();
296
  if (!r.ok) { $("photoNote").textContent = j.detail || "Could not read the photo."; return; }
297
+ $("picker").value = j.hex; $("targetChip").style.background = j.hex; targetFromPhoto = true;
298
+ const h = j.home_recipe;
299
+ if (h) {
300
+ const drops = Object.entries(h.drops).filter(([, n]) => n > 0).map(([d, n]) => `${n} drop${n === 1 ? "" : "s"} ${esc(d)}`).join(" · ");
301
+ $("homeRecipe").hidden = false;
302
+ $("homeRecipe").innerHTML = h.reachable
303
+ ? `<b>Dye it at home:</b> ${drops || "plain water"} in ${h.teaspoons} teaspoons of water. A first-dip estimate for household colours; brands vary, so dip a white cotton strip and let Rangrez correct it.`
304
+ : `<b>At home:</b> household blue, red and yellow can't quite reach this shade. Closest: ${drops} <span class="sw" style="background:${h.closest_hex}"></span>${h.closest_hex}.`;
305
+ }
306
  $("photoNote").textContent = {
307
  card: `Calibrated against the colour card: ${j.hex}`,
308
  paper: `Balanced against the white paper: ${j.hex}. Photograph every dip in the same spot and light.`,
 
330
  };
331
  $("useWebcam").onclick = () => sendAssist("/api/assist/webcam");
332
  $("stop").onclick = () => fetch("/api/stop", { method: "POST" });
333
+
334
+ // -- hands-free: Muse Voice Transcribe hears the dyer; the browser reads instructions aloud ----------
335
+ let handsFree = false, replaying = false, mic = null, targetFromPhoto = false;
336
+ $("previewBtn").onclick = async () => {
337
+ const form = new FormData(); form.append("passcode", $("passcode").value);
338
+ $("previewNote").textContent = "Muse Image is recolouring the garment; SAM 3.1 will check it…";
339
+ const r = await fetch("/api/preview", { method: "POST", body: form });
340
+ const j = await r.json();
341
+ if (!r.ok) { $("previewNote").textContent = j.detail || "Preview failed."; return; }
342
+ $("previewImg").src = `/api/preview.jpg?t=${Date.now()}`; $("previewImg").hidden = false;
343
+ $("previewNote").textContent = `Dip ${j.dip}'s shade on the customer's garment. Muse Image's first attempt was ΔE00 ${j.raw_de00} off; measured and corrected, it is now ${j.de00}. For the eye: the dip is the evidence.`;
344
+ };
345
+ function say(text) {
346
+ if (!("speechSynthesis" in window)) return;
347
+ speechSynthesis.cancel();
348
+ speechSynthesis.speak(new SpeechSynthesisUtterance(text));
349
+ }
350
+ function wav16k(samples, rate) {
351
+ const ratio = rate / 16000, n = Math.floor(samples.length / ratio), pcm = new Int16Array(n);
352
+ for (let i = 0; i < n; i++) pcm[i] = Math.max(-1, Math.min(1, samples[Math.floor(i * ratio)])) * 0x7fff;
353
+ const buf = new ArrayBuffer(44 + pcm.length * 2), v = new DataView(buf);
354
+ const str = (o, t) => [...t].forEach((ch, i) => v.setUint8(o + i, ch.charCodeAt(0)));
355
+ str(0, "RIFF"); v.setUint32(4, 36 + pcm.length * 2, true); str(8, "WAVEfmt "); v.setUint32(16, 16, true);
356
+ v.setUint16(20, 1, true); v.setUint16(22, 1, true); v.setUint32(24, 16000, true); v.setUint32(28, 32000, true);
357
+ v.setUint16(32, 2, true); v.setUint16(34, 16, true); str(36, "data"); v.setUint32(40, pcm.length * 2, true);
358
+ new Int16Array(buf, 44).set(pcm);
359
+ return new Blob([buf], { type: "audio/wav" });
360
+ }
361
+ async function sendTurn(blob) {
362
+ const form = new FormData();
363
+ form.append("audio", blob, "turn.wav"); form.append("passcode", $("passcode").value);
364
+ $("voiceNote").textContent = "Transcribing with Muse Voice Transcribe…";
365
+ const r = await fetch("/api/voice", { method: "POST", body: form });
366
+ const j = await r.json();
367
+ if (!r.ok) { $("voiceNote").textContent = j.detail || "Voice failed."; return; }
368
+ $("voiceNote").textContent = `Heard “${j.transcript}” → ${j.intent}${j.result ? ": " + j.result : ""}`;
369
+ if (j.result && j.intent !== "note") say(j.result);
370
+ }
371
+ async function startListening() {
372
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } });
373
+ const ctx = new AudioContext(), src = ctx.createMediaStreamSource(stream), node = ctx.createScriptProcessor(4096, 1, 1);
374
+ let floor = 0.01, speaking = false, quiet = 0, chunks = [];
375
+ node.onaudioprocess = (ev) => {
376
+ const x = ev.inputBuffer.getChannelData(0);
377
+ let rms = 0; for (let i = 0; i < x.length; i++) rms += x[i] * x[i];
378
+ rms = Math.sqrt(rms / x.length);
379
+ const loud = rms > Math.max(0.015, floor * 3), dt = x.length / ctx.sampleRate;
380
+ if (!speaking) floor = 0.95 * floor + 0.05 * rms;
381
+ if (loud) { speaking = true; quiet = 0; }
382
+ if (speaking) { chunks.push(new Float32Array(x)); if (!loud) quiet += dt; }
383
+ const length = chunks.reduce((n, c) => n + c.length, 0) / ctx.sampleRate;
384
+ if (speaking && (quiet > 0.8 || length > 8)) { // end of a turn
385
+ const all = new Float32Array(chunks.reduce((n, c) => n + c.length, 0));
386
+ let o = 0; for (const c of chunks) { all.set(c, o); o += c.length; }
387
+ speaking = false; quiet = 0; chunks = [];
388
+ if (length > 0.5) sendTurn(wav16k(all, ctx.sampleRate));
389
+ }
390
+ };
391
+ src.connect(node); node.connect(ctx.destination);
392
+ mic = { stream, ctx };
393
+ }
394
+ $("handsFree").onclick = async () => {
395
+ handsFree = !handsFree;
396
+ $("handsFree").setAttribute("aria-pressed", handsFree);
397
+ $("handsFree").textContent = `Hands-free voice: ${handsFree ? "listening" : "off"}`;
398
+ if (handsFree) {
399
+ try { await startListening(); $("voiceNote").textContent = "Listening. Say \"done\", \"repeat\" or \"stop\"."; }
400
+ catch (err) { handsFree = false; $("handsFree").textContent = "Hands-free voice: off"; $("voiceNote").textContent = "Microphone unavailable: " + err.message; }
401
+ } else if (mic) { mic.stream.getTracks().forEach((t) => t.stop()); mic.ctx.close(); mic = null; }
402
+ };
403
  drawChart();
404
  </script>
405
  </body>
src/rangrez/vision.py CHANGED
@@ -451,7 +451,13 @@ def measure(jpeg: bytes, segmenter: Segmenter, with_target: bool = False, seed:
451
 
452
 
453
  def target_from_photo(jpeg: bytes, concept: str, segmenter: Segmenter) -> tuple[np.ndarray, str]:
454
- """Lab of the named object in a customer's photo (e.g. "teal kurta").
 
 
 
 
 
 
455
 
456
  If the photo also shows the colour card, the result is calibrated;
457
  with white paper in view it is white-balanced the same way dips are; otherwise
@@ -488,7 +494,7 @@ def target_from_photo(jpeg: bytes, concept: str, segmenter: Segmenter) -> tuple[
488
  M, _ = _white_balance(lin, paper, objects)
489
  mode = "paper" if paper is not None else "none"
490
  lab, _, _, _ = _lab_of_region(lin, obj.mask, M, rng)
491
- return lab, mode
492
 
493
 
494
  def overlay(jpeg: bytes, m: Measurement) -> bytes:
 
451
 
452
 
453
  def target_from_photo(jpeg: bytes, concept: str, segmenter: Segmenter) -> tuple[np.ndarray, str]:
454
+ """Lab of the named object in a customer's photo (e.g. "teal kurta"), and how it was calibrated."""
455
+ lab, mode, _ = read_object(jpeg, concept, segmenter)
456
+ return lab, mode
457
+
458
+
459
+ def read_object(jpeg: bytes, concept: str, segmenter: Segmenter) -> tuple[np.ndarray, str, np.ndarray]:
460
+ """Lab of the named object in a customer's photo (e.g. "teal kurta"), its calibration, and its mask.
461
 
462
  If the photo also shows the colour card, the result is calibrated;
463
  with white paper in view it is white-balanced the same way dips are; otherwise
 
494
  M, _ = _white_balance(lin, paper, objects)
495
  mode = "paper" if paper is not None else "none"
496
  lab, _, _, _ = _lab_of_region(lin, obj.mask, M, rng)
497
+ return lab, mode, obj.mask
498
 
499
 
500
  def overlay(jpeg: bytes, m: Measurement) -> bytes:
src/rangrez/voice.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Hands-free lab: the dyer's gloves are wet with dye, so they talk instead of tapping.
2
+
3
+ Muse Voice Transcribe (Meta Model API, /v1/asr/transcribe) turns a spoken turn into text,
4
+ biased towards the lab's words; a small deterministic parser turns the text into one action.
5
+ Nothing spoken can dispense dye or change a recipe: commands only take the photo, repeat the
6
+ instructions or stop, and everything else becomes a note in the lab notebook for Wonder to read.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import os
13
+ import re
14
+ import time
15
+ import urllib.request
16
+ import uuid
17
+
18
+ from .profiles import PROFILE
19
+
20
+ MODEL = "muse-voice-transcribe-1.0"
21
+ URL = "https://api.meta.ai/v1/asr/transcribe"
22
+ KEYWORDS = ["done", "dip", "photo", "webcam", "repeat", "stop", "drops", "patchy", "darker", "lighter", "Rangrez",
23
+ *PROFILE.names]
24
+
25
+ INTENTS = [ # first match wins; whole words only
26
+ ("stop", r"\b(stop|cancel|abort)\b"),
27
+ ("repeat", r"\b(repeat|again|say that|what was|instructions)\b"),
28
+ ("photo", r"\b(done|ready|photo|picture|capture|webcam|shoot|snap|dry now|it'?s dry)\b"),
29
+ ]
30
+
31
+
32
+ def transcribe(wav: bytes, log=None) -> str:
33
+ """Text of one spoken turn (a 16 kHz mono WAV)."""
34
+ from .vision import ApiCall
35
+
36
+ boundary = uuid.uuid4().hex
37
+ request = json.dumps({"model": MODEL, "audioEncoding": "WAV", "mode": "PUSH_TO_TALK", "keywords": KEYWORDS})
38
+ body = (f"--{boundary}\r\nContent-Disposition: form-data; name=\"request\"\r\nContent-Type: application/json\r\n\r\n"
39
+ f"{request}\r\n--{boundary}\r\nContent-Disposition: form-data; name=\"audio\"; filename=\"turn.wav\"\r\n"
40
+ "Content-Type: audio/wav\r\n\r\n").encode() + wav + f"\r\n--{boundary}--\r\n".encode()
41
+ req = urllib.request.Request(URL, data=body, method="POST", headers={
42
+ "Authorization": f"Bearer {os.environ['MODEL_API_KEY']}", "Accept": "application/json",
43
+ "Content-Type": f"multipart/form-data; boundary={boundary}"})
44
+ started = time.monotonic()
45
+ try:
46
+ with urllib.request.urlopen(req, timeout=30) as r:
47
+ text = json.loads(r.read()).get("transcript", "").strip()
48
+ except Exception as exc:
49
+ if log is not None:
50
+ log.add(ApiCall(MODEL, "voice command", time.monotonic() - started, False, str(exc)[:200]))
51
+ raise RuntimeError(f"could not transcribe: {exc}") from exc
52
+ if log is not None:
53
+ log.add(ApiCall(MODEL, "voice command", time.monotonic() - started, True, text[:80]))
54
+ return text
55
+
56
+
57
+ def intent(text: str) -> str:
58
+ """One of stop, repeat, photo, or note (anything else is something the dyer observed)."""
59
+ t = text.lower().strip(" .!?")
60
+ if not t:
61
+ return "none"
62
+ for name, pattern in INTENTS:
63
+ if re.search(pattern, t):
64
+ # "it looks done but patchy" is an observation with a command in it: a long turn is a note.
65
+ if name == "photo" and len(t.split()) > 6:
66
+ return "note"
67
+ return name
68
+ return "note"