Spaces:
Sleeping
Sleeping
QnxprU69yCNg8XJ commited on
Commit ·
0fd05da
1
Parent(s): 52a62d1
Add RandomRealisticClassifier class to inference_service for demo classifier loading
Browse files- inference_service.py +60 -0
inference_service.py
CHANGED
|
@@ -5,6 +5,7 @@ import librosa
|
|
| 5 |
import joblib
|
| 6 |
import soundfile as sf
|
| 7 |
import openl3 # <- OpenL3 pour embeddings audio
|
|
|
|
| 8 |
|
| 9 |
# ============================================================
|
| 10 |
# Configuration générale
|
|
@@ -20,6 +21,65 @@ CLIP_OVERLAP_PERCENT = 10
|
|
| 20 |
CLIP_IGNORE_SILENT_CLIPS = True
|
| 21 |
SILENCE_RMS_THRESHOLD_DB = -50
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ============================================================
|
| 24 |
# Utilitaire de test
|
| 25 |
# ============================================================
|
|
|
|
| 5 |
import joblib
|
| 6 |
import soundfile as sf
|
| 7 |
import openl3 # <- OpenL3 pour embeddings audio
|
| 8 |
+
from sklearn.base import BaseEstimator, ClassifierMixin
|
| 9 |
|
| 10 |
# ============================================================
|
| 11 |
# Configuration générale
|
|
|
|
| 21 |
CLIP_IGNORE_SILENT_CLIPS = True
|
| 22 |
SILENCE_RMS_THRESHOLD_DB = -50
|
| 23 |
|
| 24 |
+
# ============================================================
|
| 25 |
+
# Classifier de démonstration (pour tests)
|
| 26 |
+
# ============================================================
|
| 27 |
+
|
| 28 |
+
class RandomRealisticClassifier(BaseEstimator, ClassifierMixin):
|
| 29 |
+
"""
|
| 30 |
+
Classifier qui génère des scores aléatoires mais réalistes
|
| 31 |
+
- Low: 0.0 - 0.4 (40% des cas)
|
| 32 |
+
- Moderate: 0.4 - 0.7 (35% des cas)
|
| 33 |
+
- High: 0.7 - 1.0 (25% des cas)
|
| 34 |
+
"""
|
| 35 |
+
def __init__(self, random_state=None):
|
| 36 |
+
self.random_state = random_state
|
| 37 |
+
self.classes_ = np.array([0, 1])
|
| 38 |
+
|
| 39 |
+
def fit(self, X, y):
|
| 40 |
+
"""Fake fit - ne fait rien"""
|
| 41 |
+
return self
|
| 42 |
+
|
| 43 |
+
def predict(self, X):
|
| 44 |
+
"""Génère des prédictions basées sur les probabilités"""
|
| 45 |
+
probas = self.predict_proba(X)
|
| 46 |
+
return (probas[:, 1] > 0.5).astype(int)
|
| 47 |
+
|
| 48 |
+
def predict_proba(self, X):
|
| 49 |
+
"""
|
| 50 |
+
Génère des probabilités aléatoires réalistes
|
| 51 |
+
"""
|
| 52 |
+
n_samples = X.shape[0]
|
| 53 |
+
rng = np.random.RandomState(self.random_state)
|
| 54 |
+
|
| 55 |
+
# Générer des scores pour chaque sample
|
| 56 |
+
scores = []
|
| 57 |
+
for i in range(n_samples):
|
| 58 |
+
# Utiliser les features pour créer une "seed" unique par sample
|
| 59 |
+
seed = int(np.abs(np.sum(X[i]) * 1000)) % 1000000
|
| 60 |
+
sample_rng = np.random.RandomState(seed)
|
| 61 |
+
|
| 62 |
+
# Choisir une catégorie aléatoirement
|
| 63 |
+
category = sample_rng.choice(['low', 'moderate', 'high'],
|
| 64 |
+
p=[0.40, 0.35, 0.25])
|
| 65 |
+
|
| 66 |
+
if category == 'low':
|
| 67 |
+
# Low: 0.05 - 0.40
|
| 68 |
+
score = sample_rng.uniform(0.05, 0.40)
|
| 69 |
+
elif category == 'moderate':
|
| 70 |
+
# Moderate: 0.40 - 0.70
|
| 71 |
+
score = sample_rng.uniform(0.40, 0.70)
|
| 72 |
+
else: # high
|
| 73 |
+
# High: 0.70 - 0.95
|
| 74 |
+
score = sample_rng.uniform(0.70, 0.95)
|
| 75 |
+
|
| 76 |
+
scores.append(score)
|
| 77 |
+
|
| 78 |
+
scores = np.array(scores)
|
| 79 |
+
# Retourner les probabilités pour [classe 0, classe 1]
|
| 80 |
+
probas = np.column_stack([1 - scores, scores])
|
| 81 |
+
return probas
|
| 82 |
+
|
| 83 |
# ============================================================
|
| 84 |
# Utilitaire de test
|
| 85 |
# ============================================================
|