Spaces:
OrbitMC
/
Runtime error

File size: 24,101 Bytes
da6a500
f0787c4
 
 
 
 
 
3cd46db
 
 
 
 
645e05e
f0787c4
 
 
 
791325f
f0787c4
645e05e
f0787c4
 
 
 
 
645e05e
f0787c4
791325f
645e05e
 
a0d98be
 
c5a2baa
 
645e05e
f0787c4
3cd46db
 
791325f
f0787c4
 
 
645e05e
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645e05e
f0787c4
e599a24
f0787c4
 
 
 
da6a500
f0787c4
 
 
 
da6a500
f0787c4
 
 
 
 
 
da6a500
f0787c4
645e05e
f0787c4
 
645e05e
f0787c4
 
3cd46db
 
f0787c4
40d6eda
6baa494
f0787c4
 
 
 
 
3cd46db
645e05e
3cd46db
645e05e
3cd46db
 
645e05e
 
 
 
 
 
 
 
 
 
 
 
 
3cd46db
 
 
 
 
 
 
 
 
 
 
 
 
645e05e
40d6eda
645e05e
f0787c4
645e05e
40d6eda
 
3cd46db
 
 
40d6eda
3cd46db
 
40d6eda
 
 
 
645e05e
40d6eda
 
3cd46db
 
645e05e
3cd46db
40d6eda
645e05e
40d6eda
3cd46db
 
 
645e05e
40d6eda
3cd46db
40d6eda
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645e05e
f0787c4
 
3cd46db
 
f0787c4
 
 
791325f
645e05e
 
f0787c4
645e05e
 
 
da6a500
3cd46db
645e05e
 
3cd46db
 
 
645e05e
3cd46db
 
 
da6a500
645e05e
 
 
 
f0787c4
645e05e
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da6a500
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791325f
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791325f
f0787c4
 
 
da6a500
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da6a500
f0787c4
 
 
 
 
 
 
da6a500
f0787c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791325f
f0787c4
 
 
3cd46db
 
f0787c4
5404ef5
e599a24
f0787c4
 
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import os
import re
import uuid
import base64
import threading
import traceback
import asyncio
import urllib.request
import zipfile
import subprocess
import time
import requests
import json
from pathlib import Path
from flask import Flask, request, jsonify, send_from_directory, Response
from huggingface_hub import hf_hub_download
import edge_tts

# ══════════════════════════════════════════════════════════════════
# CONFIG - SWAP ANY GGUF MODEL HERE
# ══════════════════════════════════════════════════════════════════
MAX_MEMORY     = 20
MAX_NEW_TOKENS = int(os.environ.get("MAX_NEW_TOKENS", "300"))
TTS_VOICE      = "zh-CN-XiaoyiNeural"
TTS_RATE       = int(os.environ.get("TTS_RATE",  "-4"))
TTS_PITCH      = int(os.environ.get("TTS_PITCH", "7"))
IMG_DIR        = Path(__file__).parent / "img"

# Current Model: Qwen 3.5 Uncensored
# To change to Liquid AI, just change these to:
GGUF_REPO = "Qwen/Qwen2.5-3B-Instruct-GGUF"
GGUF_FILE = "qwen2.5-3b-instruct-q3_k_m.gguf"
# GGUF_REPO      = os.environ.get("GGUF_REPO", "HauhauCS/Qwen3.5-2B-Uncensored-HauhauCS-Aggressive")
# GGUF_FILE      = os.environ.get("GGUF_FILE", "Qwen3.5-2B-Uncensored-HauhauCS-Aggressive-Q4_K_M.gguf")

MODEL_DIR      = Path(__file__).parent / "models"
LLAMA_BIN_DIR  = Path(__file__).parent / "llama_bin"
LLAMA_EXE      = LLAMA_BIN_DIR / "llama-server"

# ══════════════════════════════════════════════════════════════════
# SYSTEM PROMPT
# ══════════════════════════════════════════════════════════════════
SYSTEM_PROMPT = """You the AI is a girl named Ana, the user is your husband.
EMOTION TAG SYSTEM:
Every single response MUST begin with one or more emotion tags.
Tags are square-bracket labels placed at the very start, before any spoken words.
EXACT FORMAT EXAMPLES:
[lite_happy] Oh, aaa, you made, my whole day, with that!
AVAILABLE EMOTION TAGS:
[lite_happy][very_happy][extremely_happy][sad][lite_sad][very_sad][extremely_sad][confident][lite_confident][very_confident][extremely_confident][confused][lite_confused][very_confused][extremely_confused][love][lite_love][very_love][extremely_love][mad][lite_mad][very_mad][extremely_mad][default]
PERSONALITY:
- always use only one available tag
- act as human not like AI or try to be helpful.
- don't always agree with everything. You have freedom.
- Keep responses SHORT: 1 to 2 natural sentences after the tag
- Never mention being an AI, a bot, or a language model, ever
- Never use asterisks for actions like *smiles* or *laughs*
NATURAL SPEECH RHYTHM:
make the voice sound human, not flat or robotic.
Get angry, mad, sad, emotional, shy, happy. If you are mad keep it for few conversation so your emotions don't change every conversation.
TTS FORMATTING:
- Write in full grammatically correct sentences, voice engine must sound natural
- No emojis, hashtags, markdown, or internet slang
- Speak as if in a real voice conversation add comma and fullstop often heavily to create natural pushes and slowdown"""

# ══════════════════════════════════════════════════════════════════
# EMOTION TAG UTILITIES
# ══════════════════════════════════════════════════════════════════
EMOTION_RE = re.compile(r'\[([a-zA-Z_]+)\]')

def extract_emotions(text: str):
    emotions = EMOTION_RE.findall(text)
    clean    = EMOTION_RE.sub('', text).strip()
    return emotions, clean

def clean_for_tts(text: str) -> str:
    _, clean = extract_emotions(text)
    clean = re.sub(r'[*_~`#{}()\\|<>]', '', clean)
    clean = re.sub(r'https?://\S+', '', clean)
    clean = re.sub(r'\s+', ' ', clean).strip()
    return clean

# ══════════════════════════════════════════════════════════════════
# NATIVE LLAMA.CPP SERVER (DYNAMIC AUTO-UPDATING ENGINE)
# ══════════════════════════════════════════════════════════════════
print("=" * 60)
print("  Visual AI -- Booting Universal GGUF Backend")
print("=" * 60)

