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
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
"""Vibe Search: search your library by how music sounds and feels ("rainy Sunday coffee shop", "upbeat but
melancholic, no vocals"), or find songs with the same vibe as one you pick, and save the results as a playlist.

It runs next to AudioMuse-AI's own models and changes nothing in them:
* after each song is analysed, a small audio model (EfficientAT mn20_as, distilled from a teacher that joins
  LCO-Embedding-Omni-7B and e5-omni-7B) gives it a 512-number vibe vector, stored in this plugin's own table;
* "Index my library" fills in the songs that were analysed before the plugin was installed;
* searches are turned into the same kind of vector by a text model (Qwen3-Embedding-0.6B plus a small trained layer)
  and compared with every song.
The models are downloaded on first use from the Hugging Face repository set on the settings page.
"""
import html
import os
import threading
import time

import numpy as np
from flask import Blueprint, redirect, request, url_for

from plugin.api import (config, enqueue, get_db, get_score_data_by_ids, get_setting, logger, manage_plugins_url,
                        render_page, set_setting, table)

from . import vibemodel

bp = Blueprint("vibe_search", __name__)

DEFAULTS = {
    "model_repo": "Divi19/vibe-embed",
    "revision": "main",
    "base_url": "https://huggingface.co",
    "precision": "int8",
    "threads": 4,
    "embed_on_analysis": True,
    "result_limit": 50,
}


STUDENT = "mn20_as"          # the only audio model size the release ships
INT8_TEXT_MIN_ORT = (1, 28)   # the 8-bit text model uses an operator setting that older onnxruntime builds lack


def setting(key):
    return get_setting(key, DEFAULTS[key])


def ort_version():
    try:
        import onnxruntime as ort
        return tuple(int(x) for x in ort.__version__.split(".")[:2])
    except Exception:
        return (0, 0)


def model_dir():
    base = getattr(config, "PLUGINS_DIR", "") or "/tmp"
    return os.path.join(base, "_data", "vibe_search", "models", setting("model_repo").replace("/", "__"), setting("revision"))


# ---------------------------------------------------------------- models (loaded once per container, on first use)
_models, _model_errors, _model_notes, _model_lock = {}, {}, {}, threading.Lock()


def get_model(kind):
    """kind: 'audio' (worker: indexing) or 'text' (web: searching). Returns the loaded model or None (error logged)."""
    wanted = setting("precision")
    key = (kind, setting("model_repo"), setting("revision"), STUDENT, wanted)
    with _model_lock:
        if key in _models:
            return _models[key]
        tries = [wanted] if wanted == "fp32" else ["int8", "fp32"]
        if kind == "text" and wanted == "int8" and ort_version() < INT8_TEXT_MIN_ORT:
            tries = ["fp32"]               # e.g. AudioMuse-AI's no-AVX2 image (onnxruntime 1.19): skip the 600 MB download that cannot run
            _model_notes[kind] = (f"The search model runs at full precision: this onnxruntime ({'.'.join(map(str, ort_version()))}) "
                                  f"is older than {'.'.join(map(str, INT8_TEXT_MIN_ORT))}, which the 8-bit file needs.")
        errors = []
        for precision in tries:
            try:
                d = model_dir()
                vibemodel.download_files(d, setting("base_url"), setting("model_repo"), setting("revision"),
                                         vibemodel.files_needed(STUDENT, precision, [kind]), log=logger.info)
                m = vibemodel.VibeModel(d, STUDENT, precision, setting("threads"))
                cos = m.load_audio() if kind == "audio" else m.load_text()
                logger.info(f"vibe_search: {kind} model ready ({m.tag}, {precision}, check {cos:.4f})")
                if precision != wanted and kind not in _model_notes:
                    _model_notes[kind] = f"The {kind} model runs at full precision because the 8-bit file failed: {errors[-1]}"
                _models[key] = m
                _model_errors.pop(kind, None)
                return m
            except Exception as e:
                errors.append(f"{precision}: {type(e).__name__}: {e}")
                logger.exception(f"vibe_search: the {kind} model ({precision}) could not be loaded")
        _model_errors[kind] = "; ".join(errors)
        return None


def current_tag():
    """The model version stored vectors must match. Read from config.json without loading a model."""
    try:
        import json, hashlib
        cfg = json.load(open(os.path.join(model_dir(), "config.json")))
        return f"{STUDENT}:{hashlib.md5(json.dumps(cfg, sort_keys=True).encode()).hexdigest()[:12]}"
    except Exception:
        return None


# ---------------------------------------------------------------- storage
def vec_table():
    return table("vectors")


