-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt_voice_chat.py
133 lines (106 loc) · 4.09 KB
/
gpt_voice_chat.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
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
#!/usr/bin/env python3
# This script will generate a chat using the microphone and computer to talk to each other
# This Works - Use this script - added script to output the conversation to a file gpt_voice.txt
import pyttsx3
import speech_recognition as sr
import openai
import sounddevice as sd
import soundfile as sf
import datetime
from alive_progress import alive_bar
import time
# Sampling frequency
fs = 44100
# Set up the OpenAI API client
openai.api_key = "sk-Add-Your-Own-OpenAI-API-Key-Here"
# Set up text-to-speech engine
engine = pyttsx3.init()
# Set the voice speed
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 50) # Decrease the speed by 50
# Set up conversation variables
conversation_history = ""
user_name = "My Lord"
bot_name = "Jervis"
# Define the OpenAI GPT-3 model
model_engine = "gpt-3.5-turbo-instruct"
# Welcome Banner
print ("")
print("Processing...............")
print ("")
sleep(2.02)
print("Welcome to the ChatGPT Voice Interface!")
print("This is Version 2.0")
print ("")
print(colored("Created by David", 'green'))
sleep(2.02)
for i in tqdm(range(100), ncols=80, bar_format='{l_bar}{bar}|'):
sleep(0.02) # simulate a download
# Define function to process user input
def process_input(user_input):
global conversation_history
prompt = user_name + ": " + user_input + "\n" + bot_name + ": "
conversation_history += prompt
response = openai.Completion.create(
engine=model_engine,
prompt=conversation_history,
max_tokens=2048,
temperature=0.9
)
response_text = response.choices[0].text.strip()
conversation_history += response_text + "\n"
print(bot_name + ": " + response_text)
engine.say(response_text)
engine.runAndWait()
log_conversation(user_input, response_text)
# Initialize the speech recognizer
r = sr.Recognizer()
# Define the microphone as the audio source
mic = sr.Microphone()
# Define the prompt for the bot to start listening
bot_prompt = "🤖 My Lord, you can ask a question now!"
# Define the bot's introduction message
intro_message = "🤖 What can I help you with My Lord?"
# Define a flag to keep the conversation going
conversation_active = True
# Function to log the conversation to a text file
def log_conversation(user_input, bot_response):
with open("gpt_voice.txt", "a") as f:
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
f.write(timestamp + " " + user_name + ": " + user_input + "\n")
f.write(timestamp + " " + bot_name + ": " + bot_response + "\n")
# Start the conversation loop
engine.say("My Lord, you can ask a question now!")
engine.runAndWait()
while conversation_active:
with mic as source:
print(bot_prompt)
audio = r.listen(source)
try:
# Use the speech recognizer to convert speech to text
user_input = r.recognize_google(audio)
# Print the user's input
print("User:", user_input)
if user_input.lower() == "goodbye":
print("Goodbye My Lord!")
engine.say("Goodbye My Lord!")
engine.runAndWait()
conversation_active = False
break
# Process the user's input and generate a response from the bot
process_input(user_input)
# Prompt the user to continue the conversation
# bot_prompt = "🤖 What else can I help you with My Lord?"
# Prompt the user to continue the conversation
bot_prompt = "What else can I help you with My Lord?"
print(bot_prompt)
engine.say(bot_prompt) # speak the prompt using pyttsx3
engine.runAndWait()
except sr.UnknownValueError:
# If speech is unintelligible, print a message and prompt the user to try again
print("I'm sorry My Lord, I didn't understand what you said. Please try again.")
bot_prompt = "Please try again."
except sr.RequestError as e:
# If there's an issue with the speech recognizer, print an error message
print("Could not request results from Google Speech Recognition service; {0}".format(e))
bot_prompt = "Please try again."