Spaces:
Sleeping
Sleeping
KrishGoyani
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
def launch_fn(
|
13 |
+
wake_word = "sheila",
|
14 |
+
prob_threshold=0.5,
|
15 |
+
chunk_length_s=20.0,
|
16 |
+
stream_chunk_s=0.25,
|
17 |
+
debug=True
|
18 |
+
):
|
19 |
+
|
20 |
+
print("called")
|
21 |
+
if wake_word not in list(classifier.model.config.id2label.values()):
|
22 |
+
raise ValueError(f"wake word must be one of {list(classifier.model.config.id2label.values())}")
|
23 |
+
|
24 |
+
sampling_rate = classifier.feature_extractor.sampling_rate
|
25 |
+
|
26 |
+
mic = ffmpeg_microphone_live(
|
27 |
+
sampling_rate=sampling_rate,
|
28 |
+
chunk_length_s=chunk_length_s,
|
29 |
+
stream_chunk_s=stream_chunk_s
|
30 |
+
)
|
31 |
+
|
32 |
+
print("listening for wake up word.......")
|
33 |
+
|
34 |
+
for prediction in classifier(mic):
|
35 |
+
prediction = prediction[0]
|
36 |
+
if debug == True:
|
37 |
+
print(prediction)
|
38 |
+
if (prediction['label'] == wake_word) and (prediction['score'] > prob_threshold):
|
39 |
+
return True, f"Wake word '{wake_word}' detected with probability {prediction['score']:.2f}"
|
40 |
+
|
41 |
+
return False, "Wake word not detected"
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
launch_fn(debug=True)
|