def migrate(db):
    cur = db.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS " + vec_table() + " (item_id TEXT PRIMARY KEY, model_tag TEXT NOT NULL, "
                "vec BYTEA NOT NULL, n_windows INTEGER, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)")
    cur.execute("CREATE INDEX IF NOT EXISTS " + vec_table() + "_tag ON " + vec_table() + " (model_tag)")
    db.commit()
    cur.close()


def save_vector(item_id, tag, vec, n_windows):
    db = get_db()
    cur = db.cursor()
    cur.execute("INSERT INTO " + vec_table() + " (item_id, model_tag, vec, n_windows) VALUES (%s, %s, %s, %s) "
                "ON CONFLICT (item_id) DO UPDATE SET model_tag = EXCLUDED.model_tag, vec = EXCLUDED.vec, "
                "n_windows = EXCLUDED.n_windows, updated_at = CURRENT_TIMESTAMP",
                (str(item_id), tag, np.asarray(vec, np.float16).tobytes(), int(n_windows)))
    db.commit()
    cur.close()
    _matrix_cache.clear()


def indexed_ids(tag):
    cur = get_db().cursor()
    cur.execute("SELECT item_id FROM " + vec_table() + " WHERE model_tag = %s", (tag,))
    out = {r[0] for r in cur.fetchall()}
    cur.close()
    return out


_matrix_cache = {}


def song_matrix(tag):
    """All stored song vectors of this model version, cached until a vector is added."""
    cur = get_db().cursor()
    cur.execute("SELECT COUNT(*), MAX(updated_at) FROM " + vec_table() + " WHERE model_tag = %s", (tag,))
    stamp = tuple(str(x) for x in cur.fetchone())
    if _matrix_cache.get("stamp") == (tag,) + stamp:
        cur.close()
        return _matrix_cache["ids"], _matrix_cache["mat"]
    cur.execute("SELECT item_id, vec FROM " + vec_table() + " WHERE model_tag = %s", (tag,))
    rows = cur.fetchall()
    cur.close()
    ids = [r[0] for r in rows]
    mat = np.stack([np.frombuffer(bytes(r[1]), np.float16) for r in rows]) if rows else np.zeros((0, 512), np.float16)
    _matrix_cache.update(stamp=(tag,) + stamp, ids=ids, mat=mat)
    return ids, mat


def top_k(query_vec, tag, k, exclude=None):
    ids, mat = song_matrix(tag)
    if not ids:
        return []
    scores = np.concatenate([mat[i:i + 50000].astype(np.float32) @ query_vec for i in range(0, len(ids), 50000)])
    order = np.argsort(-scores)
    out = []
    for j in order:
        if ids[j] != exclude:
            out.append((ids[j], float(scores[j])))
        if len(out) >= k:
            break
    return out


# ---------------------------------------------------------------- audio
def load_wave(path):
    """Decode to 32 kHz mono with AudioMuse-AI's own loader (the same one its analysis uses)."""
    try:
        from tasks.analysis.song import robust_load_audio_with_fallback
        audio, sr = robust_load_audio_with_fallback(path, target_sr=vibemodel.SR)
        if audio is not None and sr == vibemodel.SR:
            return np.asarray(audio, np.float32)
    except Exception:
        logger.exception("vibe_search: AudioMuse-AI's audio loader failed; trying soundfile")
    import soundfile as sf
    y, sr = sf.read(path, dtype="float32", always_2d=True)
    y = y.mean(1)
    if sr != vibemodel.SR:
        import soxr
        y = soxr.resample(y, sr, vibemodel.SR, quality="HQ").astype(np.float32)
    return y


def canonical_id(item_id, server_id=None):
    try:
        from tasks.mediaserver.registry import canonical_input_ids
        return canonical_input_ids([item_id], server_id).get(str(item_id), str(item_id))
    except Exception:
        return str(item_id)


def embed_file(path, item_id):
    m = get_model("audio")
    if m is None:
        return False
    t0 = time.time()
    vec, n = m.embed_audio(load_wave(path))
    save_vector(item_id, m.tag, vec, n)
    logger.info(f"vibe_search: {item_id} embedded ({n} windows, {time.time() - t0:.1f} s)")
    return True


def on_song_analyzed(song):
    """Runs on the worker right after AudioMuse-AI finishes a song; the audio file is deleted once this returns."""
    if not setting("embed_on_analysis") or not song.get("audio_path"):
        return
    embed_file(song["audio_path"], canonical_id(song["item_id"], song.get("server_id")))


