ameliabb0913 commited on
Commit
39416b3
·
verified ·
1 Parent(s): ad15372

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
4
+ import librosa
5
+
6
+ # Placeholder model (Replace later with your trained model)
7
+ MODEL_NAME = "facebook/wav2vec2-base-960h"
8
+
9
+ # Load the pre-trained model and processor
10
+ processor = Wav2Vec2Processor.from_pretrained(MODEL_NAME)
11
+ model = Wav2Vec2ForSequenceClassification.from_pretrained(MODEL_NAME)
12
+
13
+ # Function to process audio input
14
+ def classify_audio(audio_file):
15
+ # Load the audio file
16
+ speech, sr = librosa.load(audio_file, sr=16000)
17
+
18
+ # Preprocess with Hugging Face's feature extractor
19
+ inputs = processor(speech, sampling_rate=16000, return_tensors="pt", padding=True, truncation=True)
20
+
21
+ # Make predictions
22
+ with torch.no_grad():
23
+ logits = model(**inputs).logits
24
+
25
+ predicted_class_id = torch.argmax(logits, dim=-1).item()
26
+ return f"Predicted Class: {predicted_class_id}"
27
+
28
+ # Gradio Interface
29
+ interface = gr.Interface(
30
+ fn=classify_audio,
31
+ inputs=gr.Audio(source="upload", type="filepath"),
32
+ outputs="text",
33
+ title="Wav2Vec2 Audio Classification",
34
+ description="Upload an audio file, and the model will classify it."
35
+ )
36
+
37
+ # Launch the Gradio demo
38
+ if __name__ == "__main__":
39
+ interface.launch()