File size: 1,254 Bytes
3bc5ba6
 
 
 
18fa643
3bc5ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Streamlit app code
import streamlit as st
from transformers import pipeline
import os
import tensorflow as tf

# Title of the Streamlit app
st.title('Music Genre Classification')

# File uploader to upload an audio file
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])

if audio_file is not None:
    # Save the uploaded file to a temporary directory
    if not os.path.exists("temp_audio"):
        os.makedirs("temp_audio")
    
    with open(os.path.join("temp_audio", audio_file.name), "wb") as f:
        f.write(audio_file.getbuffer())
    
    # Use the uploaded audio file path
    audio_path = os.path.join("temp_audio", audio_file.name)

    # Display a loading message
    st.text("Classifying the audio file... Please wait.")

    # Initialize the audio classification pipeline
    pipe = pipeline("audio-classification", model="sandychoii/distilhubert-finetuned-gtzan-audio-classification")
    
    # Perform classification
    result = pipe(audio_path)

    # Display the results
    st.subheader("Classification Results:")
    for label in result:
        st.write(f"**Genre**: {label['label']} | **Confidence**: {label['score']:.4f}")

    # Option to play the uploaded audio
    st.audio(audio_file)