-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
218 lines (176 loc) · 7.37 KB
/
Copy pathapp.py
File metadata and controls
218 lines (176 loc) · 7.37 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import requests
import re
import openai
import os
import threading
import time
import tempfile
from deepgram import DeepgramClient, LiveTranscriptionEvents, LiveOptions, Microphone
import pygame
from dotenv import load_dotenv
load_dotenv()
DEEPGRAM_API_KEY = os.getenv('DEEPGRAM_API_KEY')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
def google_search(query):
query+=query+""
api_key='AIzaSyD7Bv7RjhiYkLNUWEYB0al03W6ef_kAw70'
cx = 'd6dea6904f8c64a2b'
url = f'https://www.googleapis.com/customsearch/v1?key={api_key}&cx={cx}&q={query}'
try:
response = requests.get(url)
response.raise_for_status() # Raises an HTTPError for bad responses
data = response.json()
sresult=''
if 'items' in data:
sresult+='\n'.join(item['snippet'] for item in data['items'])
return sresult
else:
return 'No relevant results found.'
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}") # Python 3.6+
return f"Error: {http_err}"
except Exception as err:
print(f"An error occurred: {err}")
return f"Error: {err}"
# Initialize clients
dg_client = DeepgramClient(api_key=DEEPGRAM_API_KEY)
openai.api_key = OPENAI_API_KEY
client = openai.OpenAI()
DEEPGRAM_TTS_URL = 'https://api.deepgram.com/v1/speak?model=aura-helios-en'
headers = {
"Authorization": f"Token {DEEPGRAM_API_KEY}",
"Content-Type": "application/json"
}
conversation_memory = []
# Global flag to control microphone state
mute_microphone = threading.Event()
prompt = """ You re a voice support agent. Keep all answers less than two lines. Three lines at maximum if needed. Also remebember previous chat if asked questions based on it
"""
def segment_text_by_sentence(text):
sentence_boundaries = re.finditer(r'(?<=[.!?])\s+', text)
boundaries_indices = [boundary.start() for boundary in sentence_boundaries]
segments = []
start = 0
for boundary_index in boundaries_indices:
segments.append(text[start:boundary_index + 1].strip())
start = boundary_index + 1
segments.append(text[start:].strip())
return segments
def synthesize_audio(text):
payload = {"text": text}
with requests.post(DEEPGRAM_TTS_URL, stream=True, headers=headers, json=payload) as r:
return r.content
def play_audio(file_path):
pygame.mixer.init()
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
# Stop the mixer and release resources
pygame.mixer.music.stop()
pygame.mixer.quit()
# Signal that playback is finished
mute_microphone.clear()
def main():
try:
deepgram = DeepgramClient(DEEPGRAM_API_KEY)
dg_connection = deepgram.listen.live.v("1")
is_finals = []
def on_open(self, open, **kwargs):
print("Connection Open")
def on_message(self, result, **kwargs):
nonlocal is_finals
if mute_microphone.is_set():
return # Ignore messages while microphone is muted
sentence = result.channel.alternatives[0].transcript
if len(sentence) == 0:
return
if result.is_final:
is_finals.append(sentence)
if result.speech_final:
utterance = " ".join(is_finals)
print(f"Speech Final: {utterance}")
is_finals = []
conversation_memory.append({"role": "user", "content": sentence.strip()})
google_results = google_search(sentence.strip())
print(google_results)
messages = [{"role": "system", "content": prompt + "\n\nGoogle Search Results use only is needed, otherwise answer only related to prompt and previous messages from user:\n" + google_results}]
messages.extend(conversation_memory)
chat_completion = client.chat.completions.create(
model="gpt-4",
messages=messages
)
print(chat_completion)
processed_text = chat_completion.choices[0].message.content.strip()
text_segments = segment_text_by_sentence(processed_text)
with open(output_audio_file, "wb") as output_file:
for segment_text in text_segments:
audio_data = synthesize_audio(segment_text)
output_file.write(audio_data)
# Mute the microphone and play the audio
mute_microphone.set()
microphone.mute()
play_audio(output_audio_file)
time.sleep(0.5)
microphone.unmute()
# Delete the audio file after playing
if os.path.exists(output_audio_file):
os.remove(output_audio_file)
else:
print(f"Interim Results: {sentence}")
def on_metadata(self, metadata, **kwargs):
print(f"Metadata: {metadata}")
def on_speech_started(self, speech_started, **kwargs):
print("Speech Started")
def on_utterance_end(self, utterance_end, **kwargs):
print("Utterance End")
nonlocal is_finals
if len(is_finals) > 0:
utterance = " ".join(is_finals)
print(f"Utterance End: {utterance}")
is_finals = []
def on_close(self, close, **kwargs):
print("Connection Closed")
def on_error(self, error, **kwargs):
print(f"Handled Error: {error}")
def on_unhandled(self, unhandled, **kwargs):
print(f"Unhandled Websocket Message: {unhandled}")
dg_connection.on(LiveTranscriptionEvents.Open, on_open)
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
dg_connection.on(LiveTranscriptionEvents.SpeechStarted, on_speech_started)
dg_connection.on(LiveTranscriptionEvents.UtteranceEnd, on_utterance_end)
dg_connection.on(LiveTranscriptionEvents.Close, on_close)
dg_connection.on(LiveTranscriptionEvents.Error, on_error)
dg_connection.on(LiveTranscriptionEvents.Unhandled, on_unhandled)
options = LiveOptions(
model="nova-2",
language="en-US",
smart_format=True,
encoding="linear16",
channels=1,
sample_rate=16000,
interim_results=True,
utterance_end_ms="1000",
vad_events=True,
endpointing=500,
)
addons = {
"no_delay": "true"
}
print("\n\nPress Enter to stop recording...\n\n")
if not dg_connection.start(options, addons=addons):
print("Failed to connect to Deepgram")
return
microphone = Microphone(dg_connection.send)
microphone.start()
input("")
microphone.finish()
dg_connection.finish()
print("Finished")
except Exception as e:
print(f"Could not open socket: {e}")
if __name__ == "__main__":
output_audio_file = 'output_audio.mp3'
main()