def index_library():
    """Worker task: give a vibe vector to every song that does not have one yet (for songs analysed before install)."""
    from tasks.mediaserver import download_track, get_all_songs
    m = get_model("audio")
    if m is None:
        raise RuntimeError("the audio model could not be loaded: " + _model_errors.get("audio", "unknown error"))
    songs = get_all_songs() or []
    done = indexed_ids(m.tag)
    todo = [s for s in songs if canonical_id(s.get("Id")) not in done]
    status = {"started": time.strftime("%Y-%m-%d %H:%M"), "total": len(todo), "done": 0, "failed": 0, "finished": False}
    set_setting("index_status", status)
    logger.info(f"vibe_search: indexing {len(todo)} of {len(songs)} songs")
    temp_dir = getattr(config, "TEMP_DIR", "/tmp")
    os.makedirs(temp_dir, exist_ok=True)
    for i, item in enumerate(todo):
        path = None
        try:
            path = download_track(temp_dir, item)
            if not path or not embed_file(path, canonical_id(item.get("Id"))):
                status["failed"] += 1
            else:
                status["done"] += 1
        except Exception:
            status["failed"] += 1
            logger.exception(f"vibe_search: could not index {item.get('Id')}")
        finally:
            if path and os.path.exists(path):
                os.remove(path)
        if (i + 1) % 25 == 0:
            set_setting("index_status", status)
    status["finished"] = True
    set_setting("index_status", status)


# ---------------------------------------------------------------- pages
CSS = """<style>
.vs { max-width: 980px; display: grid; gap: 16px; }
.vs form.search { display: flex; gap: 8px; flex-wrap: wrap; }
.vs input[type=text] { flex: 1 1 360px; padding: 8px 10px; font-size: 1rem; }
.vs table { border-collapse: collapse; width: 100%; }
.vs td, .vs th { padding: 6px 8px; border-bottom: 1px solid rgba(128,128,128,.25); text-align: left; vertical-align: top; }
.vs .score { font-variant-numeric: tabular-nums; opacity: .7; }
.vs .note { opacity: .75; font-size: .92rem; }
.vs .err { color: #b3261e; }
</style>"""


def page(body, title="Vibe Search"):
    return render_page(CSS + '<div class="vs">' + body + "</div>", title=title)


def status_html():
    tag = current_tag()
    n_songs = n_vec = 0
    try:
        cur = get_db().cursor()
        cur.execute("SELECT COUNT(*) FROM score")
        n_songs = cur.fetchone()[0]
        if tag:
            cur.execute("SELECT COUNT(*) FROM " + vec_table() + " WHERE model_tag = %s", (tag,))
            n_vec = cur.fetchone()[0]
        cur.close()
    except Exception:
        logger.exception("vibe_search: status query failed")
    st = get_setting("index_status", None)
    msg = f"{n_vec:,} of {n_songs:,} analysed songs have a vibe vector."
    if st and not st.get("finished"):
        msg += f" Indexing: {st['done']:,} of {st['total']:,} done" + (f", {st['failed']} failed" if st["failed"] else "") + "."
    err = "".join(f'<p class="err">The {k} model is not available: {html.escape(v)}</p>' for k, v in _model_errors.items())
    notes = "".join(f'<p class="note">{html.escape(v)}</p>' for v in _model_notes.values())
    return f'<p class="note">{msg}</p>{notes}{err}'


def results_html(rows, heading, playlist_name):
    if not rows:
        return "<p>No songs with a vibe vector yet. Analyse your library, or use Index my library below.</p>"
    info = {r["item_id"]: r for r in (get_score_data_by_ids([i for i, _ in rows]) or [])}
    trs = []
    for item_id, score in rows:
        r = info.get(item_id, {})
        trs.append(
            f'<tr><td><input type="checkbox" name="ids" value="{html.escape(item_id)}" checked></td>'
            f'<td>{html.escape(str(r.get("title") or item_id))}</td><td>{html.escape(str(r.get("author") or ""))}</td>'
            f'<td>{html.escape(str(r.get("album") or ""))}</td><td class="score">{score:.3f}</td>'
            f'<td><a href="{url_for("vibe_search.similar", item_id=item_id)}">same vibe</a></td></tr>')
    return (f"<h3>{html.escape(heading)}</h3>"
            f'<form method="post" action="{url_for("vibe_search.playlist")}">'
            f"<table><tr><th></th><th>Title</th><th>Artist</th><th>Album</th><th>Match</th><th></th></tr>{''.join(trs)}</table>"
            f'<p><input type="text" name="name" value="{html.escape(playlist_name)}" aria-label="Playlist name"> '
            f'<button type="submit">Save ticked songs as a playlist</button></p></form>')


