forked from TREE-Ind/Blender-GPT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
whisper.py
56 lines (45 loc) · 1.72 KB
/
whisper.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
import pyaudio
import numpy as np
import openai
import wave
import os
import bpy
def get_api_key():
preferences = bpy.context.preferences.addons[__name__.partition('.')[0]].preferences
return preferences.api_key
def get_audio_path():
preferences = bpy.context.preferences.addons[__name__.partition('.')[0]].preferences
return preferences.audio_path
def transcribe_audio():
openai.api_key = get_api_key()
audio_file = get_audio_path()
RATE = 16000
CHUNK = int(RATE / 10) # 100ms chunks
FORMAT = pyaudio.paInt16
CHANNELS = 1
WAVE_OUTPUT_FILENAME = audio_file
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
print("Start speaking...")
frames = []
while True:
data = stream.read(CHUNK)
frames.append(data)
# Stop recording if silence is detected or after a certain number of frames
if len(frames) > 50 and np.mean(np.abs(np.frombuffer(b''.join(frames[-50:]), dtype=np.int16))) < 300:
break
stream.stop_stream()
stream.close()
audio.terminate()
# Save the audio data to a WAV file
with wave.open(WAVE_OUTPUT_FILENAME, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
# Call the Whisper API using the correct method
with open(WAVE_OUTPUT_FILENAME, "rb") as audio_file:
transcript = openai.Audio.transcribe("whisper-1", audio_file)
# Remove the local WAV file after transcription
os.remove(WAVE_OUTPUT_FILENAME)
return transcript["text"]