File size: 23,312 Bytes
6778432
 
 
 
 
 
 
2d97bc2
6778432
 
 
 
2d97bc2
6778432
2d97bc2
6778432
 
 
 
 
 
 
 
 
 
 
 
 
 
74e743a
6778432
 
 
 
 
 
 
 
ee5ec33
6778432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee5ec33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d97bc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f4b1b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
74e743a
6778432
 
 
 
 
 
 
 
 
ee5ec33
 
 
4f4b1b3
ee5ec33
 
 
6778432
2d97bc2
 
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
 
 
 
 
74e743a
6778432
 
 
 
 
 
 
 
 
 
 
 
 
74e743a
6778432
 
 
 
 
 
2d97bc2
 
 
 
6778432
 
2d97bc2
6778432
2d97bc2
 
 
 
6778432
2d97bc2
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
ee5ec33
 
 
4f4b1b3
ee5ec33
 
6778432
ee5ec33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74e743a
6778432
 
 
 
 
 
 
 
 
 
 
 
 
74e743a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6778432
74e743a
 
 
 
 
 
 
 
 
 
 
 
 
 
6778432
 
 
 
 
 
 
 
 
ee5ec33
2d97bc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba65e78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1aafe24
 
 
 
ba65e78
2d97bc2
ba65e78
 
 
2d97bc2
ba65e78
 
 
 
 
 
 
2d97bc2
ba65e78
 
 
 
 
 
 
2d97bc2
 
 
 
 
 
 
ba65e78
2d97bc2
 
ba65e78
 
 
2d97bc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee5ec33
 
 
 
 
 
 
6778432
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
100
101
102
103
104
105
106
107
108
109
110
111
112
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
156
157
158
159
160
161
162
163
164
165
166
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
262
263
264
265
266
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Wardrobe AI</title>
  <link rel="stylesheet" href="/style.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@annotorious/annotorious@3.8.6/dist/annotorious.css">
  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
  <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js"></script>
  <script type="module">
    import { Client, handle_file } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
    import { createImageAnnotator } from "https://cdn.jsdelivr.net/npm/@annotorious/annotorious@3.8.6/+esm";
    window.gradioClient = null;
    window.annotoriousFactory = createImageAnnotator;
    Client.connect(window.location.origin).then(c => { window.gradioClient = c; });
  </script>
