Djibi972 commited on
Commit
fed7d7f
·
verified ·
1 Parent(s): 34a1525

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr, numpy as np, librosa, soundfile as sf
2
+ from perch_hoplite.zoo import model_configs
3
+
4
+ # Charge Perch v2 (télécharge depuis Kaggle via hoplite)
5
+ MODEL = model_configs.load_model_by_name("perch_v2")
6
+ SR = 32000
7
+ WIN = 5 * SR
8
+
9
+ def _prep(wav, sr):
10
+ if wav.ndim > 1:
11
+ wav = np.mean(wav, axis=1)
12
+ if sr != SR:
13
+ wav = librosa.resample(wav.astype(np.float32), orig_sr=sr, target_sr=SR)
14
+ if len(wav) < WIN:
15
+ wav = np.pad(wav, (0, WIN - len(wav)))
16
+ else:
17
+ wav = wav[:WIN]
18
+ return wav.astype(np.float32)
19
+
20
+ def infer(audio):
21
+ if audio is None:
22
+ return {"error": "no audio"}
23
+ wav, sr = audio
24
+ wav = _prep(wav, sr)
25
+
26
+ out = MODEL.embed(wav)
27
+ logits = out.logits["label"]
28
+ labels = out.label_names["label"] if hasattr(out, "label_names") else None
29
+
30
+ idx = np.argsort(logits)[::-1][:3]
31
+ topk = []
32
+ for i in idx:
33
+ name = labels[i] if labels is not None else f"class_{int(i)}"
34
+ prob = float(np.exp(logits[i]) / np.sum(np.exp(logits[idx])))
35
+ topk.append({"label": name, "score": round(prob, 4)})
36
+
37
+ return {
38
+ "topk": topk,
39
+ "embedding_dim": int(out.embeddings.shape[-1]),
40
+ "note": "scores non calibrés; régler un seuil selon votre usage"
41
+ }
42
+
43
+ demo = gr.Interface(
44
+ fn=infer,
45
+ inputs=gr.Audio(type="numpy", sources=["microphone", "upload"]),
46
+ outputs=gr.JSON(label="Perch v2"),
47
+ title="Perch 2.0 — Bioacoustics",
48
+ allow_flagging="never"
49
+ )
50
+
51
+ demo.queue(api_open=True).launch()