Use Mistral and gTTS to Create Your Personal Storyteller
Estimated time needed: 30 minutes
In this project, you will learn how to use Mistral and gTTS to create your personal storyteller.
Table of Contents
- Introduction
- What does this guided project do?
- Objectives
- Background
- Setup
- watsonx API credentials and project_id
- Work with foundation models on watsonx.ai
- Generate a story with Mistral
- Convert the story to speech
- (Optional) Save the audio to a file
- Exercises
- Authors
- Contributors
Introduction
Have you ever wanted to create engaging stories and have them read aloud naturally? By combining the power of AI story generation with text-to-speech technology, we can create an interactive storytelling experience. In this project, we'll use Mistral, a large language model, to generate creative stories based on any topic you provide, and then convert these stories into natural-sounding speech.
What does this guided project do?
This project demonstrates how to create an AI storyteller by:
- Using Mistral to generate creative and informative stories based on your chosen topic
- Converting the generated story into speech using gTTS (Google Text-to-Speech)
- Playing the audio directly in your Jupyter notebook
For example, you could input a topic like "the life span of trees," and Mistral will create an engaging narrative about how trees grow, survive through seasons, and can live for hundreds or even thousands of years. This story will then be converted into spoken words, making it perfect for educational content, bedtime stories, or learning about any subject in an auditory format.
Objectives
After completing this lab you will be able to:
- Use Mistral to generate creative stories from any given topic
- Convert the generated text to speech using the gTTS library
- Create an end-to-end pipeline for AI storytelling
- Play generated audio directly in Jupyter notebooks
Background
What is large language model (LLM)?
Large language models are a category of foundation models trained on immense amounts of data making them capable of understanding and generating natural language and other types of content to perform a wide range of tasks.
What is Mistral?
Mistral is an open-source large language model developed by Mistral AI. It's a Mixture of Experts (MoE) model that achieves state-of-the-art performance among open-source models. Key features include:
- Powerful Performance: Matches or exceeds the performance of much larger models on most benchmarks
- Efficient Architecture: Uses a Sparse Mixture of Experts architecture, making it more efficient than traditional models
- Versatile Applications: Excellent at tasks like creative writing, analysis, and storytelling
- Open Source: Freely available for research and commercial use
What is gTTS (Google Text-to-Speech)?
gTTS (Google Text-to-Speech) is a Python library and CLI tool that interfaces with Google Translate's text-to-speech API. It offers:
- Multiple Languages: Support for a wide variety of languages and accents
- Natural Sound: High-quality, natural-sounding voice synthesis
- Easy Integration: Simple Python interface for converting text to speech
- Format Options: Ability to save audio in MP3 format or stream it directly
- Customization: Control over speech speed and language variants
Setup
For this lab, we will be using the following libraries:
ibm-watsonx-ai:ibm-watsonx-aiis a library that allows to work with watsonx.ai service on IBM Cloud and IBM Cloud for Data. Train, test and deploy your models as APIs for application development, share with colleagues using this python library.gtts:gttsis a library that allows to convert text to speech using Google Text-to-Speech API.
Installing required libraries
The following required libraries are not pre-installed in the Skills Network Labs environment. You must run the following cell to install them. Please wait until it completes.
This step could take several minutes, please be patient.
NOTE: If you encounter any issues, please restart the kernel and run again. You can do that by clicking the Restart the kernel icon.