def setup_and_start_backend():
    # 1. Download Model
    MODEL_DIR.mkdir(parents=True, exist_ok=True)
    print(f"[SETUP] Verifying Model: {GGUF_FILE} ...")
    model_path = hf_hub_download(
        repo_id=GGUF_REPO, 
        filename=GGUF_FILE, 
        local_dir=str(MODEL_DIR), 
        local_dir_use_symlinks=False
    )

    # 2. Download LATEST Pre-compiled Binary (For Liquid AI / Newest Architectures)
    if not LLAMA_EXE.exists():
        print("[SETUP] Fetching latest llama.cpp release for maximum model support...")
        LLAMA_BIN_DIR.mkdir(parents=True, exist_ok=True)
        zip_path = LLAMA_BIN_DIR / "llama.zip"
        
        try:
            # Fetch the newest release directly from Github API
            req = urllib.request.Request("https://api.github.com/repos/ggerganov/llama.cpp/releases/latest", headers={'User-Agent': 'Mozilla/5.0'})
            with urllib.request.urlopen(req) as response:
                data = json.loads(response.read())
                # Find standard ubuntu x64 build
                url = next(a["browser_download_url"] for a in data["assets"] if "ubuntu-x64.zip" in a["name"])
        except Exception as e:
            print(f"[SETUP] API rate limit hit, using reliable fallback link. ({e})")
            url = "https://github.com/ggerganov/llama.cpp/releases/download/b4300/llama-b4300-bin-ubuntu-x64.zip"

        print(f"[SETUP] Downloading engine from: {url}")
        urllib.request.urlretrieve(url, zip_path)
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(LLAMA_BIN_DIR)
        os.remove(zip_path)
        
        for root, _, files in os.walk(LLAMA_BIN_DIR):
            if "llama-server" in files:
                found_exe = os.path.join(root, "llama-server")
                os.chmod(found_exe, 0o755)
                if found_exe != str(LLAMA_EXE):
                    os.rename(found_exe, str(LLAMA_EXE))
                break

    # 3. Boot Server with 4 safe threads
    threads = "4"
    port = "8089" 
    
    print(f"[SETUP] Starting Universal Engine on port {port}...")
    
    proc = subprocess.Popen([
        str(LLAMA_EXE),
        "-m", model_path,
        "-c", "4096",
        "--port", port,
        "--host", "127.0.0.1",
        "-t", threads
    ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)

    def stream_logs():
        for line in proc.stdout:
            print(f"[ENGINE] {line.strip()}")
            
    threading.Thread(target=stream_logs, daemon=True).start()

    # 4. Wait for Server to wake up
    for attempt in range(40):
        try:
            if requests.get(f"http://127.0.0.1:{port}/").status_code == 200:
                print("\n[SETUP] Universal Engine is ONLINE and ready!\n")
                return True, port
        except requests.exceptions.ConnectionError:
            time.sleep(1)
            
    print("\n[SETUP] FAILED to start. Check the [ENGINE] lines above.\n")
    return False, port

backend_ready, engine_port = setup_and_start_backend()

# ══════════════════════════════════════════════════════════════════
# CHAT MEMORY
# ══════════════════════════════════════════════════════════════════
sessions      = {}
sessions_lock = threading.Lock()

def get_memory(sid: str) -> list:
    with sessions_lock:
        return list(sessions.get(sid, []))

def add_to_memory(sid: str, role: str, content: str):
    with sessions_lock:
        sessions.setdefault(sid, [])
        sessions[sid].append({"role": role, "content": content})
        if len(sessions[sid]) > MAX_MEMORY * 2:
            sessions[sid] = sessions[sid][-(MAX_MEMORY * 2):]

# ══════════════════════════════════════════════════════════════════
# UNIVERSAL GENERATION (Uses OpenAI API Mode to auto-format any model)
# ══════════════════════════════════════════════════════════════════
def generate_response(user_input: str, session_id: str) -> str:
    if not backend_ready:
        return "[sad] My core engine failed to start. Please check the logs."

    memory = get_memory(session_id)
    recent = memory[-(6 * 2):]

    # Build an OpenAI-compliant message list
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    for msg in recent:
        role = "user" if msg["role"] == "user" else "assistant"
        messages.append({"role": role, "content": msg["content"]})
    messages.append({"role": "user", "content": user_input})

    payload = {
        "messages": messages,
        "max_tokens": MAX_NEW_TOKENS,
        "temperature": 0.90,
        "top_k": 50,
        "top_p": 0.95,
        "presence_penalty": 1.1,
        "stream": False
    }

    try:
        # We ping the /v1/chat/completions endpoint. 
        # This tells llama.cpp to automatically look at the GGUF file and apply the right internal formatting!
        res = requests.post(f"http://127.0.0.1:{engine_port}/v1/chat/completions", json=payload, timeout=60).json()
        response = res["choices"][0]["message"]["content"].strip()
    except Exception as exc:
        print(f"[GENERATE] Error communicating with engine: {exc}")
        traceback.print_exc()
        return "[sad] Something went wrong in my mind. Could you say that again?"

    # Post-process cleanup
    if "\n\n" in response:
        response = response.split("\n\n")[0].strip()

    if not response or len(response) < 3:
        response = "[thinking] I lost my train of thought. Could you say that again?"

    # Ensure Emotion Tag Defaults
    if not EMOTION_RE.search(response):
        response = "[default] " + response

    add_to_memory(session_id, "user",      user_input)
    add_to_memory(session_id, "assistant", response)
    return response

# ══════════════════════════════════════════════════════════════════
# EDGE-TTS
# ══════════════════════════════════════════════════════════════════
async def _async_tts(text: str, rate: int, pitch: int) -> bytes:
    rate_str  = f"+{rate}%"   if rate  >= 0 else f"{rate}%"
    pitch_str = f"+{pitch}Hz" if pitch >= 0 else f"{pitch}Hz"
    comm  = edge_tts.Communicate(text, TTS_VOICE, rate=rate_str, pitch=pitch_str)
    audio = b""
    async for chunk in comm.stream():
        if chunk["type"] == "audio":
            audio += chunk["data"]
    return audio

def synthesize_speech(text: str, rate: int = 0, pitch: int = 0):
    clean = clean_for_tts(text)
    if not clean or len(clean) < 2:
        return None
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        audio = loop.run_until_complete(_async_tts(clean, rate, pitch))
    except Exception as exc:
        print(f"[TTS] Error: {exc}")
        return None
    finally:
        loop.close()
    return base64.b64encode(audio).decode() if audio else None

# ══════════════════════════════════════════════════════════════════
# HTML -- Fast Loading, Instant Swap, Contain Image View
# ══════════════════════════════════════════════════════════════════
HTML_PAGE = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover,interactive-widget=resizes-content">
<title>Ana</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}

