File size: 1,509 Bytes
3bc5ba6
0cec391
3bc5ba6
 
 
 
 
0cec391
 
 
 
 
 
 
3bc5ba6
 
 
 
 
 
 
 
0cec391
 
3bc5ba6
 
 
 
 
 
0cec391
 
3bc5ba6
 
 
 
0cec391
3bc5ba6
 
0cec391
 
 
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
41
42
import streamlit as st
from transformers import AutoModelForAudioClassification, AutoFeatureExtractor, pipeline
import os

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

# Load the model and feature extractor
model = AutoModelForAudioClassification.from_pretrained("sandychoii/distilhubert-finetuned-gtzan-audio-classification")
feature_extractor = AutoFeatureExtractor.from_pretrained("sandychoii/distilhubert-finetuned-gtzan-audio-classification")

# Create the audio classification pipeline
pipe = pipeline(task="audio-classification", model=model, feature_extractor=feature_extractor)

# 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")
    
    temp_path = os.path.join("temp_audio", audio_file.name)
    with open(temp_path, "wb") as f:
        f.write(audio_file.getbuffer())
    
    # Display a loading message
    st.text("Classifying the audio file... Please wait.")
    
    # Perform classification
    result = pipe(temp_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)

    # Clean up the temporary file
    os.remove(temp_path)