ahmad-fakhar's picture
Update app.py
18fa643 verified
raw
history blame
1.25 kB
# 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)