udaykumar351 commited on
Commit
ce74472
·
verified ·
1 Parent(s): 247e894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -2,26 +2,21 @@
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
- # Pretrained model for audio classification
6
- MODEL = "superb/wav2vec2-base-superb-ks" # keyword spotting (yes, no, up, down...)
7
 
8
- classifier = pipeline("audio-classification", model=MODEL)
 
 
 
 
9
 
10
- def classify_audio(audio_file):
11
- # audio_file is a tuple: (sample_rate, numpy_array) if "numpy", or path if "filepath"
12
- if isinstance(audio_file, str): # filepath
13
- return classifier(audio_file)
14
- else: # (sr, data)
15
- sr, data = audio_file
16
- return classifier({"array": data, "sampling_rate": sr})
17
-
18
- demo = gr.Interface(
19
- fn=classify_audio,
20
- inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
21
- outputs=gr.Label(),
22
- title="🎵 Audio Classification",
23
- description="Upload or record audio. Model: wav2vec2-base-superb-ks"
24
- )
25
 
26
  if __name__ == "__main__":
27
  demo.launch()
 
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
+ # Load pipeline (pretrained model)
6
+ classifier = pipeline("audio-classification", model="superb/wav2vec2-base-superb-ks")
7
 
8
+ def classify_audio(audio):
9
+ # audio is a tuple: (sample_rate, numpy array)
10
+ if audio is None:
11
+ return "No audio provided"
12
+ return classifier(audio)
13
 
14
+ with gr.Blocks() as demo:
15
+ gr.Markdown("## 🎵 Audio Classification (Keyword Spotting)")
16
+ with gr.Row():
17
+ audio_input = gr.Audio(sources=["microphone", "upload"], type="numpy")
18
+ output = gr.JSON()
19
+ audio_input.change(classify_audio, audio_input, output)
 
 
 
 
 
 
 
 
 
20
 
21
  if __name__ == "__main__":
22
  demo.launch()