Setup Lint Testing in SUSI Android

As developers tend to make mistakes while writing code, small mistakes or issues can cause a negative impact on the overall functionality and speed of the app. Therefore it is necessary to understand the importance of Lint testing in Android. Android Lint is a tool present in Android studio which is effective in scanning and reporting different types of bugs present in the code, it also find typos and security issues in the app. The issue is reported along with severity level thus allowing the developer to fix it based on the priority and level of damage they can cause. It is easy to use and can significantly improve the quality of your code. Effect of Lint testing on Speed of the Android App Lint testing can significantly improve the speed of the app in the following ways Android Link test helps in removing the declaration redundancy in the code, thus the Gradle need not to bind the same object again and again helping to improve the speed. Lint test helps to find bugs related to Class Structure in different Activities of the Application which is necessary to avoid the case of memory leaks. Lint testing also tells the developer above the size of resources use for example the drawable resources which sometimes take up a large piece of memory in the application. Cleaning these resources or replacing them with lightweight drawables helps in increasing the speed of the app. Overall Lint Testing helps in removing Typos, unused import statement, redundant strings, hence refactoring the whole code and increasing stability and speed. Setup We can use Gradle to invoke the list test by the following commands in the root directory of the folder. To set up we can add this code to our build.gradle file lintOptions { lintConfig file("lint.xml") } The lint.xml generated will look something like this <?xml version="1.0" encoding="UTF-8"?> <lint>    <!-- Changes the severity of these to "error" for getting to a warning-free build -->    <issue id="UnusedResources" severity="error"/> </lint> To explicitly run the test on Android we can use the following commands. On Windows gradlew lint On Mac ./gradlew lint We can also use the lint testing on various variants of the app, using commands such as gradle lintDebug    or  gradle lintRelease. The xml file generated contains the error along with their severity level . <?xml version="1.0" encoding="UTF-8"?> <lint>    <issue id="Invalid Package" severity="ignore" />    <!-- All below are issues that have been brought to informational (so they are visible, but don't break the build) -->    <issue id="GradleDependency" severity="informational" />    <issue id="Old TargetApi" severity="informational" /> </lint> Testing on Susi Android:- After testing the result on Susi Android we find the following errors. As we can see that there are two errors and a lot of warnings. Though warning are not that severe but we can definitely improve on this. Thus making a habit of testing your code with lint test will improve the performance of your app and will make it more faster. The test provides a complete…

Continue ReadingSetup Lint Testing in SUSI Android

Implementing a chatbot using the SUSI.AI API

SUSI AI is an intelligent Open Source personal assistant. It is a server application which is able to interact with humans as a personal assistant. The first step in implementing a bot using SUSI AI is to specify the pathway for query response from SUSI AI server. The steps mentioned below provide a step-by-step guide to establish communication with SUSI AI server: Given below is HTML code that demonstrates how to connect with SUSI API through an AJAX call. To put this file on a Node Js server, see Step 2.  To view the response of this call, follow Step 4. <!DOCTYPE html> <body> <h1>My Header</h1> <p>My paragraph.</p> //Script with source here //Script to be written here </body> </html> In above code add scripts given below and end each script with closing tag </script>. In the second script we are calling SUSI API with hello query and showing data that we are receiving through call on console. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> <script> $(function (){ $.ajax({ dataType: 'jsonp', type:'GET', url: 'http://api.susi.ai/susi/chat.json? timezoneOffset=-300&q=hello', success: function(data){ console.log('success', data); } }); }); Code below is in node js to setup localhost and getting the same above result on browser. Below is Node Js code to setup a server at localhost for the above created HTML file. var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) {  fs.readFile('YOURFILENAME.html', function(err, data) {    res.writeHead(200, {'Content-Type': 'text/html'});    res.write(data);    res.end();  }); }).listen(9000); We will get following response by running this Node js code and checking results on http://localhost:9000/ To run this code install Node Js and write “node filename.js” in command line. You can open above window by right clicking on page and selecting Inspect. Go to the Network option and select the relevant api call from left section of Inspect window. We have successfully got response from SUSI API and now we can use this response for building bots for receiving replies for user.

Continue ReadingImplementing a chatbot using the SUSI.AI API

Deploying SUSI.AI with Docker

Docker is much more efficient than VM in allocating shared resources between various containers as shown in figure. To deploy SUSI we need to create docker container. There are two ways to build it. First way is fork the SUSI project in github. Then you can signup in dockerhub and create autobuild docker container. The second way is to manually build docker file from command prompt of your computer. The following instructions needs to be executed in cloud shell or linux machine. sudo apt-get update sudo apt-get upgrade sudo apt-get -y install docker.io sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile sudo docker build https://github.com/fossasia/susi_server.git The first three commands install docker software to the machine. Next three lines give required permissions and execution abilities to docker. Final command builds docker container from github. Thus, We have successfully made docker container. We can deploy it on the cloud by using following command. sudo docker run -d -p 80:80 -p 443:443 susi Deploying Susi on cloud with kubernetes We will use Google Cloud Service platform for demonstration. Create your GCS account. Goto dashboard and click on compute engine. Enable Billing and Create New Project named XYZ. Open the terminal, by clicking the Google cloud shell button on the top. Please set the compute zone to your nearest zone by running the below command. gcloud config set compute/zone us-central1-a Now we need to create cluster, on which we deploy susi app. We do it by this command. gcloud container clusters create hello-cluster --num-nodes=3 We need to get docker from dockerhub and push it to our project repo.We do it by these commands. sudo docker pull jyothiraditya/susi_server gcloud docker -- push <image-id> gcr.io/<project-id>/<name> We run the docker image on cluster by following commands. kubectl run susi-server --image=gcr.io/<project-id>/<name> --port=80 kubectl get pods We expose the container to external traffic, with help of load balancer by the following command. kubectl expose deployment susi-server --type="LoadBalancer --port=80" We get the external ip-address to access susi from browser. By entering kubectl get service susi-server You can now view the app by going to “EXTERNAL-IP:80”. References : Docker build , Kubernetes deployment, Google cloud deployment   

Continue ReadingDeploying SUSI.AI with Docker

How to teach SUSI skills calling an External API

SUSI is an intelligent  personal assistant. SUSI can learn skills to understand and respond to user queries better. A skill is taught using rules. Writing rules is an easy task and one doesn’t need any programming background too. Anyone can start contributing. Check out these tutorials and do watch this video to get started and start teaching susi. SUSI can be taught to call external API’s to answer user queries. While writing skills we first mention string patterns to match the user's query and then tell SUSI what to do with the matched pattern. The pattern matching is similar to regular expressions and we can also retrieve the matched parameters using $<parameter number>$ notation. Example : My name is * Hi $1$! When the user inputs “My name is Uday” , it is matched with “My name is *” and “Uday” is stored in $1$. So the output given is “Hi Uday!”. SUSI can call an external API to reply to user query. An API endpoint or url when called must return a JSON or JSONP response for SUSI to be able to parse the response and retrieve the answer. Rule Format for a skill calling an external API The rule format for calling an external API is : <regular expression for pattern matching> !console: <return answer using $object$ or $required_key$> { “url”: “<API endpoint or url>”, “path”: “$.<key in the API response to find the answer>”, } eol Url is the API endpoint to be called which returns a JSON or JSONP response. The parameters to the url if any can be added using $$ notation. Path is used to help susi know where to look for the answer in the returned response. If the path points to a root element, then the answer is stored in $object$, otherwise we can query $key$ to get the answer which is a value to the key under the path. eol or end of line indicates the end of the rule. Understanding the Path Attribute Let us understand the Path attribute better through some test cases. In each of the test cases we discuss what the path should be and how to retrieve the answer for a given required answer from the json response of an API. API response in json : { “Key1” : “Value1” } Required answer : Value1 Path : “$.Key1    =>   Retrieve Answer:  $object$   API response in json : { “Key1” : [{“Key11” : “Value11”}] } Required answer : Value11 Path : $.Key1[0]   =>  Retrieve Answer: $Key11$ Path : $.Key1[0].Key11   => Retrieve Answer: $object$   API response in json : { “Key1” : {“Key11” : “Value11”} } Required answer : Value11 Path : $.Key1  => Retrieve Answer:  $Key11$ Path : $.Key1.Key11  => Retrieve Answer: $object$   API response in json : { “Key1” : { “Key11” : “Value11”, “Key12” : “Value12” } } Required answer : Value11 , Value12 Path : $.Key1  => Retrieve Answer:  $Key11$ , $Key12$ Where to write these rules? Now, since we know…

Continue ReadingHow to teach SUSI skills calling an External API