ardneebwar
commited on
Commit
•
4862fc7
1
Parent(s):
710b523
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
username = "ardneebwar" ## Complete your username
|
6 |
+
model_id = f"{username}/distilhubert-finetuned-gtzan"
|
7 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
+
pipe = pipeline("audio-classification", model=model_id, device=device)
|
9 |
+
|
10 |
+
# def predict_trunc(filepath):
|
11 |
+
# preprocessed = pipe.preprocess(filepath)
|
12 |
+
# truncated = pipe.feature_extractor.pad(preprocessed,truncation=True, max_length = 16_000*30)
|
13 |
+
# model_outputs = pipe.forward(truncated)
|
14 |
+
# outputs = pipe.postprocess(model_outputs)
|
15 |
+
|
16 |
+
# return outputs
|
17 |
+
|
18 |
+
|
19 |
+
def classify_audio(filepath):
|
20 |
+
"""
|
21 |
+
Goes from
|
22 |
+
[{'score': 0.8339303731918335, 'label': 'country'},
|
23 |
+
{'score': 0.11914275586605072, 'label': 'rock'},]
|
24 |
+
to
|
25 |
+
{"country": 0.8339303731918335, "rock":0.11914275586605072}
|
26 |
+
"""
|
27 |
+
preds = pipe(filepath)
|
28 |
+
# preds = predict_trunc(filepath)
|
29 |
+
outputs = {}
|
30 |
+
for p in preds:
|
31 |
+
outputs[p["label"]] = p["score"]
|
32 |
+
return outputs
|
33 |
+
|
34 |
+
|
35 |
+
title = "🎵 Music Genre Classifier"
|
36 |
+
description = """
|
37 |
+
demo to showcase the music
|
38 |
+
classification model that we just trained on the [GTZAN](https://huggingface.co/datasets/marsyas/gtzan)
|
39 |
+
"""
|
40 |
+
|
41 |
+
filenames = ['blues.00098.wav', "disco.00020.wav", "metal.00014.wav", "reggae.00021.wav", "rock.00058.wav"]
|
42 |
+
filenames = [[f"./{f}"] for f in filenames]
|
43 |
+
demo = gr.Interface(
|
44 |
+
fn=classify_audio,
|
45 |
+
inputs=gr.Audio(type="filepath"),
|
46 |
+
outputs=gr.outputs.Label(),
|
47 |
+
title=title,
|
48 |
+
description=description,
|
49 |
+
examples=filenames,
|
50 |
+
)
|
51 |
+
demo.launch()
|