-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtalking-princess.py
129 lines (109 loc) · 3.89 KB
/
talking-princess.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import json
import os
import sys
import pygame
import sounddevice as sd
import soundfile as sf
from ChatGPTMinimalAPI import ChatGPTAPIClient
from TTSClient import TTSApiClient
from WhisperClient import WhisperAPIClient
pygame.init()
# Define the size of the window
WINDOW_SIZE = (1024, 1024)
# Define the colors to use
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
# Define the font to use
FONT = pygame.font.Font(None, 36)
# Define the recording state
SMILING = "smilingmouth.png"
OPEN = "openmouth.png"
THINKING = "thinking.png"
# Define the filename to use
FILENAME = "recording.wav"
file = None
# Define the callback function to use
class SoundRecorder:
def __init__(self):
if os.path.exists("recording.wav"):
os.remove("recording.wav")
self.file = sf.SoundFile(FILENAME, mode='x', samplerate=44100, channels=1)
self.stream = sd.InputStream(channels=1,samplerate=44100, callback=self.callback)
def Start(self):
if self.stream.closed:
self.__init__()
self.stream.start()
def Stop(self):
self.stream.stop()
self.stream.close()
self.file.close()
def callback(self,indata, frames, time, status):
if status:
print(status, file=sys.stderr)
self.file.write(indata.copy())
class SoundPlayer:
filename = "output.wav"
def Play(self):
data, fs = sf.read(self.filename)
sd.play(data, fs)
class Secrets:
TokenOpenAI =""
KeyGoogleAPI = ""
def __init__(self):
with open('secrets.json', 'r') as secretFile:
secrets = json.load(secretFile)
self.TokenOpenAI = secrets["OpenAI-token"]
self.KeyGoogleAPI = secrets["GoogleAPIKey"]
# Define the main function
def main():
# Create the Pygame window
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Sound Recorder")
# Create the text surfaces for the buttons
button_text = FONT.render("Start Recording", True, BLACK)
# Create the buttons
button = button_text.get_rect()
button.center = (WINDOW_SIZE[0] // 4, WINDOW_SIZE[1] // 2)
soundRecorder = SoundRecorder()
soundPlayer = SoundPlayer()
secrets = Secrets()
transcriber = WhisperAPIClient(secrets.TokenOpenAI)
synthesizer = TTSApiClient(secrets.KeyGoogleAPI)
chatbot = ChatGPTAPIClient(secrets.TokenOpenAI)
RECORDING = False
BACKGROUND = pygame.image.load(SMILING)
# Main loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.MOUSEBUTTONDOWN:
if button.collidepoint(event.pos):
if RECORDING is False:
RECORDING = True
soundRecorder.Start()
button_text = FONT.render("Stop Recording", True, BLACK)
BACKGROUND = pygame.image.load(THINKING)
print("Recording started")
else:
RECORDING = False
soundRecorder.Stop()
BACKGROUND = pygame.image.load(OPEN)
print("Recording stopped")
transcribedText = transcriber.Transcribe(FILENAME)
print(transcribedText)
chatbotResponse = chatbot.chat(transcribedText)
print(chatbotResponse)
synthesizer.Synthesize(chatbotResponse)
soundPlayer.Play()
button_text = FONT.render("Start Recording", True, BLACK)
# Draw the buttons
screen.blit(BACKGROUND,(0,0))
pygame.draw.rect(screen, GRAY, button)
screen.blit(button_text, button)
# Update the display
pygame.display.update()
if __name__ == '__main__':
main()