Spaces:
Runtime error
Runtime error
AlejandroMolina
commited on
Commit
•
2fe7e34
1
Parent(s):
90ceb17
Update app.py
Browse files
app.py
CHANGED
@@ -70,6 +70,51 @@ def predict(filepath):
|
|
70 |
tensor = index_to_label(tensor.squeeze())
|
71 |
return tensor
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
|
|
|
|
|
|
|
|
|
70 |
tensor = index_to_label(tensor.squeeze())
|
71 |
return tensor
|
72 |
|
73 |
+
def record(seconds=1):
|
74 |
+
|
75 |
+
from google.colab import output as colab_output
|
76 |
+
from base64 import b64decode
|
77 |
+
from io import BytesIO
|
78 |
+
from pydub import AudioSegment
|
79 |
+
|
80 |
+
RECORD = (
|
81 |
+
b"const sleep = time => new Promise(resolve => setTimeout(resolve, time))\n"
|
82 |
+
b"const b2text = blob => new Promise(resolve => {\n"
|
83 |
+
b" const reader = new FileReader()\n"
|
84 |
+
b" reader.onloadend = e => resolve(e.srcElement.result)\n"
|
85 |
+
b" reader.readAsDataURL(blob)\n"
|
86 |
+
b"})\n"
|
87 |
+
b"var record = time => new Promise(async resolve => {\n"
|
88 |
+
b" stream = await navigator.mediaDevices.getUserMedia({ audio: true })\n"
|
89 |
+
b" recorder = new MediaRecorder(stream)\n"
|
90 |
+
b" chunks = []\n"
|
91 |
+
b" recorder.ondataavailable = e => chunks.push(e.data)\n"
|
92 |
+
b" recorder.start()\n"
|
93 |
+
b" await sleep(time)\n"
|
94 |
+
b" recorder.onstop = async ()=>{\n"
|
95 |
+
b" blob = new Blob(chunks)\n"
|
96 |
+
b" text = await b2text(blob)\n"
|
97 |
+
b" resolve(text)\n"
|
98 |
+
b" }\n"
|
99 |
+
b" recorder.stop()\n"
|
100 |
+
b"})"
|
101 |
+
)
|
102 |
+
RECORD = RECORD.decode("ascii")
|
103 |
+
|
104 |
+
print(f"Recording started for {seconds} seconds.")
|
105 |
+
display(ipd.Javascript(RECORD))
|
106 |
+
s = colab_output.eval_js("record(%d)" % (seconds * 1000))
|
107 |
+
print("Recording ended.")
|
108 |
+
b = b64decode(s.split(",")[1])
|
109 |
+
|
110 |
+
fileformat = "wav"
|
111 |
+
filename = f"_audio.{fileformat}"
|
112 |
+
AudioSegment.from_file(BytesIO(b)).export(filename, format=fileformat)
|
113 |
+
return torchaudio.load(filename)
|
114 |
+
|
115 |
|
116 |
+
model = torch.load('export.pkl',map_location=torch.device('cpu'))
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Audio(source=record()[0]), outputs=gr.outputs.Label(num_top_classes=3)).launch(share=True)
|