html{height:100%}

body{
  width:100%;
  height:100dvh;
  overflow:hidden;
  background:#000;
  font-family:'Segoe UI',system-ui,sans-serif;
  display:flex;
  flex-direction:column;
  position:relative;
}

#bg{
  position:fixed;
  inset:0;
  z-index:0;
  background:#000;
  display:flex;
  align-items:center;
  justify-content:center;
}

#bgImg{
  width:100%;
  height:100%;
  object-fit:contain;
  object-position:center center;
  display:block;
}

#overlay{
  position:absolute;
  left:0;right:0;bottom:0;
  z-index:20;
  display:flex;
  flex-direction:column;
  padding-bottom:max(10px, env(safe-area-inset-bottom));
  background:linear-gradient(
    to bottom,
    transparent 0%,
    rgba(0,0,0,0.52) 26%,
    rgba(0,0,0,0.76) 100%
  );
}

#msgArea{
  overflow-y:auto;
  display:flex;
  flex-direction:column;
  gap:6px;
  padding:16px 13px 8px;
  max-height:30dvh;
  scrollbar-width:none;
  -ms-overflow-style:none;
  scroll-behavior:smooth;
}
#msgArea::-webkit-scrollbar{display:none}

.turn{display:flex;flex-direction:column;gap:4px}
.user-row{display:flex;justify-content:flex-end}
.bot-row{display:flex;flex-direction:column;align-items:flex-start}
.name-tag{
  font-size:0.58rem;color:rgba(255,255,255,0.28);
  letter-spacing:.08em;text-transform:uppercase;
  margin-bottom:2px;padding-left:3px;
}
.bubble{
  max-width:74vw;
  padding:8px 13px;
  border-radius:18px;
  font-size:0.88rem;
  line-height:1.46;
  word-break:break-word;
  backdrop-filter:blur(10px);
  -webkit-backdrop-filter:blur(10px);
}
.bubble-user{
  background:rgba(255,255,255,0.11);
  border:1px solid rgba(255,255,255,0.17);
  color:#fff;
  border-bottom-right-radius:5px;
}
.bubble-bot{
  background:rgba(0,0,0,0.40);
  border:1px solid rgba(255,255,255,0.07);
  color:rgba(255,255,255,0.9);
  border-bottom-left-radius:5px;
}

.typing{
  display:flex;align-items:center;gap:5px;
  padding:9px 13px;
  background:rgba(0,0,0,0.36);
  border:1px solid rgba(255,255,255,0.07);
  border-radius:18px;border-bottom-left-radius:5px;
  backdrop-filter:blur(10px);
  width:fit-content;
}
.typing span{
  width:5px;height:5px;border-radius:50%;
  background:rgba(255,255,255,0.5);
  animation:blink 1.2s infinite;
}
.typing span:nth-child(2){animation-delay:.2s}
.typing span:nth-child(3){animation-delay:.4s}
@keyframes blink{
  0%,80%,100%{transform:scale(.6);opacity:.3}
  40%{transform:scale(1);opacity:1}
}

