File size: 1,618 Bytes
da37fd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import whisper
import gradio as gr 
import openai
from TTS.api import TTS
# Create an instance of the TTS class
tts = TTS()

# Call the list_models() method on the instance
model_list = tts.list_models()

# Now, you can access the models in the model_list and choose one
model_name = model_list[9]

# Create another instance of the TTS class with the selected model
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")
openai.api_key = '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'
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)