File size: 1,540 Bytes
0481720
 
 
 
 
854a72d
0481720
 
 
854a72d
 
0481720
 
 
854a72d
 
 
 
 
0481720
854a72d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
import gradio as gr
from transformers import pipeline

# Point this to your new Hub model ID
model_id = "aditya20t/distilhubert-musicClassifier"
# Explicitly set the device (0 for GPU, -1 for CPU)
classifier = pipeline("audio-classification", model=model_id)

def predict(audio):
    if audio is None:
        return None
    preds = classifier(audio)
    return {p["label"]: p["score"] for p in preds}

# Custom CSS for a cleaner look
custom_css = """
#title {text-align: center;}
#description {text-align: center; margin-bottom: 20px;}
"""

with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🎵 Music Genre Classifier", elem_id="title")
    gr.Markdown("Upload an audio clip (up to 30s) to identify its music genre.", elem_id="description")
    
    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(
                type="filepath", 
                label="Upload Audio or Record",
                sources=["upload", "microphone"]
            )
            submit_btn = gr.Button("Analyze Genre", variant="primary")
        
        with gr.Column():
            label_output = gr.Label(num_top_classes=5, label="Predictions")

    # Add Examples (ensure these files exist in your directory or use URLs)
    gr.Examples(
        examples=[], # Add paths to local .wav files here if available
        inputs=audio_input
    )

    submit_btn.click(
        fn=predict,
        inputs=audio_input,
        outputs=label_output,
    )

if __name__ == "__main__":
    demo.launch()