File size: 1,800 Bytes
091748b 3a33f11 fed7d7f 3a33f11 3d8e084 3a33f11 051530c fed7d7f 3a33f11 051530c fed7d7f 051530c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import gradio as gr
import numpy as np
import librosa
from perch_hoplite.zoo import model_configs
# Utiliser le nom de modèle "perch_8"
MODEL = model_configs.load_model_by_name("perch_8")
SR = 32000
WIN = 5 * SR
def _prep(wav, sr):
if wav.ndim > 1:
wav = np.mean(wav, axis=1)
if sr != SR:
wav = librosa.resample(wav.astype(np.float32), orig_sr=sr, target_sr=SR)
if len(wav) < WIN:
wav = np.pad(wav, (0, WIN - len(wav)))
else:
wav = wav[:WIN]
return wav.astype(np.float32)
def infer(audio):
if audio is None:
return {"error": "no audio"}
sr, wav = audio
wav = _prep(wav, sr)
out = MODEL.embed(wav)
logits = out.logits["label"]
labels = out.label_names.get("label")
idx = np.argsort(logits)[::-1][:3]
topk = []
top_logits = logits[idx]
exp_logits = np.exp(top_logits - np.max(top_logits))
sum_exp_logits = np.sum(exp_logits)
for i in range(len(idx)):
class_index = idx[i]
name = labels[class_index] if labels is not None and class_index < len(labels) else f"classe_{int(class_index)}"
prob = float(exp_logits[i] / sum_exp_logits)
topk.append({"label": name, "score": round(prob, 4)})
return {
"topk": topk,
"embedding_dim": int(out.embeddings.shape[-1]),
"note": "Scores non calibrés; régler un seuil selon votre usage."
}
# CORRECTION : Retrait du paramètre obsolète "allow_flagging"
demo = gr.Interface(
fn=infer,
inputs=gr.Audio(type="numpy", sources=["microphone", "upload"]),
outputs=gr.JSON(label="Perch 8 Inference"),
title="Perch Bioacoustics"
)
# CORRECTION : Retrait de .queue() et du paramètre api_open
# L'API est activée par défaut dans cette version de Gradio.
demo.launch() |