How To Generate Sentiment Analysis For An Audio Using Python
Over the years, the use of AI has grown exponentially. The technology is used for a tonne of things right now, and one of them is the Speech To Text category. The Speech To Text technology was first invented in 1952 by Bell Laboratories and it was called 'Audrey'. It was a technology that could recognize digits spoken by a single voice.
In the modern days, this technology has received massive recognition, and it vastly used by almost every mobile phone users on a daily basis. There are now some advanced features that can be implemented using the Speech To Text technology, and we are going to be looking at one of it today which is 'Sentiment Analysis'.
Sentiment Analysis, according to wikipedia, is the use of natural language processing, text analysis, computational linguistics, and biometrics to systematically identify, extract, quantify, and study affective states and subjective information. In other words, we can use this sentiment analysis feature to determine weather the sentences said in an audio is a positive, negative or a neutral one
To do this, we are gonna use AssemblyAI. AssemblyAI is an API that simplifies the concept of converting speech to text. Their API is very advanced and it has a lot of features, but we are going to be looking at the Sentiment Analysis feature in this article. To use AssemblyAI, you need to head over to assemblyai.com, create an account by clicking the 'Start now for free' button in the top-right corner of the site. Once you have an account, login and you should be taken to your dashboard. The process should look like this:
Once you have created your account, and you are redirected to your dashboard, on the right corner, you'll see where your API key is written, just click on it to copy it. once you have it copied, paste it somewhere
Once you have your API key and the link to the audio file you want to use, the next thing to do is to jump into your code editor. For this tutorial, we are gonna be using python. Paste the code below, into your code editor:
1 import json
2 import requests
3 import pprint
4
5 api_key = 'you-api-key'
6
7 TRANSCRIPT_ENDPOINT = 'https://api.assemblyai.com/v2/transcript'
8
9 response = requests.post(
10 TRANSCRIPT_ENDPOINT,
11 headers={'authorization': api_key, 'content-type': 'application/json'},
12 json={
13 'audio_url': 'https://www.americanrhetoric.com/mp3clipsXE/politicalspeeches/joebideninauguraladdressARXE.mp3',
14 'sentiment_analysis': True
15 },
16 )
17
18 response_json = response.json()
19 print(pprint.pprint(response_json))
- The first 3 lines, we are importing json, requests and pprint
- The json library, in this program, is used for converting the response into a json format
- Requests is used to send a GET or POST request to the API
- pprint is used to produce well formatted output of our response
- On line 5, a variable named api_key is created and it should contain your API key
- On line 7, we created a variable that contains the endpoint in where we want to send a request to
- Then on line 9, we created sent a post request to the endpoint we created on line 7 (TRANSCRIPT_ENDPOINT)
- We then specify all the elements needed to send the request, and this can be seen from line 11 to line 16
- Specifically on line 12, we create a new json dictionary that contains the audio url and the option of sentiment analysis. Make sure to replace the audio_url with your own audio, and set sentiment_analysis to True.
- On line 18 and 19, the response is retrieved from the request sent and converted into a json format, then we use pprint to print it out in a well structured format.
In the output, you need to copy the ID of this request from it. It should look like this:
Once you have the ID, we can now get the sentiment analysis of this audio. To do this change your initial code to this code
1 import json
2 import requests
3 import pprint
4
5 api_key = 'you-api-key'
6
7 TRANSCRIPT_ENDPOINT = 'https://api.assemblyai.com/v2/transcript/o6c5xd4uja-3301-4b74-a7e6-48a159e2df1d'
8
9 response = requests.get(
10 TRANSCRIPT_ENDPOINT,
11 headers={'authorization': api_key},
12 )
13
14 response_json = response.json()
15 print(pprint.pprint(response_json))
- On line 7, modify the TRANSCRIPT_ENDPOINT by adding '/the-id-you-copied' to the url in there
- On line 9, change 'request.post' to 'request.get'
- In response, remove the json dictionary in it and in headers, remove 'content-type' and it's value
Post a Comment
0 Comments