Build a Flutter Voice Assistant App with ChatGPT & Dall-E AI Image Generation | Machine Learning

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

that's so cool!

👍︎︎ 1 👤︎︎ u/Ronin_Spect 📅︎︎ Mar 30 2023 🗫︎ replies
Captions
explain Quantum Computing to me Quantum Computing is a type of computing that uses quantum mechanics principles to process information in simple terms it makes use of the ability of quantum bits or qubits to exist in multiple States at the same time instead of just 0 s or 1s like classical bits which enables it to perform computations in parallel quantum computers are built using specialized Hardware that can manipulate qubits and perform operations on them such as entangling them and measuring their state explain like I'm a five-year-old child Quantum Computing is like having a really special toy that can do many things at once it's like having a magic ball that can be in different colors and shapes all at the same time this toy can help grown ups solve big problems like figuring out very hard math problems or making secret codes much faster than normal toys but this toy is still very new and grown-ups are still trying to make it even better foreign on a futuristic interface so this is the voice assistant app that we are going to develop we'll be using charge GPT and Dali API along with flutter to build this voice assistant so let's get started so the first step is to create a flutter project so to create it we are going to run the flutter create command and then the name of the project followed by dash dash org which specifies the organization name and it's going to be com.revone dot Allen we are specifying an organization name here because randomly it generates com.example dot Allen and testing it on a real iOS device isn't allowed that's why we are specifying an organization name right now then we'll click on enter then we can move to the Allen directory and open it in vs code after that we can go to the lib folder in the main.file remove the my home page class remove the comments in my app class and create a new file called home underscore page dot dot in this and we'll import material dot we create a stateful widget call it home page and return a scaffold now I can take this home page password turn over here let's remove this we'll just import it using this line and then we'll save it now let's run the app on an iOS simulator if you want you can go ahead and test it on an Android emulator as well just to see that we got the basic app running so let's run it without debugging and while the app is running let's go ahead and install some assets that are required in our app the assets can be found on this link I'll mention it in the description below what you need to do is go to this folder and install all three of these fonts which will add a unique font to our app the image which is the virtual assistant image you can use your own if you want and sounds which is related to speech to text so when we get to it we'll be using these three audio files after you've got the folder video just open the Allen folder and drag the assets folder to it after we do that we get an assets folder right here now we just need to go ahead and register all of these Assets in our Pub spec.tamil so let's go ahead and do it so here in the assets part let's uncomment these things and add assets slash images so it will register the virtual assistant image so we'll remove this then we need assets sounds then we'll remove this line so it will register all the sounds and this is all that we need then we need to scroll down and register the font so let's uncomment this line let's call this setup Pro which is the family name then we'll register the font which is assets slash fonts then we need setup row bold Dot OTF if we just see it over here and similar to this we need Sarah Pro medium so let's register it right here and save it the style is going to be it's not needed so let's remove it actually so we have these two assets and this is the font family we are going to access these fonts using this family now we can exit this let's see this is our app an empty scaffold that's correct now let's remove the debug show check mode Banner so it will remove this line at the top and also we'll change the title so let's call this Allen we'll also remove the theme data it's not required anymore and the last thing we need to do before moving to the UI part is installing the colors that are required so we login go to this GitHub repository go to the left folder go to palette dot dot and copy this this contains all the colors required in our app so that we don't waste much time writing them all now we can go ahead building the UI on our home page so the first step to it is just adding a theme here so the theme is not going to be theme data because we'll have to put in a lot of things here what I want to do is theme dot dot light because you want a light theme for our app but if we just do light theme what happens is this background isn't Pure White what I want is pure white but it gives us somewhat grayish color so I'm just going to copy with and pass in a background color which is careful background color and change it to palette dot white color palette dot or white color is basically colors.white we are just storing all the colors in one single class so that whenever we want to change the theme of our app we can do it from one class itself also one thing I want to do is use material 3 set to true that means my entire app is of the design of material 3. if you want to check out the difference after we built the UI you can set it to false and see how it looks with and without material 3. there's not much difference but it just looks better in my opinion with material 3. now let's get to the home page and start designing the app so the first thing that is required is the app bar and just so that we are on the same page I've opened this file so this is how our app bar looks here what we need is a title and then in the corner we need this menu icon so let's get it we have an app bar we'll pass and an app bar then in the title we are going to pass in a text which is going to have a text of Allen and also we'll put a const and then in the side we need this icon so to get this we are going to pass in a property called as leading with icon as I can start menu this is also going to be a const and save it also since we added some things to our perspect.yaml file let's restart our app and see how it looks so app has restarted and if we see we are having an app bar with a menu like but if you closely observe the color of the ABBA is different from this body the reason for that is in the main node.file we have registered a theme where the scaffold background color is white color but you have not specified that the color for this app bar should be white as well so to do that we are just going to mention Abba theme aware pass in Abba theme and the background color is going to be palette Dot white color then we can save it and if we see both of them are of the same color now also in the title right now it's in the center because you are running it on an iPhone simulator but if we run it on an Android emulator this text will be here it's not in the center so to always make sure that it's in the center we are going to have Center title set to true so let's go to home page and here we'll pass in Center title true now we need a body so we are going to have a body where we are going to pass in a column and in those column we are going to have a children why do we need a column because if you see this we are going to have this image we are going to have this part right here we are going to have this text and when then we are going to have these three suggestion boxes so we need a column so that we can lay them in a vertical orientation so let's pass it in the first thing that's needed is this part right here so let's figure out how to work on this part if you closely observe this image is bigger than the circle that's the kind of design we want so we can't use just a container passing in the background damage over here what we need is a stack so that we can have a circle background and on top of it we have this virtual assistant picture which looks slightly bigger than the circle behind so to demonstrate and code what I'm talking about is just you know having stack then we'll have children again and the first child that I want is this circle behind because that's how stack Works thing that you mentioned first is going to be below so you can just pass in a container where the height is suppose 120 the width is 120 the margin is constant Edge and such dot only and from the top I want 4 that means the ab bar and this they're not stuck together and there's a difference in them but if I do just this much it's not visible on the screen to make it visible I need to add a color so I'm just going to pass in decoration which is going to be box decoration pass in a color and the color is going to be palette dot assistant Circle color now if I do this you can see this right here but it's a square right it's not really a circle so to convert it into a circle we'll pass in a shape property which will be box shape dot Circle and then we have a circle with us that's looking great but what I want to do is put it in the center so to do that I'm going to wrap my container widget with a centered widget after we do this this comes right in the center perfect now that I have circled with me let's put a const here I want the virtual assistant picture to show up that means I want to access this virtual assistant image so to do that I'm just going to pass in another container then I'll pass in a height which is going to be 123. I said it's going to be slightly bigger than the circle right that's why the height is 123 then the decoration which will be box decoration shape will again be box shape dot Circle so that you know we have this aligned with the circle that's going to be behind it and then we'll pass in the image and the image properties present on the container we didn't pass the image property here as I said because we want this image to be slightly bigger than the container so we have this image we'll pass in the decoration image and then the image here is going to be asset image because we want to get access to this image right so we'll pass in the asset which is assets slash Images slash virtual assistant dot PNG and save it now let's put a constant wire so that we remove the warning if you're wondering why I'm getting the warnings in line it's because of the extension year which is error lens so it will show all the errors to you in one single line and now if we take a look at the app here we have it let's expand this app so that it looks bigger after this what we need is this part right here it looks like a chat bubble saying good morning what task can I do for you so in this case as well we are going to use a container so let's get out of the stack so this was the profile part let's call it virtual assistant picture after this we need to work on the chat bubble let's call this chat bubble because this is the place we are also going to get our outputs right now let's get to our container we are not going to specify any height because whatever height we specify it might exceed that for example good morning what task can I do for you has a specific height but if the chat gpt's response comes it's going to be bigger than this so we can't specify any proper height because we don't know what the output is going to be and how many characters long it is going to be so for this we are not going to mention any height we are just going to tell whatever the space of the text is going to be take that much space but leave some padding for us whatever you do just leave some padding for us so here we are just going to pass in a padding which is going to be constant Edge inserts dot symmetric and if you see from the horizontal Direction I want a space of 20 and from the vertical Direction I want a space of 10. so let's pass it in from horizontal 20 vertical 10. other than this what else I need is a space from here so I can either use a size box over here or I can just specify margin which is going to be constant as in such dot symmetric horizontal 40 so it leaves a margin from here you can see that and from the top also I need one margin that's what I'm going to do now so from horizontal I need 40 but down below I need 30 but I don't want it at the bottom so I cannot pass in vertical 30 because from the bottom I don't need 30 that's why what we are going to do here is dot copy with so it's going to copy the horizontal value which means left is 40 right as 40 and from the top we are going to specify 30. now let's just see the container how we've got that so we are going to pass in a decoration which is going to be box decoration border and border is going to be border dot all where we need to pass in the color and color is going to be palette dot border color so from all the sides we are having a border and the border is of this border color now let's see how it looks and here it is now it is not like this but we can work on it first of all this container is looking very small because we don't have any text in it so let's pass a child to this container so that we have a text and we're going to call this text good morning what task can I do for you okay so let's put a constable here let's increase the font size as well so we have style constant textile where the color is palette dot main font color Also let's remove a cons here because the text is already a constant and save it and here we go but the text here isn't very big so let's increase the font size so that's 25 save it here we have it but the text isn't looking like this so we need to change the font family which we've already registered in pubspec.aml file if we scroll down so let's pass it in font family setup Pro it should be exactly what you've typed over here and then save it and you see the text has changed and this looks much better however what I want to do is this text should have much more padding from the top so I'm just going to wrap this text with a padding widget and then have constant agencies Dot symmetric from vertical I want let's say 10. let's remove cons from your cons from here and make this entire padding widget a constant to come away and this looks better now what I want to do is style this so I want to give it a border radius like this but here the Border radius shouldn't be there what I mean is if you look at this part this part has a border radius this also has one this also has one but this one doesn't have any so let's figure out how to do that here we have a property called body radius and then we can pass in Border radius dot circular 20 if we do this the entire thing is now circular this looks good but if you see carefully this part is also rounded we don't want it rounded so what I'm going to do here is border radius dot circular20 dot copy with and the top left part because this is top left and we are going to mark it as 0. and the argument type int cannot be assigned to the parameter type radius so I'm just going to pass in radius dot 0 because it requires a radius and the radius here is zero save it and here we have it this part doesn't have about radius and all of these things have and it looks good now after this container the next thing we need is this text so let's call this suggestions list but before that we are just going to have our text which says here are a few commands or whatever let's see what it says here are a few commands let's call this your r a few features then we are going to have a style text Style font family is again Sarah Pro if you're wondering why we didn't specify the font family over here in the theme itself so that we don't have to type again and again it's because font family is not present in copy with function of themedata.light if you just use theme data you'll be able to pass in font family but when you're using copy web you don't have access to it that's why we constantly have to type it out here the color is going to be palette dot main font color and font size is 20. font weight is font weight dot bull so what I've done is given it a shorter height than this the color is the same and I've bolded it and if you see here are a few features but still this isn't looking very good so I'm just going to wrap this text with an align widget where I'll pass in the alignment as alignment Dot Center left because I want to shift this to the left right so I'll save it and here it goes but still this isn't enough because it completely shifted it to the left hand side I still want the padding to be there so what I'm going to do is convert this align to a container container is basically made up of several widgets if you just go inside it and inspect it's made of alignment widget padding widget so we can pass in alignment here then we can also pass in a padding which is constant Edge and such dot all and then I'll pass in 10. save it and here we are also I need some kind of you can say margin from here so we can leave that margin we have margin engine sets dot only from the top I'll leave let's say 10 and from the left I'll leave 22. and then save it so here we go looks good also let's pass a constant over here luxury in this case padding and margin are doing the same properties I just wanted to Showcase it to you if you want you can just integrate this with a single padding property but basically in this case margin and pirating are the same things of course if you add borders and stuff it becomes different but for our use case both of them are doing the same thing right now after having that we'll get to the suggestions list the suggestions list is basically this boxes right here that tells us features so let's just call it features list and let's figure out how to get done with this suggestion boxes so here I'm going to specify a column widget pass in children and why do we need a column widget you'll understand that when we start integrating the chart GPT and Dally API it's basically like we will cover this entire column widget with a visibility widget so that we can conditionally show this column and it will be much more easier if we have all of these three boxes that do the same work in a single column but you will get a greater idea when we get to the integration part for now I would recommend you to just follow along so the next thing is this feature box and since we have three of them what I'm going to do is create a usable widget for it in the lip folder let's call this feature underscore box dot dot import material dot we are going to create a stateless widget for you and call it feature box the point of this feature box is just to you know showcase the features nothing much so here we are going to have a container where we are going to have a margin and the margin is going to be engine sets dot symmetric and if you see from the horizontal direction we have a greater margin let's say 35 and from the vertical direction there's 10 because the space over here as compared to your you can see the difference this is vertical this is horizontal right so let's put a constable here then we might also need decoration which is box decoration so that we can pass in a color and the color is going to be taken from the Constructor because each of these boxes have a different color right so here I'm just going to ask final color color let's add the final Field property now I can use this color over here then we are also going to pass in a border radius which is border radius dot all radius dot circular 15. so we're just adding a border radius we don't have any border the border is not there but there's a border radius here which will help us clip out things let's put a constant here as well we won't be able to see anything so let's take this feature box paste it over here we'll pass in the color the first color is going to be palette DOT first suggestion box color I couldn't think of any better name my apologies and you're still not able to see anything but that's because we have not put a child to it so it doesn't have any height once we add a child it will be able to show it to us what we've got so here we'll pass in a child and the child is also going to have a column and then we are going to have children the first child is text and this is just going to be this header text so let's take it from The Constructor because every feature box has a different header text we'll have this let's require it we have required this dot header text path and header text then the last Style textile font family is setup Pro if you are annoyed by writing a ton and feel like you'll forget and mistype this what you can do is just save it in a new file in a variable which can be used anywhere in your app and just keep using it that would be a better technique so that you don't have any errors and spend hours on it so let's pass in a color and color is going to be palette dot main font color let's not take me in font color this should have a black color so let's pass in black color the font size should be 18 and font weight should be font weight dot bull save it also put a constable here we'll go to the home page we'll pass in our first header text which is chat GPT save it and here we have it doesn't look good at all but this will be better when we have this line right here because it will take the entire screen with right so let's ask for a description text as well so we have final string description text then we are going to require it through the Constructor I'm just going to copy this text and make changes over here so I have text this is going to be description text the font family is Sarah Pro the font size is not 18 it's going to be the default one 14 or 16 whatever the font is not bold anymore and that's basically it so let's pass in a description text what is our description text a smarter way and all of these things you can make your own just paste it over here and if we come you can see the box is now bigger but it still doesn't look very good there are multiple problems with that first of all the padding problem so let's pass in a pairing to this column so that you know these texts have certain pairing to them and they are well spaced out like this you know there's some padding right here it just looks cleaner that way so let's fasten up padding widget where we are going to pass in constant.h in search dot only top is 20 left is 15 bottom is 20 again so what you can do is since top and bottom is 20 you can add edge in such dot symmetric vertical 20 so that means top and bottom is 20 and then we want left to be 15 so we are just going to have copy with left 15. so it's the same thing like top 20 bottom 20 left 15 is just another way of doing it and we have a better looking box here it doesn't look you know very messy next thing I want to do is take this chat GPT text and push it to the left hand side so for that I'm going to wrap this with a container or a line widget whatever you want then we can pass in an alignment which is alignment Dot Center left save it and there we go looks better and it looks similar to this one perfect also if you want to be Pixel Perfect what we can do is just leave a size box of height three here so that you know there's much spacing between these two elements so let's go ahead here in the home page and put this feature box again this time we are going to have this text dial e the color is going to be second suggestion box color the description text is going to be this thing right here paste it in over here save it and here we have it but if you look at this we have get inspired and stay creative with your personal and this is looking pretty shabby to be honest because it's going too much in the right hand side corner if you look over here the personal assistant drop to the next line because there was something over here was that something that's padding widget so what we're going to do is go to this feature box wrap this text with a padding widget and pass in dot only from the right hand side I want a padding of 20. right so with this the personal goes to the next line and it looks less shabby again we can go to the home page now and add a third feature box paste it in over here this is going to be called smart voice assistant paste it over here then we'll copy this line paste it over here and save it now to resolve these errors you are just going to put a constant over here also this is going to be third suggestion box color perfect so RDI looks good now next thing and the last thing is the mic button so you just want to say that you know if you want to record your voice just click over here and then only we listen so we are just going to go here in the scaffold where we have floating action button then we are going to have on pressed which is going to be an empty function again then there's going to be a child and the child is icon icons dot mic of course there's more stuff to be done in this Floating Action button because just a mic button isn't enough but this looks good right and this is what material 3 does to us material 3 we have the squared kind of rounded rectangle kind of mic or Floating Action button with just no material three it's going to be a rounded button let's see the difference after we add a color to this Floating Action button so we are going to pass in the background color as palette DOT first suggestion box color so whatever color was in this feature box it's going to be over here as well all right and this looks good now let's go to the main.dart file I'm just going to take the simulator from here put it in vs code so that we are able to see the difference as we do it and you'll get a better understanding of material three versus other things in this application of course so let's remove this use material Theory save it and you see this is the difference our app has changed basically the font size and everything there was much more Gap or an increased font size in material 3 the elevation was not present in the air bar and the text won't automatically of the black color when we use theme data.live if we add use material 3 this app bar doesn't have any elevation of its own these things are bigger in size as you can see and this Floating Action button again if you want to see was rounded here it's square and looks much better okay so this was about the UI part of this now let's get to the interesting part so now the next thing we want to do is whenever the user clicks on the mic button we want to convert whatever voice the user gets to a text so to do that we are going to use a plugin let's search it we want flutter speech to text so whatever plugin comes up first we are just going to use that and here it is speech to text and it was maintained last three months ago so it's a safe package to use let's go ahead and use it we'll add this dependency also what I've done right now is connected my iPhone to this system so that I can just test it out in real life because I'll have to give my voice if you want you can find a workaround in the iOS simulator itself or the Android emulator itself I'll just run the app on a real device so after this is done let's see if there are any installation processes to complete let's scroll down here it is permissions so we need to add these two permissions in the n4.pls file just follow this if you are working on Android on iOS simulator or Mac OS because you cannot develop iOS apps on Windows so you can just have info.plist let's copy one of this Keys here paste them so you need two keys one is the speech recognition other one is micro microphone usage also make sure to write a description since I'm not publishing it on the Apple or Apple Store I am not going to mention any string here as a description then we can close this let's see what else we need to do then we need to add these permissions to androidmanifest.xml so let's go ahead here paste it and write below this manifest tag then if you're using Android SDK 30 or later so our Target SDK and build.gradle is more than 30 so we can just copy this line and we need to add this right after the permission section that means right here let's indent it properly and there we have it luxur then we can close this and you can see the optional tab here adding sounds for iOS these are the sounds that we've added right here so if you just see these three sounds these are needed for iOS so whenever you click on the button like the start recording button these sounds will play so when we start it's going to start this m4r file after that when we stop or cancel we are going to run these files and this is only needed for iOS for Android it's automatically done if you just read this through Android atom automatically plays system sounds when speech listening starts or stops but iOS doesn't so I think we have this done now let's try to run the app on our iPhone so I'm selecting that device running without debugging and let's get started with it and while that is happening let's go ahead and write some code in our home page basically we want to set up the speech to text plugin right so let's go ahead first of all we want to make sure that we have instantiated the plugin properly so what we are going to do is call in its data wire let's call it yep also I think our build failed so let's look why here you can see no profiles for com.ravon.allen.allen were found so to fix this what I'm going to do is go to my iOS folder right click and open a next code then we can click on Runner and then from here I'm just going to run my app and if I just minimize this as you can see it's running on the iPhone so that means everything is working well our configuration is hopefully working well and since this is a smaller iPhone I'm getting this render flow error so to fix this I just need to go to the home page and in the column I'm adding a widget called single child scroll View then we can save it and obviously since this is running from xcode we'll have to restart the process so I'm just going to close it from here hopefully the issue has been resolved and try running it from here and as you can see the app has launched now so we have the hot reload feature and if we come over here the error has been resolved now let's instantiate the speech to text plugin that's why we have in its state over here so in any state we are just going to call init a speech to text this is the command we are going to call copy this or future void and this is going to be asynchronous and yeah we are going to use the plugin so we need access to the object of speech to text class so let's have final speech to text which is equal to speech to text okay now we have this now we can use this page.txt and call the method of instantiating so we can call Dot set initialize and then save it Also let's await it and after the initialization process has started we are just going to call such date so that the build function rebuilds and we know that it's ready to be used to know how I found this method basically it's all written over here you can follow this documentation and we will be able to have it as you can see awaits speech to text Dot initialize I just edited out this documentation based on my needs so let's not spend much time over here what we can do is just copy all of these methods so let's copy these things copy it and paste it right here also these are not private methods anymore we can just remove them then let's take the speech to text pass it over here over here this is just a callback of this so we can have on speech result I'll explain what these functions do but let's just type it out first let's resolve all of the errors and we'll have it okay so this is one error basically first let's understand all of these methods so first of all we have start listening after we have made sure that we initialize the plugin we are calling speech to text Dot listen so whenever the user clicks on the mic button over here we start listening to it and what it does is basically constantly listen and whenever the user speaks and speech to text is able to convert that speech into text it runs the on result function and in the on result what we are doing is basically getting speech recognition result and we are setting state to last words equal to result dot recognize words what this means is this last words is going to contain whatever the user has said so we can just create a global variable for it let's call this last words you can even name it speech or something then we'll take this last words and paste it over here which already done okay then we have the stop listening method whenever the user is stop saying something they can obviously just click on the mic button again and stop listening will be done let's remove all of these comments and here we have it so one thing to keep in mind is on result the Callback function over here which we have in the form of on speech result will only get run when it recognizes some words and whenever it recognizes certain words it will just save it to last words also last thing we can just call dispose method and in the dispose we are just going to call speech to text Dot stop we are not calling this method because it will call such State and if the screen has been disposed of there's no way we can call such state right I mean we have gone off the screen how can we run the build function right so now that we have this let's go to The Floating Action button here we have the on pressed and in the on press we are just going to have an if condition since we are first checking if the speech to text has permission so if await speech to text Dot has permission also since we called await we need to call async over so if speech to text has permission and speech to text is listening and actually is not listening so that means if the speech to text has permission and it is not listening so then we can save it can start recording so we can just call start listening as this speech to text Dot is listening so that means it is listening that means the recording is on and when we click on this we need to disable the recording I mean we just need to pause the recording so in that case we are just going to call stop listening and since this function is asynchronous let's mark it as future y okay now we can call a weight over here and if none of these conditions match that means we don't have the permission or that time we are just going to call in its speech again right in a speech to text function so that means if speech to text has permission and speech to text is not listening then we start listening if the speech to text is listening and then we click on The Floating Action button then we stop listening else we are going to enhance speech to text because that means we don't have the permission if we don't have the permission we need to ask it to initialize and while initializing it will just ask the user if it's fine if they you know start recording so they need the microphone access or something that place okay let's open the terminal let's restart the app and yeah if you see Alan would like to access speech recognition we are going to say okay then it would like to access the microphone and the description here is mean because if we go to info dot PLS we have main written over here so let's put OK over here as well and then start recording so in the recording I'm just going to click on The Floating Action button and here you can see whenever I say something it's able to recognize it over here then we can click on the button again and you can see finish successfully invoke flutter notify status that means the plugin has stopped and it's not able to identify anymore and if you see over here recognize words and here you can see whenever I see something it's it's not proper but you can see we're getting recognized words we are getting the confidence with which it said 0.797 that's 79.7 percent and these are the alternate things it said and it's arranged in defending order so you can be sure that the first result is the most confident result and that's what we are going to go with and result dot recognize words just does that alternates which is the list which we have right here DOT first so it's accessing the first element and its recognized words okay so nice now that we have this done next step is to contact the API so we need to get access to the open API so first of all we need to go to open AI so let's type here open AI then we can go on the website then we can move to Developers documentation or API reference whatever you want so we have the introduction you can read all of these things I've already done it but basically what we need over here is access to a secret or an API key so to generate an API key we are just going to go over here then click on view API Keys I've already created one I'm just going to create another one create new secret key I'll copy it and make sure you read this you won't be able to view this API key again so just save it and directly go to your lib folder Secrets dot dot then a constant open AI API key and save it in a string of course you won't be able to access my API key if you try to do that because I'm just going to delete this API key once I'm done with the project so make your own API key if it asks you to log in you can see I've all I'm already logged in and then you have to create your API key now after that is done we can go to API reference and here we can find everything about the API the thing we are most interested in so for authentication you can see we need to pass this thing in the HTTP header so first of all we need to make HTTP calls so for that we can install HTTP package if you want you can go ahead with do or anything else you like we're just going to go ahead with the standard HTTP because that's enough for us then if you want to see the models which one you want to use you can just do that but I'll directly get the point we're just going to go ahead with the chart API and the images API one more API which we could have used was completions API which uses models like text DaVinci or whatever but I didn't find this API particularly useful in our keys so I'm just going to go ahead with chat which is chat GPD with the 3.5 model and images API which is basically the Dali API which will help us to generate images so first of all let's understand what we want to do and to explain it to you simply I'm just going to make a diagram let's understand what we want to do well we've converted speech to text so let's make a box over here so we have speech let's put your speech to text once we get access to the text we can use this text to send instruction to the API right and from the API whatever text we get we are just going to convert it from text to speech but in this API key there's a big thing to understand so let's make a big rectangle why is this part interesting because you know we have to contact two apis and we also need to automatically detect if the user wants to generate an AI image or not that's the tricky part because we know the user has to use the chart GPT API and the Dali API right but how do we know which API to use so for that again we are going to use the chart GPT API so let me write it down we are going to use chart GPT API at first to know whether we want to use chart GPT or we want to use Dali right and in this case I tried using the completion API which is the text DaVinci 003 model but it didn't return a useful output that's why we are going to use charge GPT API here now why are we doing that well I just want to detect if we want to generate an AI art image or we want to create an image or we just want to give a useful response to the user so what I'm going to do here is basically send the text the user has to charge GPD and charge GPT will tell us if we want to generate an AI art image or not so it will be an answer in the form of yes or no if it's yes then we will call the dally API otherwise we are going to call the chart GPT API if we try to eliminate this process you know if we just try to remove this process out how will we know if we want to contact chart GPT API or Dali API because both of them are different apis right then we'll have to hard code some string and tell it that if these strings are included in the text like you know image or AI or whatever then we have to use the Dali API but that's not right because if the user wants a response from chat GPD but is framed such that our algorithm just thinks that it wants to contact Dali API it will give the wrong result that's why we are just adding our charge GPT model here so it will cost us two requests instead of one but we are guaranteed with the result right I hope you understood this if not let's dive into code and you'll understand it better so I'm just going to create open AI underscore service dot dot then we are going to create a class of open AI service and here we are going to have multiple functions so the first function that we are going to have is going to return future string is art prompt API where it will get some prompt and obviously will be an async function so this function is this part right here the first chart GPT it will just tell us if the user wants to generate an art or not then we are going to have something similar it's going to return a string again but this time it's going to be the charge GPT API so we are going to have chat GPT API and after this finally we have the dial e API so this function will decide if this function is going to get called or the dally API function is going to get called I hope you're able to follow along now let's just return a string here saying chart gbd and a string here saying Dali we'll get to it after we successfully recognize if the user wants to generate an art or not so let's have here we are going to create a try and a catch block so that if there's any exception we just tell it out so we can just have written e dot to string because at the end of the day we just have to return a string and here we are going to call http.host because that's what the API says over here whenever you want to call chat GPT which is this URL we have to send a post request so first of all let's import http and we are going to import it as HTTP with the suffix and then we can have HTTP dot post then we'll pass in URI dot pass and then we'll pass in the API which is this part right here so let's copy it paste it over here and there we have it now post is going to return something so let's await it and then we are going to have final response is equal to this okay but still we are not done with the post request because we need to add certain things Dash H in call means header so we need to add these two headers and Dash D is basically the body part so we need to add body over here so let's have headers it's going to be in the map format and here we are going to copy content type which is application Json so let's have content type application Json I'm following along with the documentation so that in any case you know if the API changes a little bit you can come to this API and reference it for your own then we have authorization and the authorization is going to have better followed by the API key so we are just going to have better here followed by the API key so we'll have dollar and our API key is in the secrets open AI API key so let's have open AI API key and let's import it awesome now we need body and in the body we have to return an object by an object not a map because we need to Json encode it so whatever map we send to it we just have to convert it to Json and send it across that's why Json encode and what are the things we need well this is a model and then we have messages so we can have messages which is going to be in a list format and then we are going to have the role of the user so we have rule so the rule can either be user or assistant or there's one more I think there's admin or something but this is the one we care about role and obviously this is going to be in a map so that we can have this role used like this and then finally we need content over here so we can copy this content paste it and what is the content you're going to be well we basically won't charge GPT to say to us if this message the user has the prompt aware is asking us to generate an AI picture image Art or anything else and we only want the answer in a yes or a no so we are just going to have the content like this does this message want to generate an AI picture image art or anything similar so if we want to generate picture image Art or anything similar then we'll pass in the message which is the prompt we are getting from the parameter then we'll have simply answer with a yes or no okay third will help us get the answer in a yes or a no format and that way we can just check if it wants us to generate an AI art image or not so after we have this let's print response dot body then we are going to have the checking so if response dot status code is 200 that means everything went well then we want to do something so for now we can just say yay okay because I don't have any clue of what I want to do okay and also since you want to return a string over here we can just return AI okay now I can take this open AI service go to the home page instantiate this open AI Service open AI service which is equal to open AI service now I'll take this open AI service and whenever the user clicks on you know stop recording that means we have to stop listening before that I'm just going to call openai service Dot is Art prompt API then I'll pass in the prompt which is last words because last words is basically the recognized words okay and with this let's also await it and I'll restart the app so now I'm going to click on the mic button and generate an image for a unicorn please create a unicorn image for me now I've closed the mic let's see what it gets and here is the encoded Json result so it was able to recognize please create a unicorn image for me now I can go up and here you can see this is the result from the chart GPT or open AI service where it says the content is yes so this is the message we are waiting for and says that we want to generate an image that means it's working it's able to give us an option in a yes or a no but here's a problem sometimes it can give yes like this otherwise in small cases like a small yes so we need to capture all of them but first of all let's store this in a variable so we are going to have string content which is equal to and if we just do response dot body and try to access this content we won't be able to do it because body is of the type string we cannot call methods like this on a string that's why we need to call Json D code then it will decode this Json rest dot body then we can access like this and what do we want to access well our end product is content so let's see what we need well first of all we'll go to choices so we love choices then we have to capture the first choice because it's in a list format so we'll capture the first choice because it's going to be the most accurate then we'll access the message on it the message property so let's have message and then we are going to access the content property on it okay so with this we are able to get this content if you weren't able to do it you can just do it from here you know you get the response here so we go to choices since it's in less format we want the zeroth element then we want to go to message then content okay now we can just have content is equal to content dot trim so it will remove any spaces it has just you know sanitization clearing things off then we can have a switch statement that says content and these are our cases so first case is a yes like this then our second case is yes like this third case is a yes that we got here this is and the next case is yes like this so these are the possible yeses we can get of course you can also put case yes like this but I hardly think we are going to get that these are the four possible options for us so if the any of these cases are there then what do we want to do so if it sees yes that means we want to call the chart GPT ABI so we will just have a weight charge GPT API path in the prompt which is this exact thing also it will give us some response so we have final response is equal to this then we can return this response then we'll have a default option that means it's a no or something similar so we can just have final response is equal to await and actually you're not the chart GPT API the daily API and here we are going to call the charge GPT API then return response so if it says yes to our message our messages like this does this message want to generate an AI picture so if it says yes then of course we need to go to the dally API otherwise the chart GPT API and we are returning response for them and here we are not going to return AI here we are just going to see an internal error occur because if it is not returning response from Dali or charge GPT that means something has gone wrong so this error might occur because the status code is not 200 and if it is not 200 then maybe it is because we have exhausted the API that means call the API we cannot call it any more so we'll have to generate a new API key or something so that time we are going to get an internal error occurred cool now that we know this part is working we can test it out but I don't find the need to now I can go to the charge GPD API and call it so let's copy this entire thing because it's the same thing this was using charge epd to give us an answer you could use the DaVinci Resolve API not DaVinci is all sorry The DaVinci text model but as I said it didn't work quite well for me that's why I'm using the chart GPT model and here we have it so we are going to post to the same API but this time the message is going to be different because this is a chart gbd API right Char GPT is able to remember our conversations so if we wanted to remember our conversation what we need to do is create a final list of map of string comma string messages which is an empty list so we have this empty list over here we are calling it messages and here what we do is before going into the try and catch block before making any API requests we just say messages dot add the role of the user is user and the content is the prompt so what's happening over here is we are just storing whatever the user says into the messages why are we doing that so that we can just replace this entire thing and have messages over here in this case the message was just a single thing which was does this message want to generate an AI picture because it's independent of the context right if I say something like generate an AI image for me my next question doesn't matter to this it just has a job of returning a yes or a no but in this case it has to give smart responses so we need to store the entire message history in this messages variable so whatever we say whatever the assistant says we all we have to store all of it over here so that it can remember the context and give us smarter replies so we can just take this messages let's remove this res dot body so the content is going to be the same because it's the same API model but this time we are not going to have a switch or a case let's remove this here we are just going to our messages dot add role is going to be assistant and content is going to be the content over here so I'm storing everything in this message variable over here the role is user that means I said it right whatever prompt I pass in this is what I've said but here whatever response I get it's what the assistant has said and that will just help charge GPT to give us better responses because that's the key feature of charge GPT in my opinion it's able to remember the conversation and based on that it is able to give you know such wonderful responses also we can return content over here not to forget because after storing it in the message variable we have to return something and that something is going to be content so that over here we get response and this return responses directly done now let's test it out let's come over here we are going to have final speech is equal to avoid this then I'm going to print the speech and that's good so let's restart let's see what we get also let me think of something I can ask something like what is two plus two that should be good so I'll just click over here what is 2 plus 2 and here you can see it was correctly able to identify what is 2 plus 2 and the content here was no and the response here we got 4. which is because of this line print speech so it's able to give us the correct output now the only thing is converting the speech which is in the string format in a text format to a real speech so we'll do that later but first let's just get started with the Dali API because this is going to be a bit different from our normal charge GPT API so let's copy this again paste it over here let's move to the images section which is this part charging daily API we'll copy this URL paste it right here and we obviously need messages dot add over here as well it's not a function thing I've created this message as variable globally so that all of the functions can use it and I've added this because later on if you have questions regarding the art as well it will be able to tell us right that's how chart GPT will help us we have pasted the URL let's see the headers content type and authorization that's correct the body is going to be different we just need a prompt so let's remove messages part from here and the model as well we can just have prompt and the prompt is going to be prompt that we got from the argument or the parameter over here then you can decide to pass in the number of images you want the size you want and the response format you want I think the default output is a URL so I'm not concerned about the response format you can see defaults to URL whatever you like I'll just pass n as 1 because I only want one image to be generated every single time if you want more you'll have to customize your way when we display it on the screen you'll have to use a list view Builder to generate a list of URLs it gives not too difficult but I just want to generate one image then if the status code is 200 then what do we want well the content is not going to be the same let's look at the responses that it has it's way different from charge epd basically we have to call data and again it's a list so we have to access the zeroth element and call the URL property so let's do it we are just going to remove all of these then we are going to have data then 0 because here we have a list the zeroth element and then we want URL of course if you're having N More than one you'll have to customize your way out basically you'll just have to update our DOT map and store it in a list format and here you'll have to return a list of strings and based on that you'll have to render it on the screen but whatever then we'll add it to messages let's call this image URL so that we are more descriptive about it and there we have it so this is the dally API I'm hoping it works so let's get it all together we just have to call this function and this function does the work of calling chart GPT and the Dali API based on the user's prompt itself and we are not worried because we know chart gbt will give us the correct thing so let me ask something let me ask it to generate an image generate an image for me where there's a unicorn in it and here you can see we are getting this image link and if we just go over there let's open it this is an image of a unicorn so it has been able to create a cute little image and that means it's working it's able to correctly identify stuff and put it out now the next part is to convert whatever text we have to speech so for that we'll have to find a package so let's go to pub.dev then we'll have flutter text to speech here we have the speech to text plugin but we are not going to use that we need text to speech and this is a plugin but it was last maintained three years ago so we are not going to use it this was maintained 20 months ago we are not going to use it this was maintained two months ago so let's try to use this let's copy this add it to the dependency let's see if there are any installation processes so first of all for Android the minimum SDK should be 21 so let's go to bell.gradle you're the minimum SDK version is 21. it's close it for iOS there's a known issue with integrating plugins that you Swift into a flutter project with The Objective C template but we are not doing that we have added this now we only need to instantiate the flutter TTS plugin so let's go right at the top let's create an instance of it let's call let's import this package as well final flutter TTS now we can copy this and we also need to instantiate this part right here so let's call init in a text to speech then we can create a method on this so we have future void then this is going to be asynchronous again now we can copy these lines so we have searched instance to be true but this is only for iOS part then we can call set state in case you know it wants to Rebel based on this I don't think such State affects here if you even if you remove it it wouldn't really matter but just for the sake of it and if nothing goes wrong let's pass it in also in the dispose let's have flutter TTS Dot stop and finally we need last one function which is future void system speak so basically whatever prompt we get or the content we get we just have to speak it out using the flutter TTS model so we're just going to have flutter tts.speak then we'll pass in the correct content and there we have it now we can use the system speak and pass it in a way await system speak and then pass in the speech now let's restart the app and see what it gets and here you can see unandled exception missing plugin that's because we added a new plugin but we didn't rerun the application we just need to do that and after this let's test our app I know I haven't been able to test it on Android yet but we'll do it after we get done with the whole process so app has loaded now let's try to see what it got so I'm just going to click on record button now explain Quantum Computing to mean simple words and I'm not sure if you can hear it but it has worked you can try running it on your own iOS simulator but if it still doesn't work and if you get like some error that says you know the voice wasn't able to run what you can do is just go to settings then you need to go to the accessibility tab in voiceover you need to go to speech voice and change your voice once you change your voice and run the app again it should work for you and there it is it works but still I want to make some changes what I need to do is whenever the voiceover says something I want it to display on the screen instead of good morning what task can I do for you it should show this thing so to do it let's create some variables first of all it's going to be generated content and other one is going to be image URL so generated image URL both of them are initially null but whenever charge GPT or Dali runs it will assign the content or variable some value so let's go down whenever we have a speech first of all we want to detect if it is dally which is speaking or charge GPD because if it's daily we don't want it to say anything we just want to directly show the image if there is a voiceover it will tell us the URL right because that's what the speech contains it contains the URL of the image see here we are just going to say a speech dot contains https like this so if it contains hdps of course it's a generated image URL so we'll set it equal to speech also we'll set generated content equal to null why is this line lead needed because suppose the user first gets data from charge GPT API then the user generates an image so that it gets data from dally in this case generated content and generated image URL both are not null due to which both will be shown even this text box and even the image so we are just setting generated content to null when there is generated image URL because either of them will be null because we are never getting data from both charge GPT and Dali together so we can take this paste it over here this time generated image URL is null and generated content is speech also we need to call this system speak only when there's no Dali so let's have this and then obviously we need to call such date so we'll call such state after this and set state after this why such State needed such state is needed because these values have been given after these values have been given we want to render it on the screen so if there is suppose an image URL we want to show it on the screen if there's some text we want to show it over a year in this chart bubble that's why I had commented chat bubble over here so here we are just going to check remove a constant from here this will be a constant but in the text we are going to have if generated content is equal to null that means good morning should be shown that means initially or when Dali runs and if it is not null then we want generated content to show but generated content can be long so we want the font size to also change so here again we'll change this if generated content is equal to null then 25 so good morning what task can I do for you will be 25 otherwise it will be 80 okay now let's try to run it again and see if it works so we are just going to have explain Quantum Computing to me in simple words and here you can see it has given us a paragraph and there it is looks good Quantum Computing is a new type of computing and it's also saying it for me I don't think you'll be able to hear it though so now next what I need to do is just remove these three boxes whenever the generator content is there and that's why we had created a column over here I said we are going to wrap it with a widget known as visibility and here we are going to set the value visible so if generated content is equal to null then we don't want to show the column and this way we don't have those boxes but even then I don't want this thing to be there this container so I'll also wrap this with a visibility widget and say it's visible when generated content is equal to null so if generated content is null then only we will show this when chart GPT gives us some value then this is not going to be there perfect now if you come away up the box has been removed great now let's try the Dali model so I'm just going to say generate an image for me with a unicorn in it and here you can see we are getting all of our data back over here right at the top but we don't want that we want to remove all of these things that were shown and only show an image so first of all let's get an image up so we can do that right after this part or this part this looks good so you can have image dot Network generated image URL but this cannot always be there so it should only be there if generated image URL is not equal to null so if it is not equal to null then we have this image.network showing up but if it is null then the generated image URL will not be shown and this way you can see we are getting the image URL but still we want all of these boxes to disappear so we are just going to have the visibility as when generated content is null and the generated image URL is null that means we are confirming that these will only show up when the app starts right after we start giving it some data you know like speech to text using charge gbt or Dali models only then after that these won't be visible so also here we just paste it and if we go there it has been removed also we want this text box to remove so we can just go right at the top wrap this container with a visibility widget and yeah we can pass invisible as generated image URL is equal to null so when generated image URL is null only then will it be visible but this time it's not visible that means we only have the Unicorn showing over here we don't use visibility widget right in the image.network part here we don't use visibility widget because we were calling exclamation mark over here if there is an exclamation mark it basically means like it will give us an error like null check operator used on a null value we don't want that that's why we have checked with an if condition instead of wrapping it with a visibility widget if you didn't understand you can still go ahead and wrap it with visibility widget and see what error you get then you'll have a greater understanding of what I'm talking about now also this image needs to have padding from the side so let's wrap it with a padding widget and the padding is going to be from all the sides let's say 10. and there we have it looks better also I want there to be a curve so I can just have this image.network rounded by a clip or direct widget which helps us to give a border radius and the Border radius as border radius Dot circular 20. let's put some commas for formatting and there we have it looks great if you want you can increase the padding now but I don't want to do that 10 looks perfect and there we have it great now let's try to test our app again now I'm going to use the GPT model so we can just ask what is programming let's see if we get the correct answer and here it says I'm sorry but I don't understand the context so let's try say again what is programming and there we have it this is what it said now I'm just going to use the context of what it said so that I can generate a better response so I'm just going to ask it explain to me like I'm a five year old and here you can see programming is like telling a robot what to do so that means it has access to our context now and that context is only available because of this thing right here the messages part because of this it has access to all the context of what we have said and that's why it's necessary of course if you restart the app it will forget everything because the messages will again then be empty and messages will be empty because this thing will run again open AI service so it will instantiate this to nmd list so if you want this data to be persistent you can store it in local storage using sqf Lite or whatever but I'm not focusing on it in this tutorial now one of the last Parts in this app is now in The Floating Action button I just want to show the correct icon so if we are going to check here if speech to text is listening so if speech to text is listening then I want the stop button to be there otherwise the mic button to be there and that makes sense right so with this we are able to create a proper functioning app now the next part is the animations that we need to add so let's add those very quickly so for animations there are two to three approaches we can take first approach is basically building the whole animation on our own that's not what we are going to do in this tutorial we are basically going to use the second approach which is manually using a package and using the classes provided by the package because of this the process can be faster for basic animations of course if you want to create much complex animations you'll have to do it on your own but for this one we are going to use animate underscore do package it's a very simple easy to use app of course you need to restart your app and then here I've just loaded up my Android emulator so that we can test over here and it asks us the same thing and then we can go ahead testing the app so what I'm going to do is go from top to bottom and add animations to the things I prefer so basically it's just going to be everything in this app just to demonstrate it to you you can configure it according to your own liking first one is this Abba I just want it to you know come down and you know it should bounce a little bit so for that I'm just going to go over here wrap the text widget with a widget known as bounce in down this is given by the anime do package and if you do this and restart you can see it pumps down after that we need this so for this I need a zoom in effect so I'm just going to wrap this entire stack with a zoom in widget if I do this you can see it zooms in after that I need something for the suggestion box so for this I'm just going to make sure that it comes from here from the right hand side so let's just wrap this chat bubble with a widget known as fade in right save it and here you see it comes again let's try it this drops down this Zooms in and this comes from the right hand side now the next thing is this part right here we want this to come from the left so we are just going to have this widget wrapped up with slide in lift okay see here it comes perfect now we can scroll down and here we want these to come in from the left as well but here's the problem you know if I just wrap this entire column with a slide and left widget let's do it all of them come together I don't want all of them to come together I want a delay in each and every one of them so what I'm going to do is in the feature box wrap the with the slide and left widget this can't be a const anymore here we'll have to pass in a delay and the delay is going to be let's say duration milliseconds and since we are going to use the same variable again and again for all of them we are going to have a variable created right at the top so we can have and start is equal to 200 and also in delay is equal to 200 since we are here I've created both of them you'll understand the purpose of delay in a while but for start we are just going to put it okay here we'll pass in constant now if we just start this comes in perfect but I don't want only this to come these should also come but they should only come after this has almost reached its final position so what I mean is I'm going to wrap this widget with a slide in left widget again here the delay is going to be millisecond start but if I just do millisecond start they both are going to start and end at the same time I don't want that so here the delay is going to be start Plus delay all right let's put a const here and now if you see if I restart after it has almost reached this comes in and the same thing I want away up so I'm just going to wrap this with a slide in left widget then again I'm going to add this but this time the delay is going to be the but this time the delay is going to be 2 times what was over here so here we had D layer 0 then we added 200 millisecond delay and this time we are adding 400 second millisecond delay and also we'll put a const over here and save it let's restart and here you see this looks good finally I want this to appear as if it's zooming in again so I'm just going to wrap this Floating Action button with a zoom in widget save it and let's restart the app and here it comes looks good and if you want you can pass in a delay here as well so this button will only appear once this has come okay so after the last suggestion box comes in this comes in right so let's have three into delay now let's restart and you see after this box comes in this zoom in effect takes place again let's demonstrate it to you and here it comes look through it so this was the entire app we were able to integrate charge GPT and Dali together and we found a way such that we don't have to hard code to use either of the apis we are sure and confident that the correct API will get run and it will generate proper results for us and this works on Android and iOS of course if you want you can run it on Mac OS Windows Web anywhere because we are just using HTTP client animation package nothing else so this was it for the tutorial thank you so much for watching and I'll see you in the next video
Info
Channel: Rivaan Ranawat
Views: 51,601
Rating: undefined out of 5
Keywords: flutter, flutter 3, flutter 4, flutter chatgpt, chatgpt, gpt4, openai, open ai api, chatgpt api, dall-e, flutter dall-e, flutter api integration, flutter tutorial, flutter tutorial for beginner to advanced, rest api integration, flutter project, flutter virtual assistant, flutter voice assistant, full stack clone, flutter ui, flutter ui tutorial, flutter responsive ui, responsive ui, rest api, restful api, flutter clone
Id: Q_pz4xFow3Q
Channel Id: undefined
Length: 90min 49sec (5449 seconds)
Published: Wed Mar 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.