AnjaneyaTurai commited on
Commit
0d75c17
·
verified ·
1 Parent(s): 2e475a0

Deploy the StemDeck GPU service

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -147,7 +147,8 @@ def _run_job(kind: str, audio_path: str, params: dict[str, Any], source_name: st
147
  db.get_engine.cache_clear()
148
  settings = get_settings()
149
  song = Song(title="job", owner="space", quality=str(params.get("quality", "best")))
150
- folder = settings.songs_dir / song.id
 
151
  folder.mkdir(parents=True, exist_ok=True)
152
  song.duration_sec = round(probe_duration(Path(audio_path)), 3)
153
  with Session(db.get_engine()) as session:
@@ -160,12 +161,12 @@ def _run_job(kind: str, audio_path: str, params: dict[str, Any], source_name: st
160
  stems_dir.mkdir(parents=True, exist_ok=True)
161
  source_file = stems_dir / f"{source_name}.flac"
162
  shutil.copyfile(audio_path, source_file)
163
- session.add(Stem(song_id=song.id, name=str(source_name), label=str(source_name), kind="base",
164
  file_path=str(source_file)))
165
  for name in existing or []:
166
  if name != source_name:
167
- session.add(Stem(song_id=song.id, name=name, label=name, file_path=""))
168
- job = Job(song_id=song.id, type=kind, params=params, status="running", started_at=utcnow())
169
  session.add(job)
170
  session.commit()
171
  job_id = job.id
@@ -174,15 +175,16 @@ def _run_job(kind: str, audio_path: str, params: dict[str, Any], source_name: st
174
  job = session.get(Job, job_id)
175
  if job is None or job.status != "done":
176
  raise gr.Error((job.error if job else None) or "Processing failed. Please try again.")
177
- song = session.get(Song, song.id)
178
- rows = session.exec(select(Stem).where(Stem.song_id == song.id).order_by(Stem.order)).all()
 
179
  stems = [{"name": r.name, "label": r.label, "kind": r.kind, "detected": r.detected,
180
  "experimental": r.experimental, "color": r.color, "rms_db": r.rms_db, "order": r.order,
181
  "file": r.file_path} for r in rows if r.file_path and Path(r.file_path).is_file()]
182
  if kind == "extract": # only what changed: the new stems and the rewritten source stem
183
  unchanged = set(existing or []) - {source_name}
184
  stems = [s for s in stems if s["name"] not in unchanged]
185
- result: dict[str, Any] = {"song": {"duration_sec": song.duration_sec, "null_test_snr_db": song.null_test_snr_db},
186
  "stems": stems, "analysis": None, "peaks": None}
187
  for key, name in (("analysis", "analysis.json"), ("peaks", "peaks.json")):
188
  path = folder / name
@@ -217,6 +219,18 @@ def _chords(result: dict[str, Any]) -> list[dict[str, Any]]:
217
  return []
218
 
219
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  def _response(result: dict[str, Any]) -> tuple[dict[str, Any], list[str]]:
221
  files = [s["file"] for s in result["stems"]]
222
  meta = {**result, "stems": [{**{k: v for k, v in s.items() if k != "file"}, "file": Path(s["file"]).name}
@@ -233,7 +247,7 @@ def split(audio: str | None, quality: str) -> tuple[dict[str, Any], list[str]]:
233
  _check_song(audio)
234
  _clean_old_jobs()
235
  _prefetch_done.wait()
236
- result = _split_on_gpu(str(audio), quality)
237
  result["chords"] = _chords(result)
238
  return _response(result)
239
 
@@ -253,7 +267,7 @@ def extract(audio: str | None, source_name: str, existing_names: str,
253
  _check_song(audio)
254
  _clean_old_jobs()
255
  _prefetch_done.wait()
256
- result = _extract_on_gpu(str(audio), source_name, existing, extractor)
257
  return _response(result)
258
 
259
 
 
147
  db.get_engine.cache_clear()
148
  settings = get_settings()
149
  song = Song(title="job", owner="space", quality=str(params.get("quality", "best")))
150
+ song_id = song.id # read once: after a commit the object is expired and detached from its session
151
+ folder = settings.songs_dir / song_id
152
  folder.mkdir(parents=True, exist_ok=True)
153
  song.duration_sec = round(probe_duration(Path(audio_path)), 3)
154
  with Session(db.get_engine()) as session:
 
161
  stems_dir.mkdir(parents=True, exist_ok=True)
162
  source_file = stems_dir / f"{source_name}.flac"
163
  shutil.copyfile(audio_path, source_file)
164
+ session.add(Stem(song_id=song_id, name=str(source_name), label=str(source_name), kind="base",
165
  file_path=str(source_file)))
166
  for name in existing or []:
167
  if name != source_name:
168
+ session.add(Stem(song_id=song_id, name=name, label=name, file_path=""))
169
+ job = Job(song_id=song_id, type=kind, params=params, status="running", started_at=utcnow())
170
  session.add(job)
171
  session.commit()
172
  job_id = job.id
 
175
  job = session.get(Job, job_id)
176
  if job is None or job.status != "done":
177
  raise gr.Error((job.error if job else None) or "Processing failed. Please try again.")
178
+ song = session.get(Song, song_id)
179
+ duration_sec, null_test = (song.duration_sec, song.null_test_snr_db) if song else (None, None)
180
+ rows = session.exec(select(Stem).where(Stem.song_id == song_id).order_by(Stem.order)).all()
181
  stems = [{"name": r.name, "label": r.label, "kind": r.kind, "detected": r.detected,
182
  "experimental": r.experimental, "color": r.color, "rms_db": r.rms_db, "order": r.order,
183
  "file": r.file_path} for r in rows if r.file_path and Path(r.file_path).is_file()]
184
  if kind == "extract": # only what changed: the new stems and the rewritten source stem
185
  unchanged = set(existing or []) - {source_name}
186
  stems = [s for s in stems if s["name"] not in unchanged]
187
+ result: dict[str, Any] = {"song": {"duration_sec": duration_sec, "null_test_snr_db": null_test},
188
  "stems": stems, "analysis": None, "peaks": None}
189
  for key, name in (("analysis", "analysis.json"), ("peaks", "peaks.json")):
190
  path = folder / name
 
219
  return []
220
 
221
 
222
+ def _on_gpu(run: Any, *args: Any) -> dict[str, Any]:
223
+ """Run a GPU job. Gradio's API hides the message of anything but gr.Error, so unexpected failures are turned
224
+ into a readable gr.Error (quota errors from ZeroGPU already are gr.Error and pass through)."""
225
+ try:
226
+ return run(*args)
227
+ except gr.Error:
228
+ raise
229
+ except Exception as exc: # noqa: BLE001 - logged in full here, summarised for the visitor
230
+ log.exception("GPU job failed")
231
+ raise gr.Error(f"Processing failed ({type(exc).__name__}). Please try again.") from exc
232
+
233
+
234
  def _response(result: dict[str, Any]) -> tuple[dict[str, Any], list[str]]:
235
  files = [s["file"] for s in result["stems"]]
236
  meta = {**result, "stems": [{**{k: v for k, v in s.items() if k != "file"}, "file": Path(s["file"]).name}
 
247
  _check_song(audio)
248
  _clean_old_jobs()
249
  _prefetch_done.wait()
250
+ result = _on_gpu(_split_on_gpu, str(audio), quality)
251
  result["chords"] = _chords(result)
252
  return _response(result)
253
 
 
267
  _check_song(audio)
268
  _clean_old_jobs()
269
  _prefetch_done.wait()
270
+ result = _on_gpu(_extract_on_gpu, str(audio), source_name, existing, extractor)
271
  return _response(result)
272
 
273