Sunday, April 30, 2023
HomePythonControlling Your System Utilizing Alexa (Tutorial)

Controlling Your System Utilizing Alexa (Tutorial)


Hello guys! I hope you might be all doing effective. I’ve began engaged on a small compilation of initiatives which you are able to do underneath 24 hours. I can be publishing these initiatives within the type of tutorials on my weblog. That is the primary challenge and I hope you’ll take pleasure in doing it. Hold these two issues in thoughts:

  • You don’t want an precise Alexa to comply with this tutorial. Amazon gives an internet simulator which you need to use
  • I can be omitting the directions on learn how to get a digital setting up and operating simply in order that I can preserve this tutorial concise and to the purpose. It is best to use a virtualenv and execute all of those pip directions in a virtualenv.

I did this challenge at BrickHack 4. This was the very first time I used to be utilizing Alexa. I had by no means been in its proximity earlier than. I wished to develop such a talent which was not obtainable on-line and which contained a sure stage of twists in order that it wasn’t boring. Ultimately I settled on an thought to regulate my system utilizing Alexa. The principle thought is you could inform Alexa to hold out totally different instructions in your system remotely. This may work even when your system/pc is in your workplace and also you and your Alexa are at residence. So with none additional ado lets get began.

1. Getting Began

If that is your very first time with Alexa I’d counsel that you just comply with this actually useful tutorial by Amazon and get your first talent up and operating. We can be modifying this exact same talent to do our bidding. On the time of this writing the tutorial linked above allowed you to inform Alexa your favorite coloration and Alexa will keep in mind it after which you may ask Alexa about your favorite coloration and Alexa will reply with the saved coloration.

We want a technique to interface Alexa from our system. In order that we are able to remotely management our system. For that we are going to use ngrok. Ngrok means that you can expose your native server to the web. Go to this hyperlink and you may learn the official set up directions. Come again after you might be positive that ngrok is working.

Now we additionally have to open urls from AWS-Lambda. We will use urllib for that however I want to make use of requests so now I’ll present you the way you need to use requests on AWS-lambda.

  1. Create a neighborhood folder to your AWS-lambda code
  2. Save the code from lambda_function.py to a lambda_function.py file within the native folder
  3. Set up the requests library in that folder utilizing pip: pip set up -t .
  4. Create a zipper of the listing and add that zip to AWS-Lambda

After the above steps your native listing ought to look one thing like this:

$ ls
certifi                chardet-3.0.4.dist-info    lambda_function.py        urllib3
certifi-2018.1.18.dist-info    idna                requests            urllib3-1.22.dist-info
chardet                idna-2.6.dist-info        requests-2.18.4.dist-info

You will discover the add zip button on AWS-lambda:

Now you need to use requests on AWS-lambda. Now simply to check whether or not we did all the pieces appropriately, edit the code in lambda_function.py and alter the next traces:

Change these traces with this:

def get_welcome_response():
    """ If we wished to initialize the session to have some attributes we might
    add these right here
    """

    session_attributes = {}
    card_title = "Welcome"
    html = requests.get('http://ip.42.pl/uncooked')
    speech_output = "Welcome to the Alexa Expertise Equipment pattern. " 
                    "Your AWS-lambda's IP tackle is "
                    html.textual content()
                    " Please inform me your favourite coloration by saying, " 
                    "my favourite coloration is crimson"
    # If the consumer both doesn't reply to the welcome message or says one thing
    # that isn't understood, they are going to be prompted once more with this textual content.
    reprompt_text = "Please inform me your favourite coloration by saying, " 
                    "my favourite coloration is crimson."
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

We additionally have to import requests in our lambda_function.py file. To do this add the next line on the very prime of the file:

import requests

Now zip up the folder once more and add it on AWS-lambda or edit the code for this file straight on-line. Now strive asking Alexa to run your talent and Alexa ought to greet you with AWS-lambda’s public IP tackle.

Now lets plan out the subsequent steps after which we are able to resolve how we wish to obtain them. Here’s what I bear in mind:

  1. We could have a server operating on our system
  2. We’ll ask Alexa to ship a sure command from a listing of pre-determined instructions to our system
  3. The request will go to AWS-lambda
  4. AWS-lambda will open a particular url comparable to a sure command
  5. Our native server will execute the command primarily based on the url which AWS-lambda accessed

