|
import streamlit as st |
|
from transformers import AutoModelForAudioClassification, AutoFeatureExtractor, pipeline |
|
import os |
|
|
|
|
|
st.title('Music Genre Classification') |
|
|
|
|
|
model = AutoModelForAudioClassification.from_pretrained("sandychoii/distilhubert-finetuned-gtzan-audio-classification") |
|
feature_extractor = AutoFeatureExtractor.from_pretrained("sandychoii/distilhubert-finetuned-gtzan-audio-classification") |
|
|
|
|
|
pipe = pipeline(task="audio-classification", model=model, feature_extractor=feature_extractor) |
|
|
|
|
|
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"]) |
|
|
|
if audio_file is not None: |
|
|
|
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()) |
|
|
|
|
|
st.text("Classifying the audio file... Please wait.") |
|
|
|
|
|
result = pipe(temp_path) |
|
|
|
|
|
st.subheader("Classification Results:") |
|
for label in result: |
|
st.write(f"**Genre**: {label['label']} | **Confidence**: {label['score']:.4f}") |
|
|
|
|
|
st.audio(audio_file) |
|
|
|
|
|
os.remove(temp_path) |