-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhisper_module.py
More file actions
62 lines (50 loc) · 1.63 KB
/
whisper_module.py
File metadata and controls
62 lines (50 loc) · 1.63 KB
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
60
61
62
# whisper_module.py
import pyaudio
import wave
import whisper
import keyboard # pip install keyboard
import pyautogui # pip install pyautogui
# Constants for audio recording
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024
WAVE_OUTPUT_FILENAME = "output.wav"
# Load the Whisper model once
whisper_model = whisper.load_model("base.en") # Use tiny.en for smallest English Model, base.en, small.en are the other ones.
# Function to handle transcription
def transcribe_audio(filename):
result = whisper_model.transcribe(filename)
return result["text"]
# Function to record audio
def record_audio():
audio = pyaudio.PyAudio()
# Start recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("Recording... Press Caps Lock to stop and transcribe.")
frames = []
while True:
data = stream.read(CHUNK)
frames.append(data)
if keyboard.is_pressed('capslock'):
break
print("Finished recording.")
# Stop recording
stream.stop_stream()
stream.close()
audio.terminate()
# Save the recorded audio to a file
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
return WAVE_OUTPUT_FILENAME
def record_and_transcribe():
audio_filename = record_audio()
return transcribe_audio(audio_filename)
if __name__ == "__main__":
print(record_and_transcribe())