# Auto-Tweet Your Funniest Sentences Using Speech Recognition in Python

## Метаданные

- **Канал:** AssemblyAI
- **YouTube:** https://www.youtube.com/watch?v=UTRtdIq2xCs
- **Дата:** 07.03.2022
- **Длительность:** 13:21
- **Просмотры:** 1,255

## Описание

We say the funniest things when no one is listening. But what if someone did, all the time? In this video, we will make an app that will listen to you all the time and Tweet the funniest, smartest or most relatable things you say out loud.

Get your free Assembly AI API token here 👇
https://www.assemblyai.com/?utm_source=youtube&utm_medium=referral&utm_campaign=yt_mis_21

We will build this app using Python, the Twitter API (Twython) and AssemblyAI's API.

Get the code: https://github.com/AssemblyAI/youtube-tutorials/tree/main/tweet%20with%20voice
Prefer to have this tutorial in written format. Find it here: https://www.assemblyai.com/blog/auto-tweet-your-words-using-speech-recognition-in-python/

▬▬▬▬▬▬▬▬▬▬▬▬ CONNECT ▬▬▬▬▬▬▬▬▬▬▬▬

🖥️ Website: https://www.assemblyai.com
🐦 Twitter: https://twitter.com/AssemblyAI
🦾 Discord: https://discord.gg/Cd8MyVJAXd
▶️  Subscribe: https://www.youtube.com/c/AssemblyAI?sub_confirmation=1
🔥 We're hiring! Check our open roles: https://www.assemblyai.com/careers

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

#MachineLearning #DeepLearning

## Содержание

### [0:00](https://www.youtube.com/watch?v=UTRtdIq2xCs) Intro

there is nothing worse than seeing the code you wrote two years ago tweet hey everyone today we will see exactly how to do this tweeting using voice controls we will make a python app that will constantly listen to your microphone in the background and immediately when you say tweet it will tweet the last thing that you said on your twitter account so let's get started right let's get started so the

### [0:30](https://www.youtube.com/watch?v=UTRtdIq2xCs&t=30s) Setup

first thing that i want to do of course as always is to import the libraries that i need and some other things so i'll show you what i need to import for this project we will be using web sockets this will help us connect to assembly ai's api we will be using the asynchronous io from python we will show i show you how that works but basically we want to re listen to the microphone and at the same time always send what we are receiving to assemble the ai's api so that's why we need to use an asynchronous i o in the python library we will use base64 for encryption purposes or encoding purposes and then we will also need json to send but also to understand what we receive from assembly ai we will use pi audio to listen to the microphone we will use twithon to make it easier for us to connect to the twitter api and kind of use it and lastly we will need some configuration information this includes the authentication key from assembly ai and also some other keys from twitter's api so let's first set that up for assembly ai's authentication key it's quite simple all you have to do is go to assemblyai. com or you can use the link in the description below to get a free api token otherwise you can just sign in to a sign up for assembly ai and once you sign in you will get your api key here and you can just click this one to copy it and just create a separate file that has all of these information and for your authentication key paste what you just copied from assembly ai for the other keys what you need to do is to create a twitter developer account so again it's very simple you can just go to twitter developers on google and just go to the first website that pops up you need to create an account as i said and then we will at the end up at the developer portal you just need to create a app for your twitter api to be able to use the twitter api i've already created the app that i'm going to use for this application that i'm making but otherwise you can go to overview add a new app and just follow the steps that are in front of you once you've created an app you can go to the app settings and then say authentication setup you can choose authentication 1. 0 and then just choose what kind of permissions that you need for your app so for this one we're going to need read and write we don't even need read so much we mostly want to be able to post tweets on our twitter so we choose a second one and then you just need to create fill in some information of what website that is going to redirect to and also the name of your organization or a name of your website etc once you save this information you're going to get the additional keys that you need and then all you need to do is to fill in all those keys here then you'll be

### [3:27](https://www.youtube.com/watch?v=UTRtdIq2xCs&t=207s) PiAudio

set now that we set our authentication and our connections or generally the keys and secrets that we need for these authentication and connections let's go ahead and start writing our code the first thing that i need to set up is listening to the microphone and as i said we're going to use pi audio to achieve this so for that i just need to set up some parameters like frames per buffer the format of the audio that i'm listening to the channels and the rate so you can just use what i'm using here but important to note here that the rate that you choose here needs to be the same rate as the assembly ai endpoint that you're choosing so i also show you what the assembly ai endpoint looks like it's just basically a url and here we are choosing the real-time api of assembly ai so it will transcribe the audio in real time and the sample rate is 16 000 just as the same one we set up for pi audio and then i am starting a stream using pi audio and this stream will help me listen to the microphone and what is being said to the microphone next i'm

### [4:29](https://www.youtube.com/watch?v=UTRtdIq2xCs&t=269s) Twilio

