Ask Questions to an Audio File utilizing OpenAI

Ask Questions to an Audio File utilizing OpenAI


In this text, I’ll clarify how we will cross an audio file to LLM and I’m taking OpenAI as our LLM.

There are many individuals preferring audio and video tutorials over studying together with our podcast lovers as listening appears to be simpler for them as in comparison with studying a e-book, an e-book, or an article, and it’s fairly widespread that after a sure time period, we could overlook a few of the parts of our tutorial. Now, with a view to get the insights once more, re-watching or re-listening is the one choice, which might be very time-consuming.

So, one of the best answer is to provide you with a small AI-based utility by writing only a few traces of code that may analyze the audio and reply to all of the questions which can be requested by the consumer.

Here, using generative AI might be the most suitable choice, however the issue is, that we will’t cross audio immediately as it’s text-based. Let’s deep dive into this text, to know how we will make this work in a step-by-step trend.

Audio file

High-level steps

To execute the answer from end-to-end, we have to work with beneath parts/libraries:

Audio to Text Generator

  • For transcript technology, we can be utilizing AssemblyAI

Embedding Generator

  • For producing the embeddings, we can be utilizing OpenAIEmbeddings

Vector Database

  • Chroma can be used as an in-memory database for storing the vectors

Large Language Model

All these are wrapped beneath a library referred to as Langchain, so we can be extremely using that too.

First of all, we have to seize the keys as proven beneath:

Get An OpenAI API Key

To get the OpenAI key, it’s worthwhile to go to https://openai.com/, log in, after which seize the keys utilizing the highlighted approach:

welcome to openai

Get An AssemblyAI API Key

To get the AssemblyAI key, it’s worthwhile to go to AssemblyAI | Account, log in, after which seize the keys utilizing the highlighted approach:

assembly ai account info

Install Packages

Install these packages:

assemblyai>=0.17.0
openai>=0.28.0
sentence-transformers>=2.2.2
langchain>=0.0.278
chromadb>=0.4.8
tiktoken>=0.5.1

Import Required Packages

Do set up the dependent libraries and import the beneath packages:

from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import AssemblyAIAudioTranscriptLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma

Transcribe Audio

The subsequent step is to extract textual content from an audio file. Here is the pattern audio, I’ve used for this text.

doc = AssemblyAIAudioTranscriptLoader(file_path="https://storage.googleapis.com/aai-docs-samples/nbc.mp3").load()

Here is what the doc appears to be like like:

[Document(page_content=” Load time, a new president and new congressional makeup. Same old partisan divides, right? Yes and no. There’s the traditional red blue divide you’re very familiar with, but there’s a lot more below the surface going on in both parties…….”, metadata={‘language_code’: <LanguageCode.en_us: ‘en_us’>, ‘audio_url’: ‘https://ift.tt/7S5Ixeq’, ‘punctuate’: True, ‘format_text’: True, ‘dual_channel’: None, ‘webhook_url’:26028, ‘end’: 26386, ‘confidence’: 0.94482}, {‘text’: ‘are’, ‘start’: 26418, ‘end’: 26566, ‘confidence’: 0.7851}, {‘text’: ‘what’, ‘start’: 26588, ‘end’: 26726, ‘confidence’: 0.99999}, …

Let’s update the metadata to something we want using the below code:

doc[0].metadata = {“audio_url”:doc[0].metadata[“audio_url”]}

Chunk The Text

I’m taking chunk measurement as 700 however you may change this quantity.

text_splitter = RecursiveCharacterTextSplitter(chunk_size=700, chunk_overlap=0)
texts = text_splitter.split_documents(doc)

texts will seem like this:

[Document(page_content=” Load time, a brand new president and new congressional make-up. Same previous partisan divides, proper?…, Document(page_content=”supporters of former President Donald Trump. We’re going to name them the Trump Republican. Another 17% …, Document(page_content=”Republicans are firmly in opposition to compromising with Biden with a view to acquire consensus on laws, as y…, Document(page_content=”make it simpler on your self to type a governing coalition, one thing the Biden White House could need to take into consideration. When we come again…

Generate Embeddings And Save To Database

In this step, we are going to generate embeddings for the above textual content utilizing beneath traces:

db = Chroma.from_documents(texts,OpenAIEmbeddings())
llm = ChatOpenAI(mannequin="gpt-3.5-turbo", temperature=0)

Query And Get The Response

This step will create a QA chain and passing the question to that, will give a solution.

chain = RetrievalQA.from_chain_type(llm,retriever=db.as_retriever(search_type="mmr", search_kwargs={'fetch_k': 3}))
question = "What this audio file is all about?"
chain({"question":question})

Here is the output:

{‘question’: ‘What this audio file is all about?’, ‘outcome’: ‘The audio file discusses the present political panorama within the United States, particularly specializing in the divisions throughout the Democratic and Republican events. It mentions the emergence of 4 political events inside these two main events and discusses their differing views on compromising with President Biden to cross laws.’}

You can see the way it was to generate the textual content and get our questions answered.

HI-FI News

by way of C-Sharpcorner Latest Content https://ift.tt/FM8pou2

November 12, 2023 at 07:39AM