multimodalart HF Staff commited on
Commit
8cc4a31
·
verified ·
1 Parent(s): adb3ad3

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +37 -11
app.py CHANGED
@@ -95,6 +95,12 @@ async def set_collider(request: Request):
95
  "cfg_drums": float(body.get("cfg_drums", 4.0)),
96
  "model": body.get("model", prev.get("model", "mrt2_base")),
97
  "buffer": int(body.get("buffer", 0)),
 
 
 
 
 
 
98
  })
99
  return {"ok": True, "tokens": tokens}
100
 
@@ -117,32 +123,52 @@ def stream(session_id: str) -> str:
117
  t0 = time.time()
118
  buf_log, buf_t = [], t0
119
  last_enc = 0.0
 
 
 
120
  while time.time() - t0 < 55.0:
121
  c = read_slot(session_id)
122
  if c is None:
123
  time.sleep(0.02)
124
  continue
125
  mname = c.get("model", "mrt2_base")
126
- if mname != cur_name: # live model switch -> re-init state
127
- cur_name, model = mname, MODELS.get(mname, mrt_base)
 
128
  dstate = model.model.decoder.init_streaming_f(1, dev, dt)
129
  history = torch.zeros((1, 0, model.cfg.num_codebooks), dtype=torch.long, device=dev)
130
- emitted, cur_tokens, cur_notes, source = 0, None, None, None
 
 
 
 
131
  toks = []
132
  for _ in range(10): # per-frame slot read -> steering applies ~instantly
133
  c = read_slot(session_id) or c
134
  tokens = c["style_tokens"]
135
- active = c.get("notes") or []
136
- if source is None or ((tokens != cur_tokens or active != cur_notes) and time.time() - last_enc >= 0.2):
137
- cur_tokens, cur_notes = tokens, active
138
- last_enc = time.time()
139
- aset = set(active)
140
- notes = [8 if i in aset else -1 for i in range(128)]
141
- cfgs = [discretize_cfg(c.get("cfg_musiccoca", 3.0), 0.2, 40),
 
 
 
 
 
 
 
 
 
 
 
 
142
  discretize_cfg(c.get("cfg_notes", 2.4), 0.2, 40),
143
  discretize_cfg(c.get("cfg_drums", 4.0), 1.0, 8)]
144
  cond = model._conditioning((list(tokens) + [-1] * model.num_musiccoca)[:model.num_musiccoca],
145
- notes, drums, cfgs)
146
  source = model.model.encode(cond).to(dt)
147
  sampler = make_sampler(c.get("temperature", 1.3), c.get("top_k", 40), gen)
148
  toks.append(model.model.decoder.step_f(dstate, source, sampler=sampler,
 
95
  "cfg_drums": float(body.get("cfg_drums", 4.0)),
96
  "model": body.get("model", prev.get("model", "mrt2_base")),
97
  "buffer": int(body.get("buffer", 0)),
98
+ "notes": list(body.get("notes", [])),
99
+ "unmaskwidth": int(body.get("unmaskwidth", 0)),
100
+ "drumless": bool(body.get("drumless", False)),
101
+ "onsetmode": bool(body.get("onsetmode", False)),
102
+ "reset": int(body.get("reset", 0)),
103
+ "seed": int(body.get("seed", 0)),
104
  })
105
  return {"ok": True, "tokens": tokens}
106
 
 
123
  t0 = time.time()
124
  buf_log, buf_t = [], t0
125
  last_enc = 0.0
126
+ cur_sig = None
127
+ prev_active = set()
128
+ cur_reset = cur_seed = 0
129
  while time.time() - t0 < 55.0:
130
  c = read_slot(session_id)
131
  if c is None:
132
  time.sleep(0.02)
133
  continue
134
  mname = c.get("model", "mrt2_base")
135
+ reset = int(c.get("reset", 0))
136
+ if mname != cur_name or reset != cur_reset: # model switch / state reset -> re-init
137
+ cur_name, model, cur_reset = mname, MODELS.get(mname, mrt_base), reset
138
  dstate = model.model.decoder.init_streaming_f(1, dev, dt)
139
  history = torch.zeros((1, 0, model.cfg.num_codebooks), dtype=torch.long, device=dev)
140
+ emitted, source, cur_sig, prev_active = 0, None, None, set()
141
+ seed = int(c.get("seed", 0))
142
+ if seed != cur_seed:
143
+ cur_seed = seed
144
+ gen = torch.Generator(device=dev).manual_seed(seed)
145
  toks = []
146
  for _ in range(10): # per-frame slot read -> steering applies ~instantly
147
  c = read_slot(session_id) or c
148
  tokens = c["style_tokens"]
149
+ active = set(c.get("notes") or [])
150
+ unmask = int(c.get("unmaskwidth", 0))
151
+ onsetmode = bool(c.get("onsetmode", False))
152
+ drumless = bool(c.get("drumless", False))
153
+ sig = (tuple(tokens), tuple(sorted(active)), unmask, onsetmode, drumless)
154
+ if source is None or (sig != cur_sig and time.time() - last_enc >= 0.2):
155
+ onsets = active - prev_active
156
+ prev_active = set(active)
157
+ cur_sig, last_enc = sig, time.time()
158
+ nvec = [] # per-pitch: -1 masked / 0 off / 1 cont / 2 onset / 3 on
159
+ for pitch in range(128):
160
+ if pitch in active:
161
+ nvec.append((2 if pitch in onsets else 1) if onsetmode else 3)
162
+ elif active and (unmask >= 127 or any(abs(pitch - h) <= unmask for h in active)):
163
+ nvec.append(0) # solo / unmasked-off
164
+ else:
165
+ nvec.append(-1) # masked (model free)
166
+ drm = [0] if drumless else [-1] # 0 no-drum / -1 masked
167
+ cfgs = [discretize_cfg(c.get("cfg_musiccoca", 1.6), 0.2, 40),
168
  discretize_cfg(c.get("cfg_notes", 2.4), 0.2, 40),
169
  discretize_cfg(c.get("cfg_drums", 4.0), 1.0, 8)]
170
  cond = model._conditioning((list(tokens) + [-1] * model.num_musiccoca)[:model.num_musiccoca],
171
+ nvec, drm, cfgs)
172
  source = model.model.encode(cond).to(dt)
173
  sampler = make_sampler(c.get("temperature", 1.3), c.get("top_k", 40), gen)
174
  toks.append(model.model.decoder.step_f(dstate, source, sampler=sampler,