</head>
<body>
  <div class="app" x-data="wardrobe()" x-init="init()">
    <header>
      <h1>Wardrobe AI</h1>
      <p>Your clothes, organized. Upload photos, get outfit ideas, ask anything.</p>
    </header>

    <nav>
      <button :class="{ active: tab === 'wardrobe' }" @click="tab = 'wardrobe'; loadWardrobe()">My Wardrobe</button>
      <button :class="{ active: tab === 'add' }" @click="tab = 'add'">Add Clothes</button>
      <button :class="{ active: tab === 'outfits' }" @click="tab = 'outfits'">Get Dressed</button>
      <button :class="{ active: tab === 'ask' }" @click="tab = 'ask'; loadWardrobe()">Ask</button>
    </nav>

    <!-- My Wardrobe -->
    <section x-show="tab === 'wardrobe'">
      <h2>My Wardrobe <span class="count" x-text="garments.length ? `(${garments.length})` : ''"></span></h2>

      <div class="garment-grid" x-show="garments.length > 0">
        <template x-for="g in garments" :key="g.id">
          <div class="garment-card" @click="selectGarment(g)">
            <img :src="g.image_url" :alt="g.type" loading="lazy">
            <div class="info">
              <div class="type" x-text="g.type || 'garment'"></div>
              <div class="color" x-text="g.color || ''"></div>
            </div>
          </div>
        </template>
      </div>

      <div class="empty-state" x-show="garments.length === 0 && !loading">
        <p>Your wardrobe is empty.</p>
        <button class="btn btn-primary" @click="tab = 'add'">Add your first clothes</button>
      </div>
    </section>

    <!-- Garment Detail Overlay -->
    <div class="detail-overlay" x-show="selectedGarment" @click.self="closeDetail()" @keydown.escape.window="closeDetail()">
      <div class="detail-panel" x-show="selectedGarment">
        <button class="detail-close" @click="closeDetail()" aria-label="Close"></button>
        <div class="detail-image">
          <img :src="selectedGarment?.image_url" :alt="selectedGarment?.type" loading="lazy">
        </div>
        <div class="detail-info">
          <h3 class="detail-title" x-text="selectedGarment ? `${selectedGarment.color} ${selectedGarment.type}` : ''"></h3>
          <p class="detail-description" x-show="selectedGarment?.description" x-text="selectedGarment?.description"></p>
          <div class="attr-list">
            <div class="attr-row">
              <span class="attr-key">Type</span>
              <span class="attr-val" x-text="selectedGarment?.type || '—'"></span>
            </div>
            <div class="attr-row">
              <span class="attr-key">Color</span>
              <span class="attr-val" x-text="selectedGarment?.color || '—'"></span>
            </div>
            <div class="attr-row">
              <span class="attr-key">Material</span>
              <span class="attr-val" x-text="selectedGarment?.material || '—'"></span>
            </div>
            <div class="attr-row">
              <span class="attr-key">Pattern</span>
              <span class="attr-val" x-text="selectedGarment?.pattern || '—'"></span>
            </div>
            <div class="attr-row">
              <span class="attr-key">Season</span>
              <span class="attr-val" x-text="selectedGarment?.season || '—'"></span>
            </div>
            <div class="attr-row">
              <span class="attr-key">Formality</span>
              <span class="attr-val" x-text="selectedGarment?.formality || '—'"></span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Add Clothes -->
    <section x-show="tab === 'add'">
      <h2>Add Clothes</h2>

      <div class="upload-area"
           @click="$refs.fileInput.click()"
           @dragover.prevent="dragover = true"
           @dragleave="dragover = false"
           @drop.prevent="dragover = false; handleFile($event.dataTransfer.files[0])"
           :class="{ dragover }">
        <div class="icon">+</div>
        <p>Tap to upload a photo of your clothes</p>
        <p style="font-size:0.8rem; margin-top:0.5rem; color:#9ca3af">or drag and drop an image here</p>
        <input type="file" accept="image/*" x-ref="fileInput" @change="handleFile($refs.fileInput.files[0])" style="display:none">
      </div>

      <div class="status" :class="uploadStatus.type" x-show="uploadStatus.message" x-text="uploadStatus.message"></div>

      <!-- Bounding box editor (shown after upload) -->
      <div class="editor-area" x-show="editorActive">
        <p class="editor-hint">
          <strong x-text="editorAutoBoxCount > 0 ? `${editorAutoBoxCount} garment${editorAutoBoxCount > 1 ? 's' : ''} auto-detected.` : 'No garments auto-detected.'"></strong>
          Draw rectangles over any missing ones, adjust or delete existing boxes, then click Analyse.
        </p>
        <div class="editor-canvas-wrap">
          <img id="annoImage" :src="editorImageUrl" alt="Garment photo">
        </div>
        <div class="editor-actions">
          <button class="btn btn-primary" @click="analyzeBoxes()" :disabled="analyzing || editorBoxCount === 0">
            <span x-text="analyzing ? 'Analysing…' : `Analyse ${editorBoxCount} garment${editorBoxCount !== 1 ? 's' : ''}`"></span>
          </button>
          <button class="btn btn-secondary" @click="cancelEditor()" :disabled="analyzing">Cancel</button>
        </div>
        <div class="status" :class="analyzeStatus.type" x-show="analyzeStatus.message" x-text="analyzeStatus.message"></div>
      </div>

      <div style="margin-top:2rem">
        <h2>Or load a sample dataset</h2>
        <div class="dataset-row">
          <select x-model="datasetKey">
            <option value="second-hand">Second-hand (individual garments)</option>
            <option value="fashion-1k">Fashion-1K (multi-garment, slower)</option>
          </select>
          <button class="btn btn-secondary" @click="loadDataset()" :disabled="datasetLoading">
            <span x-text="datasetLoading ? 'Loading...' : 'Load Dataset'"></span>
          </button>
        </div>
        <div class="status" :class="datasetStatus.type" x-show="datasetStatus.message" x-text="datasetStatus.message"></div>
      </div>
    </section>

    <!-- Get Dressed -->
    <section x-show="tab === 'outfits'">
      <h2>Get Dressed</h2>
      <div class="context-input">
        <input type="text" x-model="context" placeholder="What's the occasion? (e.g. dinner with friends, casual Friday...)" @keydown.enter="generateOutfits()">
        <button class="btn btn-primary" @click="generateOutfits()" :disabled="outfitsLoading">
          <span x-text="outfitsLoading ? 'Generating...' : 'Generate'"></span>
        </button>
      </div>

      <div class="outfit-grid" x-show="outfits.length > 0">
        <template x-for="combo in outfits" :key="combo.id">
          <div class="outfit-card" :style="combo.rated ? 'opacity:0.4; transform:scale(0.97)' : ''">
            <div class="outfit-images">
              <img :src="combo.top.image_url" :alt="combo.top.type" loading="lazy">
              <img :src="combo.bottom.image_url" :alt="combo.bottom.type" loading="lazy">
            </div>
            <div class="outfit-info">
              <span class="label" x-text="`${combo.top.color} ${combo.top.type} + ${combo.bottom.color} ${combo.bottom.type}`"></span>
              <div class="outfit-actions">
              <button class="dislike" @click="rateOutfit(combo, false)">-</button>
              <button class="like" @click="rateOutfit(combo, true)">+</button>
              </div>
            </div>
          </div>
        </template>
      </div>

      <div class="empty-state" x-show="outfits.length === 0 && !outfitsLoading">
        <p>No combinations yet. Add at least one top and one bottom to your wardrobe, then hit Generate.</p>
      </div>
    </section>

    <!-- Ask -->
    <section x-show="tab === 'ask'">
      <h2>Ask about your wardrobe</h2>
      <div class="chat-container">
        <div class="chat-messages" x-ref="chatMessages">
          <template x-for="(msg, i) in messages" :key="i">
            <div class="chat-message" :class="msg.role">
              <div class="bubble" x-html="msg.role === 'assistant' ? renderMessage(msg.text) : escapeHtml(msg.text)"></div>
            </div>
          </template>
        </div>
        <div class="chat-input">
          <input type="text" x-model="question" placeholder="What should I wear for..." @keydown.enter="askQuestion()">
          <button @click="askQuestion()" :disabled="asking">Send</button>
        </div>
      </div>
    </section>

    <!-- Log Dock: fixed bottom panel shown during dataset loading -->
    <div class="log-dock" x-show="datasetLog.length > 0" :class="{ minimized: logMinimized }">
      <div class="log-dock-header" @click="logMinimized = !logMinimized">
        <span class="log-dock-title" x-text="datasetLoading ? `Loading dataset — ${datasetCount}/50` : `Done — ${datasetCount} garments added`"></span>
        <div class="log-dock-actions">
          <button class="log-dock-btn" :title="logMinimized ? 'Expand' : 'Minimize'" @click.stop="logMinimized = !logMinimized" x-text="logMinimized ? '▲' : '▼'"></button>
          <button class="log-dock-btn" title="Close" @click.stop="datasetLog = []"></button>
        </div>
      </div>
      <div class="log-dock-body" x-show="!logMinimized">
        <div class="log-console" x-ref="logConsole">
          <template x-for="(line, i) in datasetLog" :key="i">
            <div class="log-line" x-text="line"></div>
          </template>
        </div>
        <div class="dataset-previews" x-show="datasetPreviews.length > 0">
          <template x-for="(url, i) in datasetPreviews" :key="i">
            <img :src="url" :alt="`Garment ${i + 1}`" loading="lazy">
          </template>
        </div>
      </div>
    </div>
  </div>

  <script>
    function wardrobe() {
      return {
        tab: 'wardrobe',
        loading: false,
        garments: [],
        garmentsById: {},
        dragover: false,

        // Upload
        uploadStatus: { type: '', message: '' },

        // Dataset
        datasetKey: 'second-hand',
        datasetLoading: false,
        datasetStatus: { type: '', message: '' },
        datasetLog: [],
        datasetPreviews: [],
        datasetCount: 0,
        logMinimized: false,

        // Garment detail
        selectedGarment: null,

        // Bounding-box editor
        editorActive: false,
        editorImageUrl: '',
        editorToken: '',
        editorBoxCount: 0,
        editorAutoBoxCount: 0,
        analyzing: false,
        analyzeStatus: { type: '', message: '' },
        anno: null,

        // Outfits
        context: '',
        outfits: [],
        outfitsLoading: false,

        // Chat
        messages: [{ role: 'assistant', text: 'Hi! Ask me anything about your wardrobe — outfit ideas, what to wear for an event, or care tips.' }],
        question: '',
        asking: false,

        async init() {
          await this.waitForClient();
          await this.loadWardrobe();
        },

        async waitForClient() {
          while (!window.gradioClient) {
            await new Promise(r => setTimeout(r, 100));
          }
        },

        async loadWardrobe() {
          this.loading = true;
          try {
            const result = await window.gradioClient.predict("/get_wardrobe", {});
            this.garments = result.data[0].garments || [];
            this.garmentsById = Object.fromEntries(this.garments.map(g => [g.id, g]));
          } catch (e) { console.error(e); }
          this.loading = false;
        },

        async handleFile(file) {
          if (!file) return;
          this.uploadStatus = { type: 'loading', message: 'Detecting garments…' };
          this.editorActive = false;
          this.analyzeStatus = { type: '', message: '' };

          try {
            const { handle_file } = await import("https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js");
            const result = await window.gradioClient.predict("/prepare_image", { image_path: handle_file(file) });
            const data = result.data[0];

            if (data.error) {
              this.uploadStatus = { type: 'error', message: data.error };
              return;
            }

            this.uploadStatus = { type: '', message: '' };
            this.editorToken = data.token;
            this.editorImageUrl = data.image_url;
            this.editorAutoBoxCount = data.boxes.length;
            this.editorBoxCount = data.boxes.length;
            this.editorActive = true;

            this.$nextTick(() => this.initAnnotator(data.boxes, data.width, data.height));
          } catch (e) {
            this.uploadStatus = { type: 'error', message: 'Something went wrong. Please try again.' };
            console.error(e);
          }
        },

        async loadDataset() {
          this.datasetLoading = true;
          this.datasetLog = [];
          this.datasetPreviews = [];
          this.datasetCount = 0;
          this.logMinimized = false;
          this.datasetStatus = { type: 'loading', message: 'Starting...' };

          try {
            const job = window.gradioClient.submit("/load_dataset", { dataset_key: this.datasetKey });
            for await (const event of job) {
              if (event.type !== "data") continue;
              const data = event.data[0];
              if (!data) continue;

              if (Array.isArray(data.log) && data.log.length) {
                this.datasetLog = data.log;
                this.$nextTick(() => {
                  const el = this.$refs.logConsole;
                  if (el) el.scrollTop = el.scrollHeight;
                });
              }
              if (typeof data.count === "number") this.datasetCount = data.count;
              if (data.preview_url && !this.datasetPreviews.includes(data.preview_url)) {
                this.datasetPreviews.push(data.preview_url);
              }

              if (data.done) {
                if (data.error) {
                  this.datasetStatus = { type: 'error', message: data.error };
                } else {
                  this.datasetStatus = { type: 'success', message: `Loaded ${data.count} garments into your wardrobe!` };
                  await this.loadWardrobe();
                }
                break;
              } else {
                this.datasetStatus = {
                  type: 'loading',
                  message: data.count > 0 ? `Processing garments... ${data.count}/50` : 'Downloading dataset...',
                };
              }
            }
          } catch (e) {
            this.datasetStatus = { type: 'error', message: 'Failed to load dataset.' };
            console.error(e);
          }
          this.datasetLoading = false;
        },

        async generateOutfits() {
          this.outfitsLoading = true;
          this.outfits = [];
          try {
            const result = await window.gradioClient.predict("/get_combinations", { context: this.context });
            const data = result.data[0];
            this.outfits = (data.combinations || []).map(c => ({ ...c, rated: false }));
          } catch (e) { console.error(e); }
          this.outfitsLoading = false;
        },

        async rateOutfit(combo, liked) {
          combo.rated = true;
          try {
            await window.gradioClient.predict("/rate_outfit", {
              top_id: combo.top.id,
              bottom_id: combo.bottom.id,
              liked,
            });
          } catch (e) { console.error(e); }
        },

        async askQuestion() {
          const q = this.question.trim();
          if (!q) return;
          this.messages.push({ role: 'user', text: q });
          this.question = '';
          this.asking = true;

          this.$nextTick(() => {
            this.$refs.chatMessages.scrollTop = this.$refs.chatMessages.scrollHeight;
          });

          try {
            if (!this.garments.length) await this.loadWardrobe();
            const result = await window.gradioClient.predict("/ask_question", { question: q });
            this.messages.push({ role: 'assistant', text: result.data[0] });
          } catch (e) {
            this.messages.push({ role: 'assistant', text: 'Sorry, something went wrong. Please try again.' });
            console.error(e);
          }
          this.asking = false;

          this.$nextTick(() => {
            this.$refs.chatMessages.scrollTop = this.$refs.chatMessages.scrollHeight;
          });
        },

        normalizeGarmentId(num) {
          const n = parseInt(num, 10);
          return Number.isNaN(n) ? `garment_${num}` : `garment_${String(n).padStart(3, '0')}`;
        },

        buildGarmentChip(id) {
          const g = this.garmentsById[id];
          if (!g) return `<span class="garment-ref">${id}</span>`;
          const label = g.description
            ? (g.description.length > 72 ? g.description.slice(0, 72) + '…' : g.description)
            : `${g.color} ${g.type}`;
          return `<span class="garment-inline">
            <img src="${g.image_url}" alt="${this.escapeHtml(g.type)}">
            <span>${this.escapeHtml(label)}</span>
          </span>`;
        },

        renderMessage(text) {
          const chips = [];
          const toPlaceholder = (id) => {
            const idx = chips.length;
            chips.push(this.buildGarmentChip(id));
            return `GARMENTCHIP${idx}END`;
          };

          let raw = text.replace(/\[garment_(\d+)\]/gi, (_, num) => toPlaceholder(this.normalizeGarmentId(num)));
          raw = raw.replace(/\bgarment_(\d+)\b/gi, (_, num) => toPlaceholder(this.normalizeGarmentId(num)));

          let html = marked.parse(raw);
          chips.forEach((chip, i) => {
            html = html.replaceAll(`GARMENTCHIP${i}END`, chip);
            html = html.replaceAll(`<p>GARMENTCHIP${i}END</p>`, `<p>${chip}</p>`);
          });
          return html;
        },

        escapeHtml(str) {
          const div = document.createElement('div');
          div.textContent = str;
          return div.innerHTML;
        },

        initAnnotator(boxes, imgW, imgH) {
          // Destroy any previous instance
          if (this.anno) {
            try { this.anno.destroy(); } catch (_) {}
            this.anno = null;
          }

          const factory = window.annotoriousFactory;
          if (!factory) {
            console.error('Annotorious not loaded yet');
            return;
          }

          const el = document.getElementById('annoImage');
          if (!el) return;

          const start = () => {
            this.anno = factory(el, {
              drawingEnabled: true,
              style: { fill: '#3b82f6', fillOpacity: 0.15, stroke: '#3b82f6', strokeWidth: 2 },
            });
            this.anno.setDrawingTool('rectangle');

            // Load auto-detected boxes using Annotorious v3 internal format
            if (boxes.length > 0) {
              const annotations = boxes.map((b, i) => ({
                id: `auto-${i}`,
                bodies: [],
                target: {
                  selector: {
                    type: 'RECTANGLE',
                    geometry: {
                      x: b.x, y: b.y, w: b.w, h: b.h,
                      bounds: { minX: b.x, minY: b.y, maxX: b.x + b.w, maxY: b.y + b.h },
                    },
                  },
                },
              }));
              this.anno.setAnnotations(annotations);
            }

            // Keep editorBoxCount in sync
            const updateCount = () => {
              this.editorBoxCount = this.anno.getAnnotations().length;
            };
            this.anno.on('createAnnotation', updateCount);
            this.anno.on('deleteAnnotation', updateCount);
            this.anno.on('updateAnnotation', updateCount);
          };

          // Wait for the image to be loaded before creating the annotator
          if (el.complete && el.naturalWidth > 0) {
            start();
          } else {
            el.addEventListener('load', start, { once: true });
          }
        },

        async analyzeBoxes() {
          if (!this.anno || this.editorBoxCount === 0) return;
          this.analyzing = true;
          this.analyzeStatus = { type: 'loading', message: 'Extracting garment attributes…' };

          // Map Annotorious v3 RECTANGLE geometry to {x,y,w,h} pixel boxes
          const annotations = this.anno.getAnnotations();
          const boxes = annotations.map(ann => {
            const geom = ann.target?.selector?.geometry;
            if (geom && geom.w > 0 && geom.h > 0) {
              return { x: Math.round(geom.x), y: Math.round(geom.y), w: Math.round(geom.w), h: Math.round(geom.h) };
            }
            return null;
          }).filter(Boolean);

          if (boxes.length === 0) {
            this.analyzeStatus = { type: 'error', message: 'No valid boxes to analyze.' };
            this.analyzing = false;
            return;
          }

          try {
            const result = await window.gradioClient.predict("/analyze_boxes", {
              token: this.editorToken,
              boxes: JSON.stringify(boxes),
            });
            const data = result.data[0];
            if (data.error) {
              this.analyzeStatus = { type: 'error', message: data.error };
            } else {
              this.analyzeStatus = { type: 'success', message: `Added ${data.count} garment${data.count !== 1 ? 's' : ''} to your wardrobe!` };
              await this.loadWardrobe();
              this.cancelEditor();
            }
          } catch (e) {
            this.analyzeStatus = { type: 'error', message: 'Something went wrong. Please try again.' };
            console.error(e);
          }
          this.analyzing = false;
        },

        cancelEditor() {
          if (this.anno) {
            try { this.anno.destroy(); } catch (_) {}
            this.anno = null;
          }
          this.editorActive = false;
          this.editorToken = '';
          this.editorImageUrl = '';
          this.editorBoxCount = 0;
          this.editorAutoBoxCount = 0;
        },

        selectGarment(g) {
          this.selectedGarment = g;
        },

        closeDetail() {
          this.selectedGarment = null;
        },
      };
    }
  </script>
</body>
</html>