Speak & Hear - Conversing with OpenAI
Today, I will demonstrate a modern version of the 1980s "Speak & Spell Electronic Game." In this version, leveraging Python and a headset, one can leverage several Python libraries - SpeechRecognition, OpenAI, PYTTSX3, PyAudio, and a few others to enable a recordable conversation with OpenAI. Forget typing into a chatbot - with this script, one can speak to OpenAI and hear the responses. At the end of the conversation, say, "Exit chat," and OpenAI will acknowledge your request with a friendly, "Goodbye! Let me know if you have any more questions in the future. Have a great day!" The conversation will be stored as an MP3 and emailed to the audience of your choice.
Here's a transcript of a recent conversation I had:
Speak:
You said: what is 5 factorial
ChatGPT replied: 5 factorial, denoted as 5!, is equal to 5 x 4 x 3 x 2 x 1, which equals 120.
Speak:
You said: how many days until Christmas as of November 27, 2024
ChatGPT replied: There are 28 days until Christmas on December 25th, 2024.
You said: exit chat
ChatGPT replied: Goodbye! Let me know if you have any more questions in the future. Have a great day!
Conversation saved to Conversation_2024-11-27_06-47-33.mp3
The email was sent successfully.
A couple of important notes before I share the code.
Now, for the moment you have been waiting for - drum roll. Please show me the code.
import speech_recognition as sr
import openai
import pyttsx3
import smtplib
import os
import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from datetime import datetime
class ChatGPTAssistant:
def __init__(self):
# Initialize text-to-speech engine
self.engine = pyttsx3.init()
self.engine.say("OpenAI - ChatGPT Python script is now running")
self.engine.runAndWait()
# Initialize speech recognizer
self.recognizer = sr.Recognizer()
# Initialize OpenAI API key
self.api_key = os.environ.get('OPENAI_API_KEY')
if not self.api_key:
raise ValueError("OpenAI API key not found. Please set the 'OPENAI_API_KEY' environment variable.")
openai.api_key = self.api_key
# Initialize conversation log
self.conversation_log = ""
def send_conversation_to_email(self, mp3_file_path):
"""Send the conversation log and MP3 file to the configured email address."""
msg = MIMEMultipart()
msg['Subject'] = 'ChatGPT Transcript'
msg['From'] = os.environ.get('EMAIL_FROM')
msg['To'] = os.environ.get('EMAIL_TO')
# Add the conversation log as the email body
body = MIMEText(self.conversation_log, 'plain')
msg.attach(body)
# Attach the MP3 file
try:
with open(mp3_file_path, 'rb') as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(a
'Content-Disposition',
f'attachment; filename={os.path.basename(mp3_file_path)}'
)
msg.attach(part)
except FileNotFoundError:
print(f"Error: MP3 file '{mp3_file_path}' not found. Email will be sent without the attachment.")
except Exception as e:
print(f"Error attaching MP3 file: {e}")
# Send the email
try:
with smtplib.SMTP('smtp.office365.com', 587) as server:
server.starttls()
server.login(os.environ.get('EMAIL_FROM'), os.environ.get('EMAIL_PASSWORD'))
server.send_message(msg)
print("The email was sent successfully.")
except Exception as e:
print(f"Failed to send email: {e}")
def get_response(self, input_text):
"""Get a response from OpenAI's ChatGPT."""
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": input_text}],
max_tokens=1024,
temperature=0.5,
)
return response.choices[0].message['content'].strip()
except Exception as e:
print(f"Error getting response from OpenAI: {e}")
return "I'm sorry, I couldn't process your request."
def process_audio_input(self):
"""Capture audio input, process it, and interact with ChatGPT."""
with sr.Microphone() as source:
print("Speak:")
audio = self.recognizer.listen(source)
try:
# Convert speech to text
user_input = self.recognizer.recognize_google(audio)
self.engine.say(f"You said: {user_input}")
self.engine.runAndWait()
# Log user input
self.conversation_log += f"You said: {user_input}\n\n"
print(f"You said: {user_input}")
# Get ChatGPT response
response = self.get_response(user_input)
self.conversation_log += f"ChatGPT replied: {response}\n\n"
print(f"ChatGPT replied: {response}")
# Speak the response
self.engine.say(response)
self.engine.runAndWait()
# Exit condition
if user_input.lower() == "exit chat":
mp3_file_path = self.save_conversation_to_file()
self.send_conversation_to_email(mp3_file_path)
sys.exit(0)
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
def save_conversation_to_file(self):
"""Save the conversation log to an MP3 file."""
now = datetime.now()
timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
file_path = f'Conversation_{timestamp}.mp3'
try:
self.engine.save_to_file(self.conversation_log, file_path)
self.engine.runAndWait()
print(f"Conversation saved to {file_path}")
return file_path
except Exception as e:
print(f"Failed to save conversation to file: {e}")
return None
def run(self):
"""Run the assistant in a loop."""
try:
while True:
self.process_audio_input()
except KeyboardInterrupt:
print("Exiting...")
except Exception as e:
self.handle_exception(e)
def handle_exception(self, e):
"""Handle exceptions and send error details via email."""
exception_type, _, exception_traceback = sys.exc_info()
filename = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
error_message = (
f"Error: {e}\n"
f"Exception type: {exception_type}\n"
f"File name: {filename}\n"
f"Line number: {line_number}"
)
print(error_message)
# Send error details via email
msg = MIMEText(error_message)
msg['Subject'] = 'Error occurred in ChatGPTAssistant'
msg['From'] = os.environ.get('EMAIL_FROM')
msg['To'] = os.environ.get('EMAIL_TO')
try:
with smtplib.SMTP('smtp.office365.com', 587) as server:
server.starttls()
server.login(os.environ.get('EMAIL_FROM'), os.environ.get('EMAIL_PASSWORD'))
server.send_message(msg)
except Exception as email_error:
print(f"Failed to send error email: {email_error}")
sys.exit(1)
if __name__ == "__main__":
assistant = ChatGPTAssistant()
assistant.run()
* To obtain an OpenAI API key, follow these steps: