DEV Community

Cover image for Want Your Python AI to Speak? Using Pythonaibrain’s `speak()` Function
Divyanshu Sinha
Divyanshu Sinha

Posted on

Want Your Python AI to Speak? Using Pythonaibrain’s `speak()` Function

Want Your Python AI to Speak? Using Pythonaibrain’s speak() Function

Author: Divyanshu Sinha
Tags: #python #ai #tts #pythonaibrain #tutorial


Want Your AI to Talk?

Ever wanted your AI to speak out messages instead of just printing text? 🤖
With Pythonaibrain v1.1.8, it’s super easy. The speak() function allows your Python program to convert text to speech using voices available on your device.

In this tutorial, we’ll cover:

  • Installing Pythonaibrain
  • Using speak() with different voices
  • Cross-platform tips

Step 1: Install Pythonaibrain

If you haven’t installed it yet, run:

pip install pythonaibrain==1.1.8
Enter fullscreen mode Exit fullscreen mode

Make sure your Python version is 3.8 or higher.


Step 2: Basic Usage of speak()

Here’s the simplest way to make your AI speak:

from pyaitk import speak

# Speak a message using the default voice
speak("Hello! I am your AI speaking!")
Enter fullscreen mode Exit fullscreen mode

✅ That’s it! Your computer will read the message aloud.


Step 3: Using Different Voices

You can select a voice installed on your system. Example:

from pyaitk import speak

# Speak with a specific voice
speak("Hello! I am David speaking.", voice="david")
Enter fullscreen mode Exit fullscreen mode

On Windows, common voices include "david", "zira", "mark".
On macOS, system voices like "alex" or "victoria" are available.
On Linux, espeak voices are used ("en-us+f3", "en-us+m3", etc.).


Step 4: How to Check Available Voices

Windows Example (via pyttsx3)

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
    print(voice.name)
Enter fullscreen mode Exit fullscreen mode

Make sure the voice name matches exactly when using speak(voice="...").


Step 5: Combine with Chatbot

You can make your AI chatbot talk instead of just printing responses:

from pyaitk import Brain, speak

brain = Brain()
brain.train()
brain.save()

username = input("Your name: ")

while True:
    query = input(f"{username}: ")
    answer = brain.process_messages(query, False)

    # Speak the AI response
    speak(answer, voice="david")

    if query.lower() == "bye":
        break
Enter fullscreen mode Exit fullscreen mode

Step 6: Optional Enhancements

  • Add dynamic voices depending on the user or mood.
  • Use cross-platform voice checks to select an available voice automatically.
  • Combine with GUI libraries (Tkinter, CustomTkinter) for a talking chatbot interface.

Conclusion

With Pythonaibrain’s speak(), you can make your AI interactive and engaging by adding speech. 🗣️

You now know how to:

  • Convert text to speech in Python
  • Use different voices
  • Integrate speaking into a chatbot

Go ahead, make your Python AI talk like a real assistant! 🚀


For more check it out Pythonaibrain v1.1.8

Top comments (0)