ahmad-fakhar commited on
Commit
0cec391
1 Parent(s): 6b96c77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -1,12 +1,17 @@
1
- # Streamlit app code
2
  import streamlit as st
3
- from transformers import pipeline
4
  import os
5
- import tensorflow as tf
6
 
7
  # Title of the Streamlit app
8
  st.title('Music Genre Classification')
9
 
 
 
 
 
 
 
 
10
  # File uploader to upload an audio file
11
  audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])
12
 
@@ -15,25 +20,23 @@ if audio_file is not None:
15
  if not os.path.exists("temp_audio"):
16
  os.makedirs("temp_audio")
17
 
18
- with open(os.path.join("temp_audio", audio_file.name), "wb") as f:
 
19
  f.write(audio_file.getbuffer())
20
 
21
- # Use the uploaded audio file path
22
- audio_path = os.path.join("temp_audio", audio_file.name)
23
-
24
  # Display a loading message
25
  st.text("Classifying the audio file... Please wait.")
26
-
27
- # Initialize the audio classification pipeline
28
- pipe = pipeline("audio-classification", model="sandychoii/distilhubert-finetuned-gtzan-audio-classification")
29
 
30
  # Perform classification
31
- result = pipe(audio_path)
32
-
33
  # Display the results
34
  st.subheader("Classification Results:")
35
  for label in result:
36
  st.write(f"**Genre**: {label['label']} | **Confidence**: {label['score']:.4f}")
37
-
38
  # Option to play the uploaded audio
39
  st.audio(audio_file)
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoModelForAudioClassification, AutoFeatureExtractor, pipeline
3
  import os
 
4
 
5
  # Title of the Streamlit app
6
  st.title('Music Genre Classification')
7
 
8
+ # Load the model and feature extractor
9
+ model = AutoModelForAudioClassification.from_pretrained("sandychoii/distilhubert-finetuned-gtzan-audio-classification")
10
+ feature_extractor = AutoFeatureExtractor.from_pretrained("sandychoii/distilhubert-finetuned-gtzan-audio-classification")
11
+
12
+ # Create the audio classification pipeline
13
+ pipe = pipeline(task="audio-classification", model=model, feature_extractor=feature_extractor)
14
+
15
  # File uploader to upload an audio file
16
  audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])
17
 
 
20
  if not os.path.exists("temp_audio"):
21
  os.makedirs("temp_audio")
22
 
23
+ temp_path = os.path.join("temp_audio", audio_file.name)
24
+ with open(temp_path, "wb") as f:
25
  f.write(audio_file.getbuffer())
26
 
 
 
 
27
  # Display a loading message
28
  st.text("Classifying the audio file... Please wait.")
 
 
 
29
 
30
  # Perform classification
31
+ result = pipe(temp_path)
32
+
33
  # Display the results
34
  st.subheader("Classification Results:")
35
  for label in result:
36
  st.write(f"**Genre**: {label['label']} | **Confidence**: {label['score']:.4f}")
37
+
38
  # Option to play the uploaded audio
39
  st.audio(audio_file)
40
+
41
+ # Clean up the temporary file
42
+ os.remove(temp_path)