greenarcade commited on
Commit
4f2c315
·
1 Parent(s): 347c8ed

add bird sound classification app and update .gitattributes

Browse files
Files changed (3) hide show
  1. .gitattributes +2 -0
  2. .gitignore +1 -0
  3. app.py +47 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.mp3 filter=lfs diff=lfs merge=lfs -text
37
+ *.wav filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .idea
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import librosa
4
+ from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2FeatureExtractor
5
+
6
+ # Load model from Hugging Face Hub
7
+ model_name = "greenarcade/wav2vec2-vd-bird-sound-classification"
8
+ model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
9
+ feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name)
10
+
11
+
12
+ def predict(audio_file):
13
+ # Handle MP3/WAV files
14
+ audio, sr = librosa.load(audio_file, sr=16000)
15
+
16
+ # Process audio
17
+ inputs = feature_extractor(
18
+ audio,
19
+ sampling_rate=16000,
20
+ return_tensors="pt",
21
+ padding=True,
22
+ truncation=True,
23
+ max_length=16000 * 5,
24
+ )
25
+
26
+ # Predict
27
+ with torch.no_grad():
28
+ logits = model(**inputs).logits
29
+ probs = torch.softmax(logits, dim=-1).squeeze().tolist()
30
+
31
+ # Format results - return actual float values instead of formatted strings
32
+ predictions = {model.config.id2label[i]: prob for i, prob in enumerate(probs)}
33
+ sorted_preds = sorted(predictions.items(), key=lambda x: x[1], reverse=True)[:3]
34
+ return {k: v for k, v in sorted_preds}
35
+
36
+
37
+ # Gradio Interface
38
+ demo = gr.Interface(
39
+ fn=predict,
40
+ inputs=gr.Audio(sources=["upload"], type="filepath"),
41
+ outputs=gr.Label(num_top_classes=3),
42
+ title="🦜 Bird Sound Classifier (Indian birds)",
43
+ description="Upload a 5-second audio clip to identify bird species",
44
+ examples=[["greyheron-sample.wav"], ["blue-tail-sample.mp3"]]
45
+ )
46
+
47
+ demo.launch()