JackismyShephard commited on
Commit
8c72189
1 Parent(s): 76172d0

add application file

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+ from transformers.pipelines.audio_utils import ffmpeg_read
4
+ import gradio as gr
5
+
6
+ MODEL_NAME = "JackismyShephard/whisper-medium.en-finetuned-gtzan"
7
+
8
+ device = 0 if torch.cuda.is_available() else "cpu"
9
+
10
+ pipe = pipeline(
11
+ task="audio-classification",
12
+ model=MODEL_NAME,
13
+ device=device,
14
+ )
15
+
16
+ def classify_audio(filepath):
17
+ preds = pipe(filepath)
18
+ outputs = {}
19
+ for p in preds:
20
+ outputs[p["label"]] = p["score"]
21
+ return outputs
22
+
23
+
24
+
25
+ demo = gr.Blocks()
26
+
27
+ file_transcribe = gr.Interface(
28
+ fn=transcribe,
29
+ #TODO not sure we need list here
30
+ inputs=[
31
+ #TODO not sure we need '.inputs.'
32
+ gr.inputs.Audio(source="upload", optional=True, label="Audio file", type="filepath"),
33
+ #TODO add inputs source upload here, if possible?
34
+ #TODO add inputs source youtube here, if possible?
35
+ ],
36
+ outputs="label", #TODO not sure about this
37
+ layout="horizontal", #TODO not sure we need this
38
+ theme="huggingface",
39
+ title="Classify Genre of Music",
40
+ description=(
41
+ "Classify long-form audio or microphone inputs with the click of a button! Demo uses the"
42
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to classify audio files"
43
+ " of arbitrary length."
44
+ ),
45
+ examples=[
46
+ ["./example.flac", "transcribe", False],
47
+ ["./example.flac", "transcribe", True],
48
+ ],
49
+ cache_examples=True,
50
+ allow_flagging="never",
51
+ )
52
+
53
+ mic_transcribe = gr.Interface(
54
+ fn=transcribe,
55
+ inputs=[
56
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
57
+ ],
58
+ outputs="label", #TODO not sure about this
59
+ layout="horizontal",
60
+ theme="huggingface",
61
+ title="Classify Genre of Music",
62
+ description=(
63
+ "Classify long-form audio or microphone inputs with the click of a button! Demo uses the"
64
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to classify audio files"
65
+ " of arbitrary length."
66
+ ),
67
+ allow_flagging="never",
68
+ )
69
+
70
+ with demo:
71
+ gr.TabbedInterface([file_transcribe, mic_transcribe], ["Classify Audio File", "classify Microphone input"])
72
+
73
+ demo.launch(enable_queue=True)