So naturally the subsequent step is to get a server up and operating. I can be utilizing Python/Flask for this objective.

2. Making a boilerplate Flask challenge

The Flask web site gives us with some very primary code which we are able to use as our place to begin.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def howdy():
    return "Hi there World!"

Save the above code in a app.py file. Run the next command within the terminal:

$ Flask_APP=app.py flask run

This may inform the flask command line program about the place to seek out our flask code which it must serve. If all the pieces is working effective, you need to see the next output:

 * Operating on http://localhost:5000/

If issues don’t work the primary time, strive looking out round on Google and you need to be capable to discover a answer. If nothing works then write a remark and I’ll strive that will help you as a lot as I can.

3. Creating Customized URL endpoints

Firstly, lets make our Flask app accessible over the web and after that’s completed we’ll create customized URL endpoints. With the intention to do this we might want to run our Flask app in a single terminal tab/occasion and ngrok within the different.

I’m assuming that your flask app is operating at present in a single terminal. Open one other terminal and kind the next:

./ngrok http 5000

Just remember to run this above command within the folder the place you positioned the ngrok binary. If all the pieces is working completely you need to see the next output:

ngrok by @inconshreveable                                                                                                                                         (Ctrl+C to stop)

Session Standing                on-line                                                                                                                                              
Account                       Muhammad Yasoob Ullah Khalid (Plan: Free)                                                                                                           
Model                       2.2.8                                                                                                                                               
Area                        United States (us)                                                                                                                                  
Net Interface                 http://127.0.0.1:4040                                                                                                                               
Forwarding                    http://instance.ngrok.io -> localhost:5000                                                                                                          
Forwarding                    https://instance.ngrok.io -> localhost:5000                                                                                                         

Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                         
                              0       0       0.00    0.00    0.00    0.00

Which means each request to http://instance.ngrok.io can be routed to your system and domestically operating app.py file will cater to the entire requests. You’ll be able to check this by opening http://instance.ngrok.io in a browser session and you have to be greeted with this:

Hi there World!

This confirms that until now all the pieces goes in line with plan. Now we’ll transfer on and create a customized url endpoint. Open up your app.py file in your favorite textual content editor and add within the following piece of code:

@app.route('/command', strategies=['GET'])
def handle_command():
    command = request.args.get('command','')
    return command

Right here we’re utilizing a unique module (request) from the flask package deal as properly so we have to add an import on the very prime. Modify from flask import Flask to from flask import Flask, request.

Now restart app.py which was operating within the terminal. The above piece of code merely takes the question parameters within the URL and echoes them again to the caller. As an illustration should you entry: http://localhost:5000/command?command=That is wonderfulyou’ll get That is wonderful because the response. Let’s check whether or not all the pieces is working effective by modifying our AWS-lambda code and making use of this endpoint.

4. Testing the endpoint with Alexa

Open up lambda_function.py and once more modify the beforehand modified code to mirror the next modifications:

def get_welcome_response():
    """ If we wished to initialize the session to have some attributes we might
    add these right here
    """

    session_attributes = {}
    card_title = "Welcome"
    html = requests.get('http://instance.ngrok.io/command?command=working')
    speech_output = "Welcome to the Alexa Expertise Equipment pattern. " 
                    "Your ngrok occasion is "
                    html.textual content()
                    " Please inform me your favourite coloration by saying, " 
                    "my favourite coloration is crimson"
    # If the consumer both doesn't reply to the welcome message or says one thing
    # that isn't understood, they are going to be prompted once more with this textual content.
    reprompt_text = "Please inform me your favourite coloration by saying, " 
                    "my favourite coloration is crimson."
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

Just remember to modify instance.ngrok.io to your personal endpoint which ngrok gives you with. Now save this code to AWS-lambda and ask Alexa to make use of the Colour Picker talent (the talent which we have now been working with for the reason that starting). Alexa ought to reply with one thing alongside the traces of:

Welcome to the Alexa expertise package pattern. Your ngrok occasion is working. 
Please inform me your favorite coloration by saying, "My favorite coloration is crimson".

