patchy-planet commited on
Commit
191d479
·
verified ·
1 Parent(s): 6157b88

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import sys
4
+
5
+ sys.path.insert(0, ".")
6
+
7
+ # Models are loaded at startup, not per-request.
8
+ # Cold start time on CPU Basic: 3–8 min. This cost is paid once per Space
9
+ # wake-up, not on every extraction call.
10
+ from feature_extractor_cpu import extract_mir_features, ENG_DIMS
11
+ from clap_embedder import get_clap_embedding
12
+
13
+ print("[startup] All models loaded. Space ready.")
14
+
15
+
16
+ def extract(audio_file_path: str) -> str:
17
+ """
18
+ Receives a local file path from Gradio (gr.File type='filepath').
19
+ Returns JSON with keys: l3, clap, eng_raw, true_peak_dbfs.
20
+ Any exception propagates to gradio_client as a job error — no silent failures.
21
+ """
22
+ if audio_file_path is None:
23
+ raise ValueError("No audio file received.")
24
+
25
+ l3, eng_raw, true_peak = extract_mir_features(audio_file_path)
26
+ clap = get_clap_embedding(audio_file_path)
27
+
28
+ if len(eng_raw) != len(ENG_DIMS):
29
+ raise ValueError(
30
+ f"eng_raw length mismatch: got {len(eng_raw)}, expected {len(ENG_DIMS)}"
31
+ )
32
+
33
+ return json.dumps({
34
+ "l3": l3.tolist(),
35
+ "clap": clap.tolist(),
36
+ "eng_raw": eng_raw.tolist(),
37
+ "true_peak_dbfs": float(true_peak),
38
+ })
39
+
40
+
41
+ # demo.queue() is REQUIRED — not optional.
42
+ # Without it, Gradio's async event loop kills any function that runs longer
43
+ # than a few seconds. CPU demucs with shifts=5 runs for 33–136 min per track.
44
+ # queue() enables proper long-running background job execution.
45
+ demo = gr.Interface(
46
+ fn=extract,
47
+ inputs=gr.File(label="Audio file (.mp3 or .wav)", type="filepath"),
48
+ outputs=gr.Textbox(label="Feature JSON"),
49
+ title="Playlisting Feature Extractor (CPU Basic)",
50
+ )
51
+ demo.queue(max_size=50)
52
+
53
+ if __name__ == "__main__":
54
+ demo.launch(show_error=True)