shaymolinakolon commited on
Commit
a0ddef6
verified
1 Parent(s): a873185

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -1,16 +1,32 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- classifier = pipeline("audio-classification", model="Wiam/baby-cry-classification-finetuned-babycry-v4")
 
 
5
 
6
- def classify_baby_cry(audio):
7
- results = classifier(audio)
8
- return {res["label"]: res["score"] for res in results}
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  gr.Interface(
11
  fn=classify_baby_cry,
12
  inputs=gr.Audio(type="filepath"),
13
  outputs=gr.Label(num_top_classes=3),
14
  title="讝讬讛讜讬 讘讻讬 转讬谞讜拽讜转",
15
- description="讛注诇讛 拽讜讘抓 拽讜诇 砖诇 讘讻讬 转讬谞讜拽讜转 讜谞讚注 诇讝讛讜转 诪讛 讛讜讗 专讜爪讛"
16
  ).launch()
 
1
  import gradio as gr
2
+ import torch
3
+ import torchaudio
4
+ from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
5
 
6
+ model_id = "Wiam/baby-cry-classification-finetuned-babycry-v4"
7
+ model = AutoModelForAudioClassification.from_pretrained(model_id)
8
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
9
 
10
+ def classify_baby_cry(audio_file):
11
+ waveform, sample_rate = torchaudio.load(audio_file)
12
+
13
+ # 讗诐 讛讗讜讚讬讜 讘讬讜转专 诪注专讜抓 讗讞讚 (Stereo) 谞讞转讜讱 诇注专讜抓 1
14
+ if waveform.shape[0] > 1:
15
+ waveform = waveform.mean(dim=0, keepdim=True)
16
+
17
+ inputs = feature_extractor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt")
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ probs = torch.nn.functional.softmax(outputs.logits[0], dim=0)
21
+
22
+ labels = model.config.id2label
23
+ results = {labels[i]: float(probs[i]) for i in range(len(labels))}
24
+ return results
25
 
26
  gr.Interface(
27
  fn=classify_baby_cry,
28
  inputs=gr.Audio(type="filepath"),
29
  outputs=gr.Label(num_top_classes=3),
30
  title="讝讬讛讜讬 讘讻讬 转讬谞讜拽讜转",
31
+ description="讛诪注专讻转 诪讗讝讬谞讛 诇拽讜讘抓 拽讜诇 讜诪讞讝讬专讛 诪讛 谞专讗讛 砖讛转讬谞讜拽 诪谞住讛 诇讘讟讗"
32
  ).launch()