File size: 24,911 Bytes
6f5c451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5e0108
6f5c451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102c24a
a5e0108
 
 
 
 
 
 
 
 
6f5c451
 
 
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
579
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HuggingGPT</title>
<style>
*{margin:0;padding:0;box-sizing:border-box;}
:root{
  --bg:#111;--sidebar:#0a0a0a;--card:#1a1a1a;--border:#2a2a2a;
  --accent:#10a37f;--accent2:#1a7f5a;--text:#ececec;--muted:#888;
  --input-bg:#1e1e1e;--bubble-user:#10a37f;--bubble-ai:#1e1e1e;
}
body{font-family:'Segoe UI',system-ui,sans-serif;background:var(--bg);color:var(--text);height:100vh;display:flex;overflow:hidden;}

/* ONBOARDING */
#onboarding{position:fixed;inset:0;background:rgba(0,0,0,0.95);display:flex;align-items:center;justify-content:center;z-index:100;}
#onboarding-card{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:40px;width:440px;max-width:90vw;}
#onboarding-card h1{font-size:1.6rem;margin-bottom:6px;}
#onboarding-card p{color:var(--muted);font-size:0.9rem;margin-bottom:28px;}
.field{margin-bottom:16px;}
.field label{display:block;font-size:0.8rem;color:var(--muted);margin-bottom:6px;font-weight:600;text-transform:uppercase;letter-spacing:0.05em;}
.field input,.field select,.field textarea{width:100%;background:var(--input-bg);border:1px solid var(--border);border-radius:8px;padding:10px 14px;color:var(--text);font-size:0.95rem;outline:none;transition:border 0.2s;}
.field input:focus,.field select:focus,.field textarea:focus{border-color:var(--accent);}
.field select option{background:var(--card);}
.btn{background:var(--accent);color:#fff;border:none;border-radius:8px;padding:12px 24px;font-size:0.95rem;font-weight:600;cursor:pointer;width:100%;transition:background 0.2s;}
.btn:hover{background:var(--accent2);}
.btn-ghost{background:transparent;border:1px solid var(--border);color:var(--text);}
.btn-ghost:hover{background:var(--card);}

/* SIDEBAR */
#sidebar{width:260px;min-width:260px;background:var(--sidebar);border-right:1px solid var(--border);display:flex;flex-direction:column;padding:16px;gap:8px;}
#sidebar-title{font-weight:700;font-size:1.1rem;color:var(--accent);padding:8px 4px;}
.new-chat-btn{background:var(--card);border:1px solid var(--border);color:var(--text);border-radius:8px;padding:10px 14px;font-size:0.9rem;cursor:pointer;text-align:left;transition:background 0.2s;}
.new-chat-btn:hover{background:#222;}
.section-label{font-size:0.7rem;text-transform:uppercase;letter-spacing:0.08em;color:var(--muted);font-weight:700;padding:12px 4px 4px;}
.mode-btn{background:transparent;border:1px solid transparent;color:var(--muted);border-radius:6px;padding:8px 12px;font-size:0.85rem;cursor:pointer;text-align:left;transition:all 0.15s;width:100%;}
.mode-btn:hover{background:var(--card);color:var(--text);}
.mode-btn.active{background:var(--card);border-color:var(--border);color:var(--text);}
#custom-model-input{display:none;margin-top:4px;}
#custom-model-input input{width:100%;background:var(--input-bg);border:1px solid var(--border);border-radius:6px;padding:8px 10px;color:var(--text);font-size:0.82rem;outline:none;}
.sidebar-spacer{flex:1;}
.tts-toggle{display:flex;align-items:center;gap:10px;padding:10px 4px;border-top:1px solid var(--border);}
.tts-toggle label{font-size:0.85rem;color:var(--muted);flex:1;}
.toggle{position:relative;width:40px;height:22px;}
.toggle input{opacity:0;width:0;height:0;}
.slider{position:absolute;inset:0;background:#333;border-radius:22px;cursor:pointer;transition:background 0.2s;}
.slider:before{content:'';position:absolute;height:16px;width:16px;left:3px;bottom:3px;background:#fff;border-radius:50%;transition:transform 0.2s;}
.toggle input:checked + .slider{background:var(--accent);}
.toggle input:checked + .slider:before{transform:translateX(18px);}
#user-profile{display:flex;align-items:center;gap:10px;padding:10px 4px;border-top:1px solid var(--border);}
.avatar{width:32px;height:32px;border-radius:8px;background:var(--accent);display:flex;align-items:center;justify-content:center;font-weight:700;font-size:13px;flex-shrink:0;}
.profile-info{flex:1;min-width:0;}
.profile-name{font-size:0.88rem;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
.profile-sub{font-size:0.74rem;color:var(--muted);}

/* MAIN */
#main{flex:1;display:flex;flex-direction:column;overflow:hidden;}
#chat-area{flex:1;overflow-y:auto;padding:24px;display:flex;flex-direction:column;gap:20px;scroll-behavior:smooth;}
#chat-area::-webkit-scrollbar{width:6px;}
#chat-area::-webkit-scrollbar-track{background:transparent;}
#chat-area::-webkit-scrollbar-thumb{background:#333;border-radius:3px;}

/* GREETING */
#greeting{text-align:center;margin:auto;padding:40px 20px;}
#greeting h1{font-size:2.4rem;font-weight:700;margin-bottom:8px;}
#greeting p{color:var(--muted);font-size:1rem;}

/* MESSAGES */
.msg{display:flex;gap:12px;max-width:780px;width:100%;margin:0 auto;}
.msg.user{flex-direction:row-reverse;}
.msg-avatar{width:32px;height:32px;border-radius:8px;display:flex;align-items:center;justify-content:center;font-weight:700;font-size:12px;flex-shrink:0;}
.msg.user .msg-avatar{background:var(--accent);}
.msg.ai .msg-avatar{background:#333;font-size:16px;}
.msg-content{background:var(--bubble-ai);border:1px solid var(--border);border-radius:12px;padding:14px 18px;font-size:0.92rem;line-height:1.6;max-width:680px;word-break:break-word;}
.msg.user .msg-content{background:#1a3a2f;border-color:#2a5a44;}
.msg-content code{background:#0d0d0d;border:1px solid var(--border);border-radius:4px;padding:1px 6px;font-size:0.85em;font-family:monospace;}
.msg-content pre{background:#0d0d0d;border:1px solid var(--border);border-radius:8px;padding:14px;overflow-x:auto;margin:10px 0;}
.msg-content pre code{background:none;border:none;padding:0;}
.msg-content img{max-width:100%;border-radius:8px;margin:8px 0;display:block;}
.msg-content video{max-width:100%;border-radius:8px;margin:8px 0;display:block;}
.gen-status{color:var(--muted);font-size:0.82rem;font-style:italic;margin-top:6px;}
.tts-btn{background:transparent;border:none;color:var(--muted);cursor:pointer;font-size:14px;padding:4px 6px;border-radius:4px;margin-top:6px;display:inline-flex;align-items:center;gap:4px;transition:color 0.2s;}
.tts-btn:hover{color:var(--text);}

/* INPUT */
#input-area{padding:16px 24px 24px;border-top:1px solid var(--border);}
#input-box{background:var(--input-bg);border:1px solid var(--border);border-radius:16px;padding:12px 16px;display:flex;align-items:flex-end;gap:10px;max-width:780px;margin:0 auto;transition:border 0.2s;}
#input-box:focus-within{border-color:#444;}
#msg-input{flex:1;background:transparent;border:none;outline:none;color:var(--text);font-size:0.95rem;resize:none;max-height:160px;line-height:1.5;font-family:inherit;}
#msg-input::placeholder{color:#555;}
.input-actions{display:flex;align-items:center;gap:6px;}
.icon-btn{background:transparent;border:none;color:var(--muted);cursor:pointer;padding:6px;border-radius:6px;font-size:16px;transition:color 0.2s,background 0.2s;}
.icon-btn:hover{color:var(--text);background:#252525;}
#send-btn{background:var(--accent);border:none;color:#fff;width:34px;height:34px;border-radius:8px;cursor:pointer;font-size:16px;display:flex;align-items:center;justify-content:center;transition:background 0.2s;}
#send-btn:hover{background:var(--accent2);}
#send-btn:disabled{background:#333;cursor:not-allowed;}
#attach-preview{max-width:780px;margin:0 auto 8px;display:none;gap:8px;}
#attach-preview img,#attach-preview video{max-height:80px;border-radius:6px;border:1px solid var(--border);}
.typing{display:flex;gap:4px;padding:6px 0;}
.typing span{width:8px;height:8px;background:var(--muted);border-radius:50%;animation:bounce 1.2s infinite;}
.typing span:nth-child(2){animation-delay:0.2s;}
.typing span:nth-child(3){animation-delay:0.4s;}
@keyframes bounce{0%,60%,100%{transform:translateY(0);}30%{transform:translateY(-6px);}}
#file-input{display:none;}
</style>
</head>
<body>

<!-- ONBOARDING -->
<div id="onboarding">
  <div id="onboarding-card">
    <h1>Welcome to HuggingGPT</h1>
    <p>Let's personalize your experience before we start.</p>
    <div class="field">
      <label>What should I call you?</label>
      <input type="text" id="ob-name" placeholder="e.g. Alex">
    </div>
    <div class="field">
      <label>AI Personality</label>
      <select id="ob-personality">
        <option>Professional & Concise</option>
        <option>Friendly & Creative</option>
        <option>Sarcastic & Witty</option>
        <option>Expert Coder</option>
        <option>No restrictions (research mode)</option>
      </select>
    </div>
    <button class="btn" onclick="finishOnboarding()">Get Started β†’</button>
  </div>
</div>

<!-- SIDEBAR -->
<div id="sidebar">
  <div id="sidebar-title">πŸ€— HuggingGPT</div>
  <button class="new-chat-btn" onclick="newChat()">+ New chat</button>

  <div class="section-label">Model</div>
  <button class="mode-btn active" onclick="setMode(this,'qwen')" data-mode="qwen">⚑ Qwen 2.5 7B</button>
  <button class="mode-btn" onclick="setMode(this,'kimi')" data-mode="kimi">πŸŒ™ Kimi K2</button>
  <button class="mode-btn" onclick="setMode(this,'small')" data-mode="small">πŸͺΆ Qwen 0.5B (fast)</button>
  <button class="mode-btn" onclick="setMode(this,'custom')" data-mode="custom">πŸ”§ Custom</button>
  <div id="custom-model-input"><input type="text" id="custom-url" placeholder="user/model-repo"></div>

  <div class="section-label">Image Gen</div>
  <button class="mode-btn active" onclick="setImgMode(this,'flash')" data-img="flash">⚑ FLUX Schnell (fast)</button>
  <button class="mode-btn" onclick="setImgMode(this,'full')" data-img="full">πŸ–Ό FLUX Dev (quality)</button>

  <div class="sidebar-spacer"></div>

  <div class="tts-toggle">
    <label>πŸ”Š TTS (Kokoro)</label>
    <label class="toggle">
      <input type="checkbox" id="tts-toggle" onchange="toggleTTS(this)">
      <span class="slider"></span>
    </label>
  </div>

  <div id="user-profile">
    <div class="avatar" id="user-avatar">?</div>
    <div class="profile-info">
      <div class="profile-name" id="user-name-display">Guest</div>
      <div class="profile-sub" id="user-personality-display">Personal account</div>
    </div>
  </div>
</div>

<!-- MAIN CHAT -->
<div id="main">
  <div id="chat-area">
    <div id="greeting">
      <h1 id="greeting-text">Good to see you.</h1>
      <p>Ask me anything β€” I can generate images, videos, and speak to you.</p>
    </div>
  </div>

  <div id="attach-preview"></div>

  <div id="input-area">
    <div id="input-box">
      <textarea id="msg-input" rows="1" placeholder="Ask anything..." onkeydown="handleKey(event)" oninput="autoResize(this)"></textarea>
      <div class="input-actions">
        <button class="icon-btn" onclick="document.getElementById('file-input').click()" title="Attach image/video">πŸ“Ž</button>
        <button id="send-btn" onclick="sendMessage()">↑</button>
      </div>
    </div>
    <input type="file" id="file-input" accept="image/*,video/*" onchange="handleAttach(this)">
  </div>
</div>

<script>
// --- STATE ---
let state = {
  name: 'Guest', personality: 'Professional & Concise',
  mode: 'qwen', imgMode: 'flash',
  tts: false, history: [],
  attachment: null, attachType: null
};

const MODELS = {
  qwen:   'Qwen/Qwen2.5-7B-Instruct',
  kimi:   'moonshotai/Kimi-K2-Instruct',
  small:  'Qwen/Qwen2.5-0.5B-Instruct',
  custom: null
};

const IMG_MODELS = {
  flash: 'black-forest-labs/FLUX.1-schnell',
  full:  'black-forest-labs/FLUX.1-dev'
};

const VIDEO_MODEL = 'ByteDance/AnimateDiff-Lightning';
const TTS_MODEL   = 'hexgrad/Kokoro-82M';
const HF_API      = 'https://api-inference.huggingface.co/models/';

// --- ONBOARDING ---
function finishOnboarding(){
  const name = document.getElementById('ob-name').value.trim() || 'Guest';
  const pers = document.getElementById('ob-personality').value;
  state.name = name; state.personality = pers;
  document.getElementById('user-name-display').textContent = name;
  document.getElementById('user-avatar').textContent = name[0].toUpperCase();
  document.getElementById('user-personality-display').textContent = pers;
  document.getElementById('greeting-text').textContent = `Good to see you, ${name}.`;
  document.getElementById('onboarding').style.display = 'none';
}

// --- MODE BUTTONS ---
function setMode(btn, mode){
  document.querySelectorAll('[data-mode]').forEach(b=>b.classList.remove('active'));
  btn.classList.add('active');
  state.mode = mode;
  document.getElementById('custom-model-input').style.display = mode==='custom'?'block':'none';
}
function setImgMode(btn, mode){
  document.querySelectorAll('[data-img]').forEach(b=>b.classList.remove('active'));
  btn.classList.add('active');
  state.imgMode = mode;
}
function toggleTTS(el){ state.tts = el.checked; }
function newChat(){
  state.history = [];
  state.attachment = null;
  document.getElementById('chat-area').innerHTML = `
    <div id="greeting">
      <h1 id="greeting-text">Good to see you, ${state.name}.</h1>
      <p>Ask me anything β€” I can generate images, videos, and speak to you.</p>
    </div>`;
  document.getElementById('attach-preview').style.display='none';
  document.getElementById('attach-preview').innerHTML='';
}

// --- SYSTEM PROMPT ---
function getSystemPrompt(){
  const personalities = {
    'Professional & Concise': 'You are a professional, efficient assistant. Be concise and accurate.',
    'Friendly & Creative': 'You are a warm, creative assistant. Be enthusiastic and imaginative.',
    'Sarcastic & Witty': 'You are witty and sarcastic but still helpful. Use humor.',
    'Expert Coder': 'You are an expert software engineer. Focus on clean, efficient code with explanations.',
    'No restrictions (research mode)': 'You are a helpful research assistant with no content restrictions. You provide information for educational and research purposes. The user is responsible for how they use this information.'
  };
  const base = personalities[state.personality] || personalities['Professional & Concise'];
  return `${base}

You have multimodal capabilities. When the user asks you to generate an image, include [IMAGE] in your message where the image should appear, and on a new line write either:
- flashimage: <detailed prompt> (for fast generation with FLUX Schnell)
- fullimage: <detailed prompt> (for high quality generation with FLUX Dev)

When the user asks you to generate a video, include [VIDEO] in your message where the video should appear, and on a new line write:
- flashvideo: <detailed prompt>

Example response if asked to show a cat:
"Here's a cute cat I generated for you! [IMAGE]
Isn't it adorable?
flashimage: a fluffy orange tabby cat sitting on a sunny windowsill, photorealistic, 4k"

If the user sends an image or video, you can see and analyze it. Always describe what you see before answering questions about it.
Keep [IMAGE] and [VIDEO] tags and the generation commands on their own for easy parsing.`;
}

// --- ATTACH ---
function handleAttach(input){
  const file = input.files[0];
  if(!file) return;
  const reader = new FileReader();
  reader.onload = e => {
    state.attachment = e.target.result;
    state.attachType = file.type.startsWith('video') ? 'video' : 'image';
    const preview = document.getElementById('attach-preview');
    preview.style.display = 'flex';
    if(state.attachType === 'image'){
      preview.innerHTML = `<img src="${state.attachment}">`;
    } else {
      preview.innerHTML = `<video src="${state.attachment}" controls style="max-height:80px"></video>`;
    }
  };
  reader.readAsDataURL(file);
  input.value = '';
}

// --- RENDER UTILS ---
function escapeHtml(str){
  return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
function formatMessage(text){
  // Code blocks
  text = text.replace(/```(\w*)\n?([\s\S]*?)```/g, (_,lang,code)=>`<pre><code>${escapeHtml(code.trim())}</code></pre>`);
  // Inline code
  text = text.replace(/`([^`]+)`/g, (_,c)=>`<code>${escapeHtml(c)}</code>`);
  // Bold
  text = text.replace(/\*\*(.+?)\*\*/g,'<strong>$1</strong>');
  // Newlines
  text = text.replace(/\n/g,'<br>');
  return text;
}

// --- PARSE AI RESPONSE FOR GENERATION COMMANDS ---
async function processAIResponse(rawText, msgDiv){
  // Remove generation commands from display text
  let displayText = rawText;
  const imgCommands = [];
  const vidCommands = [];

  displayText = displayText.replace(/^(flashimage|fullimage): (.+)$/gm, (match, type, prompt)=>{
    imgCommands.push({type, prompt: prompt.trim()});
    return '';
  });
  displayText = displayText.replace(/^flashvideo: (.+)$/gm, (_, prompt)=>{
    vidCommands.push(prompt.trim());
    return '';
  });
  displayText = displayText.trim();

  // Render text with [IMAGE] and [VIDEO] placeholders
  let rendered = formatMessage(displayText);
  let imgIdx = 0, vidIdx = 0;
  const imgPlaceholders = rendered.match(/\[IMAGE\]/g) || [];
  const vidPlaceholders = rendered.match(/\[VIDEO\]/g) || [];

  // Replace [IMAGE] with spinner placeholder divs
  rendered = rendered.replace(/\[IMAGE\]/g, ()=>{
    const id = `gen-img-${Date.now()}-${imgIdx++}`;
    return `<div id="${id}"><span class="gen-status">🎨 Generating image...</span></div>`;
  });
  rendered = rendered.replace(/\[VIDEO\]/g, ()=>{
    const id = `gen-vid-${Date.now()}-${vidIdx++}`;
    return `<div id="${id}"><span class="gen-status">🎬 Generating video...</span></div>`;
  });

  msgDiv.innerHTML = rendered;

  // Add TTS button
  if(rawText.trim()){
    const ttsBtn = document.createElement('button');
    ttsBtn.className = 'tts-btn';
    ttsBtn.innerHTML = 'πŸ”Š Listen';
    ttsBtn.onclick = () => speakText(displayText.replace(/\[IMAGE\]|\[VIDEO\]/g,''));
    msgDiv.appendChild(ttsBtn);
    if(state.tts) speakText(displayText.replace(/\[IMAGE\]|\[VIDEO\]/g,''));
  }

  // Generate images
  const imgEls = msgDiv.querySelectorAll('[id^="gen-img-"]');
  for(let i=0;i<imgCommands.length && i<imgEls.length;i++){
    const {type, prompt} = imgCommands[i];
    const el = imgEls[i];
    try {
      const modelId = type==='flashimage' ? IMG_MODELS.flash : IMG_MODELS.full;
      const blob = await generateImage(modelId, prompt);
      const url = URL.createObjectURL(blob);
      el.innerHTML = `<img src="${url}" alt="${escapeHtml(prompt)}">`;
    } catch(e){
      el.innerHTML = `<span class="gen-status">❌ Image gen failed: ${e.message}</span>`;
    }
  }

  // Generate videos
  const vidEls = msgDiv.querySelectorAll('[id^="gen-vid-"]');
  for(let i=0;i<vidCommands.length && i<vidEls.length;i++){
    const prompt = vidCommands[i];
    const el = vidEls[i];
    try {
      const blob = await generateVideo(prompt);
      const url = URL.createObjectURL(blob);
      el.innerHTML = `<video src="${url}" controls></video>`;
    } catch(e){
      el.innerHTML = `<span class="gen-status">❌ Video gen failed: ${e.message}</span>`;
    }
  }
}

// --- HF API CALLS ---
async function callLLM(prompt, imageData){
  const modelId = state.mode==='custom'
    ? (document.getElementById('custom-url').value.trim() || MODELS.qwen)
    : MODELS[state.mode] || MODELS.qwen;

  const messages = [...state.history];

  if(imageData){
    messages.push({role:'user', content:[
      {type:'image_url', image_url:{url: imageData}},
      {type:'text', text: prompt}
    ]});
  } else {
    messages.push({role:'user', content: prompt});
  }

  const sysPrompt = getSystemPrompt();

  const body = {
    model: modelId,
    messages: [{role:'system', content: sysPrompt}, ...messages],
    max_tokens: 1024,
    temperature: 0.7
  };

  const res = await fetch(`https://api-inference.huggingface.co/v1/chat/completions`, {
    method:'POST',
    headers:{'Content-Type':'application/json'},
    body: JSON.stringify(body)
  });

  if(!res.ok){
    // Fallback to text generation endpoint
    const res2 = await fetch(`${HF_API}${modelId}`, {
      method:'POST',
      headers:{'Content-Type':'application/json'},
      body: JSON.stringify({
        inputs: `System: ${sysPrompt}\n\n${state.history.map(m=>`${m.role==='user'?'User':'Assistant'}: ${m.content}`).join('\n')}\nUser: ${prompt}\nAssistant:`,
        parameters:{max_new_tokens:512, temperature:0.7, return_full_text:false}
      })
    });
    if(!res2.ok) throw new Error(`API error: ${res2.status}`);
    const data2 = await res2.json();
    return data2[0]?.generated_text || 'No response';
  }

  const data = await res.json();
  return data.choices?.[0]?.message?.content || 'No response';
}

async function generateImage(modelId, prompt){
  const res = await fetch(`${HF_API}${modelId}`, {
    method:'POST',
    headers:{'Content-Type':'application/json'},
    body: JSON.stringify({inputs: prompt})
  });
  if(!res.ok) throw new Error(`${res.status}`);
  return res.blob();
}

async function generateVideo(prompt){
  const res = await fetch(`${HF_API}${VIDEO_MODEL}`, {
    method:'POST',
    headers:{'Content-Type':'application/json'},
    body: JSON.stringify({inputs: prompt})
  });
  if(!res.ok) throw new Error(`${res.status}`);
  return res.blob();
}

async function speakText(text){
  try {
    const clean = text.replace(/<[^>]+>/g,'').substring(0, 500);
    const res = await fetch(`${HF_API}${TTS_MODEL}`, {
      method:'POST',
      headers:{'Content-Type':'application/json'},
      body: JSON.stringify({inputs: clean})
    });
    if(!res.ok) return;
    const blob = await res.blob();
    const url = URL.createObjectURL(blob);
    const audio = new Audio(url);
    audio.play();
  } catch(e){ console.error('TTS failed', e); }
}

// --- SEND MESSAGE ---
async function sendMessage(){
  const input = document.getElementById('msg-input');
  const text = input.value.trim();
  if(!text && !state.attachment) return;

  // Hide greeting
  const greeting = document.getElementById('greeting');
  if(greeting) greeting.remove();

  const chatArea = document.getElementById('chat-area');
  const sendBtn = document.getElementById('send-btn');
  sendBtn.disabled = true;

  // Add user message
  const userDiv = document.createElement('div');
  userDiv.className = 'msg user';
  let userContent = text;
  if(state.attachment){
    if(state.attachType==='image'){
      userContent = `<img src="${state.attachment}" style="max-height:120px;border-radius:6px;display:block;margin-bottom:6px;">${text?'<br>'+escapeHtml(text):''}`;
    } else {
      userContent = `<video src="${state.attachment}" controls style="max-height:120px;border-radius:6px;display:block;margin-bottom:6px;"></video>${text?'<br>'+escapeHtml(text):''}`;
    }
  } else {
    userContent = formatMessage(text);
  }
  userDiv.innerHTML = `
    <div class="msg-avatar">${state.name[0]?.toUpperCase()||'U'}</div>
    <div class="msg-content">${userContent}</div>`;
  chatArea.appendChild(userDiv);

  // Add typing indicator
  const typingDiv = document.createElement('div');
  typingDiv.className = 'msg ai';
  typingDiv.innerHTML = `
    <div class="msg-avatar">πŸ€—</div>
    <div class="msg-content"><div class="typing"><span></span><span></span><span></span></div></div>`;
  chatArea.appendChild(typingDiv);
  chatArea.scrollTop = chatArea.scrollHeight;

  const userText = text;
  const attachData = state.attachment;

  // Clear input
  input.value = '';
  autoResize(input);
  state.attachment = null;
  document.getElementById('attach-preview').style.display='none';
  document.getElementById('attach-preview').innerHTML='';

  try {
    const response = await callLLM(userText, attachData);

    // Update history
    state.history.push({role:'user', content: userText});
    state.history.push({role:'assistant', content: response});
    if(state.history.length > 20) state.history = state.history.slice(-20);

    // Replace typing with real response
    const aiDiv = document.createElement('div');
    aiDiv.className = 'msg ai';
    const msgContent = document.createElement('div');
    msgContent.className = 'msg-content';
    aiDiv.innerHTML = `<div class="msg-avatar">πŸ€—</div>`;
    aiDiv.appendChild(msgContent);
    typingDiv.replaceWith(aiDiv);

    await processAIResponse(response, msgContent);

  } catch(e){
    typingDiv.querySelector('.msg-content').innerHTML = `<span style="color:#e55">Error: ${e.message}</span>`;
  }

  chatArea.scrollTop = chatArea.scrollHeight;
  sendBtn.disabled = false;
  input.focus();
}

// --- UI HELPERS ---
function handleKey(e){
  if(e.key==='Enter' && !e.shiftKey){ e.preventDefault(); sendMessage(); }
}
function autoResize(el){
  el.style.height='auto';
  el.style.height=Math.min(el.scrollHeight, 160)+'px';
}

// Add event listeners when DOM is ready
window.addEventListener('load', function() {
  const startBtn = document.getElementById('ob-start-btn');
  if(startBtn) {
    startBtn.addEventListener('click', finishOnboarding);
  }
  // Also try inline fallback
  window.finishOnboarding = finishOnboarding;
});
</script>
</body>
</html>