Speak & Hear - Conversing with OpenAI

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.

  • You need to obtain an * OpenAI API Key (see note below).
  • Replace the email-related code with the SMTP email of your choice with any username or password necessary to send an email. In my code example, I have leveraged environment variables for EMAIL_FROM, EMAIL_TO, and EMAIL_PASSWORD.

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:

  1. Create or Log In to Your OpenAI Account: Navigate to the OpenAI API homepage and either log in with your existing account or create a new one if you don’t already have one
  2. Access the API Key Section: Once logged in, click on your profile name or icon in the top-right corner of the page. From the dropdown menu, select the "View API Keys" option
  3. Generate a New API Key: On the API keys page, click the "Create new secret key" button. This will generate a new API key for you
  4. Save Your API Key Copy the generated API key and store it securely. Note that you won’t be able to view the full key again after leaving the page, so make sure to save it somewhere safe
  5. Use the API Key: You can now use this API key to access OpenAI's services, such as GPT models, by including it in your API requests.

要查看或添加评论,请登录

Mike Givens的更多文章

  • Fun with the Barcode Reader in a Power App

    Fun with the Barcode Reader in a Power App

    A colleague at work asked me the other day if I ever worked with barcode scanning with the Microsoft Power Platform…

  • Leveraging JavaScript in a Power Platform Model-Driven App (MDA)

    Leveraging JavaScript in a Power Platform Model-Driven App (MDA)

    An interesting question popped up today on a Team's channel. Is it possible to do the following.

  • Let Python Make You a Winner

    Let Python Make You a Winner

    A few years ago, two colleagues at HP joked about their ongoing struggle to pick numbers for the Powerball lottery…

  • Eden AI - Try it, and you will love it.

    Eden AI - Try it, and you will love it.

    I have found Eden AI extremely useful in my daily automation tasks. As of January 17, 2025, I counted 60 different…

  • Windows Toast Reminder to Drive the Car

    Windows Toast Reminder to Drive the Car

    Heads-down work or coding might occasionally need a reminder to do something important, like picking your child up from…

  • Converting Videos to GIFs with Python

    Converting Videos to GIFs with Python

    Have you ever wanted to take that best video clip of yours and loop it into a GIF? Whether making a meme, a tutorial or…

  • Story Time with Python and OpenAI

    Story Time with Python and OpenAI

    As a part-time writer and a full-time automation explorer, an idea popped up this evening. I realized that by…

  • Happy Holidays with PIL

    Happy Holidays with PIL

    Using the Python Image Library (PIL) to create a "happy holidays" email blast to your colleagues, friends, and family…

  • Christmas Tree with Python Turtle Graphics

    Christmas Tree with Python Turtle Graphics

    Happy Holiday Season. As promised this is the follow-up to my Thanksgiving Turkey.

  • Image Fun with the Python Imaging Library (PIL)

    Image Fun with the Python Imaging Library (PIL)

    The following code allows one to upload any image file, and the Python Pillow Library (PIL) transforms the original…

社区洞察

其他会员也浏览了