Spaces:
Sleeping

Rthur2003 commited on
Commit
87518a8
·
1 Parent(s): 8be5b97

feat: integrate wav2vec2 score into score fusion logic

Browse files
app/routes/analyze.py CHANGED
@@ -494,6 +494,8 @@ async def _analyze_file(
494
  + features.harmonic_structure) / 3.0, 3,
495
  ),
496
  }
 
 
497
  if vocals and vocals.has_vocals:
498
  tower_scores["vocals"] = round(vocals.vocal_ai_score, 3)
499
  if clap_result and clap_result.available:
 
494
  + features.harmonic_structure) / 3.0, 3,
495
  ),
496
  }
497
+ if wav2vec2_result and wav2vec2_result.available:
498
+ tower_scores["wav2vec2"] = round(wav2vec2_result.p_ai, 3)
499
  if vocals and vocals.has_vocals:
500
  tower_scores["vocals"] = round(vocals.vocal_ai_score, 3)
501
  if clap_result and clap_result.available:
app/services/score_fusion.py CHANGED
@@ -15,6 +15,7 @@ from .feature_extractor import AudioFeatures
15
  from .vocal_analyzer import VocalFeatures
16
  from .clap_detector import CLAPResult
17
  from .fst_client import FSTResult
 
18
  from .logging_config import get_logger
19
 
20
  logger = get_logger(__name__)
@@ -51,6 +52,7 @@ def fuse_scores(
51
  *,
52
  clap_result: Optional[CLAPResult] = None,
53
  fst_result: Optional[FSTResult] = None,
 
54
  external_music_ai: Optional[dict] = None,
55
  external_ses_analizi: Optional[dict] = None,
56
  ) -> FusionResult:
@@ -87,6 +89,11 @@ def fuse_scores(
87
  vocal_ai_score = vocals.vocal_ai_score
88
  has_vocals = vocals is not None and vocals.has_vocals
89
 
 
 
 
 
 
90
  # ── CLAP score (Layer 2) ─────────────────────────────────────────
91
  clap_ext = _parse_clap_result(clap_result)
92
 
@@ -99,7 +106,8 @@ def fuse_scores(
99
 
100
  # ── Fusion logic ─────────────────────────────────────────────────
101
  confidence, decision_source, mode = _fuse(
102
- local_score, clap_ext, fst_ext, ext_music_ai, ext_ses
 
103
  )
104
 
105
  is_ai = confidence > 0.5
@@ -238,39 +246,45 @@ def _fuse(
238
  fst: Optional[_ExternalScore],
239
  ext_mai: Optional[_ExternalScore],
240
  ext_ses: Optional[_ExternalScore],
 
241
  ) -> tuple[float, str, str]:
242
  """
243
- Fuse local + CLAP + FST + legacy external scores.
244
 
245
  Weight allocation (normalized when layers are missing):
246
- Layer 1 (local features+vocals): 0.40
247
- Layer 2 (CLAP embedding): 0.20
248
- Layer 3 (FST external): 0.25
249
- Legacy (music_ai): 0.10
250
- Legacy (ses_analizi): 0.05
 
251
 
252
  Returns: (confidence, decision_source, analysis_mode)
253
  """
254
  scores: list[tuple[float, float, str]] = []
255
 
 
 
 
 
256
  # Layer 1 — always present
257
- scores.append((local, 0.40, "auris_local"))
258
 
259
  # Layer 2 — CLAP embeddings (reduced weight if heuristic fallback)
260
  if clap and clap.available:
261
- clap_weight = 0.20 if clap.classifier_used == "clap_embedding" else 0.08
262
  scores.append((clap.confidence, clap_weight, "clap"))
263
 
264
- # Layer 3 — FST external (highest individual accuracy)
265
  if fst and fst.available:
266
- scores.append((fst.confidence, 0.25, "fst"))
267
 
268
  # Legacy external services
269
  if ext_mai and ext_mai.available:
270
- scores.append((ext_mai.confidence, 0.10, "music_ai"))
271
 
272
  if ext_ses and ext_ses.available:
273
- scores.append((ext_ses.confidence, 0.05, "ses_analizi"))
274
 
275
  # Normalize weights
276
  total_weight = sum(w for _, w, _ in scores)
 
15
  from .vocal_analyzer import VocalFeatures
16
  from .clap_detector import CLAPResult
17
  from .fst_client import FSTResult
18
+ from .wav2vec2_detector import Wav2Vec2Result
19
  from .logging_config import get_logger
20
 
21
  logger = get_logger(__name__)
 
52
  *,
53
  clap_result: Optional[CLAPResult] = None,
54
  fst_result: Optional[FSTResult] = None,
55
+ wav2vec2_result: Optional[Wav2Vec2Result] = None,
56
  external_music_ai: Optional[dict] = None,
57
  external_ses_analizi: Optional[dict] = None,
58
  ) -> FusionResult:
 
89
  vocal_ai_score = vocals.vocal_ai_score
90
  has_vocals = vocals is not None and vocals.has_vocals
91
 
92
+ # ── wav2vec2 score (Tower 1) ─────────────────────────────────────
93
+ w2v_score: Optional[float] = None
94
+ if wav2vec2_result and wav2vec2_result.available:
95
+ w2v_score = wav2vec2_result.p_ai
96
+
97
  # ── CLAP score (Layer 2) ─────────────────────────────────────────
98
  clap_ext = _parse_clap_result(clap_result)
99
 
 
106
 
107
  # ── Fusion logic ─────────────────────────────────────────────────
108
  confidence, decision_source, mode = _fuse(
109
+ local_score, clap_ext, fst_ext, ext_music_ai, ext_ses,
110
+ wav2vec2_score=w2v_score,
111
  )
112
 
113
  is_ai = confidence > 0.5
 
246
  fst: Optional[_ExternalScore],
247
  ext_mai: Optional[_ExternalScore],
248
  ext_ses: Optional[_ExternalScore],
249
+ wav2vec2_score: Optional[float] = None,
250
  ) -> tuple[float, str, str]:
