iamironman4279 commited on
Commit
da37fd1
·
1 Parent(s): eecd14e

create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ import gradio as gr
3
+ import openai
4
+ from TTS.api import TTS
5
+ # Create an instance of the TTS class
6
+ tts = TTS()
7
+
8
+ # Call the list_models() method on the instance
9
+ model_list = tts.list_models()
10
+
11
+ # Now, you can access the models in the model_list and choose one
12
+ model_name = model_list[9]
13
+
14
+ # Create another instance of the TTS class with the selected model
15
+ tts = TTS(model_name)
16
+ tts.tts_to_file(text="I love playing Chess", file_path="output.wav")
17
+ from IPython.display import Audio, display
18
+
19
+ display(Audio('output.wav', autoplay=True))
20
+ model = whisper.load_model("medium")
21
+ openai.api_key = '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'
22
+ def voice_chat(user_voice):
23
+
24
+ messages = [
25
+ {"role": "system", "content": "You are a kind helpful assistant."},
26
+ ]
27
+
28
+
29
+ user_message = model.transcribe(user_voice)["text"]
30
+
31
+ #reply = user_message
32
+
33
+ messages.append(
34
+ {"role": "user", "content": user_message},
35
+ )
36
+
37
+ print(messages)
38
+
39
+ chat = openai.ChatCompletion.create(
40
+ model="gpt-3.5-turbo", messages=messages
41
+ )
42
+
43
+ reply = chat.choices[0].message.content
44
+
45
+ messages.append({"role": "assistant", "content": reply})
46
+
47
+ tts.tts_to_file(text=reply, file_path="output.wav")
48
+
49
+ return(reply, 'output.wav')
50
+ text_reply = gr.Textbox(label="ChatGPT Text")
51
+ voice_reply = gr.Audio('output.wav')
52
+
53
+ gr.Interface(
54
+ title = 'AI Voice Assistant with ChatGPT AI',
55
+ fn=voice_chat,
56
+ inputs=[
57
+ gr.inputs.Audio(source="microphone", type="filepath")
58
+ ],
59
+
60
+ outputs=[
61
+ text_reply, voice_reply
62
+ ], live = True).launch(debug = True)