Create your first own voice assistant in python in less then 90 minutes | Dan We | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Create your first own voice assistant in python in less then 90 minutes

teacher avatar Dan We, Anything is possible

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

10 Lessons (1h 18m)
    • 1. 0 Project Introduction

    • 2. 1 Getting started Editor and Libaries

    • 3. 2 Transform audio into text

    • 4. 3 Speaking and Language settings wiht Voices

    • 5. 4 Starting with days and times

    • 6. 5 What time is it

    • 7. 6 Greetings from our voice assistant on starting

    • 8. 7 Time to finalize our voice assistant

    • 9. 8 Completing our voice assistant

    • 10. 9 Finally Lets test our Voice Assistant

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

In this project you will learn how to build your first virtual assistant VA in python in less then 90 minutes.

This is a great small project to get started (even if you have little or no prior knowledge in python) and learning opportunity.

Learning through your own little projects is the best and most enjoying way to improve your skills.

In the world of today we all already use Virtual Assistants like Alexa, Siri, Google and others more and more in our daily lifes. It's convenient nd this will only increase the next few years. So wouldn't it be interesting to learn the fundamentals to create our own VA?

This is exactly the goal of this class and it makes a lot of fun doing it. We can ask our Virtual Assistant to play songs, search youtube, return stock quotes and many other things.

Doesn't that sound intersting?

So let's don't waste any time and start working on this project together. Every step is explained so no worries! Just get started.

You can also add this project to your own portfolio later on to show potential employers your skills!

What are you waiting for?

All you need is

  1. python installed
  2. a microphone (headset with microphone would be enough)

The project is for beginners (but basic python skills are certainly beneficial)

Let's do it!

Meet Your Teacher

Teacher Profile Image

Dan We

Anything is possible


Dan is a teacher by heart. He loves learning and sharing knowledge with others.

He is committed to support other people by offering them educational services to help them accomplishing their goals and becoming the best in their profession.

"Life is a journey, and I will give my best to help you on your way to your goals"

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. 0 Project Introduction: Hey friends, welcome to the brief introduction about this course. Now this class is about how to create your own voice assistant in Python in less than 90 minutes, I promise. So no matter if you're a beginner in Python and intermediate or expert, you will be able to fulfill this class. So it does matter. Let's dive into it together. Let's build this VA and Alexa, siri Cortana. You all heard about them, but screw them. Let's do it together in Python. Let's dive in. 2. 1 Getting started Editor and Libaries: Hey friends, welcome to this course, project voice assistant. Now, in this course, we learn together how we can create our own voice assistant in Python. And this course is created for beginners of Python as well as for expert users. So don't worry if your Python skills aren't too deep, you still will be able to go through this project together with me because I'll try to explain everything in detail in here. So on my screen you can see that I already opened Jupyter Notebook, which comes as installation when you install Python via Anaconda. So if you have not installed Python yet, that I would recommend you use Anaconda and download this. And then of course, feel free to use Jupiter as I do here. Or of course, any other kind of editor of your choice if you want to do that. So Visual Studio Code, for instance, is pretty good, but there are also other editors out there. It's up to you. I personally prefer Jupiter, but as I said, it's totally up to you. Now, in order to get started, we first want you, in my case, at least rename my project. Currently it's untitled here, so I can click on site and I can name this prince and gets a voice assistant. Systems Python project. I like that. So I can click on Rename and you see that now I have renamed here. So next, let me maybe zoom in a little bit. So hopefully this is big enough on your screens. And now let's import the modules we're going to use. So at first, we need to have Pi TTS X3, which is a module which allows Python to speak to us. So of course, this is something we need because we need our voice assistant or chat bot, whatever you want to call it, of course, to tell us something. So that's why I will import pi, TTS IQ3. Okay? So import is just a statement. You can see it here in green. And we import this module, this library. So if you don't have the library yet, what you can do if you have Jupiter, then you could run the following commands. An exclamation mark and then pip install, and then pi TTS X3. Okay? This will be a command you can directly run into butter to install a specific module. If you don't do it in Jupiter, you can also try to install it by a conda or using pip. So you open PIP or go into a specific folder with pip. And there's any run pip installed pi, tt, SH3 or other modules, just remove the exclamation mark. This is only required if you tried to install it within Jupyter notebook itself. So let me just comment that out because we don't need this. So I use Ashton bolts are commented out and I will import the libraries. So we will use speech recognition. So import speech recognition, cognition. Need to spell it correctly. Of course, S as our, as our with just let me make it even bigger. Maybe hopefully this is big enough on your screens. So we'll use this one here simply because it's an abbreviation, right? So we don't need to read, right, speech recognition whole time. We can simply use the abbreviation here. And then I will also import a web browser. This is a kneaded library which allows us to open any kind of browser. So normally it would be our default browser. So my case it's Google Chrome. So that's why Google Chrome would, would open if I use web browser. So that's that. And then I want to import datetime simply because this module allows us to get the current date as well as the current time. And I want my voice assistant just to tell this to me if I ask him or her. And then I will also import pi what kit. It's called. This an interesting module because this allows, for instance, to search and open YouTube. So next to, of course, using web browser and refer to the YouTube homepage. You could also use pi what kit, which as an example, allows you to open YouTube, but also other kinds of neat things you can do with it. I can import OS. So operating system as a molecule. This could be used, for instance, if you want to tell your voices stunt to shut down your computer or to restart your computer. You can do this using the US module. That would be an option if you want to implement this. Just as an example. And then I will also import, imports. Yahoo Finance. So why finance? Finance as y f, which allows us, for instance, to ask for a stock price. So it's current stock prices for specific stocks we have in our portfolio for instance. So we can use this library for that. And why F again is just an abbreviation USA. And then I will also import pi jokes. By jokes is also an interesting library which allows you to ask, in this case, the voice assistant for a joke. And then there was, this isn't a joke which is available from Pi jokes. And of course, if you have your own, I want to create your own jokes and put this in a module, then you can use this as well, right? There's just predefined for us, so we can use it directly in here. And these are the libraries I'm going to import for now, maybe later a few additional ones, but for now I will use those. And as I said, if you don't have one of them, simply run the commands in Jupyter notebook would be exclamation mark and then pip install, and then a specific library we have here. Or of course, if you use Conda, then don't use pip at all. So just Conda install and the things. Or if you use pip but not in Jupiter notebook, then don't use the exclamation mark when you installed. Okay. So if you have imported and installed oldest libraries, then you could run this cell. And then we should not get any errors here. And you see that it has been run successfully. So I can see the first line, the one here number, and now we have all libraries we need imported. Okay, So that's it for the first video. And in the next video, we will start creating our voice assistant. Can't wait to see you. There. 3. 2 Transform audio into text: Okay, Now in the last video, we installed all the required libraries. And now let me just use or enter a new line here. And let's start creating our first function. And this first function, what will it do? I will write it as a comment here. It will listen to our microphone, microphone and return the audio as text. And it will do this using Google, of course, right? So the omniscient Google. So in order to do this, we define a function, let's call it death here, and then call it transform. Okay? So you can name this function whatever you like. I simply call it transform because what it really does is it listens to the microphone, so what we speak, and then it uses or transforms this speech into text using Google for that. So transform. And we don't need any parameters here. For this function itself, we need to add first instantiate the recognizer from speech recognition library. So I say R is equal to SR dot recognizer. So we instantiate the class and we name it. Oh, let's give it this way, which didn't instantiate it. And then we use a context manager, which means we write the with keyword here and Python. And we define from the speech recognition, what kind of source do we want to use? What in this case, we want to use the microphone. So we call this microphone. And again, it's a class, so we need to instantiate it here. And we call it source. So we have context managers with the, with keyword. You can give this a name, in this case source, but this name could be whatever you want it to be. Then we need 0 here. And then we say, in this case, our dots pause threshold and simply say, your dog ate. It just means that when we start actually here recognizing to what someone says to the microphone, we just wait just a tiny little fraction of a second, right? Just a little waiting time. And then we say, in this case, set is equal to, so this again is a variable I named set here, but it can call, So do you like can call it audio or any other kind of word. And what it does is, or what we stored in here is r dot, listen to the source. So what this does is it falling? Again, we have a speech recognizer object which recognizes speech. We use from the speech recognition, the microphone as our source. And we listen with the recognizer, we have instantiated here to the source. So in this case the microphone. And after listening to the audio file, we store it in this variable set. Okay, so far so good. And now what we do is we use a try command here using try-except blocks, because sometimes we might run into errors. And in order to avoid those as good as we can, we simply try to do the next lines. And just in case we run into errors, we can capture them with the except blocks. So I can say in this case at first prints and let's call that. I am listening like that. And by the way, with the print commands, they could be quite helpful. So I would encourage you, when you write your code, just use print commands in between just to make sure that the code is running smoothly until the specific line. And then if you do not see a specific print command, then at least you already know, okay, something is wrong in the code before this print command. So that's why printing is a lot of helpful, is helpful and that's why I would encourage you to do that. Okay, so we are printing, I'm listening. And now we store in the queue variable, which is our query, our dot recognize. And there's a recognise, a nice Google. So Google misspelled it here. Sorry, I'll read. So read Kafka, nice Google. That would be the case here. And recognize Google. And what we want to put in this recognition is of the set. So the text from the microphone, which we stored in here, or better say that audio, it's still audio. The microphone and the art of recognized Google courts now Google service in order to convert this audio, this was someone has set into a text file. And we also need to specify here the language. So language. As equal to, in my case, I'm using English year, so, uh, putting EN. But of course you can also use different kinds of languages, like French or German, or I think Hindi of course, it's also possible probably, and other kinds of languages right up to you. But here I think, for starters, here we go with the English language and then we simply want to return the Q. So this is it for the try commands except we running into errors. So that's why we use the except construct, accept, and specify here as our dots, there is an unknown value error which we can capture. And then we can simply prints in this case here, let's say this case we don't need an f string. That's simply say, sorry. I did not understand. Again, here you could write whatever you want and your code supreme using Gibbs print commands. And I also want to return something returned and then simply return. I am waiting, okay? Okay. And then I'm using another except now that except block here excepts another era which could be as r dot requests error, which is also available request Iran, this one. And here we simply specify, again the print commands. And I simply specify here, sorry, the service is down. Okay, like that. And then I also want to return something here. Return and we'll do the same as before. I return. I am waiting. Just scroll up a little bit. Maybe at a funeral additional lines here. I'm waiting. And then finally, just in case those two exceptions which we have here do not will get triggered because we got forever reason at different exception. Then we simply at the ends call except and specify here, return, again, return. I M waiting like that. Okay? So the reason why we do it in the following order is also because first, it checks for just in case an error occurs, it checks for the unknown Valley era. If this error is not met, then it checks for the aggressor era. And if this error is also not met, then we use the general except function which simply accepts then all other kinds of errors. If it's not an unknown value error, request URL. So this is our transform function. So that's all we need to do. So we instantiated there, recognize the object we use from the speech recognition, the microphone is our source. We want to listen to. We listen to the source in here. We stored in a variable called set. And then we try to convert the audio file from set into text using Google, which is in this line here. And the language we said is English. So someone needs to speak in English and Google knows, okay, I need to translate that from the English into what the audience English and translated into English texts. But this is something you can't adjust. And if you want to know all the languages available, then I would encourage you to simply look up the speech recognition library on the Internet and search for specific languages which are available because there are tons of languages. And I don't know, of course, all the individual shortcuts by heart, but you can look them up if you want. Okay? And that's it for the transform function and also for this video and next video, we will have a look what this function does and why that works properly. Until then. 4. 3 Speaking and Language settings wiht Voices: All right, Now let's add first of course, execute this. So let me run it. So now our function is available, and now let's try that function simply by calling it called transform. And now I need to have, of course microphone, which I have because occurred it speak to you. And now let me run it and speak something. And we will see whether our transform function is able to convert what I say into text and then also return this text. So let's do this. Hello voice assistant, how are you? And you can see here, I'm listening is just a prince because we printed that out here. And then it basically converted the audio. So what I have spoken here into text, and it returns this text. Hello voice assistant, how are you? So this obviously works exactly the way we want it. So far, so good. Now, this function, transform function can now handle what we say, right? So when we tell our voices isn't something, we now know that the audio is converted into text. Now of course, we also need to have it the other way round. What I mean by that is basically if the voice assistant now has the text, so what we tell him or her to do, and of course, the voice assistant should also tell us something. So we need to create a function which allows us or the voice assistant better say, to speak something in return to us. And we can do this using the Pi TTS X3 five module which we've imported up there. That's why we need this one. So the speech recognition was to recognize our audio and convert it into text. And the Pi TSS 3 is 4. Actually. Now speaking something ends by the way, I know there are also other libraries out there. One of them, for instance, could be placed sound, which I've used also in the past. But currently I think this one is the best, at least I know. So let's go with Pi t as X3. Okay? So when we want to use it, we need to start an engine to do that. So maybe let's first actually create the function. So let's call this speaking, speaking. And again, the function name itself could be anything you want. And now we specify in this case a message. Because here for this function, of course, we need to give it some message, some text which we want the, in this case the module or the engine to say. So now we have the text specified and now we need to at first initialize the engine. So that's why engine is often used here as a word. Again, you could name it differently if you want spurred, I will go with the default, which most often other people also use, which is engine is equal to. And then I say pi tt Sx, three dots in it like that. And next we want to say something. So I say engine dot c. And then we use the message, which is actually the text which this function receives. And then here it simply says what the text itself isn't. And then we finally need to call engine dot, run and wait. And also its function. So we need to do parentheses here. And basically that's it. That's the whole function. I could execute it here, and that's what it does. So it initializes here an engine object. This object itself has the ability to speak, and it's simply speaks using this dot. Say, what's does its beak? Well, it speaks the message we put into the function itself. So the text we put in here. And then we simply call run and wait in order to 3D and then output this message as an audio. Okay, so let's do that. And let's give this a try. Let's say, for instance, speaking. And here I need to be careful, because here it really depends what kind of language you have on computer and what kind of default language. Because in my case, my default language here on my computer is German, which means that by default, the voice which is activated and here is a German voice. But let me show this to you. Let's keep it simple. Let's say here at first, I will try Hello World, okay, as text. And I want now the, in this case, the voice assistance on this case, simply my, my sound system here to return hello world. So let me run this. Hello, Good. So hopefully you heard it. I can also make it a little bit louder. Just one more time. Hello, Good. So you see that it doesn't really sound too good. And the reason is that, as I said, currently, as my operating system here is by default set to the language of German. That is why it is using a German voice from Windows, from for my Windows operating system in order to return the English helloworld. We will fix this in a minute, but I just want to show you that if I will translate this Hello World into German, and I would call, speaking, in this case, it's called Hello date. So you don't need to learn Germany air, but this will be hello world in German. And if I comment this out for now and run this hallow bed, this sounds, at least in Germany, it would sound pretty decent, speaking like that. So this is one thing we need to be aware of. So of course, if you're operating system is in English, then you should not face this problem. But if you have an operating system in Hindi, in Chinese, French, in I don't know which kind of language, then of course, that would be a problem if you want to try or use this as an English, as we see here, an English translation using English sentences. So what can we do about that? Well, this is great here. Read news because we are really flexible here. So here, if I go inside my, my engine and before I do this, I need to actually modify this a little bit because currently the engine itself is initialized here in the function. So I want to do it differently. I want to copy this for now and re-initialize it here outside of the function. So let me just do that and remove the four here, okay? And now we can actually take a look at what kind of voices are available to us. So that means if you have by default your native language, let's say French, and you want to use a different kind of language, maybe English, for instance. You can easily do this. What do you need do is you need to actually iterate. We can do this using a for loop here, for voice in and then in the engine itself. And this is the reason why a initially listed here. Again, we can actually get the property. So property. And then we can search for the property voices. And if we just print those, if I run here, print voice like that. Let me run this here. Now you see all the voices which are installed on your local system. So in my case, I have these languages installed. So I have here a German voice, which is my default one, which is this one German, as you can see here. It's called header. And it's a woman, as you've heard probably. But then there's also two additional ones. There is David. Here's David, which is English United States. Then there is Sarah, which is also English United States, but would be the woman. This would be the manly tone. And then I also yesterday installed Helena. Helena is in this case a Spanish, which I could also use. And as I said, you can install many others. So in order to do this, you just need to go to your language settings, on your window, windows settings and simply installed specific languages. That's only to do so by default, normally probably only English and your native languages installed. But if you go to the Window Properties and the search for languages and install any kind of language you want could be Arabic, could be, I think Chinese is possible, right? Could be Spanish, could be French, could be Russian, whatever you want, probably, right? There are a lot of languages available. You can also search on the Microsoft homepage, search for languages, download and you'll find all of them, but you can also install them directly if you go to the Window settings and search for languages. So it's really easy to do that. And then if you do this, you can rerun here, this for loop. And then you should see all the different kinds of languages you haven't here. So that's it for the languages. And if I reset my language here, I can do that. For instance, using David's language. How would I do that? Well, let me just maybe open here a new line in here. And then I simply need to actually set the property here. So what I could use a could use the idea itself. So here ID equals and then this one here for David. And I think it's until this one, yes, this one here. So I could say here, for my voices here, I could use ID is equal to and then actually I need to don't need this one here. I just need to put this in quotes, like nouns. So you can use single or double quotes, doesn't matter. So now I store this in the ID variable. And then I say engine dot set property. And what kind of property do we want to change or modify? It is the voice property. And the idea itself is the, actually the string, in this case to display voice is here. This David one, that now and now I simply say that for instance, engine dot say. And I'm using again Hello World as an example, but this time with the pia English language. And then I can simply call engine dot run and wait. And let me show you this one more time at the beginning when I'm using speaking here, how love it. I love it. It's simply German. If I would try to do this with the English one and comment this out and run it. Hello, good. It sounds kind of strange, but if I modified it to David and run this year, Hello World, you see it completely, it's completely different, right? And if I would use 0, all I need to do is I go inside here. I remove David because the string itself isn't otherwise the same. I simply type in 0. So it would be this string here, right? And I'll put it in here and use this as my property voice and run this helloworld. I would have 0 and a female voice in this case, right? So that's how easy it is very changed this. And as I said, you can do this for any kind of language. You just need to install it first. Then you can simply run this for-loop, see what you have installed. And all you need to do is then set the specific property of your engine to the specific, this case string which you want to use. So female, male, and of course the language itself. And I could also try Spanish for now. So if I go with Arnie to modify this completely because this ES for Espana, so birdie, I copy this, Control C to copy it. And then a pace insight here, control V to paste it like that. And then of course I should probably say something in Spanish. So my Spanish is not too good. But I think it's something like a win. Yeah. I think right. Could be. I think it's when DR. com waste. That's right. I think it's more status. So if this is not correct, then I'm really sorry. I'm not Spanish-speaking, but I think it's something like that. And we can run this when Diego's deaths. And you see that the languages again different, but it really sounds like a Spanish speaking language, right? And if I would use, for instance, an English language here, then this would not work at all, probably. So let's get this one last time. A try. Use 0 in this case and paste this insight and try to speak Spanish with an English language here. asked us, it's not really Spanish, right? Concerning the the other language itself. So that's it for this video. So this was about, of course, running this speaking engine. Also. Be sure that we get the properties here. See what kind of voices you have installed. If the voice you want to use is missing, then just install it via Windows. You can easily do this using the settings in Windows, go to the language settings and installed the specific language. There are a lot of languages available. And then you can run this for loop again, see what you have installed and all introduced. Then set this property for the wash you want to use and then you can actually use the voice that you want, right? So one last thing I'd like to do here, I have you, Sarah here. I want to actually remove this and I will use David for now. And check that that works. So one more time. Go in here and say helloworld. Run this hello world, and this works. And by the way, if we set this property here, then of course, this is a global set. Because if I now run this here at there, so helloworld with my speaking, the engine here and I run this Hello World, you see that this is still in place. So the idea itself, the property we're set is also now available generally here for our whole code. So that's it for this video. Hopefully enjoyed it. Thanks for watching, and I'll see you in the next video. Until then as guys. 5. 4 Starting with days and times: Alright, now let's create our first function which allows us to return the specific date which we have. So let me just enter a few new additional lines here, and then let's write our function. So maybe a comment here. So returns the weekday name as an example. And okay. Okay, that's it. I think you could, of course, commented and write additional things, but for me that's fine. And uses query day as function. And here what we need is we need the datetime module. That's why we imported it at the top here, which is this one here, datetime, so we can use it now. If I go to my function here, go inside and say my day is equal to, and I'll say date, time dot date dot today. So this will simply return here the specific date we have. And I can print this day if I want. Again, printing out things in between. It's always helpful because then we know we on the right track. So let's actually print the day for now like that. And then we want to get to the weekday. So weekday and say This weekday is equal to day dots weekday. So okay, so this would return the weekday and also let's actually print this, Let's say prints Weekday, weekdays with that. And then we need to do some kind of mapping. Because the problem here, and actually I can show this to you if I run this function and simply call it here. Very day. You'll see that what we get. So today's the first September 2021. And what this weekday returns actually is the number of the week day, which would be two in this case. So obviously it's starting here for me at Monday with a 0. Tuesday would be one and Wednesday because today's Wednesday would be two. Now the problem is, of course, that if we ask our voices systems for the day, and I don't want to get the number two. I want to get the actual name of the d. So in this case Wednesday. So this is why we need to do a few additional things here. And we will create a mapping table for that. So it's called this mapping. The name itself again is completely up to you. But what we do is we create a dictionary. And then this dictionary, we simply map the numbers to the weekdays. So 0 would be Monday, then one, sorry, one, this case would be Tuesday. Tuesday. Then the number two would be Wednesday. The number three would be Thursday. Four would be in this case, then Friday, Friday, fifth would be Saturday. And the number 6 then would be Sunday. Okay. So we map basically the numbers to the weekdays. So now we could use actually a weekday itself. So in this case would be two. And look it up in the dictionary. So in mapping and say, Okay, well if it's two, then we want to return Wednesday. And that's what we do here. So then again, we use a try except command here and say we want to try and reuse our speaking function, right? We call it, record it here speaking, that's fine. And we call it here and say speaking. And then use an f string here and say Today is. And the reason why I'm using f string here is because the f string allows us to put string and a specific value we get together in the string itself. So we can say mapping. And from the mapping, we want to use the weekday like that. And then of course I also need to close the quotes. Just need one of them like that. By the way, I think I didn't mention it. I sometimes use single quotes and sometimes I also use double-quotes. So it doesn't matter. Python is of course, accepting both of them. Just can't combine them. I couldn't use a double quote in here and a single quote here to end this, this will not work. But if I put the Monday in single quotes or I use double quotes, it doesn't matter. Both work. So, okay, try speaking today is, and then from the mapping, we use the weekday. Remember the weekday here gives us 2. So mapping of two would be Wednesday. So this would return Wednesday. And then simply Speaks, Today is Wednesday. Okay. So that's it. And then of course we can also use the except just to catch any errors and say except I'm using a pass command here. So don't do anything, just pass. So let's actually run this again. And let's now just get the weekday. Today is Wednesday. And you see that it works. Okay. So that's fine. Great. And then of course now we could comment this out the print commands because we don't need it anymore. It was just to help to understand what actually happens at the specific step. Okay. Let me just run this one more time and okay. So we got that and now it should not print anything. Today is Wednesday. Exactly. Okay, great work. So we have created the day function and next we will do some kind of similar query. For what time is it? Okay, but we'll do this in the next video. I'll see you there. 6. 5 What time is it: Okay, Now we want to query the time. So let's call another function and call this query time. Again, it's up to you how you want to name it, but I will use this one here. And let's say in here, go inside and also use a comment here, returns the time. Maybe you could also use an S here and this year and also not that. Okay, so we've got the function, and now we could try a few things. Let's just say, what do we get if we say time is equal to and say date time dot h time, time. Now, so we can call this, and it's actually try that and see what we get. So let's say I want to print time. Just as an example, I run this and let's see what we get here. Okay? So let's call it pretty time. And you see that's what we get. We get the date and the exact specific time. So we probably don't want to speak this, but let's try that and let's see what we get. Okay, so let's say I want to call speaking them. And I will use time itself like that and run this. And now if we query the time 2020, one dash 09, dash 0109 colon 43 colon 41.1896, 700. Okay. So probably you heard David's and you probably don't think that this is really helpful, right? So we need to at least format this. So let's try that. Let's call this now, but then call, let's say string format time. So STR F time, short form for string form time. And then we can use a percent symbol, then I double point person at symbol. And then it's M. And that will point in person symbol S for the seconds. Okay? So end just in case you want to find out what kind of well, abbreviations there are and exist. Daughter the datetime module. So just a Google or use any other kind of search engine and search for specific datetime module and that you find all the specific abbreviations. Because for me, for instance, I don't know all of them by heart. I just need to look them up. So and I've done this. And if we try this, and let's run this here and credit the time and see what we get. Nine hours, 44 minutes, and 56 seconds. So you see the difference and to be could use this if we want, or we could also do a differently. Another option would be that we refer to specific elements we have here. So for instance, you've seen that the time is nine hours and so on. So if refer to time and only part of that. So I think it would be times one. I think Let's actually try that. And let's run this. Now. You see that now we only got the hour. So of course we could also try now to combine this with the minutes and the seconds. But for me now, I just like to keep it simple. So let's say it's nine o'clock, for instance, right? For the specific time. So to do this, I can then simply say speaking. And instead of only using the time 1, which is 9, so part of this string from a time, I could say I'm using an f string here again. And say, in this case it is nine o'clock. So I can actually put this in curly braces. And then I simply tell it, it's O, think it's o'clock, right? So clock. And to do this, I just need to use here because he is a single insight like that. Okay. That should be it. So nine o'clock should return now, please at my current time. So let me run this and let's just see every time nine o'clock. And you see that now it will return nine o'clock. So as I sat, it's an abbreviation because our only return the current hour. But I could do something kind of similar to the minutes, right. So a time, probably number two, and then end so many minutes. So for instance, nine o'clock. And let's call it times 2 and then minutes. Let's run that query that nine o'clock and minutes. Nine o'clock and four minutes. And probably I need to modify it a little bit. It's three to four. And that's right, that and run that and run it again nine o'clock and four minutes. And if I modify this a little bit, three to five, and let's run this here. I should also get them in. It's nine o'clock and 47 minutes. You see that this will be another option. So you need to try and maybe a little bit with that to figure out what exactly once. But this should work fine at least for me. Okay? So that's it for querying the time. 7. 6 Greetings from our voice assistant on starting: The next function we will build is simply our welcome function. So if we start our voice assistant, then of course, in this case David, or 0 or whatever kind of name you want to give. It should actually greet us, right? Should maybe see something like hello, welcome. How are you, anything like that? So to do this, of course, let me just enter a few more lines here. And ICP call a function def. And I call this function maybe what's up? Okay, what's up? Of course again, you can use any kind of name for the function you want and call this. This is the intro greeting at startup. Okay? Alright, so that's function. And now I simply say in this case, I'm calling speaking directly. And I simply type in some text here. So for instance, I'll use it using multi-line string. So I use three single quotes. So this allows us to write the code into multiple lines if you want. So I can type in here, for instance, Hi, my name is David, or whatever kind of name you want to give your chat bot. So Alexa and Siri, they are already use those names, so now prefer you owning, okay, and this has amazing David. But again, you can call them whatever you like. So for instance, this is Islam if you want. So and then I am your personal assistant. And then maybe how may I help you? Okay, like that. So if you use single quotes here at the beginning and the end, then you could not write into multiple lines. Then you would need to write all your text into one line. If you was triple quotes, like I do here, you can write a code in multiple lines if you want. Okay, that's the only thing here. And of course we want to check this out whether that works. So maybe just call this function here and let's try that. What's up? And run it? My name is David. I am your personal assistant. How may I help you on we see that this works fine, okay? And I encourage you try this out with different kinds of options for the voices you have. You could use 0 as well. So we could use Sara and listen to 0 and abundant. Then of course you would need to modify the moisture again. So if we do that, if I write in 0 here, I run this Hello World. You see that k that works. And now if I run this again for the ones up here. Hi, my name is David. I am your personal assistant. How may I help you? So you see that now we have Sarah, but Suez still called David. So that probably doesn't make sense. But you see that how easy it is active, switch the language to your preferred language. Okay, so that's it for this video. Hopefully our state what we did and see you in the next one under them. Best guys. 8. 7 Time to finalize our voice assistant: Alright, now let's write our heart function of the voices instance. So I call it the heart function because it's the main function, so where the commands actually are executed. So let's call this here, the heart of our assistant and takes queries and returns, hopefully, of course, ANSYS. Right? Okay. Now this function, I call it varying, varying. Again, it's up to you what you want to name it. But in this case, I simply call it prime. End at the beginning. When this function starts, what it should do is at first of course, greet us. So when we run our voices than at the beginning, we want him or her just to say, hello, how are you? Something like that, right? In our case, just tie. My name is David or let's say make it shorter, maybe brief. Hello. I am David's. How may I help you? Hey, maybe three for their short throttle is a little bit. Mike might be easier actually, like that. Okay. So hi, I'm David says How may I help me run this again? And at the startup of this function, when someone runs it, then of course we want to actually start with though, what's up? Okay? First call this at the beginning so that our voices are sent leads us. Then I want to actually create a variable. Let's say starch is equal to true. And then we use a while loop. So while start. In this case. And during the start actually, we want to run the voice assistant. So that means using this while loop here, while start, we simply make sure that the voice assistant does not stop after we have asked him or her just one question. Right. So we want to keep it running. That's why we use this, this while loop here. And of course, when we do this, the loop will run indefinitely. Besides something crashes. That is why of course, we also need to end this wave function by setting the stars to false at some certain point in time. Of course, the time would be if we tell the voice assistant that it should shut down, right? Okay, so whilst not, what we wanna do is at first, we will get the query. The query is equal to, in this case from our transform function transform. And we want to just use lowercase. So I would say I want to call lower here as well. So, because so far, Let's actually repeat. Take a look what we've done. We have created this transform function. This transform function listens to the microphone to that what we say, and transforms what we say into text. Then we created the speaking function. The speaking function actually takes text and then returns an audio output. So it tells something to us, right? Using specific voices we have set here in different kinds of languages. Now of course, we need to combine this because what does the chat bot actually do? Or in this case our isn't. Well, it listens to what we say. It converts using transform function, what we say into text. And then it works with the text and returns as an audio what we want right to us. So we combine Actually now that transform and speaking. So for that, of course now we store what we see here in the verbal cue. And then of course, we can now check what's actually inside of q. So using if conditions. So can say for instance, if, in this case here, let's say start YouTube. Youtube. So if this is in q, then what you wanna do is we want to open the web browser. And let's say, we also say that speaking and then say, starting YouTube for you. Just a second. Or maybe just make it short and crisp, right? So starting YouTube just a second. Okay, so, and that's that. And then of course, after the voice assistant has said that he or she will start YouTube, then of course we use the web browser here, but browser dot open. And we will open YouTube homepage. So https slash slash and then, okay, like that. And that's it actually for that. So that means that the voice assistant here is listening to what we say. If we say stock YouTube or I would like you to start YouTube. It just needs to have stock YouTube in the text, could be anything, but start YouTube needs to be in Q. So in what we say. And if that's the case, then it tells us I'm starting YouTube in just a second. And then we'll open the web browser in YouTube. Okay, so we got this. And then I want to also called continue to new you afterwards. So that is not then maybe you can also do the same directly with L if for actually open or staff web browser starts. Web browser. You could also use open. You just need to be aware of what kind of law. Well, texts you actually use here, okay? So we check if this is enqueue in the query, then we'll also called speaking here again at the beginning and say, it's case opening. Opening, in this case, browser. Browser. Again, you could call just a second if you want. Or just just caught this for speaking. And then you call web browser open. In this case. And this case you could use HTTPS points and then or any kind of page you want at your starting pH here. I'd just like to mention that probably I'm not sure whether this works properly for me here because as you can see that I'm using Jupyter Notebook inside my browser. So starting web browser would actually probably here not sure whether it will open a second web browser or if it would not work at all because I already have my browser open, could also be the case. But if you try this from, let's say, a different editor, not from your browser, then this should work fine. You can try this out if you want. Okay, so we got those two end because we have created two functions using the date and the time, we could actually build those also inside directly. So maybe I like to actually put in some kind of space in here and also call continue after that. So it continues here with the coat. And then I call l If again. And now we again check what's in the text. Someone tells that was peaks inside the microphone and say, Let's see, what date is it. So if this is in the queue, so in the query itself, after we transformed the audio into text using the transform function, then we want to speak. Speaking. And actually we don't need to call speaking. We could use one of the functions we've created here directly. So query time for the time and query day for the day. So we couldn't actually So from here and go on-site and say, can vary due date. Right? And this is nothing else than the function we have already credit here. We have seen that if we query it, today is Wednesday, we get this for the day. And this is actually what happens here. If we tell the voice assistant or asked there wasn't this isn't for the day itself. So we got this. And the same will be true, of course, for the time. But one thing I missed here, I also want to call continue yeah. Afterwards. Okay. And LF So what time is it in Q? If this is the case, then we want to query the time. And of course also called continue afterwards. And then finally here, of course also as an L if, as I said before, we need to break this at a certain point in time. If we want to shut down the wizard systems. So this will be, for instance, elif, in this case, a goodbye or stop. Maybe we could say stop in. And it's actually say shutdown, Right? Again, this is really up to you, what you want to call it, but let's say shut down in Q. So if that's the case, then we want to shut it down. And then I simply say in this case speaking, okay. I M shutting down like that. Okay. Or any other kind of texts. For instance, I also tried goodbye, Have a nice day. That could also be the case or whatever you would want to do here. And then let me actually go here. And for me, let's say set a, a break for now inside rake. Okay, so let's run this. And now let's actually call querying and see a few things. Co-vary ink. And let's call it actually for the day, for the time. Let's try this and then also maybe start YouTube. Okay, Let's maybe start with browser. We can also can try it at least, and then we can shut it down. Okay. So let's run this. Hi, I am David. How may I help you? What day is it? Today is Wednesday. What time is it? 0 o'clock and 05 minutes. Start YouTube. Starting YouTube. Just a second. And you see it's opening YouTube here. So that matrix close it. So let's say start web browser. And you see it's listening, but it wasn't an open, a new browser, so that could be an issue here. So let's shut it down. Shut down. Try one more time. Shutdown. Okay, I am shutting down. And you see it's shutting down the browser over and discuss shutting down the query here, the voice assistant, and less closed here. Okay? So sometimes might be the case that you need to repeat it, at least I need to do it here, might also depend on my microphone. I'm not sure. But you see that it actually does what we ask it to do. Okay? Maybe the only, the only issue here is we using Sarah and still use David as a name. But I mean that that should not matter too much. Okay, so that's it for this video. And in the next video, we like to extend this and maybe at a few additional functionalities in here. Okay, So that's it. So I hope you enjoyed it and I'll see you in the next video. Until then. 9. 8 Completing our voice assistant: Okay, So one additional thing I could think of, we could also import is the Wikipedia library. So Wikipedia allows us actually to, well, go to Wikipedia and look up certain questions. So this is something of course, the Alexa or Siri and other kinds of voices is sends out there were also do, right? So you ask them for a specific keyword or phrase and you want to have some kind of information. So to get this, we can also import Wikipedia here. So I call here imports the key pedia module. Let me run this and Okay, that works fine. Okay, So to this as well, and in case you don't have it, you know how to install it. So this is that, Let's go down here and let's continue here. Our, this case, our voices is then to options which are available using LDF again. So we also check here for us. We start YouTube. We could start a web browser. We asked for the day, for the time. We can shut it down. And now let's actually call here a few additional things. For instance, as a set, we could ask for Wikipedia. So it's called L if, and then say. From Wikipedia. Actually only called Wikipedia maybe. Or search wikipedia for something. That's call it from Wikipedia. Wikipedia. Pedia here in Q. And again, it's up to you what kind of texts you want to ask it. I mean, maybe you have some specific phrases you want to use then of course, use them instead of only from Wikipedia. But it makes sense of course, that Wikipedia is included in your questioning. So if this the case, then we call speaking again and shoot at first tell us that it is checking Wikipedia. So let's call checking the key pedia. And then we simply want to replace in our queue Wikipedia. So Q is equal to Q dot replace. And we replace wiki pedia with, let's say nothing, right? So put it, put it in quotes here. And I want to replace it with, with nothing. So putting empty strings here. Now, why do we do this? Well, you need to think of that like this way. If we ask for something searching wikipedia, then of course we only search for a specific keyword, like Coca-Cola for instance, right? We're not searching for Wikipedia, Coca-Cola. So that's why we want to remove the Wikipedia here in the query itself. So just part of the text so that we only get this specific keyword we are searching for. Okay, so we got this end. It might be the case that you want to or need to remove additional keywords, not only Wikipedia, dipping it depending on your query, right? But you can easily do this using the Replace function and stored again in the verbal cue or a different variable you can use later on as well. So then I say the results, results is equal to n. Now we use the Wikipedia module which we imported at the top, wiki pedia dot summary. And we are searching for the query we have. And then we can also specify the sentences. I will use two here because I want to have the explanation or what's the voices and finds a really short. But if you want to have long explanations, just to increase the number here for the sentences to return. Now we've got the results and then we call speaking. Let's call it bounce on Wikipedia. And then again we call speaking, again speaking, and we return the result. So what it finds here as a summary and speaks then the summary. Okay, so that's what this actually does here and that's everything. Sorry for that. Let me go back. That's everything I wanted really. Okay. So we got that. And now what else can we do? Well, of course we can ask it for his or her name. So L, if let's say your name in the query. So the actually the amine, the scaffold, how it works is the same. We simply use L If he undoes and we check what's inside the query, which would be asked. And then we of course do with certain kinds of commands. So if your name isn't a query, then please speaking, then I'm David. What's your name again? Because remember at the beginning, actually we also call What's up. What's our function also already speaks. I am David Tommy. I help you. Okay. So if your name again, we can stump still return it. I'm David. Your VA So this could be maybe an issue, maybe it's called var instead of we'e, but we can try that. Okay? Otherwise we could also write of course virtual assistant as whole word. I would. Okay, So we got that. And then we can also check for, say, searching the web. And this is what pyplot kid can do. So for instance, L, If this case search, search when you're in the Curie in q, then we can use the module which we import it with Pi. What kit? This case, dot search. Q. So this should actually then open Google search and search faults with a keyword. We import it here. So the same is if you call Cortana for instance, right? And ask for something that normally, at least from my point of view, what I've seen so far is that the Microsoft Edge opens and shows me some results, which is not always that helpful, but that's something we can actually also do in here using this pi what kid. So dot search query. And then of course we can also speak something, let's say speaking. And that's what I've found. Like that. Okay. So this is not, and an author I need to or want to call continue. And it's actually something I forgot here. So let me actually go in here, called Continue here as well. And also call continue here as well. Okay, So we get that. Alright, so that's it from here. And then we could also do something else. For instance, there's an option to play a YouTube some directly. So this would also work with, I think, by what kit. So we call LF and then say a play in queue. So if this is the case, then we could say speaking plane. And if he was an f string, f playing. And then we could actually ask for something like playing Q. Or actually we could write probably the whole cuz PQ could also work. Yeah, playing. Otherwise it would be probably playing play you but because you could also use, again Replace function to replace something. But let's stick for it for now. Let's just see whether that works. So playing the query. And then we call pi what get dots, in this case, play on YT. So play on YouTube, not pay on YouTube, but play on YouTube. So I got, and then we simply call for the cubed here. So let's see whether it works and then let's call it continue. So could also be the case that we want to replace it here. I will see you with that. Okay. So this is that. And then we could also do the job, right? We had the Duke. So if this case the joke in q, then we would like to call the joke, so speaking. And then I saw pay jokes, dot get joke. It's just a function here which you can call it. And then it tells us a joke. And then a boss call continue. And this is it for the joke. And maybe one last thing we could do is, is actually we could ask for the stock price. That's why I imported Yahoo Finance. So let's do that here as well. So also LF. And now it's called this case stock price, stock price in the query. Then what do we want to do? Well, we want to search in this case. And here we need to be aware of how we talk to the voices systems. Of course, we could also try to do something with regular expressions, but this is going too far for now. I can simply say search is equal to, and I'll say q dot split. And I'm splitting by, let's say off. So if someone asks, or if we ask, what is the stock price of Tesla, for instance, right, are off Amazon or whatever kind of stock you want. And then we split by the off and only use the last word. We can refer to it by using minus1. So if someone asks what is the stock price of Tesla, then the splitting would be done by OFF and minus1 would refer to the word Tesla. So the last word ends to get rid of any kind of empty spaces here. We can also call strip here. So a strip, It's often used here for just make sure that we don't have any additional whitespace is here. Okay, so we have this search and we can then actually look this up. So okay, so let's call it lookup or whatever you want. And again, we need a dictionary. Now, this of course, can become a little bit bigger depending on how many stocks actually you want to use here. Because what we do is we map here the specific name with the ticker symbol. Because as an example, the ticker symbol for Amazon is, am. I think I'm so. Of course, if we searched Yahoo Finance, we would need to have the ticker symbol, right? And not Amazon itself. So why we need to map the, the stocks here? So let's just do a few as an example. And of course, if you want to use this for your own, then just use the stocks you are, well, have on your watch list or which on your portfolio. So let's say Apple here. And then let's say, I don't know, Amazon I set could be an example. And then as a sub n. And then I just need maybe because I use Google, okay, let's say Google here and say, I think it's G, G, L, and I think there's a second Google stock, but I don't I'm not I don't know, but you can look this up on the Internet if you want. And as I said, just, just use the stocks you are interested in. Maybe the stock market S and P 500. Or I think in India it's, it's nifty 50. Sounds like that's in Germany would be at the tax for instance, in other kind of stocking, he says around the world gate, what would your unborn use? A Hongzhang I think exists and other kinds of illnesses or specific socks. It's up to you. Okay. So we got our lookup here. So I'll stop here and then we do a try-except. So I'd say try. And here we say stock is equal to lookup. In this case, the search. So remember, when we search here for Apple for instance, then we map here the search Apple shoe, AAPL. And then we return this year, this mapping and stored in stock. So stock would be AAPL for Apple. And then we actually return here the ticker. So stock is equal to Yahoo finance dot ticker of this specific stock. And then we get just the current price. If you want to explore this more, just take a look at the library because it gives you a few additional information about the stock and not only the price, but I think this is probably the most interesting right now. So let's look at the current price year and say is equal to stock dot info. And then we need to use here from info because it returns a dictionary. The regular, I think regular market price, price, this should be it. And then we can actually speak this. So it's called speaking. And then use f string here and say I found it. And then the price is, and then call for the price, right? Or let's say the price for. And because he's an f string here, we could say the, this case the search, not the stock because we want to have the name. So search is. And then we could go in here and it's a current price like that. So of course then we need to close this here. And I think that should be fine. Okay, let's try that. And then excepts. And otherwise we say Speaking and say, sorry, I have no data about, let's say four. And again, using the search, you search like wrap up. Okay, so that should work. Okay? And then of course we also call here, continue. Okay, so that's it. This is our voices and stones. So it allows us to search on YouTube. Start a browser, asks for the Dane time. We can shut it down. We can ask for Wikipedia. We can ask him or her for her name or his name. We can search the web. We can play something, we can ask it for joke. And we can look up stocks. And these are just a few functionalities, but of course you can also add additional ones. But that's it. And yeah, that's it for this video and hope you enjoyed it and I'll see you in the next one. Until then, there's guys. 10. 9 Finally Lets test our Voice Assistant: Okay, so finally, let's try our chatbots or a voice assistant here. And these different kinds of commands we have programmed, let's give it a go and see whether that works the way we think and wish it works. So the only thing I modify it, which you had not seen as a shutdown, I used, in this case, space in-between. I think For whatever reason this works better for me. So otherwise, the code is the same here. Maybe one more thing. I put the continued here also. I intended it one more time. Probably not necessary, but I've done it here. So if you want to modify this as well, then do this please. And then let me just run this time. I am David. How may I help you stock YouTube? Starting YouTube? Just a second. Okay, that works. What time is it? 0 o'clock and 42 minutes. What day is it? Today is Wednesday. Dr. Pepper from Wikipedia. Checking Wikipedia. Found on Wikipedia. Dr. Pepper is a carbonated soft drink. It was created in the 1800s by pharmacists Charles altered and in Waco, Texas and first-served around one hundred, ten hundred eight hundred and eighty five. What is your name? I am David. Your BA search web Googler. That is what I found. Play helloworld. Playing Play Hello World. What is the stock price of Apple? Founded, the price for Apple is 151.83. What is the stock price for Tesla? Sorry, I have no data for what is the stock price for Tesla? Tell me a joke. Later. He's choking. Is anyone a doctor, programmer and of him user. Shut down. Okay. I am shutting down. So you see, it has shut down now and everything worked fine. One little issue we had was with the time, I think, because if I go back to my time function, Let's go up here. So what we did here, we use time one, which was referring to nine, and run this ten o'clock and 44 minutes. So you see that you can modify this as a set. It's a work in progress. But in general, the voice assistant is working correctly and does all these things. And yeah, my final suggestion here is at first, of course with me say congratulations, you did it. You create your first voices systems. And now if you like to do this, you of course can dive a little bit deeper. You can maybe modify the text here. So if you don't think start YouTube is the perfect sentence. You could modify this. Or you can explore a different libraries which are also available, which can give you additional functionalities, okay? But that's it for this project. So thanks a lot for you interest. I really hope you enjoyed it. If you do, then please give this a like, please like the course and also recommend to your friends. Okay, so that's it. Stay healthy, stay safe, and hopefully see you in the next video. Until then, best guys.