fmagot01 commited on
Commit
06d1427
1 Parent(s): 44d8356

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py CHANGED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
+ from timeit import default_timer as timer
5
+
6
+ username = "fmagot01" ## Complete your username
7
+ model_id = f"{username}/distil-wav2vec2-finetuned-giga-speech"
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+ pipe = pipeline("audio-classification", model=model_id, device=device)
10
+
11
+ # def predict_trunc(filepath):
12
+ # preprocessed = pipe.preprocess(filepath)
13
+ # truncated = pipe.feature_extractor.pad(preprocessed,truncation=True, max_length = 16_000*30)
14
+ # model_outputs = pipe.forward(truncated)
15
+ # outputs = pipe.postprocess(model_outputs)
16
+
17
+ # return outputs
18
+
19
+
20
+ def classify_audio(filepath):
21
+ """
22
+ Goes from
23
+ [{'score': 0.8339303731918335, 'label': 'Gaming'},
24
+ {'score': 0.11914275586605072, 'label': 'Audiobook'},]
25
+ to
26
+ {"Gaming": 0.8339303731918335, "Audiobook":0.11914275586605072}
27
+ """
28
+ start_time = timer()
29
+ preds = pipe(filepath)
30
+ # preds = predict_trunc(filepath)
31
+ outputs = {}
32
+ pred_time = round(timer() - start_time, 5)
33
+ for p in preds:
34
+ outputs[p["label"]] = p["score"]
35
+ return outputs, pred_time
36
+ #return outputs
37
+
38
+
39
+ title = "Classifier of Music Genres"
40
+ description = """
41
+ This is the demo of the finetuned classification model that we just trained on the [GTZAN](https://huggingface.co/datasets/marsyas/gtzan). You can upload your own audio file or used the ones already provided below.
42
+ """
43
+
44
+ filenames = ['TAINY_88_melodic_loop_keys_las_Emin.wav', "TAINY_92_melodic_loop_keys_lam_Ebmin.wav", "TunePocket-Lively-Polka-Dance-30-Sec-Preview.mp3"]
45
+ filenames = [[f"./{f}"] for f in filenames]
46
+ demo = gr.Interface(
47
+ fn=classify_audio,
48
+ inputs=gr.Audio(type="filepath"),
49
+ outputs=[gr.outputs.Label(label="Predictions"),
50
+ gr.Number(label="Prediction time (s)")
51
+ ],
52
+ title=title,
53
+ description=description,
54
+ examples=filenames,
55
+ )
56
+ demo.launch()