-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlivewhisper.py
59 lines (47 loc) · 1.57 KB
/
livewhisper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import asyncio
import whisper
import sounddevice as sd
import soundfile as sf
import numpy as np
import os
model = whisper.load_model("small")
def inference(audio):
audio = whisper.load_audio(audio)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
options = whisper.DecodingOptions(fp16=False)
result = whisper.decode(model, mel, options)
transcription = result.text
print(transcription)
return transcription
async def record_audio(filename, duration, samplerate, channels):
recording = []
def callback(indata, frames, time, status):
recording.append(indata.copy())
with sd.InputStream(samplerate=samplerate, channels=channels, callback=callback):
await asyncio.sleep(duration)
audio = np.concatenate(recording, axis=0)
sf.write(filename, audio, samplerate)
async def transcribe():
# Parameters
filename = "recorded_audio.wav"
duration = 5 # recording duration in seconds
samplerate = 16000
channels = 1
while True:
# Record audio
print("Listening...")
await record_audio(filename, duration, samplerate, channels)
# Transcribe the recorded audio
print("Processing...")
transcription = inference(filename)
# Delete the audio file
if os.path.exists(filename):
os.remove(filename)
else:
print(f"Error: The file {filename} does not exist.")
async def main():
await transcribe()
# Run the main coroutine
asyncio.run(main())