Make your own Audiobook using Azure Cognitive Service | by Sagnik Chattopadhyaya | Towards Data Science

Lets say you already have an PDF version of a book that you got from some sources (maybe for free) and you are not getting time to read it line by line. Also it may happen that you don’t want to spend money on buying its audio version or maybe its audio version is not available. Then this article is for you. In this article, you will see how you can make your own Audiobook using simple python scripts and text to speech feature of Azure Cognitive service. But first let me tell you a bit about an Audiobook.
“An audiobook (or a talking book) is a recording of a book or other work being read out loud. A reading of the complete text is described as ‘unabridged’, while readings of a shorter version are an abridgement.” — Wikipedia.
There are many services available in market that provide you the facility of an audiobook. Some of the popular one are Audible, Scribd, Audiobooks and many more. But all these charges you a subscription fee.
So, let us start building our own audiobook. For that make sure you have the following prerequisites.
Make sure you have Python 3.5 or later installed in your PC. In case you don’t have it, then you can install it from official website. In my PC, I have Python 3.6 installed and all these demo will be based on that. Don’t worry, there wont be any difference if you are working with the latest Python 3.9 .Next we will need something that will help us in working with PDF. For that Python has an awesome library, which would act as a PDF toolkit, known as PyPDF2. You can look at its full documentation also.We would also need an Azure account for using Azure Cognitive Services. But wait, we were looking for ways to make free Audiobook, right? Yes! Don’t worry I will tell things that comes for free.
Make sure you have Python 3.5 or later installed in your PC. In case you don’t have it, then you can install it from official website. In my PC, I have Python 3.6 installed and all these demo will be based on that. Don’t worry, there wont be any difference if you are working with the latest Python 3.9 .
Next we will need something that will help us in working with PDF. For that Python has an awesome library, which would act as a PDF toolkit, known as PyPDF2. You can look at its full documentation also.
We would also need an Azure account for using Azure Cognitive Services. But wait, we were looking for ways to make free Audiobook, right? Yes! Don’t worry I will tell things that comes for free.
First we will install PyPDF2. For that open your terminal or command prompt and type :
pip install PyPDF2
Now if you are using Python (or maybe PIP) for the first time then its of high probability that you will get an error, something like “pip is not recognised as an internal or external command.....” . That basically means you didn’t set path for pip. Below I’m giving a link of an article that will explain in details how to solve it.
medium.com
Though we would need some other libraries but lets first look into working with the pdf.
Our PyPDF2 library can do a huge variety of operations with a PDF file. Among which we will use reading capability of it. To do so we first need to create an python file (an file with .py as extension) and import the library.
import PyPDF2
Then we will create an object called book which we will use to open our pdf in read mode.
book = open("yourPdfName.pdf","rb")
An important point to note here is the location of the pdf. The code above assumes that the desired pdf is in same folder as of the python file. In case it is in different folder then you have two options. 1: Move the file (pdf or python) to the same folder as of the other. 2: Give exact file location to the pdf file. Suppose my pdf is in Desktop then I would write, “C:\\Users\\Sagnik\\Desktop\\yourPdfName.pdf”. This would solve the problem.
Now it might be a question that what is “rb” written towards the end. “rb” is basically an access mode used in file handling. To know more about other access modes or File handling in Python, refer to Python File Handling.
stackabuse.com
Till now we just opened our PDF file. Now we need to assign a reader who can read our pdf and extract text from the pdf file page by page. To do this we will use the following lines of code.
reader = PyPDF2.PdfFileReader(book)for num in range(0,reader.numPages):    text = reader.getPage(num).extractText()    # print(text)
Here the object reader makes use of PyPDF2 library to read our book. After that it goes through each page of the book and extract text from it by using a for-loop based on book page number. In case you are curious about what text it would read then you can go ahead and remove that ‘#’ and see what it prints. Even you can match it with the text from your pdf.
Now we got our text and the only thing we are left with is to convert it to speech and listen to it. To do so we would use Azure Cognitive Service. To know more about Cognitive Services you can visit the links below.
azure.microsoft.com
docs.microsoft.com
In order to use Azure Cognitive Services you would need an Azure account. But we need to get that for free, right? To do so, visit Azure Sign up page and select Start free to create your new Azure account. You can also watch this YouTube video for exact help in setting up your free account.
If you are a student (like me) then you have three different options to get Azure account and use that for free.
Using Azure for Students. But this would require you to sign in using your university email id (an .edu email). If your school doesn’t provide any such, then you have next two options.
azure.microsoft.com
Using GitHub Student developer pack. This pack has a huge number of other premium facilities as well. This gives you $100 Azure credits.
education.github.com
Becoming a Microsoft Learn Student Ambassador. This program gives you $150 Azure credit monthly and lot of other benefits including MTC voucher and a domain name for free. I am personally in huge love for this community.
studentambassadors.microsoft.com
After we got our Azure account, its time to create some Azure resources. But Why? For that we need to look at the definition of Azure Cognitive services to understand.
Cognitive Services brings AI within reach of every developer — without requiring machine-learning expertise. All it takes is an API call to embed the ability to see, hear, speak, search, understand and accelerate decision-making into your apps. Enable developers of all skill levels to easily add AI capabilities to their apps.
Now we can clearly see that we need to make an API call in order to embed the utilities of Cognitive Service. To make an API call we would need some keys and endpoints. And that would be provided by our Azure resource.
For that first Sign in to the Azure portal. It would look something like shown below.
Then we will head towards Create a resource and click on it. Look for the big plus (+) sign in top left and you will find the option there.
Here you can find the list of all available services the Azure provides. Now search for Speech in the search bar, as shown in above image, and hit Enter. Then you can see a page like shown below.
Now Click Create to create your Azure resources for Speech. It would now ask you about a name. Give a significant name to your resource. This name would help you find out your resource later. For me, I choose ‘learnoverflow’ as its name as our learning from this article should overflow.
In the Subscription area choose a subscription or create a new one which you want to use. This basically determines how the Azure fees will be billed to you. but don’t worry you wont be billed anything this time. I will explain how in next few steps.
It also asks for a Location. By that it means in which area you want to use the resource. Azure being a global platform, it offers you a large number of locations where you can create your resource. But in order to get best performance from your resource, choose a location that is nearest to you.
Next comes an important part, this is the Pricing Tier. For Speech resource you would get two type of options: 1. Free F0 , 2. Standard S0 . Both have different advantages and benefits. If you use the free, low-volume Speech service tier you can keep this free subscription even after your free trial or service credit expires. And obviously we will use Free F0 for our Resource this time. This is the key to use this resource for free. If you are curious about all pricing tiers than you can see speech service pricing.
In the Resource Group section create a new resource group for this Speech subscription or assign the subscription to an existing resource group. Resource groups are a container that help you keep your various Azure subscriptions organized by holding related resources at a place.
Now that you have filled up all the details, just click on Create and wait for few seconds for resource allocation.
Now click on ‘Go to resource’ to visit your resources.
In the image above, you can see all the details about the resource. But we are searching for API key, right. On the overview page (the main page on which you land every time you open your resource) we don’t have so many work to do.
Now look on the left panel (of the above image), I marked something in RED, this is Keys and Endpoints. All of our API key and other required information are in this tab. After you click on it, you would find something like the image below.
Here you can see you have two API keys. Either one of the key is enough to access your Cognitive Service API. We would need one of the key and the location in our Python code. You can note them down in Notepad for future reference or can directly come over this page and find out as required.
Now open your Python code editor and start implementing your API that you just got. But before that we would need to prepare our local machine to handle such API call. For that we need to make some Python package installations.
We would require the Cognitive Service Speech SDK Python package. To do so we will again use pip command.
pip install azure-cognitiveservices-speech
This package would be completely helpful for us.
We will start with importing the speech library we just installed. Add this to the 2nd line after previous import statement that we wrote previously.
import azure.cognitiveservices.speech as sdk
After that we will assign our API key and region to an variable in our Python code. This is basic setup of our subscription info. Keep these statements before the start of the for-loop that we wrote earlier.
key = "YourSubscriptionKey"region = "YourServiceRegion"
Replace YourSubscriptionKey and YourServiceRegion with the key and region you got after creating your Azure resources. After that we will create an instance of speech config with our subscription key and service region using the code below.
config = sdk.SpeechConfig(subscription=key, region=region)
We will move on to create our Speech Synthesizer using our default speaker as audio output. This would work as a human voice which is produced artificially.
synthesizer = sdk.SpeechSynthesizer(speech_config=config)
Now we will move inside the for-loop that we created earlier to read each pages of our book. Our approach will be to make a audio out of the text we get at each page we go through. But if you wish you can also modify the code to get the text of your whole book and then pass it through the below code.
result = synthesizer.speak_text_async(text).get()
Remember the variable text we created earlier? Now its time to synthesize the received text to speech. Once your Python Interpreter execute the above line, then you can expect your speaker to play the text written on the particular page.
Once you hear a voice coming out of your computer speaker or from your earphone that is connected with your PC, you be sure that you finally made your own Audiobook. Hurray!!!
If you wish to see this program in action, then at the end I will link a YouTube video where you can go through the whole article in a Video format. But before that let me discuss something important.
If your are using this Azure Speech library for the first time then there can be few problems that you might face. It might prompt that “azure is not recognised as internal or external command” even though you have installed the proper libraries with the command written above. To avoid those make sure your pip in updated before you actually install the Speech SDK. Let us assume that you have already installed the Speech SDK and now you are getting these errors, then How to solve?
In that case you will first need to uninstall your Speech SDK.
pip uninstall azure-cognitiveservices-speech
Then upgrade pip using the below command. Its always recommended to make sure your pip is updated before you install any libraries.
python -m pip install --upgrade pip
And after that install Azure speech SDK using the same command used previously.
This would solve your issue. In any case you still face the same issue then you might need to install azure using the command below.
pip install azure
But don’t worry just upgrading your pip will definitely solve your problem.
There can be a lot of Improvements that you can make to this. To listen to a particular book again and again you don’t need to run this program again again. You can actually save your generated audio in a your desired audio format. Also you can create your own custom voice. And many more other things with this. But remember that not all features come under the free tier API. If you are interested you can definitely view the Full Documentation of using this Text-to-Speech feature.
Finally as you have successfully build your Audiobook, its time to listen to it. But this article is written document and its impossible to show final output here. So, below is a video implementation of the whole process of building it as shown in this article. Skip to 23:00 to only view its implementation. Hopefully this would help you.
In this article, we have covered the implementation of a basic audiobook, that can read the entire book using a few lines of python code and Azure Cognitive Services. The end audio result of the audiobook is not the best, as the text needs some pre-processing before feeding to the speaker engine.
Thank You for reading!
I’m Sagnik Chattopadhyaya, a Microsoft Learn Student Ambassador. You can Visit my website to know more about me.
Twitter: @sagnik_20 . YouTube: Learn Overflow
Hope you got to learn something from this story. ❤
Happy Learning! 🐱‍💻