-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWhisper-stt.py
79 lines (56 loc) · 1.64 KB
/
Whisper-stt.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pyaudio
import openai
import wave
import requests
######### UPDATE THESE ############
HOME_ASSISTANT_URL = "https://your_home_assistant_utl.xyz"
HA_LONG_LIVED_ACCESS_TOKEN = "ey....."
openai.api_key = "sk-...."
###################################
CHUNK = 1024
WIDTH = 2
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 10
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(p.get_format_from_width(WIDTH)))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
# Transcribe audio file
audio_file = open(WAVE_OUTPUT_FILENAME, "rb")
transcript = openai.Audio.transcribe(model="whisper-1", file=audio_file, response_format="text")
print(transcript)
# Define the URL for the Home Assistant API call
url = f"{HOME_ASSISTANT_URL}/api/states/input_text.openassist_prompt"
# Define the headers for the API call
headers = {
"Authorization": f"Bearer {HA_LONG_LIVED_ACCESS_TOKEN}",
"content-type": "application/json",
}
# Define the data for the API call
data = {
"state": transcript
}
# Make the API call
response = requests.post(url, headers=headers, json=data)
# Print the response
print(response.text)
print(response.json())