setting up twithon so all you need to do with tryton basically to connect to it is to pass your configuration information including the consumer key consumer secrets access token and access token secret using this from now on we will just be using the twithon's interface to interact with the twitter's api all right so time for the actual fun part so for this part i'm going to be creating an asynchronous function and it will be called send and receive because as i said we want to get what is being said to the microphone send it to assembly as api and then receive an input and to run this function of course i need to call it at the end of my python script here the first thing that you want to do in this asynchronous send and receive function is to connect to assembly ai's api and to do that i'm going to be using websockets so what i'm doing is basically announcing that i am going to try to connect to this url that you this endpoint that you were specified and using websockets i'm going to use the authentication key and if you need some other information also include those here but basically what we need here is url the end point the authorization key the ping interval and the ping timeout so how long it should wait before it times out and make a connection after we set the connection we try to establish the connection here and if everything goes well that means that we are ready to send the messages to assembly as api

### [6:01](https://www.youtube.com/watch?v=UTRtdIq2xCs&t=361s) AssemblyAI

next is setting up the function that will send the data to assembly ai's api it is quite a simple function it's called the send function we will use the while true loop to indefinitely run this i will be using try and accept to make sure that if there are any errors i am able to catch them but the main functionality of this function is basically here what we're doing is we get the stream from the stream we read the data the audio data and then we encode it into whatever we need format that we need it in and then we send it to assembly ai using the web socket that we have just created this function only waits 0. 01 seconds in between runs and we achieve that using the await keyword here lastly of course we have to get information back from assembly ai's api so what we're going to do is to have another asynchronous function that is the receive function again it will run indefinitely it will run all the time using the while through loop again i will be using the try and accept keywords here to make sure that if there are any errors i will be able to catch them but again the main functionalities here so let's walk through this one so at first what i'm going to do is again connect to the websocket and listen for any responses and assembly ai will always return responses to me with every word that i say so maybe let's first understand what assembly ai's response looks like so i will remove all the other functionalities for now so what i want to see here is just a response and show you how fast the response actually happens so if i just run my code here hello one of the most annoying things is to have to read your own code and fix it so basically what's happening here is assembly ai is constantly transcribing the audio that we're sending to it and it is sending us responses and once you finish a sentence it actually fixes the punctuation adds dots or commas if it's necessary and also capitalizes the important the beginning of the sentence or if there are any other words that need to be capitalized in between so basically this will be the final sentence that we get and there is actually a way to understand which sentence is the final sentence to understand that better let's um look and into the results str so the resulting string this is basically a json format response so if you look at look into it we will understand better what the assembly ai response looks like but i don't want to constantly see it because it's always going to be returning responses to us so i only want to see that adjacent response if the result is not an empty string so let's run this again hello world okay i can stop it now so basically what you can see here is that when the result is not empty it tells me or it shows me what the whole response looks like so there are a bunch of things in this response we have the when the audio started when it ended the confidence with which that it um recognized the speech in this audio uh the text so what kind of text or words have been uh recognized in this audio but also another important thing to show here is the message type so as you can see here it only says that this is a partial transcript so it is still listening the sentence is not done and when the sentence is done the message type turns into final transcript and this is the one that we're going to use to tweet our sentences because we don't want to tweet out sentences that are half done so let's see how we're going to do that at first of course as i said the main thing is to filter out the sentences that are not final transcripts so that's why i'm going to only take the ones that has a message type of final transcript and i want to also print these results just to kind of you know have a sanity check so that i can see what they look like and to tweet these out we actually only need to write for lines of code so to send a tweet using uh twython it's very simple we already created a twitter variable using twithon here at the beginning of this script what we're going to do is to say update the status using the previous result so here i'm saying previous result but because how this app is going to look like is you're going to say a sentence and then if after saying the sentence you decide that it is twitter worthy you can say tweet and that's why we need to keep an eye out or keep in mind what the previous result or previous sentence that we said so that's why in this line here i am saving the previous result or the result of this sentence to be used in the next time the program runs as a previous result so if the resulting transcript from this timestamp is tweet if i just said tweet we will look at the previous result and if it's not empty i'm going to update my twitter status using the previous results and i'm also going to print it out on the terminal screen to kind of do a sanity check so let's see if this is going to work for this i'm going to put my terminal screen right next to my profile on twitter so let me clear this first and then we can run it again all right so here we will see that it will transcribe everything i say but it will not tweet things yet because i am not saying tweets specifically on a single sentence i can even refresh my page and then you will see that nothing has been tweeted yet it's still my last tweet is from two hours ago so let's try to tweet something let me think of something fun to say you know maybe i'm just coding by myself at home and i need to fix some bugs and a code that i wrote like five years ago maybe i will say something like hmm there is nothing more annoying than to have to fix the bugs of a piece of code that you wrote five years ago tweet all right so it gives me the confirmation that it has tweeted the sentence so let me refresh my page and see if that actually happened hey yeah and it is here there is nothing more annoying than to have to fix the bugs of a piece of code that you wrote five years ago as you can see it has been posted just recently so this is amazing that means that it's working and you can see that it is still transcribing everything that i'm saying here i hope it worked for you too let me know how it went in the comment section below hey thanks for watching and i hope you enjoyed this video if you liked it don't forget to give us a like and maybe even subscribe we would also love to hear your questions or comments in the comment section below but for now thanks for watching again and i will see you in the next video

---
*Источник: https://ekstraktznaniy.ru/video/13179*