16
16
17
17
"""
18
18
19
- # Uncomment the following line to run in Google Colab
19
+ # Uncomment the line corresponding to your "runtime type" to run in Google Colab
20
20
21
21
# CPU:
22
- # !pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
22
+ # !pip install pydub torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
23
23
24
24
# GPU:
25
- # !pip install torch==1.7.0+cu101 torchvision==0.8.1+cu101 torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
26
-
27
- # For interactive demo at the end:
28
- # !pip install pydub
25
+ # !pip install pydub torch==1.7.0+cu101 torchvision==0.8.1+cu101 torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
29
26
30
27
import torch
31
28
import torch .nn as nn
32
29
import torch .nn .functional as F
33
30
import torch .optim as optim
34
31
import torchaudio
32
+ import sys
35
33
36
34
import matplotlib .pyplot as plt
37
35
import IPython .display as ipd
38
- from tqdm .notebook import tqdm
36
+
37
+ from tqdm import tqdm
39
38
40
39
41
40
######################################################################
@@ -482,39 +481,40 @@ def predict(tensor):
482
481
# will record one second of audio and try to classify it.
483
482
#
484
483
485
- from google .colab import output as colab_output
486
- from base64 import b64decode
487
- from io import BytesIO
488
- from pydub import AudioSegment
489
-
490
-
491
- RECORD = """
492
- const sleep = time => new Promise(resolve => setTimeout(resolve, time))
493
- const b2text = blob => new Promise(resolve => {
494
- const reader = new FileReader()
495
- reader.onloadend = e => resolve(e.srcElement.result)
496
- reader.readAsDataURL(blob)
497
- })
498
- var record = time => new Promise(async resolve => {
499
- stream = await navigator.mediaDevices.getUserMedia({ audio: true })
500
- recorder = new MediaRecorder(stream)
501
- chunks = []
502
- recorder.ondataavailable = e => chunks.push(e.data)
503
- recorder.start()
504
- await sleep(time)
505
- recorder.onstop = async ()=>{
506
- blob = new Blob(chunks)
507
- text = await b2text(blob)
508
- resolve(text)
509
- }
510
- recorder.stop()
511
- })
512
- """
513
-
514
484
515
485
def record (seconds = 1 ):
516
- display (ipd .Javascript (RECORD ))
486
+
487
+ from google .colab import output as colab_output
488
+ from base64 import b64decode
489
+ from io import BytesIO
490
+ from pydub import AudioSegment
491
+
492
+ RECORD = (
493
+ b"const sleep = time => new Promise(resolve => setTimeout(resolve, time))\n "
494
+ b"const b2text = blob => new Promise(resolve => {\n "
495
+ b" const reader = new FileReader()\n "
496
+ b" reader.onloadend = e => resolve(e.srcElement.result)\n "
497
+ b" reader.readAsDataURL(blob)\n "
498
+ b"})\n "
499
+ b"var record = time => new Promise(async resolve => {\n "
500
+ b" stream = await navigator.mediaDevices.getUserMedia({ audio: true })\n "
501
+ b" recorder = new MediaRecorder(stream)\n "
502
+ b" chunks = []\n "
503
+ b" recorder.ondataavailable = e => chunks.push(e.data)\n "
504
+ b" recorder.start()\n "
505
+ b" await sleep(time)\n "
506
+ b" recorder.onstop = async ()=>{\n "
507
+ b" blob = new Blob(chunks)\n "
508
+ b" text = await b2text(blob)\n "
509
+ b" resolve(text)\n "
510
+ b" }\n "
511
+ b" recorder.stop()\n "
512
+ b"})"
513
+ )
514
+ RECORD = RECORD .decode ("ascii" )
515
+
517
516
print (f"Recording started for { seconds } seconds." )
517
+ display (ipd .Javascript (RECORD ))
518
518
s = colab_output .eval_js ("record(%d)" % (seconds * 1000 ))
519
519
print ("Recording ended." )
520
520
b = b64decode (s .split ("," )[1 ])
@@ -525,9 +525,11 @@ def record(seconds=1):
525
525
return torchaudio .load (filename )
526
526
527
527
528
- waveform , sample_rate = record ()
529
- print (f"Predicted: { predict (waveform )} ." )
530
- ipd .Audio (waveform .numpy (), rate = sample_rate )
528
+ # Detect whether notebook runs in google colab
529
+ if "google.colab" in sys .modules :
530
+ waveform , sample_rate = record ()
531
+ print (f"Predicted: { predict (waveform )} ." )
532
+ ipd .Audio (waveform .numpy (), rate = sample_rate )
531
533
532
534
533
535
######################################################################
0 commit comments