nsajadi commited on
Commit
927023e
1 Parent(s): 1a5afb6

Create app.py

Browse files

app.py is added

Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import torch
3
+
4
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
5
+
6
+ classifier = pipeline(
7
+ "audio-classification", model="MIT/ast-finetuned-speech-commands-v2", device=device
8
+ )
9
+
10
+ from transformers.pipelines.audio_utils import ffmpeg_microphone_live
11
+
12
+
13
+ def launch_fn(
14
+ wake_word="marvin",
15
+ prob_threshold=0.5,
16
+ chunk_length_s=2.0,
17
+ stream_chunk_s=0.25,
18
+ debug=False,
19
+ ):
20
+ if wake_word not in classifier.model.config.label2id.keys():
21
+ raise ValueError(
22
+ f"Wake word {wake_word} not in set of valid class labels, pick a wake word in the set {classifier.model.config.label2id.keys()}."
23
+ )
24
+
25
+ sampling_rate = classifier.feature_extractor.sampling_rate
26
+
27
+ mic = ffmpeg_microphone_live(
28
+ sampling_rate=sampling_rate,
29
+ chunk_length_s=chunk_length_s,
30
+ stream_chunk_s=stream_chunk_s,
31
+ )
32
+
33
+ print("Listening for wake word...")
34
+ for prediction in classifier(mic):
35
+ prediction = prediction[0]
36
+ if debug:
37
+ print(prediction)
38
+ if prediction["label"] == wake_word:
39
+ if prediction["score"] > prob_threshold:
40
+ return True
41
+
42
+ launch_fn(debug=True)