-
Notifications
You must be signed in to change notification settings - Fork 47
/
assist.py
46 lines (38 loc) · 1.53 KB
/
assist.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
from openai import OpenAI
import time
from pygame import mixer
import os
#https://platform.openai.com/playground/assistants
# Initialize the client and mixer
client = OpenAI(default_headers={"OpenAI-Beta": "assistants=v2"})
mixer.init()
assistant_id = ""
thread_id = ""
# Retrieve the assistant and thread
assistant = client.beta.assistants.retrieve(assistant_id)
thread = client.beta.threads.retrieve(thread_id)
def ask_question_memory(question):
global thread
client.beta.threads.messages.create(thread.id, role="user", content=question)
run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant.id)
while (run_status := client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)).status != 'completed':
if run_status.status == 'failed':
return "The run failed."
time.sleep(1)
messages = client.beta.threads.messages.list(thread_id=thread.id)
return messages.data[0].content[0].text.value
def generate_tts(sentence, speech_file_path):
response = client.audio.speech.create(model="tts-1", voice="echo", input=sentence)
response.stream_to_file(speech_file_path)
return str(speech_file_path)
def play_sound(file_path):
mixer.music.load(file_path)
mixer.music.play()
def TTS(text):
speech_file_path = generate_tts(text, "speech.mp3")
play_sound(speech_file_path)
while mixer.music.get_busy():
time.sleep(1)
mixer.music.unload()
os.remove(speech_file_path)
return "done"