In the event you get this response its time to maneuver on and make some modifications to the Alexa talent from the Amazon developer dashboard. Open the dashboard and navigate to the talent which you created whereas following the Amazon 5 min talent improvement tutorial. Now we’ll change the title of the talent, its invocation title and the interplay mannequin.

  • Change the Ability title and invocation title to something which you discover satisfying:
  • Change the intent schema on the subsequent web page to this:
{
  "intents": [
    {
      "slots": [
        {
          "name": "Command",
          "type": "LIST_OF_COMMANDS"
        }
      ],
      "intent": "MyCommandIsIntent"
    },
    {
      "intent": "WhatsMyCommandIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    }
  ]
}
  • Change the Customized Slot Varieties to this:
Sort: LIST_OF_COMMANDS

Values:

    shutdown
    sleep
    restart
    utilizing

  • Change the pattern utterances by these:
MyCommandIsIntent ship the {Command} command
MyCommandIsIntent ship {Command} command

Now edit your lambda_function.py as properly and substitute each occasion of Colour with Command. The file ought to look one thing like this after making the required modifications:

https://gist.github.com/yasoob/f98b17f67b48081e8828fe90d9d6aab6

A lot of the modifications are self-evident. Please undergo the code. The principle addition/change which I made are the next traces within the set_command_in_session operate:

html = requests.get('http://instance.ngrok.io/command?command='+favorite_command)
speech_output = "I despatched the " + 
                html.textual content() + 
                " command to your system." 
                "Let me know if you'd like me to ship one other command."

What this does is that after recognizing that the consumer has requested it to ship a command to the system, it accesses the precise customized endpoint which we created. The entire command circulate will work one thing like this:

Person:  Alexa open up System Supervisor
Alexa: Welcome to the Alexa Expertise Equipment pattern. Your ngrok occasion is working. 
       Please inform me what command I ought to ship to your system

Person:  Ship the shutdown command
Alexa: I despatched the shutdown command to your system. Let me know if you'd like me to ship one other command.

Person:  Thank You
Alexa: Your final command was shutdown. Goodbye

Growth! Your Alexa aspect of this system is full. Now we simply have to edit the customized URL endpoint to truly perform these instructions. I can’t be doing that. As an alternative I can be including voice output for these instructions in order that we all know the instructions are working. Edit the app.py file to mirror the next modifications:

from flask import Flask, request
import vlc

app = Flask(__name__)

@app.route("/")
def howdy():
    return "Hi there World!"

@app.route('/command', strategies=['GET'])
def handle_command():
    command = request.args.get('command','')
    p = vlc.MediaPlayer(command+".mp3")
    p.play()
    return command

Now earlier than you’ll be able to run this code that you must do two issues.The primary one is to put in the libvlc Python bindings. That is required to run .mp3 information in Python. There are a few different methods as properly however I discovered this to be the simplest. You’ll be able to set up these bindings by operating the next pip command:

pip set up python-vlc

The opposite factor that you must do is to create an mp3 file for each totally different command which you wish to give by Alexa. These are two of the information which I made:

Now place these mp3 information in the identical listing as app.py. Restart app.py , add your entire AWS-lambda particular code on-line and check out your model new customized Alexa Ability!

Points which you would possibly face

The ngrok public url modifications everytime you restart ngrok so make it possible for the url in your lambda_function.py file is upto-date.

Additional Steps

Congrats on efficiently finishing the Alexa customized Ability improvement challenge! You now know the fundamentals of how one can create customized Alexa expertise and how one can make a localhost server obtainable on the web. Attempt to combine and match your concepts and create some fully totally different expertise! I learn about somebody who made story studying expertise for Alexa. You can ask Alexa to learn you a particular form of story and Alexa would do this for you. Another person made a talent the place Alexa would ask you about your temper after which primarily based in your temper it’ll curate a customized Spotify playlist for you.

Let me share some extra directions about how you’ll go about doing the latter challenge. You’ll be able to extract the Intent from the voice enter. That is much like how we extracted the Command from the enter on this tutorial. Then you may ship that Intent to IBM Watson for sentiment evaluation. Watson will let you know the temper of the consumer. Then you need to use that temper and the Spotify API to create a playlist primarily based on that particular temper. Lastly, you may play the customized generated playlist utilizing the Spotify API.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments