Spaces:
Running
Running
Commit
·
8df571c
0
Parent(s):
initial commit
Browse files- .gitattributes +35 -0
- .gitignore +5 -0
- README.md +90 -0
- app.py +79 -0
- requirements.txt +5 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
################################################################################
|
2 |
+
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
|
3 |
+
################################################################################
|
4 |
+
|
5 |
+
/.vs
|
README.md
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: MusicGenClassify
|
3 |
+
emoji: 🎼
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.25.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
short_description: Classify a song for use in MusicGen model training
|
11 |
+
thumbnail: >-
|
12 |
+
https://cdn-uploads.huggingface.co/production/uploads/6346595c9e5f0fe83fc60444/1O6woCP6rRcUAE6dZqbuB.png
|
13 |
+
---
|
14 |
+
|
15 |
+
# Audio Classifier for MusicGen Fine-Tuning
|
16 |
+
|
17 |
+
## Overview
|
18 |
+
The **Audio Classifier for MusicGen Fine-Tuning** is a web-based application designed to generate metadata for `.wav` audio files, facilitating dataset preparation for fine-tuning music generation models like MusicGen from Audiocraft. Built using **Gradio**, the app allows users to upload a single `.wav` file, input a custom label, and receive a JSON output containing detailed metadata. This tool is optimized for deployment as a **Hugging Face Space**, enabling community use for music classification tasks.
|
19 |
+
|
20 |
+
## Functionality
|
21 |
+
The application processes an uploaded `.wav` file to extract key audio features and predict its music genre, producing a JSON metadata file suitable for MusicGen training. The main features include:
|
22 |
+
|
23 |
+
- **User Inputs**:
|
24 |
+
- **Audio File**: Upload a `.wav` file via a Gradio `Audio` component.
|
25 |
+
- **Custom Label**: Enter a descriptive label (e.g., "A calm melody") in a textbox.
|
26 |
+
- **Audio Processing**:
|
27 |
+
- **Duration**: Extracts the audio file's length in seconds using `librosa`.
|
28 |
+
- **Tempo**: Estimates beats per minute (BPM) with `librosa.beat.tempo`.
|
29 |
+
- **Sample Rate**: Retrieves the native sample rate (e.g., 44.1kHz) of the `.wav` file.
|
30 |
+
- **Genre Prediction**: Uses a pre-trained audio classification model (`sanchit-gandhi/distilhubert-finetuned-gtzan`) to predict one of 10 genres: blues, classical, country, disco, hiphop, jazz, metal, pop, reggae, or rock.
|
31 |
+
- **Output**: A JSON object displayed in a Gradio `JSON` component, containing:
|
32 |
+
- `filename`: Name of the uploaded file.
|
33 |
+
- `duration`: Length in seconds (rounded to 3 decimal places).
|
34 |
+
- `description`: The user-provided label.
|
35 |
+
- `genre`: Predicted music genre.
|
36 |
+
- `tempo`: Tempo in BPM (rounded to 2 decimal places).
|
37 |
+
- `sample_rate`: Native sample rate in Hz.
|
38 |
+
|
39 |
+
## Example Output
|
40 |
+
For a file `song.wav` with the user-provided label "A calm melody," the JSON output might be:
|
41 |
+
```json
|
42 |
+
{
|
43 |
+
"filename": "song.wav",
|
44 |
+
"duration": 5.123,
|
45 |
+
"description": "A calm melody",
|
46 |
+
"genre": "jazz",
|
47 |
+
"tempo": 90.5,
|
48 |
+
"sample_rate": 44100
|
49 |
+
}
|
50 |
+
```
|
51 |
+
|
52 |
+
## Technical Details
|
53 |
+
- **Framework**: Built with Gradio 5.24.0 for a user-friendly web interface.
|
54 |
+
- **Libraries**:
|
55 |
+
- `librosa`: Audio feature extraction (duration, tempo, sample rate).
|
56 |
+
- `transformers` and `torch`: Genre prediction using a pre-trained model.
|
57 |
+
- `numpy`: Numerical operations for probability calculations.
|
58 |
+
- **Model**: Utilizes `sanchit-gandhi/distilhubert-finetuned-gtzan`, a DistilHuBERT model fine-tuned on the GTZAN dataset for music genre classification.
|
59 |
+
- **Audio Processing**: Resamples audio to 16kHz for the model if the native sample rate differs, ensuring compatibility.
|
60 |
+
- **Error Handling**: Returns a JSON error message if processing fails (e.g., invalid file).
|
61 |
+
|
62 |
+
## Deployment
|
63 |
+
The application is designed for deployment as a **Hugging Face Space**, making it accessible to the community for MusicGen fine-tuning. Deployment steps include:
|
64 |
+
- Creating a Space with Gradio as the framework.
|
65 |
+
- Uploading the script as `app.py`.
|
66 |
+
- Including a `requirements.txt` with dependencies:
|
67 |
+
|
68 |
+
transformers
|
69 |
+
torch
|
70 |
+
torchaudio
|
71 |
+
librosa
|
72 |
+
numpy
|
73 |
+
|
74 |
+
- Committing files to build and host the app.
|
75 |
+
|
76 |
+
## Use Case
|
77 |
+
This tool is ideal for users preparing datasets for MusicGen fine-tuning, particularly those with collections of `.wav` files from games, music, or movies. It automates the generation of metadata, reducing manual effort in labeling audio files. The JSON output is compatible with Audiocraft’s training requirements, enabling users to fine-tune music generation models efficiently.
|
78 |
+
|
79 |
+
## Benefits
|
80 |
+
- **User-Friendly**: Simple interface for uploading files and entering labels.
|
81 |
+
- **Automated Metadata**: Generates detailed metadata (genre, tempo, sample rate) to enhance descriptions.
|
82 |
+
- **Community Access**: Deployment on Hugging Face Spaces allows others to use it for similar tasks.
|
83 |
+
- **Audiocraft Compatibility**: Produces JSON files with required fields (`duration`, `description`) for MusicGen training.
|
84 |
+
|
85 |
+
## Limitations
|
86 |
+
- **Single File Processing**: Processes one file at a time, suitable for individual classification but not batch processing.
|
87 |
+
- **Genre Scope**: Limited to 10 genres from the GTZAN dataset, which may not cover all music types.
|
88 |
+
- **Label Dependency**: Relies on user input for the description, requiring manual effort for custom labels.
|
89 |
+
|
90 |
+
This application streamlines the preparation of audio datasets for MusicGen fine-tuning, offering a practical tool for researchers and enthusiasts in music generation.
|
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import librosa
|
3 |
+
from transformers import AutoModelForAudioClassification, AutoFeatureExtractor
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load the pre-trained model and feature extractor for genre prediction
|
9 |
+
model_name = "sanchit-gandhi/distilhubert-finetuned-gtzan"
|
10 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
11 |
+
model = AutoModelForAudioClassification.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# List of genres the model can predict
|
14 |
+
genres = ["blues", "classical", "country", "disco", "hiphop", "jazz", "metal", "pop", "reggae", "rock"]
|
15 |
+
|
16 |
+
# Function to process the uploaded audio file
|
17 |
+
def process_audio(audio_file, user_label):
|
18 |
+
try:
|
19 |
+
# Extract filename from the uploaded file path
|
20 |
+
filename = os.path.basename(audio_file)
|
21 |
+
|
22 |
+
# Load the audio file with its native sample rate
|
23 |
+
audio, sr = librosa.load(audio_file, sr=None)
|
24 |
+
|
25 |
+
# Extract duration
|
26 |
+
duration = librosa.get_duration(y=audio, sr=sr)
|
27 |
+
|
28 |
+
# Extract tempo
|
29 |
+
tempo = librosa.beat.tempo(y=audio, sr=sr)[0]
|
30 |
+
|
31 |
+
# Preprocess audio for the model (resample to 16kHz if needed)
|
32 |
+
target_sr = 16000
|
33 |
+
if sr != target_sr:
|
34 |
+
audio = librosa.resample(audio, orig_sr=sr, target_sr=target_sr)
|
35 |
+
sr_model = target_sr
|
36 |
+
else:
|
37 |
+
sr_model = sr
|
38 |
+
inputs = feature_extractor(audio, sampling_rate=sr_model, return_tensors="pt")
|
39 |
+
|
40 |
+
# Predict genre using the model
|
41 |
+
with torch.no_grad():
|
42 |
+
logits = model(**inputs).logits
|
43 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1).squeeze().numpy()
|
44 |
+
predicted_genre = genres[np.argmax(probabilities)]
|
45 |
+
|
46 |
+
# Use the user-provided label as the description
|
47 |
+
description = f"{user_label}, {predicted_genre}, {tempo:.2f} BPM, {sr} Hz"
|
48 |
+
|
49 |
+
# Create metadata dictionary
|
50 |
+
metadata = {
|
51 |
+
"filename": filename,
|
52 |
+
"duration": np.round(duration, 3),
|
53 |
+
"description": description,
|
54 |
+
"genre": predicted_genre,
|
55 |
+
"tempo": np.round(tempo, 2),
|
56 |
+
"sample_rate": sr
|
57 |
+
}
|
58 |
+
|
59 |
+
return metadata
|
60 |
+
except Exception as e:
|
61 |
+
return {"error": str(e)}
|
62 |
+
|
63 |
+
# Gradio interface
|
64 |
+
with gr.Blocks(theme="Surn/beeuty") as app:
|
65 |
+
gr.Markdown("# Audio Classifier for MusicGen Fine Tuning")
|
66 |
+
gr.Markdown("Upload a audio file (preferred `.wav`), provide a label, and get metadata for MusicGen training.")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
70 |
+
label_input = gr.Textbox(label="Enter Label", placeholder="e.g., A calm melody")
|
71 |
+
|
72 |
+
submit_button = gr.Button("Classify")
|
73 |
+
|
74 |
+
output_json = gr.JSON(label="Metadata Output")
|
75 |
+
|
76 |
+
submit_button.click(process_audio, inputs=[audio_input, label_input], outputs=output_json)
|
77 |
+
|
78 |
+
# Launch the app
|
79 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
torchaudio
|
4 |
+
librosa==0.10.2.post1
|
5 |
+
numpy
|