Upload folder using huggingface_hub
Browse files- README.md +28 -0
- app.py +78 -0
- requirements.txt +3 -0
README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Spotify Genre Classifier
|
| 3 |
+
emoji: 🎵
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# 🎵 Spotify Genre Classifier
|
| 14 |
+
|
| 15 |
+
This model predicts the genre of a song based on its track name.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
- Fine-tuned GPT-2 model
|
| 19 |
+
- 114 different genres
|
| 20 |
+
- Real-time predictions
|
| 21 |
+
|
| 22 |
+
## How to Use
|
| 23 |
+
1. Enter a track name
|
| 24 |
+
2. Click "Predict Genre"
|
| 25 |
+
3. See the predicted genre and confidence
|
| 26 |
+
|
| 27 |
+
## Training Your Own
|
| 28 |
+
Check out the training pipeline: https://github.com/huggingface/transformers
|
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
MODEL_PATH = "outputs/final_model"
|
| 8 |
+
|
| 9 |
+
if Path(MODEL_PATH).exists():
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 12 |
+
model.eval()
|
| 13 |
+
MODEL_LOADED = True
|
| 14 |
+
else:
|
| 15 |
+
MODEL_LOADED = False
|
| 16 |
+
model = None
|
| 17 |
+
tokenizer = None
|
| 18 |
+
|
| 19 |
+
def predict_genre(track_name):
|
| 20 |
+
"""Predict genre for a track name"""
|
| 21 |
+
if not MODEL_LOADED:
|
| 22 |
+
return "Model not found. Please train first."
|
| 23 |
+
|
| 24 |
+
if not track_name:
|
| 25 |
+
return "Please enter a track name"
|
| 26 |
+
|
| 27 |
+
# Tokenize
|
| 28 |
+
inputs = tokenizer(track_name, return_tensors='pt', padding=True, truncation=True, max_length=256)
|
| 29 |
+
|
| 30 |
+
# Predict
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model(**inputs)
|
| 33 |
+
probs = torch.softmax(outputs.logits, dim=-1)
|
| 34 |
+
pred_id = torch.argmax(probs, dim=-1).item()
|
| 35 |
+
confidence = probs[0, pred_id].item()
|
| 36 |
+
|
| 37 |
+
# Get label
|
| 38 |
+
pred_label = model.config.id2label.get(pred_id, f"Class_{pred_id}")
|
| 39 |
+
|
| 40 |
+
return f"**Genre:** {pred_label}\n\n**Confidence:** {confidence:.2%}"
|
| 41 |
+
|
| 42 |
+
# Create Gradio interface
|
| 43 |
+
with gr.Blocks(title="Spotify Genre Classifier", theme=gr.themes.Soft()) as demo:
|
| 44 |
+
gr.Markdown("# 🎵 Spotify Genre Classifier")
|
| 45 |
+
gr.Markdown("Enter a song track name to predict its genre using a fine-tuned GPT-2 model.")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
track_input = gr.Textbox(
|
| 50 |
+
label="Track Name",
|
| 51 |
+
placeholder="e.g., Bohemian Rhapsody",
|
| 52 |
+
lines=1
|
| 53 |
+
)
|
| 54 |
+
predict_btn = gr.Button("🔮 Predict Genre", variant="primary")
|
| 55 |
+
|
| 56 |
+
with gr.Column():
|
| 57 |
+
output = gr.Textbox(label="Prediction")
|
| 58 |
+
|
| 59 |
+
# Examples
|
| 60 |
+
gr.Examples(
|
| 61 |
+
examples=[
|
| 62 |
+
"Bohemian Rhapsody",
|
| 63 |
+
"Shape of You",
|
| 64 |
+
"Old Town Road",
|
| 65 |
+
"Blinding Lights",
|
| 66 |
+
"Bad Guy",
|
| 67 |
+
"Stairway to Heaven",
|
| 68 |
+
"Smells Like Teen Spirit",
|
| 69 |
+
"Billie Jean",
|
| 70 |
+
],
|
| 71 |
+
inputs=track_input
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
predict_btn.click(fn=predict_genre, inputs=track_input, outputs=output)
|
| 75 |
+
track_input.submit(fn=predict_genre, inputs=track_input, outputs=output)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.35.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
gradio>=4.0.0
|