A-Z Guide on how to run your Python/Machine Learning code on Android

Parth Goel
Analytics Vidhya
Published in
4 min readOct 17, 2019

--

Image taken from https://py.checkio.org/blog/pygame-python-writing-your-first-game1/

As we all know, Machine Learning (ML)/Artificial Intelligence (AI) has taken the world of computer science by a storm. Everyone in the computer science community, irrespective of their specialization has started to learn about or use ML or AI in some form or the other. Not only because of how it simplifies complex tasks, but also because of the love for it. ML and AI have found applications not just in the field of computer science but also in numerous other fields. It has successfully broken the boundaries of research in these fields, by creating new opportunities that seemed impossible 20 years ago. Apart from ML, Android application development has also garnered a lot of interest over the last few years. With the increasing popularity of smartphones, android applications and its developers (flutter or native) have grown exponentially over the years. So the big question that arises is how does one connect these two mammoth technologies in order to create something more user-friendly?

This question becomes all the more important since ML models are usually written in Python(preferred) or R, while the back-end of Android applications is in Java (Native) or Dart (Flutter), depending on the framework used. So one might wonder, how do we connect these two, since they are implemented in two different languages. Although there are many ways to make this possible, e.g., using the Firebase custom model. The one that I found most appealing and easy, was to first implement an Application Program Interface(API) for the Python/ML code using Flask and then host it on a server. Finally use this API to send get and post requests in order to fetch the output in the app, to make it work.

Since this article focuses on connecting Python/ML code with Android, I won’t get into the details of my ML code. I will briefly touch upon the basics of my ML model, and then go on to explain what I expect the API to do. For more details on my ML code, feel free to visit my GitHub, where you can find the entire code along with an explanation of my ML model.

Step 1: Making the API using Flask

There are a wide variety of frameworks available to make a REST APIs, e.g., Django, Node.js etc. But the one that I find most user-friendly and easy to use is Flask. I have made a pretty basic ML-based recommendation system using Apriori and saved the recommendations in the out.csv file. My API first takes the problem string in the form of a get request, then finds a recommended solution in out.csv, and finally returns the solution string in the form of JSON.

If you want to run your API on other devices (host=’0.0.0.0') is necessary.

Step 2: Running the API

It is highly recommended to run this file in Visual Studio Code, since Spyder or Jupyter Notebook do not support hosting of servers. Change your working directory to the one where you have all the other necessary files for running your code. This can be done by using the cd command in the terminal.

cd C:\Users\goelp\Desktop\blog

Then just simply run the file using

python api.py

If your API has no errors it will show something like this

Step 3: Test your API

Go to any browser and type

http://localhost:5000/ram

If your API is working, it should return a JSON. Something that looks like this.

This means that your API is up and running, without any errors.

Step 4: Trying to send a request from another device

Since you want to run this code on your app, which is on a different device (even if you’re using an emulator on the same PC, it counts as a different device) you’ll need to follow some additional steps.

Step 4.1: Connect both the devices to the same network (skip this step if you’re using an emulator on the same PC).

Step 4.2: Set your network as the “Home Network”. Doing this allows your PC to share information with other devices on the same network. To do this on Windows 10, go to Settings> Network & Internet> WiFi>(Wifi name) and set the Network Profile of the network to Private.

Step 4.3: You will need to find out the IP address of your PC. To do so on Windows 10, simply open the command prompt and type ipconfig and save the IPv4 Address.

Step 4.4: Open the browser on your second device (phone or another PC) and type

http://IPv4-Address:5000/ram

It should give the same response as before. Congratulations! You now have a working API.

Step 5: Writing the request code in Android

If you’ve come this far, then hopefully you have a working API. So now all you need to do is to send a get or post request (depending on your API) to URL http://IPv4-Address:5000/ram from your app and fetch the JSON response. This code will vary depending on whether you’re using Native Android or Flutter for making your app (hence I will not cover that in this blog).

All right! That’s all there is to it. If you have any suggestions or corrections, I would love to hear them :) Thanks a lot for reading!

P.S. A big shoutout to my sister for proofreading the blog :)

--

--