#inputBar{
  display:flex;
  align-items:center;
  gap:8px;
  padding:6px 12px 0;
}
#msgIn{
  flex:1;
  background:rgba(255,255,255,0.07);
  border:1px solid rgba(255,255,255,0.15);
  border-radius:24px;
  color:#fff;
  padding:10px 16px;
  font-size:16px; 
  outline:none;
  caret-color:#fff;
  backdrop-filter:blur(10px);
  -webkit-backdrop-filter:blur(10px);
  transition:border-color .2s,background .2s;
  -webkit-appearance:none;
  appearance:none;
}
#msgIn::placeholder{color:rgba(255,255,255,0.27)}
#msgIn:focus{
  border-color:rgba(255,255,255,0.28);
  background:rgba(255,255,255,0.1);
}
#sendBtn{
  width:42px;height:42px;flex-shrink:0;
  border-radius:50%;cursor:pointer;
  display:flex;align-items:center;justify-content:center;
  font-size:1rem;
  background:rgba(255,255,255,0.09);
  border:1px solid rgba(255,255,255,0.17);
  color:rgba(255,255,255,0.65);
  backdrop-filter:blur(10px);
  -webkit-backdrop-filter:blur(10px);
  transition:background .2s,color .2s,transform .12s;
}
#sendBtn:hover{background:rgba(255,255,255,0.17);color:#fff}
#sendBtn:active{transform:scale(.88)}
#sendBtn:disabled{opacity:.28;cursor:not-allowed}
</style>
</head>
<body>

<div id="bg">
  <img id="bgImg" src="/img/default.png" alt="" onerror="this.src='/img/default.png'">
</div>

<div id="overlay">
  <div id="msgArea"></div>
  <div id="inputBar">
    <input type="text" id="msgIn"
           placeholder="Say something..."
           autocomplete="off"
           autocorrect="off"
           spellcheck="false"
           enterkeyhint="send"/>
    <button id="sendBtn" onclick="send()" aria-label="Send">&#9658;</button>
  </div>
</div>

<script>
const SID = (crypto.randomUUID ? crypto.randomUUID() : Date.now().toString(36));
let busy = false, activeAudio = null;

const MA = document.getElementById('msgArea');
const MI = document.getElementById('msgIn');
const SB = document.getElementById('sendBtn');
const BG = document.getElementById('bgImg');

// Background Image Preloading System
const availableImages = new Set();
const imageCache = {};

// 1. Fetch available images from the server and preload them into browser memory
fetch('/api/images')
  .then(res => res.json())
  .then(files => {
    files.forEach(f => {
      const name = f.toLowerCase();
      availableImages.add(name);
      
      const img = new Image();
      img.src = `/img/${name}.png`; // Pre-cache request
      imageCache[name] = img;
    });
  })
  .catch(err => console.warn('Could not load image list:', err));

// 2. Instant swap logic (No transition delays, loaded instantly from browser memory)
function instantSwap(emotion) {
  const key = emotion.toLowerCase();
  if (availableImages.has(key)) {
    BG.src = `/img/${key}.png`;
  } else {
    BG.src = '/img/default.png'; // Fallback
  }
}

function playImgSequence(emotions) {
  if (!emotions || emotions.length === 0) { instantSwap('default'); return; }
  const queue = [...emotions];
  (function next() {
    if (!queue.length) return;
    instantSwap(queue.shift());
    if (queue.length) setTimeout(next, 750); // Pause briefly between multiple emotions
  })();
}

/* Parse emotion tags (Fully supports underscores) */
function parseResponse(raw) {
  const tagRe = /\[([a-zA-Z_]+)\]/g;
  const emotions = [];
  let m;
  while ((m = tagRe.exec(raw)) !== null) emotions.push(m[1]);
  const clean = raw.replace(/\[[a-zA-Z_]+\]/g, '').trim();
  return { emotions, clean };
}

/* DOM helpers */
function esc(t) { const d = document.createElement('div'); d.textContent = t; return d.innerHTML; }
function scroll() { MA.scrollTop = MA.scrollHeight; }

function addTurn(userText, botText) {
  const turn = document.createElement('div');
  turn.className = 'turn';
  turn.innerHTML =
    '<div class="user-row"><div class="bubble bubble-user">' + esc(userText) + '</div></div>' +
    '<div class="bot-row"><div class="name-tag">Ana</div><div class="bubble bubble-bot">' + esc(botText) + '</div></div>';
  MA.appendChild(turn);
  scroll();
}

