-: Hey, in this video, what we're gonna cover
is how you can use OpenAI's package
to convert text into real sounding voices and audio files.
Now, there's a couple
of different packages you're gonna need for this.
You're gonna need langchain, langchain_openai
and the openai package.
You'll need to update your API key.
And then what we can do is have a look at openai's package.
Now, you can specify the file path
and you'll see I've got this speech.mp3
and you can also choose the model that you're interested in
and there's lots of different voices.
Now, the way this works is you put in your input
and then you would use response.stream_to_file.
And then after that,
then what you'll see is on the left-hand side,
we've got this speech.mp3 file that's been created,
which is a couple of seconds long here.
But what that will do is it will take the textual input
that you're putting in and it'll convert that
into an audio file.
Now, let's take a little bit step further
and figure out what is, you know, maybe a good use case
for this and like how could you integrate this
into a different flow?
And so we're gonna use one
of LangChain's community document loaders
called a WebBaseLoader.
And what you'll be able to do is you'll load a URL
and we'll look at the BBC News article
that recently came out in UK politics.
And after this, what happens here
is we've got this WebBaseLoader that can take in a URL.
So I can take in, for example, this URL here.
And then after that then, we get some data
and we can have a look at this.
And you can see we've got some documents
and we can see how many of these documents that we have.
So we've got one document in here
and if we want to have a look at the page content
of that document, you could just do the page_content
and you can see this is all the information
that we've got here.
Now what I suggest doing is taking all of that data
and then basically looping over all the documents
and replacing any new line characters.
And then you should end up with something
that looks a little bit like this
where you've got checking the character length.
And we can see this is the number of characters
and we could have a look at the webpage text if we wanted.
But basically, you've got a lot of different webpage texts.
Now, interestingly, I don't think we would want to put all
of this webpage text directly in
because you can see we've got other things
like the navigation has somehow managed to make it in here,
and that's not really what we're looking for.
One thing that can be quite good to do,
and especially as if we're over a certain character limit,
we won't be able to convert text
to speech using OpenAI's package.
It does have a character limit of around 4,906 characters,
but I've said it to 490 just to make sure.
Now, I've decided
to use something called the load_summarize_chain
that allows us to take in a bunch of documents.
And once we have those documents,
then what we can do is we can basically map over them
and reduce them all down
and get some sort of final output from a bunch of documents.
And what I've said, I want the prompters
to say the following is a set of documents
from a BBC News article.
Based on this list of docs, please extract the story
and make it sound engaging.
We've set up a ChatPromptTemplate for that.
And we've got our load_summarize_chain
that's taking in the model, the map_reduce type of chain,
and the map_prompt and the combine_prompt.
And at the moment, I'm not really specifying what I want
to happen there every single step as well as I could do,
but I found this prompt seems to work fine for now.
We invoke the chain, passing in the input_documents in here.
You can see I've just made a new LangChain document
with the web_text here.
It's a single document.
Now, what will happen is after this runs,
if the character limit's above 4,090,
we'll get the output_text
and we'll save that back to the web_text.
And the idea behind this
is that what you really wanna be doing
is not just scraping this data from the webpage,
but actually, you want to be filtering it,
maybe having some prompts
that make it into a more interesting story.
And you can see here we've got some information
about the Labour deputy leader.
Angela Rayner has accused the government
of reneging on its promise.
And so we've got a much better story here
than the original webpage context.
So we've really got the LLM to clean up the text
before we're gonna expose that into downstream LLMs
or downstream systems.
And then again, we can just run the speech.mp3
as the file_path.
We're using the client.audio.speech.create,
and then we're passing in a model, the voice
and the input text, which is now the web_text.
And then we're doing the response.stream_to_ file.
Now, I know this looks like it does give a warning.
It has recommended using
this .with_streaming_response method,
but I found that this doesn't actually work this method.
So you can ignore this warning for now.
And then that will save the audio directly
into this speech.mp3 file.
And you can see we've got
about 1 minute and 46 seconds of speech.
And so this can be a really great way for you
to transform different types of content
that were primarily textual based into an audio narrative.
Cool, I'll see you in the next one.