-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_voice_assistant.py
73 lines (49 loc) · 1.55 KB
/
openai_voice_assistant.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
# !pip install -q TTS
# !pip install -U numpy==1.21
# !pip install -q openai-whisper
# !pip install -q gradio
# !pip install -q openai
# brew install espeak
import os
import whisper
import gradio as gr
import openai
from TTS.api import TTS
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
#TTS.list_models()
model_name = 'tts_models/en/ljspeech/vits--neon'
tts = TTS(model_name)
tts.tts_to_file(text="I love playing Chess", file_path="output.wav")
from IPython.display import Audio, display
display(Audio('output.wav', autoplay=True))
model = whisper.load_model("medium")
def voice_chat(user_voice):
messages = [
{"role": "system", "content": "You are a kind helpful assistant."},
]
user_message = model.transcribe(user_voice)["text"]
#reply = user_message
messages.append(
{"role": "user", "content": user_message},
)
print(messages)
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
tts.tts_to_file(text=reply, file_path="output.wav")
return(reply, 'output.wav')
text_reply = gr.Textbox(label="ChatGPT Text")
voice_reply = gr.Audio('output.wav')
gr.Interface(
title = 'AI Voice Assistant with ChatGPT AI',
fn=voice_chat,
inputs=[
gr.inputs.Audio(source="microphone", type="filepath")
],
outputs=[
text_reply, voice_reply
], live = True).launch(debug = True)