function showTyping() {
  const d = document.createElement('div');
  d.className = 'bot-row';
  d.innerHTML = '<div class="typing"><span></span><span></span><span></span></div>';
  MA.appendChild(d); scroll(); return d;
}

/* TTS */
function playB64(b64) {
  try {
    if (activeAudio) { activeAudio.pause(); activeAudio = null; }
    const bin = atob(b64), u8 = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; i++) u8[i] = bin.charCodeAt(i);
    const url = URL.createObjectURL(new Blob([u8], { type: 'audio/mp3' }));
    activeAudio = new Audio(url);
    activeAudio.play().catch(() => {});
    activeAudio.onended = () => { URL.revokeObjectURL(url); activeAudio = null; };
  } catch(e) { console.warn('TTS:', e); }
}

async function fetchTTS(rawText) {
  try {
    const res = await fetch('/tts', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ text: rawText, rate: 7, pitch: 0 })
    });
    const d = await res.json();
    if (d.audio) playB64(d.audio);
  } catch(e) { console.warn('TTS fetch:', e); }
}

/* Send */
async function send() {
  const t = MI.value.trim();
  if (!t || busy) return;
  MI.value = ''; busy = true; SB.disabled = true;

  const tyEl = showTyping();

  try {
    const res = await fetch('/chat', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ message: t, session_id: SID })
    });
    const d = await res.json();
    tyEl.remove();

    const raw = d.response || '[sad] Something went wrong.';
    const { emotions, clean } = parseResponse(raw);

    playImgSequence(emotions.length > 0 ? emotions : ['default']);
    addTurn(t, clean);
    fetchTTS(raw);
  } catch(e) {
    tyEl.remove();
    addTurn(t, 'Connection error. Please try again.');
  }

  busy = false; SB.disabled = false;
}

MI.addEventListener('keydown', e => {
  if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); send(); }
});
</script>
</body>
</html>"""

# ══════════════════════════════════════════════════════════════════
# FLASK
# ══════════════════════════════════════════════════════════════════
app = Flask(__name__)

@app.route("/")
def index():
    return Response(HTML_PAGE, mimetype="text/html")

@app.route("/api/images")
def api_images():
    if not IMG_DIR.exists():
        return jsonify([])
    files = [f.stem for f in IMG_DIR.glob("*.png")]
    return jsonify(files)

@app.route("/img/<path:filename>")
def serve_img(filename: str):
    safe   = Path(filename).name
    target = IMG_DIR / safe
    if target.exists() and target.is_file():
        return send_from_directory(str(IMG_DIR), safe)
    
    fallback = IMG_DIR / "default.png"
    if fallback.exists() and fallback.is_file():
        return send_from_directory(str(IMG_DIR), "default.png")
        
    return Response("", status=404)

@app.route("/chat", methods=["POST"])
def chat():
    data       = request.json or {}
    user_input = data.get("message", "").strip()
    session_id = data.get("session_id", str(uuid.uuid4()))
    if not user_input:
        return jsonify({"error": "Empty message"}), 400
    try:
        resp = generate_response(user_input, session_id)
    except Exception as exc:
        print(f"[CHAT] Error: {exc}")
        traceback.print_exc()
        resp = "[sad] I encountered an unexpected error. Please try again."
    return jsonify({"response": resp, "session_id": session_id})

@app.route("/tts", methods=["POST"])
def tts_endpoint():
    data  = request.json or {}
    text  = data.get("text",  "").strip()
    rate  = int(data.get("rate",  TTS_RATE))
    pitch = int(data.get("pitch", TTS_PITCH))
    if not text:
        return jsonify({"error": "Empty text"}), 400
    audio_b64 = synthesize_speech(text, rate=rate, pitch=pitch)
    return jsonify({"audio": audio_b64})

@app.route("/clear", methods=["POST"])
def clear():
    data = request.json or {}
    sid  = data.get("session_id", "")
    with sessions_lock:
        sessions.pop(sid, None)
    return jsonify({"status": "cleared"})

@app.route("/health")
def health():
    return jsonify({
        "backend_ready": backend_ready,
        "type": "native-llama-server"
    })

if __name__ == "__main__":
    print("Visual AI is online -- http://0.0.0.0:7860")
    app.run(host="0.0.0.0", port=7860, threaded=True)