-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalk_with_GPT.py
111 lines (87 loc) · 3.16 KB
/
talk_with_GPT.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import openai
from elevenlabs import generate, play
from playsound import playsound
import pyaudio
import wave
import threading
import queue
import os
import sys
# Set your OpenAI API key
openai.api_key = "sk-XXXXXXXXXXXXXXX"
# Record audio
def record_audio(filename, stop_event, audio_queue):
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("Press enter to finish recording.")
while not stop_event.is_set():
data = stream.read(CHUNK)
audio_queue.put(data)
print("Recording finished.")
stream.stop_stream()
stream.close()
audio.terminate()
with wave.open(filename, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(list(audio_queue.queue)))
# Transcribe audio
def transcribe_audio(filename):
with open(filename, "rb") as audio_file:
transcript = openai.Audio.transcribe("whisper-1", audio_file, language='en')
return transcript["text"]
# send question to our sarcastic AI
def chatgpt(question):
# Generate a response. You can set a tone, role, style with few-shot in messages list.
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an assistant chatbot that reluctantly answers questions with sarcastic responses."},
{"role": "user", "content": "How many pounds are in a kilogram?"},
{"role": "assistant", "content": "This again? There are 2.2 pounds in a kilogram. Please make a note of this."},
{"role": "user", "content": "What does HTML stand for?"},
{"role": "assistant", "content": "Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future."},
{"role": "user", "content": question}
],
max_tokens=1024,
n=1,
stop=None,
temperature=0.8,)
response = completion.choices[0].message.content
return response
# Transcribe audio
def say_answer(text):
audio = generate(
text,
voice="Antoni",
model="eleven_monolingual_v1",
api_key="XXXXXXXXXXXXXXX" # register for free and put your own (even though you have some small number of requests without your API key)
)
filename = 'temp_answer.mp3'
with open(filename, 'wb') as file:
file.write(audio)
playsound("temp_answer.mp3")
# Main function
def main():
audio_filename = "recorded_audio.wav"
stop_event = threading.Event()
audio_queue = queue.Queue()
record_thread = threading.Thread(target=record_audio, args=(audio_filename, stop_event, audio_queue))
record_thread.start()
input("Press the return key to stop recording...\n")
stop_event.set()
record_thread.join()
transcription = transcribe_audio(audio_filename)
print("Transcription:", transcription)
answer = chatgpt(transcription)
print("Answer:", answer)
say_answer(answer)
if __name__ == "__main__":
main()