harp-dev Claude Opus 5.5 commited on
Commit
898630d
ยท
verified ยท
1 Parent(s): f86f3b3

Build both stages once in the worker, and declare the backbone's license

Browse files

Builds both stages once, in the worker, rather than per call, moves the beat checkpoint link and downloads into the worker, declares CC-BY-NC-4.0, the license of the MERT backbone, and says so in the README, and declares 24 kHz mono. It also imports spaces plainly, since it is always installed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Files changed (3) hide show
  1. README.md +4 -2
  2. app.py +44 -63
  3. inference.py +26 -10
README.md CHANGED
@@ -10,18 +10,20 @@ preload_from_hub:
10
  - teamup-tech/FST-AI-Music-Detection-checkpoints
11
  app_file: app.py
12
  pinned: false
13
- license: gpl-3.0
14
  short_description: Detects AI-generated music from structure and MERT
15
  tags:
16
  - category:analysis
17
  - subcategory:music-analysis
18
  - input:audio
19
  - output:file/txt
 
 
20
  - music
21
  ---
22
 
23
  Detects AI-generated music from structure and MERT.
24
 
25
- Deploys [FST-AI-Music-Detection](https://github.com/Mippia/FST-AI-Music-Detection) for HARP. Upstream states its license in its README rather than in a `LICENSE` file, which is the basis for the `license` field above: upstream Mippia/FST-AI-Music-Detection licenses the code GPL in its README; the checkpoints are unstated.
26
 
27
  A HARP deployment. See https://github.com/TEAMuP-dev/HARP
 
10
  - teamup-tech/FST-AI-Music-Detection-checkpoints
11
  app_file: app.py
12
  pinned: false
13
+ license: cc-by-nc-4.0
14
  short_description: Detects AI-generated music from structure and MERT
15
  tags:
16
  - category:analysis
17
  - subcategory:music-analysis
18
  - input:audio
19
  - output:file/txt
20
+ - sample-rate:24000
21
+ - channels:mono
22
  - music
23
  ---
24
 
25
  Detects AI-generated music from structure and MERT.
26
 
27
+ Deploys [FST-AI-Music-Detection](https://github.com/Mippia/FST-AI-Music-Detection) for HARP. Upstream states its license in its README rather than in a `LICENSE` file: Mippia/FST-AI-Music-Detection licenses the code GPL, and leaves the checkpoints unstated. The model runs on the MERT-v1-95M backbone, whose weights are CC-BY-NC-4.0, so the `license` field above declares that, the more restrictive of the two.
28
 
29
  A HARP deployment. See https://github.com/TEAMuP-dev/HARP
app.py CHANGED
@@ -1,21 +1,8 @@
 
 
1
  import sys
2
  sys.stdout.reconfigure(line_buffering=True)
3
 
4
- try:
5
- import spaces
6
- except ImportError:
7
- # keep @spaces.GPU usable as a no-op; ZeroGPU requires this exact name.
8
- class spaces:
9
- class GPU:
10
- def __init__(self, func=None, duration=60):
11
- self.func = func
12
-
13
- def __call__(self, *args, **kwargs):
14
- if self.func is not None:
15
- return self.func(*args, **kwargs)
16
- func = args[0]
17
- return func
18
-
19
  import gradio as gr
20
  import torch
21
  import librosa
@@ -79,83 +66,77 @@ torchaudio.info = _patched_info
79
 
80
  import contextlib
81
  import tempfile
82
- import threading
83
  from pathlib import Path
84
 
85
  from huggingface_hub import hf_hub_download
86
  from pyharp import ModelCard, build_endpoint
87
- from pyharp.tags import Subcategory
88
-
89
- from inference import inference
90
-
91
- # beat_this downloads its checkpoint from the JKU cloud on first use, which has failed both inside
92
- # a GPU call (no name resolution) and at startup (503). The same file is mirrored on the Hub and
93
- # linked where torch.hub looks before downloading.
94
- _beat_checkpoint = Path(torch.hub.get_dir()) / "checkpoints" / "beat_this-final0.ckpt"
95
- if not _beat_checkpoint.exists():
96
- _beat_checkpoint.parent.mkdir(parents=True, exist_ok=True)
97
- with contextlib.suppress(FileExistsError):
98
- _beat_checkpoint.symlink_to(hf_hub_download(
99
- "teamup-tech/Beat-This-weights", "final0.ckpt",
100
- revision="4afbe376535aaab71b76ae2ca2d7c16b08877321",
101
- ))
 
 
102
 
 
103
  CHECKPOINT_REPO = "teamup-tech/FST-AI-Music-Detection-checkpoints"
104
  CHECKPOINT_DIR = Path("checkpoints")
105
 
106
- checkpoints_loading = True
107
  checkpoint_error = None
108
 
109
 
110
- def download_checkpoints():
111
- """Fetch the Stage-1 and Stage-2 checkpoints so inference.py finds them
112
- already on disk instead of downloading ~1.3 GB on the first request."""
113
- global checkpoints_loading, checkpoint_error
114
  try:
 
 
 
 
 
 
 
 
 
 
 
 
115
  CHECKPOINT_DIR.mkdir(exist_ok=True)
116
  for filename in ("Stage-1.ckpt", "Stage-2.ckpt"):
117
  hf_hub_download(repo_id=CHECKPOINT_REPO, filename=filename, local_dir=str(CHECKPOINT_DIR),
118
  revision="801f36e6103034bbd0d20fcbfa6a1a1ad5e919d3")
 
119
  print("Checkpoints ready.")
120
  except Exception as e:
121
  checkpoint_error = str(e)
122
- print(f"Checkpoint download error: {e}")
123
- finally:
124
- checkpoints_loading = False
125
-
126
 
127
- _loader = threading.Thread(target=download_checkpoints, daemon=True)
128
- _loader.start()
129
 
130
- # pyharp's worker imports this file to reach process_fn (as __mp_main__ rather than
131
- # __main__), so it finishes loading here, before process_fn can reserve a GPU, instead
132
- # of racing the check there. The server itself keeps loading in the background, which
133
- # lets it start at once.
134
  if __name__ != "__main__":
135
- _loader.join()
136
-
137
-
138
- model_card = ModelCard(
139
- name="Fusion Segment Transformer",
140
- description=(
141
- "Detects AI-generated music by fusing MERT audio embeddings with a "
142
- "structural, downbeat-segmented view of the track. Submitted to ICASSP "
143
- "2026. Repository: https://github.com/Mippia/FST-AI-Music-Detection "
144
- "Paper: https://arxiv.org/abs/2601.13647"
145
- ),
146
- author="Yumin Kim, Seonghyeon Go",
147
- tags=[Subcategory.MUSIC_ANALYSIS, 'music'],
148
- )
149
 
150
 
151
  def process_fn(audio_path: str) -> str:
152
  """Runs the two-stage detector on an uploaded track and writes the verdict to a text file."""
153
  # Checked before the GPU is reserved, so a request that cannot run spends none of the
154
  # caller's ZeroGPU allowance
155
- if checkpoints_loading:
156
- raise gr.Error("Model checkpoints are still downloading, please wait a moment and try again.")
157
  if checkpoint_error:
158
- raise gr.Error(f"Failed to download checkpoints: {checkpoint_error}")
159
  if not audio_path:
160
  raise gr.Error("Provide the music to test.")
161
  try:
 
1
+ import spaces # imported first, as ZeroGPU requires
2
+
3
  import sys
4
  sys.stdout.reconfigure(line_buffering=True)
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import gradio as gr
7
  import torch
8
  import librosa
 
66
 
67
  import contextlib
68
  import tempfile
 
69
  from pathlib import Path
70
 
71
  from huggingface_hub import hf_hub_download
72
  from pyharp import ModelCard, build_endpoint
73
+ from pyharp.tags import Channels, SampleRate, Subcategory
74
+
75
+ from inference import inference, load_models
76
+
77
+ model_card = ModelCard(
78
+ name="Fusion Segment Transformer",
79
+ description=(
80
+ "Detects AI-generated music by fusing MERT audio embeddings with a "
81
+ "structural, downbeat-segmented view of the track. Submitted to ICASSP "
82
+ "2026. The track is mixed to mono and resampled to 24 kHz internally. The code is "
83
+ "GPL, but the MERT-v1-95M backbone it runs on is CC-BY-NC-4.0, so results cannot be "
84
+ "used commercially. Repository: https://github.com/Mippia/FST-AI-Music-Detection "
85
+ "Paper: https://arxiv.org/abs/2601.13647"
86
+ ),
87
+ author="Yumin Kim, Seonghyeon Go",
88
+ tags=[Subcategory.MUSIC_ANALYSIS, SampleRate(24000), Channels(1), 'music'],
89
+ )
90
 
91
+ DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"
92
  CHECKPOINT_REPO = "teamup-tech/FST-AI-Music-Detection-checkpoints"
93
  CHECKPOINT_DIR = Path("checkpoints")
94
 
 
95
  checkpoint_error = None
96
 
97
 
98
+ def load_checkpoints():
99
+ """Fetch the Stage-1 and Stage-2 checkpoints, where inference.py looks for them, and build
100
+ both stages from them, so that no request downloads or builds anything."""
101
+ global checkpoint_error
102
  try:
103
+ # beat_this downloads its checkpoint from the JKU cloud on first use, which has failed
104
+ # both inside a GPU call (no name resolution) and at startup (503). The same file is
105
+ # mirrored on the Hub and linked where torch.hub looks before downloading.
106
+ beat_checkpoint = Path(torch.hub.get_dir()) / "checkpoints" / "beat_this-final0.ckpt"
107
+ if not beat_checkpoint.exists():
108
+ beat_checkpoint.parent.mkdir(parents=True, exist_ok=True)
109
+ with contextlib.suppress(FileExistsError):
110
+ beat_checkpoint.symlink_to(hf_hub_download(
111
+ "teamup-tech/Beat-This-weights", "final0.ckpt",
112
+ revision="4afbe376535aaab71b76ae2ca2d7c16b08877321",
113
+ ))
114
+
115
  CHECKPOINT_DIR.mkdir(exist_ok=True)
116
  for filename in ("Stage-1.ckpt", "Stage-2.ckpt"):
117
  hf_hub_download(repo_id=CHECKPOINT_REPO, filename=filename, local_dir=str(CHECKPOINT_DIR),
118
  revision="801f36e6103034bbd0d20fcbfa6a1a1ad5e919d3")
119
+ load_models(DEVICE)
120
  print("Checkpoints ready.")
121
  except Exception as e:
122
  checkpoint_error = str(e)
123
+ print(f"Checkpoint error: {e}")
 
 
 
124
 
 
 
125
 
126
+ # pyharp runs process_fn in a worker, which imports this file as __mp_main__ (rather than
127
+ # __main__) and starts with the server, so everything is fetched and built there alone, on its
128
+ # main thread, where ZeroGPU's emulation records each placement, before process_fn can reserve a
129
+ # GPU. A failure is kept rather than raised, so that each request can report it.
130
  if __name__ != "__main__":
131
+ load_checkpoints()
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
 
134
  def process_fn(audio_path: str) -> str:
135
  """Runs the two-stage detector on an uploaded track and writes the verdict to a text file."""
136
  # Checked before the GPU is reserved, so a request that cannot run spends none of the
137
  # caller's ZeroGPU allowance
 
 
138
  if checkpoint_error:
139
+ raise gr.Error(f"Failed to load the model: {checkpoint_error}")
140
  if not audio_path:
141
  raise gr.Error("Provide the music to test.")
142
  try:
inference.py CHANGED
@@ -193,24 +193,40 @@ def get_model(model_type, device):
193
  model.eval()
194
  return model, embed_dim
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  def inference(audio_path):
197
  # device ์„ค์ •์„ ๋ช…ํ™•ํžˆ ํ•˜๊ธฐ
198
  device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
199
-
200
- backbone_model, input_dim = get_model('MERT', device)
 
 
 
 
201
  segments, padding_mask = load_audio(audio_path, sr=24000)
202
  segments = segments.to(device).to(torch.float32)
203
  padding_mask = padding_mask.to(device).unsqueeze(0)
204
  logits, embedding = backbone_model(segments.squeeze(1))
205
 
206
- # ๋ชจ๋ธ ๋กœ๋“œํ•  ๋•Œ๋„ map_location ์ถ”๊ฐ€
207
- # pointing ckpt to files loaded on teamup-tech's HF account
208
- model = MusicAudioClassifier.load_from_checkpoint(
209
- checkpoint_path='checkpoints/Stage-2.ckpt',
210
- input_dim=input_dim,
211
- map_location=device # ์ด ๋ถ€๋ถ„ ์ถ”๊ฐ€
212
- )
213
-
214
  # Run inference
215
  print(f"Segments shape: {segments.shape}")
216
  print("Running inference...")
 
193
  model.eval()
194
  return model, embed_dim
195
 
196
+ _MODELS = {}
197
+
198
+
199
+ def load_models(device):
200
+ """
201
+ Build both stages once, on the CPU, and place them on the device, so that no request builds
202
+ them. The Space calls this in its worker at startup.
203
+ """
204
+ # pointing ckpt to files loaded on teamup-tech's HF account
205
+ backbone_model = MERT_AudioCNN.load_from_checkpoint(
206
+ 'checkpoints/Stage-1.ckpt', map_location='cpu'
207
+ ).to(device).eval()
208
+ classifier = MusicAudioClassifier.load_from_checkpoint(
209
+ checkpoint_path='checkpoints/Stage-2.ckpt',
210
+ input_dim=768,
211
+ map_location='cpu',
212
+ ).to(device).eval()
213
+ _MODELS.update(backbone=backbone_model, classifier=classifier)
214
+
215
+
216
  def inference(audio_path):
217
  # device ์„ค์ •์„ ๋ช…ํ™•ํžˆ ํ•˜๊ธฐ
218
  device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
219
+
220
+ if not _MODELS:
221
+ load_models(device)
222
+ backbone_model = _MODELS["backbone"]
223
+ model = _MODELS["classifier"]
224
+
225
  segments, padding_mask = load_audio(audio_path, sr=24000)
226
  segments = segments.to(device).to(torch.float32)
227
  padding_mask = padding_mask.to(device).unsqueeze(0)
228
  logits, embedding = backbone_model(segments.squeeze(1))
229
 
 
 
 
 
 
 
 
 
230
  # Run inference
231
  print(f"Segments shape: {segments.shape}")
232
  print("Running inference...")