BilalHasan commited on
Commit
dac34e3
1 Parent(s): 3548181

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from sessions import sessions
3
+ import torchaudio
4
+ import torchaudio.transforms as T
5
+ import gradio as gr
6
+
7
+ pipe = pipeline(
8
+ "audio-classification",
9
+ model="BilalHasan/distilhubert-finetuned-ravdess",
10
+ )
11
+
12
+ audio_batch = []
13
+ def split_audio(array):
14
+ len_of_each_array = 30 * 16000
15
+ arr1, arr2 = array[0: len_of_each_array], array[int(len_of_each_array / 2):]
16
+ audio_batch.append(arr1)
17
+ if len(arr2) > len_of_each_array:
18
+ split_audio(arr2)
19
+ else:
20
+ audio_batch.append(arr2)
21
+ return audio_batch
22
+
23
+
24
+ def prediction(path):
25
+ predictions = []
26
+ array, sr = torchaudio.load(path)
27
+ resampler = T.Resample(sr, 16000)
28
+ resampled_audio = resampler(array)
29
+ audio_batch = split_audio(resampled_audio[0].numpy())
30
+ for i in range(len(audio_batch)):
31
+ predictions.append(pipe(audio_batch[i])[0]['label'])
32
+ mood = max(set(predictions), key = predictions.count)
33
+ if mood in ['neutral', 'calm', 'happy', 'surprised']:
34
+ mood = 'other'
35
+ session = sessions.mood2session[mood]
36
+ return mood, session
37
+
38
+
39
+ demo = gr.Interface(
40
+ fn=prediction,
41
+ inputs=[gr.Audio(type="filepath")],
42
+ outputs=[gr.Textbox(label="Mood"), gr.Textbox(label="Session")]
43
+ )
44
+ demo.launch()