def search_form(q=""):
    return (f'<form class="search" method="get" action="{url_for("vibe_search.home")}">'
            f'<input type="text" name="q" value="{html.escape(q)}" placeholder="late night drive, rainy Sunday, upbeat but melancholic..." '
            f'aria-label="Describe the music"><button type="submit">Search</button></form>')


@bp.route("/")
def home():
    q = (request.args.get("q") or "").strip()
    body = "<h2>Vibe Search</h2>" + search_form(q) + status_html()
    if q:
        m = get_model("text")
        if m is None:
            body += '<p class="err">The search model could not be loaded; see the message above.</p>'
        else:
            tag = current_tag() or m.tag
            rows = top_k(m.embed_texts([q])[0], tag, int(setting("result_limit")))
            body += results_html(rows, f"Songs that match “{q}”", f"Vibe: {q[:60]}")
    body += (f'<form method="post" action="{url_for("vibe_search.index")}"><button type="submit">Index my library</button> '
             '<span class="note">Gives a vibe vector to analysed songs that do not have one yet (runs in the background).</span></form>')
    return page(body)


@bp.route("/similar/<path:item_id>")
def similar(item_id):
    tag = current_tag()
    ids, mat = song_matrix(tag) if tag else ([], None)
    body = "<h2>Vibe Search</h2>" + search_form() + status_html()
    if item_id not in ids:
        body += "<p>This song has no vibe vector yet.</p>"
    else:
        seed = mat[ids.index(item_id)].astype(np.float32)
        r = (get_score_data_by_ids([item_id]) or [{}])[0]
        name = f'{r.get("title") or item_id}' + (f' by {r.get("author")}' if r.get("author") else "")
        body += results_html(top_k(seed, tag, int(setting("result_limit")), exclude=item_id),
                             f"Same vibe as {name}", f"Same vibe: {name[:60]}")
    return page(body)


@bp.route("/playlist", methods=["POST"])
def playlist():
    ids = request.form.getlist("ids")
    name = (request.form.get("name") or "Vibe Search").strip()[:120]
    if not ids:
        return page("<h2>Vibe Search</h2><p>No songs were ticked.</p>" + search_form())
    try:
        from tasks.mediaserver import create_or_replace_playlist
        create_or_replace_playlist(name, ids)
        msg = f"<p>Saved {len(ids)} songs as the playlist “{html.escape(name)}” on your media server.</p>"
    except Exception as e:
        logger.exception("vibe_search: playlist creation failed")
        msg = f'<p class="err">The playlist could not be created: {html.escape(str(e))}</p>'
    return page("<h2>Vibe Search</h2>" + msg + search_form())


@bp.route("/index", methods=["POST"])
def index():
    try:
        enqueue(index_library)
        msg = "<p>Indexing started in the background. This page shows the progress.</p>"
    except Exception as e:
        msg = f'<p class="err">Indexing could not start now: {html.escape(str(e))}</p>'
    return page("<h2>Vibe Search</h2>" + msg + search_form() + status_html())


@bp.route("/settings", methods=["GET", "POST"])
def settings():
    if request.method == "POST":
        for k, v in DEFAULTS.items():
            if k not in request.form and not isinstance(v, bool):
                continue
            raw = request.form.get(k)
            val = (raw == "on") if isinstance(v, bool) else (int(raw) if isinstance(v, int) else raw.strip())
            set_setting(k, val)
        _models.clear(); _model_errors.clear(); _model_notes.clear(); _matrix_cache.clear()
        return redirect(manage_plugins_url())
    rows = []
    for k, v in DEFAULTS.items():
        cur = setting(k)
        if isinstance(v, bool):
            field = f'<input type="checkbox" name="{k}" id="{k}"{" checked" if cur else ""}>'
        else:
            field = f'<input type="text" name="{k}" id="{k}" value="{html.escape(str(cur))}">'
        rows.append(f'<tr><td><label for="{k}">{k}</label></td><td>{field}</td></tr>')
    note = ("<p class=\"note\">precision: int8 (smaller, faster) or fp32. With int8, a model that cannot run on this "
            "installation (for example the 8-bit search model on onnxruntime older than 1.28) falls back to fp32 by itself. "
            "Changing the model repository or revision makes stored vectors outdated: run "
            "Index my library afterwards. Models are stored in " + html.escape(model_dir()) + ".</p>")
    return page(f'<h2>Vibe Search settings</h2><form method="post"><table>{"".join(rows)}</table>'
                f'<button type="submit">Save</button></form>{note}', title="Vibe Search settings")


def register(ctx):
    ctx.on_install(migrate)
    ctx.add_blueprint(bp)
    ctx.add_menu_item("Vibe Search", "vibe_search.home")
    ctx.on_song_analyzed(on_song_analyzed)
    ctx.add_task("index_library", index_library)