%pip install gTTS==2.5.4 | tail -n 1
%pip install ibm-watsonx-ai==1.1.20 | tail -n 1Successfully installed click-8.1.8 gTTS-2.5.4
Note: you may need to restart the kernel to use updated packages.
Successfully installed ibm-cos-sdk-2.13.6 ibm-cos-sdk-core-2.13.6 ibm-cos-sdk-s3transfer-2.13.6 ibm-watsonx-ai-1.1.20 jmespath-1.0.1 lomond-0.3.3 numpy-1.26.4 pandas-2.1.4 requests-2.32.2 tabulate-0.9.0 tzdata-2025.3
Note: you may need to restart the kernel to use updated packages.watsonx API credentials and project_id
This section provides you with the necessary credentials to access the watsonx API.
Please note:
In this lab environment, you don't need to specify the api_key, and the project_id is pre_set as "skills-network", but if you want to use the model locally, you need to go to watsonx to create your own keys and id.
from ibm_watsonx_ai import Credentials
import os
credentials = Credentials(
url="https://us-south.ml.cloud.ibm.com",
)
project_id="skills-network"Work with foundation models on watsonx.ai
List available models
from ibm_watsonx_ai import APIClient
client = APIClient(credentials)
# GET TextModels ENUM
client.foundation_models.TextModels
# PRINT dict of Enums
client.foundation_models.TextModels.show(){'GRANITE_3_2_8B_INSTRUCT': 'ibm/granite-3-2-8b-instruct', 'GRANITE_3_3_8B_INSTRUCT': 'ibm/granite-3-3-8b-instruct', 'GRANITE_3_8B_INSTRUCT': 'ibm/granite-3-8b-instruct', 'GRANITE_4_H_SMALL': 'ibm/granite-4-h-small', 'GRANITE_8B_CODE_INSTRUCT': 'ibm/granite-8b-code-instruct', 'GRANITE_GUARDIAN_3_8B': 'ibm/granite-guardian-3-8b', 'LLAMA_3_2_11B_VISION_INSTRUCT': 'meta-llama/llama-3-2-11b-vision-instruct', 'LLAMA_3_2_90B_VISION_INSTRUCT': 'meta-llama/llama-3-2-90b-vision-instruct', 'LLAMA_3_3_70B_INSTRUCT': 'meta-llama/llama-3-3-70b-instruct', 'LLAMA_3_405B_INSTRUCT': 'meta-llama/llama-3-405b-instruct', 'LLAMA_4_MAVERICK_17B_128E_INSTRUCT_FP8': 'meta-llama/llama-4-maverick-17b-128e-instruct-fp8', 'LLAMA_GUARD_3_11B_VISION': 'meta-llama/llama-guard-3-11b-vision', 'MISTRAL_MEDIUM_2505': 'mistralai/mistral-medium-2505', 'MISTRAL_SMALL_3_1_24B_INSTRUCT_2503': 'mistralai/mistral-small-3-1-24b-instruct-2503', 'GPT_OSS_120B': 'openai/gpt-oss-120b'}# Specify the model_id of the model we will use for the chat.
model_id = 'mistralai/mistral-medium-2505'Defining model parameters
import os
from ibm_watsonx_ai.foundation_models import ModelInference
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
params = {
GenParams.DECODING_METHOD: "greedy",
GenParams.MAX_NEW_TOKENS: 1000,
}
model = ModelInference(
model_id=model_id,
credentials=credentials,
project_id=project_id,
params=params,
)Generate a story with Mistral
Now we'll create a story using Mistral. We'll first define a function that takes a topic as input and returns a generated story. The function will use a carefully crafted prompt to ensure the story is engaging, educational, and appropriate for beginners.
Let's test our storytelling capabilities by generating a story about a simple topic and converting it to speech.
# Function to generate an educational story using the Mistral model
def generate_story(topic):
# Construct a detailed prompt that guides the model to:
# - Write for beginners
# - Use simple language
# - Include interesting facts
# - Keep a specific length
# - End with a summary
prompt = f"""Write an engaging and educational story about {topic} for beginners.
Use simple and clear language to explain basic concepts.
Include interesting facts and keep it friendly and encouraging.
The story should be around 200-300 words and end with a brief summary of what we learned.
Make it perfect for someone just starting to learn about this topic."""
# Generate text using the model with our carefully crafted prompt
response = model.generate_text(prompt=prompt)
return response
# Example usage of the generate_story function
# Here we use butterflies as a topic since it's an engaging and
# educational subject that demonstrates the function well
topic = "the life cycle of butterflies"
story = generate_story(topic)
print("Generated Story:\n", story)Generated Story:
**The Amazing Journey of a Butterfly**
Once upon a time, in a sunny garden, a tiny egg was laid on a green leaf. This little egg was the very beginning of a butterfly’s life! Inside the egg, a tiny caterpillar was growing. After a few days, the egg hatched, and out came a hungry, wiggly caterpillar.
The caterpillar’s job was to eat and grow. It munched on leaves all day long, getting bigger and stronger. As it grew, it shed its skin several times, just like changing clothes! This stage is called the *larva* stage.
After a few weeks, the caterpillar was ready for a big change. It found a safe spot, attached itself to a leaf or branch, and formed a hard shell around itself. This shell is called a *chrysalis* (or *pupa*). Inside, something magical was happening—the caterpillar was transforming into a butterfly!
After about two weeks, the chrysalis opened, and out came a beautiful butterfly! Its wings were soft and crumpled at first, but soon they dried and became strong. The butterfly stretched its wings and took its first flight.
Now, the butterfly’s job was to find flowers, sip nectar, and help pollinate plants. It would also lay eggs of its own, starting the cycle all over again!
**What We Learned:**
1. Butterflies start as tiny eggs.
2. They hatch into caterpillars that eat and grow.
3. Caterpillars turn into chrysalises and transform inside.
4. Finally, they emerge as beautiful butterflies!
Isn’t nature amazing? Keep exploring, and you’ll discover even more wonders! 🌸🦋Convert the story to speech
Now that we have generated our story, let's convert it to speech using the gTTS (Google Text-to-Speech) library.
We'll create an audio file in memory and play it directly in the notebook using an audio player widget.
This step may take a while to complete, please be patient.
NOTE: If you encounter any issues, please run the cell again.
from gtts import gTTS
from IPython.display import Audio
import io
# Initialize text-to-speech with the generated story
tts = gTTS(story)
# Save the audio to a bytes buffer in memory
audio_bytes = io.BytesIO()
tts.write_to_fp(audio_bytes)
audio_bytes.seek(0)
# Create and display an audio player widget in the notebook
Audio(audio_bytes.read(), autoplay=False)(Optional) Save the audio to a file
# Save as MP3 file
tts.save("generated_story.mp3")Exercises
Exercise 1: Generate another story
Please generate another story with the following topic.
topic = "the life cycle of a human"
topic = "the life cycle of a human"
story = generate_story(topic)
print("Generated Story:\n", story)
# Initialize text-to-speech with the generated story
tts = gTTS(story)
audio_bytes = io.BytesIO()
tts.write_to_fp(audio_bytes)
audio_bytes.seek(0)
# Create and display an audio player widget in the notebook
Audio(audio_bytes.read(), autoplay=True)Generated Story:
**The Amazing Journey of You: A Human Life Cycle Story**
Hello, little explorer! Did you know that every person goes through an incredible journey called the **life cycle**? It’s like a magical adventure with different stages—just like a butterfly! Let’s learn about the amazing stages of a human’s life.
### **1. Baby: The Tiny Beginning**
Everything starts when a baby is born! Babies are small, cute, and need lots of love and care. They can’t walk or talk at first, but they grow quickly. Did you know babies have more bones than adults? (About 300! Some bones join together as they grow.)
### **2. Child: Learning and Playing**
As babies grow, they become children! This is a fun time for learning, playing, and making friends. Children go to school, discover new things, and get stronger every day. Your body is growing, and your brain is like a sponge, soaking up knowledge!
### **3. Teenager: Big Changes**
Next comes the **teenage years**—a time of big changes! Teens grow taller, their voices change, and they start thinking more like adults. It’s a time to explore hobbies, dreams, and even new responsibilities.
### **4. Adult: Building a Life**
As a grown-up, people work, build families, and take care of others. Adults have many jobs—like doctors, teachers, or artists—and they help make the world a better place. Even though they’re big, adults never stop learning!
### **5. Senior: Wisdom and Stories**
When people get older, they become **seniors**. They have lots of wisdom and amazing stories to share. Some seniors retire and enjoy hobbies, travel, or spend time with family. Every wrinkle tells a story of a life well-lived!
### **The Circle of Life**
Just like the seasons change, people go through these stages too. And guess what? You’re part of this beautiful journey right now!
**What We Learned:**
- Humans grow through stages: **baby, child, teenager, adult, and senior**.
- Each stage is special and full of new experiences.
- Life is an amazing adventure—enjoy every part of it!
Keep exploring, little scientist! The world is full of wonders, and you’re one of them. 🌟Click here for Solution
topic = "the life cycle of a human"
story = generate_story(topic)
print("Generated Story:\n", story)
# Initialize text-to-speech with the generated story
tts = gTTS(story)
audio_bytes = io.BytesIO()
tts.write_to_fp(audio_bytes)
audio_bytes.seek(0)
# Create and display an audio player widget in the notebook
Audio(audio_bytes.read(), autoplay=True)Authors
Contributors
Copyright © IBM Corporation. All rights reserved.