Spaces:
Paused
Paused
File size: 40,558 Bytes
eb8e2ec fa32c20 eb8e2ec 6f76256 eb8e2ec 6f76256 eb8e2ec 6f76256 61657d1 6f76256 0181506 36de25c 0181506 6f76256 0181506 61657d1 0181506 6f76256 0181506 6f76256 eb8e2ec fa32c20 36de25c eb8e2ec 520c7b3 eb8e2ec 520c7b3 eb8e2ec 520c7b3 eb8e2ec 520c7b3 eb8e2ec 520c7b3 fa32c20 eb8e2ec fa32c20 eb8e2ec 6f76256 eb8e2ec fa32c20 eb8e2ec fa32c20 520c7b3 | 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 | // STUDIUS real functions — public Gradio Spaces via self-hosted @gradio/client (no token, no secrets) + client-side tools.
import { Client, handle_file } from './vendor/gradio-client.js?v=271';
import { STUDIOS, every, onStop, loop, cv } from './widgets.js?v=50';
const esc = (t) => String(t).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
const hf = (s) => `<a href="https://huggingface.co/spaces/${s}" target="_blank" rel="noopener">${s}</a>`;
const conns = new Map();
function connect(space) {
if (!conns.has(space)) conns.set(space, Promise.race([Client.connect(space, { events: ['status', 'data'] }), new Promise((_, r) => setTimeout(() => r(new Error('Verbindung: Zeitüberschreitung')), 30000))]).catch(e => { conns.delete(space); throw e; }));
return conns.get(space);
}
window.__studiusLog = [];
/** run a Space endpoint robustly: status → onStatus, partial data → onData, hard timeout, cancel on close */
async function runAnon(space, ep, data, { onStatus = () => {}, onData = () => {}, timeout = 150000, el } = {}) {
const t0 = performance.now(); const c = await connect(space); const job = c.submit(ep, data);
let last = null, done = false, err = null; const cancel = () => { if (!done) { done = true; try { job.cancel?.(); } catch (e) {} } };
onStop(cancel); const to = setTimeout(() => { err = new Error('Zeitüberschreitung nach ' + Math.round(timeout / 1000) + ' s'); cancel(); }, timeout);
try {
for await (const m of job) {
if (done || (el && !el.isConnected)) break;
if (m.type === 'status') { onStatus(m); if (m.stage === 'error') { err = new Error(String(m.message || m.title || 'Backend-Fehler').replace(/<[^>]*>/g, '').trim()); break; } if (m.stage === 'complete' && last) break; }
else if (m.type === 'data') { last = m.data; onData(m.data); }
}
} finally { clearTimeout(to); done = true; }
if (err) throw err; if (el && !el.isConnected) throw new Error('abgebrochen'); if (!last) throw new Error('keine Daten');
window.__studiusLog.push({ space, ep, via: 'anon', ms: Math.round(performance.now() - t0) }); return last;
}
/** Signed-in: run server-side with the user's own HF token/quota (token never reaches the page). SSE stream. */
async function runUser(space, ep, data, { onStatus = () => {}, onData = () => {}, timeout = 150000, el } = {}) {
const t0 = performance.now(), f = new FormData(), args = {}; let n = 0;
for (const [k, v] of Object.entries(data)) {
if (v instanceof Blob) { const id = 'f' + n++; f.append(id, v, v.name || ('input.' + ({ 'image/jpeg': 'jpg', 'image/png': 'png', 'image/webp': 'webp', 'audio/wav': 'wav', 'audio/x-wav': 'wav', 'audio/wave': 'wav', 'audio/mpeg': 'mp3', 'audio/ogg': 'ogg', 'audio/flac': 'flac', 'video/mp4': 'mp4', 'video/webm': 'webm' }[(v.type || '').split(';')[0]] || 'bin'))); args[k] = { __file__: id }; }
else if (typeof v === 'string' && v.startsWith('/api/tmp/') && k !== 'prompt') args[k] = { __tmp__: v };
else args[k] = v;
}
f.append('space', space); f.append('ep', ep); f.append('args', JSON.stringify(args)); f.append('timeout', String(Math.round(timeout / 1000)));
const ac = new AbortController(); onStop(() => ac.abort()); const to = setTimeout(() => ac.abort(), timeout + 15000);
let last = null, err = null;
try {
const r = await fetch('/api/run', { method: 'POST', body: f, credentials: 'include', signal: ac.signal });
if (!r.ok) throw Object.assign(new Error((await r.json().catch(() => ({}))).detail || 'HTTP ' + r.status), { http: r.status });
const rd = r.body.getReader(), dec = new TextDecoder(); let buf = '';
for (;;) { const { value, done } = await rd.read(); if (done) break; buf += dec.decode(value, { stream: true }); let i;
while ((i = buf.indexOf('\n\n')) >= 0) { const line = buf.slice(0, i); buf = buf.slice(i + 2); if (!line.startsWith('data: ')) continue; const m = JSON.parse(line.slice(6));
if (el && !el.isConnected) { ac.abort(); throw new Error('abgebrochen'); }
if (m.type === 'status') onStatus(m); else if (m.type === 'data') { last = Array.isArray(m.data) ? m.data : [m.data]; onData(last); } else if (m.type === 'error') err = new Error(m.message); } }
} finally { clearTimeout(to); }
if (err) throw err; if (!last) throw new Error('keine Daten');
window.__studiusLog.push({ space, ep, via: 'user', ms: Math.round(performance.now() - t0) }); return last;
}
export const signedIn = () => !!window.__ws?.me?.signedIn;
/* quota: user's own ZeroGPU seconds (HUD) + clear fallback notices */
export async function refreshQuota() { try { const q = await (await fetch('/api/quota', { credentials: 'include' })).json(); window.__quota = q; window.dispatchEvent(new CustomEvent('studius-quota', { detail: q })); return q; } catch (e) { return null; } }
const fmtReset = (iso) => { if (!iso) return ''; const ms = new Date(iso) - Date.now(); if (ms <= 0) return 'gleich'; const h = Math.floor(ms / 36e5), m = Math.round(ms % 36e5 / 6e4); return `in ${h ? h + ' h ' : ''}${m} min (${new Date(iso).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' })})`; };
const isQuota = (m) => /quota|ZeroGPU|GPU task aborted|exceeded/i.test(m || '');
function notice(el, html, kind = 'warn') { const pop = el?.closest?.('#pop'); if (!pop) return; let n = pop.querySelector('.qnote'); if (!n) { n = document.createElement('div'); n.className = 'qnote'; n.setAttribute('role', 'status'); (pop.querySelector('.credit') || pop.lastElementChild).insertAdjacentElement('beforebegin', n); } n.className = 'qnote ' + kind; n.innerHTML = html; window.__notice = n.textContent; }
let LASTM = '';
export async function run(space, ep, data, opt = {}) { LASTM = space; let why = '';
if (signedIn()) {
const q = window.__quota; if (q?.ok && q.remaining < 60) notice(opt.el, `⚡ Nur noch <b>${Math.round(q.remaining)} s</b> deiner ZeroGPU-Quota${q.resetsAt ? ' · Reset ' + fmtReset(q.resetsAt) : ''} – evtl. Fallback.`);
try { const d = await runUser(space, ep, data, opt); opt.el && via(opt.el, 'user'); refreshQuota(); return d; }
catch (e) { if (e.message === 'abgebrochen' || (opt.el && !opt.el.isConnected)) throw e; if (opt.noAnonOn?.test(e.message)) throw e; console.warn('user-quota run failed, trying anonymous:', e.message); why = e.message;
const q2 = await refreshQuota();
notice(opt.el, isQuota(why) ? `⚠ <b>Deine ZeroGPU-Quota ist aufgebraucht</b>${(m => m ? ` (${m[1]} s angefragt, ${m[2]} s übrig)` : '')(why.match(/\((\d+)s requested vs\. (\d+)s left\)/))}${q2?.ok && q2.resetsAt ? ' · Reset ' + fmtReset(q2.resetsAt) : ''}. Fallback: <b>anonyme Quota</b> – kann langsamer sein oder ebenfalls scheitern. <a href="https://huggingface.co/pricing" target="_blank" rel="noopener">Mehr Quota (PRO)</a>` : `⚠ Lauf mit deinem Konto fehlgeschlagen (${esc(why.slice(0, 120))}). Fallback: anonyme Quota.`);
opt.onStatus?.({ stage: 'pending', message: 'Fallback: anonyme Quota' }); }
}
const anon = {}; for (const [k, v] of Object.entries(data)) anon[k] = v instanceof Blob ? handle_file(v) : typeof v === 'string' && v.startsWith('/api/') && k !== 'prompt' ? handle_file(await (await fetch(v)).blob()) : v;
try { const d = await runAnon(space, ep, anon, opt); opt.el && via(opt.el, 'anon'); return d; }
catch (e) { opt.el?.closest?.('#pop')?.querySelector('.credit .via')?.remove(); if (why || isQuota(e.message)) notice(opt.el, (window.__notice && why ? esc(window.__notice) + '<br>' : '') + (isQuota(e.message) ? '⛔ <b>Auch die anonyme Quota ist erschöpft</b>' : `⛔ <b>Anonymer Fallback fehlgeschlagen</b> (${esc(e.message.slice(0, 100))})`) + ` – ${signedIn() ? 'warte auf den Reset deiner Quota' : 'melde dich mit Hugging Face an, um deine eigene Quota zu nutzen'}. Das Studio zeigt bis dahin eine <b>Simulation</b>.`, 'err'); throw e; }
}
const via = (el, v) => { const c = el.closest?.('#pop')?.querySelector('.credit'); if (c) { c.querySelector('.via')?.remove(); c.insertAdjacentHTML('beforeend', `<span class="via"> · ${v === 'user' ? '🔑 deine HF-Quota' : 'anonyme Quota'}</span>`); } };
const stTxt = (m) => m.stage === 'pending' ? (m.position != null ? `Warteschlange: Platz ${m.position + 1}${m.queue_size ? '/' + m.queue_size : ''}${m.eta ? ' · ETA ' + Math.round(m.eta) + ' s' : ''}` : 'Warte auf GPU…')
: m.stage === 'generating' ? (m.progress_data?.[0]?.index != null && m.progress_data[0].length ? `Generiere… ${m.progress_data[0].index}/${m.progress_data[0].length}` : 'Generiere…') : m.stage === 'complete' ? 'fertig' : m.stage || '';
const url = (f) => typeof f === 'string' ? f : f?.url || f?.path;
function badge(el, mode, space) {
const b = el.querySelector('.badge'); if (b) { b.className = 'badge ' + (mode === 'SIM' ? 'sim' : 'real'); b.textContent = mode; }
const f = el.closest('#pop')?.querySelector('.pf span:last-child'); if (f) { f.textContent = mode === 'SIM' ? '● SIMULATION (Fallback)' : '● ' + mode + ' · API'; f.classList.toggle('real', mode !== 'SIM'); }
}
async function dl(a, src, name) { // cross-origin download → blob URL
try { const r = await fetch(src); const b = await r.blob(); const u = URL.createObjectURL(b); onStop(() => URL.revokeObjectURL(u)); a.href = u; a.download = name; } catch (e) { a.href = src; a.target = '_blank'; }
}
function timer(el, sel) { const t0 = performance.now(); const s = el.querySelector(sel); return every(() => { if (s && s.dataset.base) s.textContent = s.dataset.base + ' · ' + ((performance.now() - t0) / 1000).toFixed(0) + ' s'; }, 1000); }
const setS = (el, txt, err) => { const s = el.querySelector('.stat'); if (!s) return; s.dataset.base = txt; s.textContent = txt; s.classList.toggle('err', !!err); };
/** typed asset bus: every studio result is announced; workspace.js adds 'Save to Workspace' + 'Send to…' */
export const result = (el, asset) => { if (!asset.model && LASTM && /^(\/api\/tmp\/|https:)/.test(asset.url || '')) asset.model = LASTM; window.__lastAsset = asset; window.dispatchEvent(new CustomEvent('studius-result', { detail: { el, asset } })); };
const S = (id) => STUDIOS.find(s => s.id === id);
const keep = (el) => el.querySelectorAll('input,textarea').forEach(i => i.addEventListener('keydown', e => e.stopPropagation()));
/* 1 CHAT */
const CHAT = 'huggingface-projects/llama-3.2-3B-Instruct';
{ const s = S('chat'); s.real = 'API'; s.build = (el) => {
el.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">REAL</span><span class="stat"></span></div><div class="chat" aria-live="polite"><p class="a">◆ STUDIUS-Chat (Llama 3.2 3B). Frag mich etwas.</p></div><div class="row"><input aria-label="Nachricht" placeholder="Nachricht… (Enter)"><button class="btn" aria-label="Senden">➤</button></div><div class="credit">powered by ${hf(CHAT)}</div>`;
const log = el.querySelector('.chat'), inp = el.querySelector('input'), btn = el.querySelector('.btn'); keep(el); let busy = false; const hist = [];
const R = ['Der Schwarm ist gerade offline – hier eine simulierte Antwort. 🐝', 'Simulation: Klar, machbar, mit Gold-Rand.'];
const send = async () => { const q = inp.value.trim(); if (!q || busy) return; busy = true; btn.disabled = true; inp.value = '';
log.insertAdjacentHTML('beforeend', `<p class="u">› ${esc(q)}</p>`); const p = document.createElement('p'); p.className = 'a'; p.textContent = '◆ …'; log.append(p); log.scrollTop = 1e5;
const ctx = hist.slice(-4).map(([u, a]) => `User: ${u}\nAssistant: ${a}`).join('\n'); const persona = window.__persona?.(el); const msg = (persona ? `System: ${persona}\n` : '') + (ctx ? ctx + '\nUser: ' + q : q) + (persona ? '\nAssistant:' : '');
setS(el, 'Verbinde…'); const tm = timer(el, '.stat'); let out = '';
try { await run(CHAT, '/generate', { message: msg, max_new_tokens: 400, temperature: .6, top_p: .9, top_k: 50, repetition_penalty: 1.2 }, { el, timeout: 90000,
onStatus: m => setS(el, stTxt(m)), onData: d => { const t = String(d[0] ?? ''); out = t.startsWith(out) ? t : out + t; p.textContent = '◆ ' + out; log.scrollTop = 1e5; } });
hist.push([q, out]); el.dispatchEvent(new CustomEvent('chat-reply', { detail: { text: out, p } })); result(el.querySelector('.chat'), { kind: 'text', mime: 'text/plain', text: out, name: 'chat-' + Date.now() + '.txt', studio: 'chat', prompt: q }); setS(el, '✔ gestreamt'); badge(el, 'REAL', CHAT);
} catch (e) { if (!el.isConnected) return; setS(el, '⚠ ' + e.message + ' – Simulation', true); badge(el, 'SIM'); const txt = R[Math.random() * R.length | 0]; p.textContent = '◆ ' + txt; }
finally { clearInterval(tm); busy = false; btn.disabled = false; window.__chatOut = out; } };
btn.onclick = send; inp.onkeydown = e => { e.stopPropagation(); if (e.key === 'Enter') send(); }; }; }
/* 2 IMAGE: Generate (FLUX) + Edit (local) */
const FLUX = 'black-forest-labs/FLUX.1-schnell', KONTEXT = 'black-forest-labs/FLUX.1-Kontext-Dev', TTS = 'Qwen/Qwen3-TTS';
{ const s = S('image'); const editBuild = s.build; s.real = 'API'; s.build = (el) => {
el.innerHTML = `<div class="tabs" role="tablist"><button class="btn" role="tab" data-tab="gen">Generate</button><button class="btn alt" role="tab" data-tab="edit">Edit</button></div><div class="tp"></div>`;
const tp = el.querySelector('.tp');
const gen = () => { tp.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">REAL</span><span class="stat"></span></div><input aria-label="Bild-Prompt" value="Goldene Biene auf Hexagon-Kristall, cinematic, dunkler Hintergrund"><div class="row"><button class="btn go">Generieren</button><a class="btn alt dl" hidden>⬇ Download</a></div><div class="gen"></div><div class="credit">powered by ${hf(FLUX)} · 512×512 · 4 Steps · Avatar-Look via ${hf(KONTEXT)}</div>`;
keep(tp); const go = tp.querySelector('.go'), a = tp.querySelector('.dl');
go.onclick = async () => { go.disabled = true; a.hidden = true; setS(tp, 'Verbinde…'); const tm = timer(tp, '.stat');
try { const ref = await window.__avatarRef?.(el); const pr = tp.querySelector('input').value;
const d = ref ? await run(KONTEXT, '/infer', { input_image: ref.blob, prompt: `${pr}. Keep the exact same character (${ref.look}).`, seed: ref.seed, randomize_seed: false, guidance_scale: 2.5, steps: 20 }, { el: tp, timeout: 240000, onStatus: m => setS(tp, 'Avatar-Look: ' + stTxt(m)) })
: await run(FLUX, '/infer', { prompt: pr, seed: 0, randomize_seed: true, width: 512, height: 512, num_inference_steps: 4 }, { el: tp, onStatus: m => setS(tp, stTxt(m)) });
const u = url(d[0]); tp.querySelector('.gen').innerHTML = `<img alt="Generiertes Bild" src="${esc(u)}">`; window.__imgUrl = u; result(tp.querySelector('.gen'), { kind: 'image', mime: 'image/webp', url: u, name: 'flux-' + d[1] + '.webp', studio: 'image', prompt: tp.querySelector('input').value }); await dl(a, u, 'studius-flux.webp'); a.hidden = false; setS(tp, '✔ Seed ' + d[1]); badge(el, 'REAL');
} catch (e) { if (!tp.isConnected) return; setS(tp, '⚠ ' + e.message + ' – Simulation', true); badge(el, 'SIM'); const c = document.createElement('canvas'); c.width = c.height = 256; const x = c.getContext('2d'); const g = x.createRadialGradient(128, 128, 10, 128, 128, 128); g.addColorStop(0, '#f2c94c'); g.addColorStop(1, '#05182e'); x.fillStyle = g; x.fillRect(0, 0, 256, 256); tp.querySelector('.gen').replaceChildren(c); }
finally { clearInterval(tm); go.disabled = false; } }; };
const tabs = el.querySelectorAll('[data-tab]');
tabs.forEach(b => b.onclick = () => { tabs.forEach(o => { o.classList.toggle('alt', o !== b); o.setAttribute('aria-selected', o === b); }); if (b.dataset.tab === 'gen') gen(); else { tp.innerHTML = '<div class="row" style="margin:0 0 6px"><span class="badge sim">LOKAL</span></div><div class="ed"></div>'; editBuild(tp.querySelector('.ed')); } });
tabs[0].click(); }; }
/* 3 MUSIC */
const ACE = 'ACE-Step/ACE-Step', TF = 'declare-lab/TangoFlux';
{ const s = S('music'); const sim = s.build; s.real = 'API'; s.build = (el) => {
el.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">REAL</span><span class="stat"></span></div><input aria-label="Musik-Prompt" value="synthwave, retro, 100 BPM, warm pads, punchy drums"><div class="row"><button class="btn go">♪ Song erzeugen (15 s)</button><a class="btn alt dl" hidden>⬇ Download</a></div><div class="out"></div><div class="credit">powered by ${hf(ACE)} · Fallback ${hf(TF)}</div>`;
keep(el); const go = el.querySelector('.go'), a = el.querySelector('.dl');
go.onclick = async () => { go.disabled = true; a.hidden = true; const pr = el.querySelector('input').value; const tm = timer(el, '.stat'); let u, used, aceErr = '';
try { try { setS(el, 'Verbinde ACE-Step…'); const d = await run(ACE, '/__call__', { audio_duration: 15, prompt: pr, lyrics: '[instrumental]', infer_step: 27, guidance_scale: 15, scheduler_type: 'euler', cfg_type: 'apg', omega_scale: 10 }, { el, timeout: 180000, onStatus: m => setS(el, 'ACE-Step: ' + stTxt(m)) }); u = url(d[0]); used = ACE; }
catch (e) { if (!el.isConnected) return; console.warn('ACE-Step failed', e); setS(el, 'ACE-Step: ' + e.message + ' → TangoFlux…'); const d = await run(TF, '/predict', { prompt: pr, steps: 25, guidance: 4.5, duration: 10 }, { el, onStatus: m => setS(el, 'TangoFlux: ' + stTxt(m)) }); u = url(d[0] ?? d); used = TF; aceErr = e.message; }
el.querySelector('.out').innerHTML = `<audio controls src="${esc(u)}"></audio>`; window.__musicUrl = u; result(el.querySelector('.out'), { kind: 'audio', url: u, name: 'music-' + Date.now() + (u.match(/\.(mp3|wav|ogg|flac)/)?.[0] || '.wav'), studio: 'music', prompt: pr, source: used }); await dl(a, u, 'studius-music' + (u.match(/\.(mp3|wav|ogg|flac)/)?.[0] || '.mp3')); a.hidden = false; setS(el, '✔ ' + used.split('/')[1] + (aceErr ? ' (ACE-Step: ' + aceErr.slice(0, 60) + ')' : '')); badge(el, 'REAL');
} catch (e) { if (!el.isConnected) return; setS(el, '⚠ ' + e.message + ' – Simulation', true); badge(el, 'SIM'); sim(el.querySelector('.out')); }
finally { clearInterval(tm); go.disabled = false; } }; }; }
/* 4 STEMS */
const SEP = 'abidlabs/music-separation';
function demoWav() { const sr = 22050, n = sr * 6, b = new ArrayBuffer(44 + n * 2), v = new DataView(b); const w = (o, s) => [...s].forEach((c, i) => v.setUint8(o + i, c.charCodeAt(0)));
w(0, 'RIFF'); v.setUint32(4, 36 + n * 2, true); w(8, 'WAVEfmt '); v.setUint32(16, 16, true); v.setUint16(20, 1, true); v.setUint16(22, 1, true); v.setUint32(24, sr, true); v.setUint32(28, sr * 2, true); v.setUint16(32, 2, true); v.setUint16(34, 16, true); w(36, 'data'); v.setUint32(40, n * 2, true);
for (let i = 0; i < n; i++) { const t = i / sr, f = [262, 330, 392, 330][(t * 2 | 0) % 4]; const s = .35 * Math.sin(2 * Math.PI * f * t) * (1 + Math.sin(2 * Math.PI * 5 * t) * .2) + .25 * Math.sin(2 * Math.PI * 55 * t) + (t % .5 < .04 ? (Math.random() - .5) * .6 : 0); v.setInt16(44 + i * 2, Math.max(-1, Math.min(1, s)) * 32000, true); }
return new Blob([b], { type: 'audio/wav' }); }
{ const s = S('stems'); s.real = 'API'; s.build = (el) => {
el.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">REAL</span><span class="stat">Audio wählen (≤ 30 s empfohlen)</span></div><div class="row"><input type="file" accept="audio/*" aria-label="Audiodatei"><button class="btn alt demo">Demo-Audio</button></div><div class="row"><button class="btn go" disabled>Trennen</button></div><div class="out"></div><div class="credit">powered by ${hf(SEP)} (Demucs)</div>`;
let file = null; const go = el.querySelector('.go'), fi = el.querySelector('input[type=file]');
fi.onchange = () => { file = fi.files[0] || null; go.disabled = !file; setS(el, file ? file.name + ' · ' + (file.size / 1e6).toFixed(1) + ' MB' : ''); };
el.querySelector('.demo').onclick = () => { file = new File([demoWav()], 'studius-demo.wav', { type: 'audio/wav' }); go.disabled = false; setS(el, 'Demo-Audio (6 s) bereit'); };
go.onclick = async () => { if (!file) return; if (file.size > 25e6) return setS(el, '⚠ Datei > 25 MB', true); go.disabled = true; const tm = timer(el, '.stat'); setS(el, 'Lade hoch…');
try { const d = await run(SEP, '/inference', { audio: file }, { el, timeout: 240000, onStatus: m => setS(el, stTxt(m)) });
const names = ['Vocals', 'Instrumental']; const o = el.querySelector('.out'); o.innerHTML = d.map((f, i) => `<div class="row"><b style="width:86px">${names[i] || 'Stem ' + i}</b><a class="btn alt" data-i="${i}">⬇</a></div><audio controls src="${esc(url(f))}"></audio>`).join('');
o.querySelectorAll('a[data-i]').forEach(a => dl(a, url(d[a.dataset.i]), 'studius-' + names[a.dataset.i].toLowerCase() + '.wav')); window.__stems = d.map(url); d.forEach((f, i) => result(o.querySelectorAll('audio')[i], { kind: 'audio', url: url(f), name: 'stem-' + names[i].toLowerCase() + '-' + Date.now() + '.wav', studio: 'stems' })); setS(el, '✔ ' + d.length + ' Stems'); badge(el, 'REAL');
} catch (e) { if (!el.isConnected) return; setS(el, '⚠ ' + e.message + ' – Simulation', true); badge(el, 'SIM'); el.querySelector('.out').innerHTML = ['Vocals', 'Drums', 'Bass', 'Other'].map(n => `<div class="row"><span style="width:52px">${n}</span><input type="range" value="80" aria-label="${n} Pegel"></div>`).join(''); }
finally { clearInterval(tm); go.disabled = false; } }; }; }
/* 5 CODE: sandboxed iframe runner + HTML/CSS live preview */
{ const s = S('code'); s.real = 'LOKAL'; s.build = (el) => {
el.innerHTML = `<div class="tabs"><button class="btn" data-m="js">JS</button><button class="btn alt" data-m="html">HTML/CSS</button><span class="badge real" style="align-self:center">LOKAL</span></div><textarea rows="6" spellcheck="false" aria-label="Code"></textarea><div class="row"><button class="btn run">▶ Run</button><span class="stat"></span></div><div class="code out" aria-live="polite">// Ausgabe</div><div class="pv"></div><div class="credit">Sandbox: iframe sandbox="allow-scripts" (keine Cookies, kein DOM-Zugriff) · Timeout 3 s</div>`;
const ta = el.querySelector('textarea'), out = el.querySelector('.out'), pv = el.querySelector('.pv'); keep(el); let mode = 'js', fr = null, deb = 0;
const SRC = { js: "function swarm(n){\n return Array.from({length:n},(_,i)=>'🐝'+i)\n}\nconsole.log('Hallo Schwarm')\nswarm(5)", html: '<style>\n h1{color:#f2c94c;font-family:sans-serif}\n body{background:#05182e}\n</style>\n<h1>Hallo STUDIUS</h1>\n<button onclick="this.textContent=\'🐝 summt\'">Klick</button>' };
const kill = () => { fr?.remove(); fr = null; }; onStop(kill); onStop(() => removeEventListener('message', onMsg));
let tok = '', to = 0; function onMsg(e) { if (!fr || e.source !== fr.contentWindow || e.data?.tok !== tok) return; clearTimeout(to); out.textContent = e.data.out; el.querySelector('.stat').textContent = e.data.ok ? '✔ ' + e.data.ms + ' ms' : '✖ Fehler'; kill(); }
addEventListener('message', onMsg);
const runJS = () => { kill(); tok = Math.random().toString(36).slice(2); out.textContent = '…'; fr = document.createElement('iframe'); fr.setAttribute('sandbox', 'allow-scripts'); fr.style.display = 'none'; fr.title = 'JS-Sandbox';
const code = JSON.stringify(ta.value.replace(/\n([^\n]*)\s*$/, (m, l) => /^\s*(return|const|let|var|function|class|if|for|while|}|\/\/)/.test(l) || !l.trim() ? m : '\nreturn (' + l.replace(/;\s*$/, '') + ')'));
const wsrc = `const L=[],t=performance.now();console.log=(...a)=>L.push(a.map(x=>typeof x==='string'?x:JSON.stringify(x)).join(' '));let ok=1,r;try{r=(new Function(${code}))();if(r!==undefined)L.push('> '+JSON.stringify(r))}catch(e){ok=0;L.push('✖ '+e.message)}postMessage({ok,out:L.join('\\n')||'(keine Ausgabe)',ms:Math.round(performance.now()-t)})`;
// user code runs in a Worker inside an opaque-origin sandboxed iframe: own thread (infinite loops can't freeze the 3D view), no DOM, no cookies/storage of this site
fr.srcdoc = `<script>const w=new Worker(URL.createObjectURL(new Blob([${JSON.stringify(wsrc).replace(/</g, '\\u003c')}],{type:'text/javascript'})));w.onmessage=e=>parent.postMessage(Object.assign({tok:'${tok}'},e.data),'*');w.onerror=e=>{e.preventDefault();parent.postMessage({tok:'${tok}',ok:0,out:'✖ '+e.message,ms:0},'*')}<\/script>`;
to = setTimeout(() => { if (fr) { kill(); out.textContent = '✖ Timeout (3 s) – Endlosschleife? Sandbox beendet.'; el.querySelector('.stat').textContent = '⏱ abgebrochen'; } }, 3000); onStop(() => clearTimeout(to)); pv.append(fr); };
const preview = () => { pv.innerHTML = ''; const f = document.createElement('iframe'); f.setAttribute('sandbox', 'allow-scripts'); f.title = 'HTML-Vorschau'; f.srcdoc = ta.value; pv.append(f); out.textContent = '// Live-Vorschau'; };
const setM = (m) => { mode = m; ta.value = SRC[m]; el.querySelectorAll('[data-m]').forEach(b => b.classList.toggle('alt', b.dataset.m !== m)); pv.innerHTML = ''; el.querySelector('.run').textContent = m === 'js' ? '▶ Run' : '↻ Vorschau'; if (m === 'html') preview(); else out.textContent = '// Ausgabe'; };
el.querySelectorAll('[data-m]').forEach(b => b.onclick = () => setM(b.dataset.m));
ta.addEventListener('input', () => { SRC[mode] = ta.value; if (mode === 'html') { clearTimeout(deb); deb = setTimeout(preview, 350); } });
el.querySelector('.run').onclick = () => mode === 'js' ? runJS() : preview(); setM('js'); }; }
/* 6a CANVAS persistence + PNG export */
const LS = (k, v) => { try { if (v === undefined) return localStorage.getItem(k); localStorage.setItem(k, v); } catch (e) { return null; } };
function pngBtn(el, c, name) { const a = document.createElement('a'); a.className = 'btn alt'; a.textContent = '⬇ PNG'; a.href = '#'; a.onclick = (e) => { a.href = c.toDataURL('image/png'); a.download = name; }; el.querySelector('.row:last-of-type').append(a); }
{ const s = S('canvas'); const base = s.build; s.real = 'LOKAL'; s.build = (el) => { base(el); const c = el.querySelector('canvas'), x = c.getContext('2d'); const sv = LS('studius-canvas');
if (sv) { const im = new Image(); im.onload = () => { x.save(); x.setTransform(1, 0, 0, 1, 0, 0); x.drawImage(im, 0, 0, c.width, c.height); x.restore(); }; im.src = sv; }
const save = () => LS('studius-canvas', c.toDataURL('image/png')); c.addEventListener('pointerup', save); el.querySelector('.cl').addEventListener('click', save); pngBtn(el, c, 'studius-canvas.png');
el.insertAdjacentHTML('afterbegin', '<div class="row" style="margin:0 0 6px"><span class="badge real">LOKAL</span><span class="stat">automatisch gespeichert (localStorage)</span></div>'); }; }
/* 6b DESIGN board persistence + PNG */
{ const s = S('design'); s.real = 'LOKAL'; s.build = (el) => {
el.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">LOKAL</span><span class="stat">Klick = Form · Ziehen = verschieben · gespeichert</span></div><canvas></canvas><div class="row"><button class="btn" data-t="rect" aria-label="Rechteck">▭</button><button class="btn alt" data-t="hex" aria-label="Hexagon">⬡</button><button class="btn alt" data-t="text" aria-label="Text">T</button><button class="btn alt undo">↶</button><button class="btn alt clr">Leeren</button></div>`;
const [c, x, w, h] = cv(el, 140); let tool = 'rect', drag = null; let shapes; try { shapes = JSON.parse(LS('studius-design')) || null; } catch (e) {} shapes ||= [{ t: 'hex', x: 60, y: 60 }, { t: 'rect', x: 150, y: 50 }, { t: 'text', x: 200, y: 95 }];
const save = () => LS('studius-design', JSON.stringify(shapes.slice(-300)));
el.querySelectorAll('[data-t]').forEach(b => b.onclick = () => { tool = b.dataset.t; el.querySelectorAll('[data-t]').forEach(o => o.classList.toggle('alt', o !== b)); });
el.querySelector('.undo').onclick = () => { shapes.pop(); save(); }; el.querySelector('.clr').onclick = () => { shapes = []; save(); };
const pos = e => { const r = c.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; };
c.onpointerdown = e => { const [mx, my] = pos(e); drag = [...shapes].reverse().find(s => Math.abs(s.x - mx) < 22 && Math.abs(s.y - my) < 16); if (drag) { c.setPointerCapture(e.pointerId); drag.m = 0; } else { shapes.push({ t: tool, x: mx, y: my }); save(); } };
c.onpointermove = e => { if (!drag) return; const [mx, my] = pos(e); drag.x = Math.max(0, Math.min(w, mx)); drag.y = Math.max(0, Math.min(h, my)); };
c.onpointerup = () => { if (drag) { delete drag.m; drag = null; save(); } };
loop(() => { x.fillStyle = '#0b0b0b'; x.fillRect(0, 0, w, h); x.fillStyle = '#1d1d1d'; for (let i = 0; i < w; i += 12) for (let j = 0; j < h; j += 12) x.fillRect(i, j, 1, 1);
shapes.forEach(s => { x.strokeStyle = s.t === 'hex' ? '#f2c94c' : '#faa968'; x.lineWidth = 1.5; if (s.t === 'rect') x.strokeRect(s.x - 22, s.y - 14, 44, 28); else if (s.t === 'hex') { x.beginPath(); for (let k = 0; k < 7; k++) { const a = k * Math.PI / 3; x.lineTo(s.x + 20 * Math.cos(a), s.y + 20 * Math.sin(a)); } x.stroke(); } else { x.fillStyle = '#8cbfb8'; x.font = '12px Outfit,sans-serif'; x.fillText('Idee', s.x, s.y); } }); });
pngBtn(el, c, 'studius-design.png'); }; }
/* 7a VIDEO EDIT: local file, real playback, trim in/out preview */
{ const s = S('videoedit'); const sim = s.build; s.real = 'LOKAL'; s.build = (el) => {
el.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">LOKAL</span><span class="stat">Video laden (bleibt lokal im Browser)</span></div><input type="file" accept="video/*" aria-label="Videodatei"><div class="vw"></div><div class="sim"></div>`;
sim(el.querySelector('.sim')); const fi = el.querySelector('input');
fi.onchange = () => { const f = fi.files[0]; if (!f) return; const u = URL.createObjectURL(f); onStop(() => URL.revokeObjectURL(u)); el.querySelector('.sim').remove();
const vw = el.querySelector('.vw'); vw.innerHTML = `<video playsinline muted src="${u}"></video><div class="row">In<input type="range" class="ti" min="0" max="1000" value="0" aria-label="Trim In">Out<input type="range" class="to" min="0" max="1000" value="1000" aria-label="Trim Out"></div><div class="row"><button class="btn pp">▶ Trim-Vorschau</button><span class="tc code" style="padding:2px 6px">00:00.0</span></div>`;
const v = vw.querySelector('video'), ti = vw.querySelector('.ti'), to = vw.querySelector('.to'); const T = () => [ti.value / 1000 * (v.duration || 0), to.value / 1000 * (v.duration || 0)]; const fmt = s => String(Math.floor(s / 60)).padStart(2, '0') + ':' + (s % 60).toFixed(1).padStart(4, '0');
const sync = () => { if (+ti.value > +to.value - 10) ti.value = +to.value - 10; const [a, b] = T(); v.currentTime = a; setS(el, `${f.name} · Schnitt ${fmt(a)} – ${fmt(b)} (${(b - a).toFixed(1)} s)`); }; ti.oninput = sync; to.oninput = sync;
v.onloadedmetadata = () => { sync(); window.__video = { dur: v.duration, w: v.videoWidth, h: v.videoHeight }; };
v.ontimeupdate = () => { const [a, b] = T(); vw.querySelector('.tc').textContent = fmt(v.currentTime); if (v.currentTime >= b) { v.pause(); v.currentTime = a; vw.querySelector('.pp').textContent = '▶ Trim-Vorschau'; } };
vw.querySelector('.pp').onclick = (e) => { if (v.paused) { const [a, b] = T(); if (v.currentTime < a || v.currentTime >= b) v.currentTime = a; v.play(); e.target.textContent = '❚❚ Pause'; } else { v.pause(); e.target.textContent = '▶ Trim-Vorschau'; } };
onStop(() => { v.pause(); v.removeAttribute('src'); v.load(); }); }; }; }
/* 7b AUDIO EDIT: local file → real waveform, selection, gain, playback, trimmed WAV export */
function wav(buf, a, b, g) { const ch = buf.numberOfChannels, sr = buf.sampleRate, s0 = Math.floor(a * sr), n = Math.max(1, Math.floor(b * sr) - s0), out = new DataView(new ArrayBuffer(44 + n * ch * 2)); const w = (o, s) => [...s].forEach((c, i) => out.setUint8(o + i, c.charCodeAt(0)));
w(0, 'RIFF'); out.setUint32(4, 36 + n * ch * 2, true); w(8, 'WAVEfmt '); out.setUint32(16, 16, true); out.setUint16(20, 1, true); out.setUint16(22, ch, true); out.setUint32(24, sr, true); out.setUint32(28, sr * ch * 2, true); out.setUint16(32, ch * 2, true); out.setUint16(34, 16, true); w(36, 'data'); out.setUint32(40, n * ch * 2, true);
const d = [...Array(ch)].map((_, c) => buf.getChannelData(c)); for (let i = 0; i < n; i++) for (let c = 0; c < ch; c++) out.setInt16(44 + (i * ch + c) * 2, Math.max(-1, Math.min(1, d[c][s0 + i] * g)) * 32767, true); return new Blob([out], { type: 'audio/wav' }); }
{ const s = S('audio'); const sim = s.build; s.real = 'LOKAL'; s.build = (el) => {
el.innerHTML = `<div class="row" style="margin:0 0 6px"><span class="badge real">LOKAL</span><span class="stat">Audio laden · Bereich ziehen = Auswahl</span></div><input type="file" accept="audio/*" aria-label="Audiodatei"><div class="sim"></div>`;
sim(el.querySelector('.sim')); const fi = el.querySelector('input');
fi.onchange = async () => { const f = fi.files[0]; if (!f) return; let ac; try { ac = new (window.AudioContext || window.webkitAudioContext)(); onStop(() => ac.close()); const buf = await ac.decodeAudioData(await f.arrayBuffer()); if (!el.isConnected) return;
const box = el.querySelector('.sim'); box.innerHTML = `<canvas></canvas><div class="row">Gain<input type="range" class="g" min="0" max="200" value="100" aria-label="Gain"><button class="btn alt nm">Normalize</button></div><div class="row"><button class="btn pp">▶ Auswahl abspielen</button><a class="btn alt ex">⬇ WAV-Schnitt</a></div>`;
const [c, x, w, h] = cv(box, 90); const D = buf.getChannelData(0), N = Math.floor(w), step = Math.max(1, Math.floor(D.length / N)); const pk = [...Array(N)].map((_, i) => { let m = 0; for (let j = i * step; j < (i + 1) * step && j < D.length; j += 4) m = Math.max(m, Math.abs(D[j])); return m; });
let gain = 1, sel = [0, w], src = null, t0 = 0, off = 0; const gn = ac.createGain(); gn.connect(ac.destination); const T = () => [Math.min(...sel) / w * buf.duration, Math.max(...sel) / w * buf.duration];
const upd = () => { const [a, b] = T(); setS(el, `${f.name} · ${a.toFixed(2)}–${b.toFixed(2)} s · Gain ${gain.toFixed(2)}`); window.__audioSel = [a, b, gain, buf.duration]; }; upd();
box.querySelector('.g').oninput = e => { gain = e.target.value / 100; gn.gain.value = gain; upd(); }; box.querySelector('.nm').onclick = () => { gain = 1 / Math.max(.01, ...pk); gn.gain.value = gain; box.querySelector('.g').value = Math.min(200, gain * 100); upd(); };
c.onpointerdown = e => { const r = c.getBoundingClientRect(); c.setPointerCapture(e.pointerId); sel = [e.clientX - r.left, e.clientX - r.left]; c.onpointermove = m => { sel[1] = Math.max(0, Math.min(w, m.clientX - r.left)); }; c.onpointerup = () => { c.onpointermove = null; if (Math.abs(sel[1] - sel[0]) < 3) sel = [0, w]; upd(); }; };
const stop = () => { try { src?.stop(); } catch (e) {} src = null; box.querySelector('.pp').textContent = '▶ Auswahl abspielen'; }; onStop(stop);
box.querySelector('.pp').onclick = async () => { if (src) return stop(); await ac.resume(); const [a, b] = T(); src = ac.createBufferSource(); src.buffer = buf; src.connect(gn); src.start(0, a, b - a); t0 = ac.currentTime; off = a; src.onended = stop; box.querySelector('.pp').textContent = '■ Stop'; };
const ex = box.querySelector('.ex'); ex.onclick = () => { const [a, b] = T(); if (ex.href) URL.revokeObjectURL(ex.href); ex.href = URL.createObjectURL(wav(buf, a, b, gain)); ex.download = 'studius-trim.wav'; };
loop(() => { x.fillStyle = '#000'; x.fillRect(0, 0, w, h); x.fillStyle = 'rgba(2,131,145,.35)'; x.fillRect(Math.min(...sel), 0, Math.abs(sel[1] - sel[0]), h); x.strokeStyle = '#f2c94c'; x.beginPath(); for (let i = 0; i < N; i++) { const v = Math.min(1, pk[i] * gain); x.moveTo(i + .5, h / 2 - v * h / 2.1); x.lineTo(i + .5, h / 2 + v * h / 2.1); } x.stroke();
if (src) { const px = (off + ac.currentTime - t0) / buf.duration * w; x.fillStyle = '#f85525'; x.fillRect(px, 0, 1.5, h); } });
} catch (e) { setS(el, '⚠ Audio nicht lesbar: ' + e.message, true); } }; }; }
export const REAL = STUDIOS.filter(s => s.real).map(s => [s.id, s.real]);
/* ---------- hand-off: studios that accept assets ---------- */
const feed = (el, sel, file) => { const fi = el.querySelector(sel); if (!fi) return false; const dt = new DataTransfer(); dt.items.add(file); fi.files = dt.files; fi.dispatchEvent(new Event('change')); return true; };
const toFile = async (a) => a.file || new File([a.blob || await (await fetch(a.url)).blob()], a.name || 'asset', { type: a.mime || '' });
S('audio').accepts = ['audio']; S('audio').accept = async (el, a) => feed(el, 'input[type=file]', await toFile(a));
S('stems').accepts = ['audio']; S('stems').accept = async (el, a) => feed(el, 'input[type=file]', await toFile(a));
S('canvas').accepts = ['image']; S('canvas').accept = async (el, a) => { const f = await toFile(a); const bm = await createImageBitmap(f); const c = el.querySelector('canvas'), x = c.getContext('2d'); x.save(); x.setTransform(1, 0, 0, 1, 0, 0); const r = Math.min(c.width / bm.width, c.height / bm.height); x.drawImage(bm, (c.width - bm.width * r) / 2, (c.height - bm.height * r) / 2, bm.width * r, bm.height * r); x.restore(); c.dispatchEvent(new PointerEvent('pointerup')); return true; };
S('design').accepts = ['image']; S('design').accept = S('canvas').accept;
S('videoedit').accepts = ['video', 'image', 'audio']; S('videoedit').accept = async (el, a) => { const f = await toFile(a);
if (a.kind === 'video') return feed(el, 'input[type=file]', f);
const u = URL.createObjectURL(f); onStop(() => URL.revokeObjectURL(u)); const vw = el.querySelector('.vw');
vw.insertAdjacentHTML('beforeend', a.kind === 'image' ? `<div class="row"><b>Standbild-Clip</b><small>${esc(a.name || '')}</small></div><img alt="Clip" src="${u}" style="width:100%;border:1px solid rgba(242,201,76,.2)">` : `<div class="row"><b>Audiospur</b><small>${esc(a.name || '')}</small></div><audio controls src="${u}"></audio>`); return true; };
S('code').accepts = ['text']; S('code').accept = async (el, a) => { const ta = el.querySelector('textarea'); ta.value = a.text ?? await (await toFile(a)).text(); ta.dispatchEvent(new Event('input')); return true; };
S('chat').accepts = ['text']; S('chat').accept = async (el, a) => { const i = el.querySelector('input'); i.value = (a.text ?? await (await toFile(a)).text()).slice(0, 2000); i.focus(); return true; };
/* ---------- Module API contract (agents operate studios as tools) ---------- */
export const MODULES = {
chat: { id: 'chat', name: 'Chat', backend: CHAT, inputs: [{ name: 'prompt', type: 'text' }], outputs: [{ kind: 'text' }],
actions: { reply: { desc: 'Antwortet auf eine Textfrage', params: { prompt: 'string' }, run: async ({ prompt }, on = {}) => { let out = ''; await run(CHAT, '/generate', { message: prompt, max_new_tokens: 350, temperature: .5, top_p: .9, top_k: 50, repetition_penalty: 1.2 }, { timeout: 90000, onStatus: on.status, onData: d => { const t = String(d[0] ?? ''); out = t.startsWith(out) ? t : out + t; on.data?.(out); } }); return { kind: 'text', mime: 'text/plain', text: out, name: 'agent-' + Date.now() + '.txt', studio: 'chat', prompt }; } } } },
image: { id: 'image', name: 'Image Generate', backend: FLUX, inputs: [{ name: 'prompt', type: 'text' }], outputs: [{ kind: 'image' }],
actions: { generate: { desc: 'Erzeugt ein Bild aus einem Prompt (FLUX.1-schnell)', params: { prompt: 'string' }, run: async ({ prompt }, on = {}) => { const d = await run(FLUX, '/infer', { prompt, seed: 0, randomize_seed: true, width: 512, height: 512, num_inference_steps: 4 }, { onStatus: on.status }); return { kind: 'image', mime: 'image/webp', url: url(d[0]), name: 'flux-' + d[1] + '.webp', studio: 'image', prompt }; } } } },
music: { id: 'music', name: 'Music Gen', backend: TF, inputs: [{ name: 'prompt', type: 'text' }], outputs: [{ kind: 'audio' }],
actions: { compose: { desc: 'Erzeugt 10 s Musik/Sound (TangoFlux)', params: { prompt: 'string' }, run: async ({ prompt }, on = {}) => { const d = await run(TF, '/predict', { prompt, steps: 25, guidance: 4.5, duration: 10 }, { onStatus: on.status }); return { kind: 'audio', url: url(d[0] ?? d), name: 'music-' + Date.now() + '.wav', studio: 'music', prompt }; } } } },
};
window.STUDIUS_MODULES = MODULES;
export { KONTEXT, TTS, esc, hf, stTxt, url, badge, dl, timer, setS, S, keep, LS, CHAT, FLUX, ACE, TF, SEP };
|