opinder2906 commited on
Commit
cf4c8e2
·
verified ·
1 Parent(s): d0c2c2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -1,25 +1,30 @@
1
  import streamlit as st
2
  import torch
3
  import torchaudio
4
- from utils import load_model, preprocess_audio, predict
5
  import joblib
 
 
6
 
7
- # Load label encoder
8
- labels = joblib.load("label_encoder.joblib")
 
9
 
10
- # Load model
11
- from model import YourCNNModelClass
12
  model = load_model("animal-sound-cnn.pth", YourCNNModelClass, num_classes=len(labels))
13
 
14
  # Streamlit UI
15
- st.title("Animal Sound Classifier 🎵🐾")
16
- uploaded_file = st.file_uploader("Upload a WAV file", type=["wav"])
 
17
 
18
  if uploaded_file:
19
  with open("temp.wav", "wb") as f:
20
  f.write(uploaded_file.read())
21
 
 
 
 
22
  input_tensor = preprocess_audio("temp.wav")
23
- prediction = predict(model, input_tensor, labels)
24
 
25
- st.success(f"Predicted Animal Sound: **{prediction}**")
 
1
  import streamlit as st
2
  import torch
3
  import torchaudio
 
4
  import joblib
5
+ from model import YourCNNModelClass
6
+ from utils import load_model, preprocess_audio, predict
7
 
8
+ # Load label encoder and get classes
9
+ label_encoder = joblib.load("label_encoder.joblib")
10
+ labels = label_encoder.classes_
11
 
12
+ # Load PyTorch model with correct number of classes
 
13
  model = load_model("animal-sound-cnn.pth", YourCNNModelClass, num_classes=len(labels))
14
 
15
  # Streamlit UI
16
+ st.title("Animal Sound Classification")
17
+
18
+ uploaded_file = st.file_uploader("Upload a WAV audio file", type=["wav"])
19
 
20
  if uploaded_file:
21
  with open("temp.wav", "wb") as f:
22
  f.write(uploaded_file.read())
23
 
24
+ st.audio("temp.wav")
25
+
26
+ # Preprocess audio and predict
27
  input_tensor = preprocess_audio("temp.wav")
28
+ pred_label = predict(model, input_tensor, labels)
29
 
30
+ st.write(f"Predicted animal: **{pred_label}**")