251
  """
252
+ Fuse local + wav2vec2 + CLAP + FST + legacy external scores.
253
 
254
  Weight allocation (normalized when layers are missing):
255
+ Tower 0 (wav2vec2 transformer): 0.30
256
+ Layer 1 (local features+vocals): 0.30
257
+ Layer 2 (CLAP embedding): 0.15
258
+ Layer 3 (FST external): 0.20
259
+ Legacy (music_ai): 0.04
260
+ Legacy (ses_analizi): 0.01
261
 
262
  Returns: (confidence, decision_source, analysis_mode)
263
  """
264
  scores: list[tuple[float, float, str]] = []
265
 
266
+ # Tower 0 — wav2vec2 transformer (highest accuracy when available)
267
+ if wav2vec2_score is not None:
268
+ scores.append((wav2vec2_score, 0.30, "wav2vec2"))
269
+
270
  # Layer 1 — always present
271
+ scores.append((local, 0.30, "auris_local"))
272
 
273
  # Layer 2 — CLAP embeddings (reduced weight if heuristic fallback)
274
  if clap and clap.available:
275
+ clap_weight = 0.15 if clap.classifier_used == "clap_embedding" else 0.06
276
  scores.append((clap.confidence, clap_weight, "clap"))
277
 
278
+ # Layer 3 — FST external
279
  if fst and fst.available:
280
+ scores.append((fst.confidence, 0.20, "fst"))
281
 
282
  # Legacy external services
283
  if ext_mai and ext_mai.available:
284
+ scores.append((ext_mai.confidence, 0.04, "music_ai"))
285
 
286
  if ext_ses and ext_ses.available:
287
+ scores.append((ext_ses.confidence, 0.01, "ses_analizi"))
288
 
289
  # Normalize weights
290
  total_weight = sum(w for _, w, _ in scores)