ahmad-fakhar commited on
Commit
3bc5ba6
1 Parent(s): b4ed874

Update app.py

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