On-Device Smart Replies on Android using Google’s ML Kit

Reply smartly with the power of machine learning

By now, you are probably familiar with the concept of “smart reply” features. In many messaging applications nowadays, you’ll see a list of suggested replies when you receive a message. And this makes it easier to respond without thinking about what you’re going to say—especially when the reply is obvious.

You can easily add Smart Reply to your mobile apps. like chat-app, Reviews, with the help of Google’s API, via its on-device machine learning framework ML Kit, which allows you to easily add this capability to your projects.

In this article, we’ll do just that on Android with the help of Google on-Device ML Kit’s Smart Reply API.

What is ML Kit?

ML Kit is a cross-platform mobile SDK (Android and iOS) developed by Google that allows developers to easily access on-device mobile machine learning models.

All the ML Kit’s APIs run on-device, allowing real-time and offline capabilities.

To use the standalone ML Kit on-device SDK, we can just implement it directly — we don’t need to create a project on Firebase or an accompanying google.json file.

If you’re using Firebase Machine Learning, then you can check this link to help migrate.

What You’ll Build

In this article, we’re going to build a simple Android app that shows you how to implement ML Kit’s Smart Reply on-device API.

We’ll implement a basic application that includes, upon receipt of a message, an edit field, a button, and three smart reply suggestions.

By the end of this tutorial, you should see something similar to the screenshot below:

Step 1: Add Dependency

First things first, we need to add a mlkit:smart-reply dependency to our Android project in the app/build.gradle file.

Disable the compression of TensorFlow Lite files—to do so we just need to add the below line in our build.gradle file:

Sync the Project

After successfully adding the dependency, just sync the project in Android Studio, as shown in the screenshot below:

Step 2: Create conversation history Object

In Step 2, we need to create a conversation history object. To do that, first we need to create a TextMessage list and pass ML Kit a chronologically-ordered List of TextMessage objects with their timestamps.

When the user sends a message, we need to add the messages and their timestamp to the conversation history object.

Now, let’s jump into some code to see how these above steps look in practice 👇

Here, when the user receives the message, we’re add its message, the timeStamp & the sender’s userId to the conversation history object. The user Id should uniquely identify the sender with the conversation.

Note that this approach will only suggest replies to the last message in the conversation history.

Let’s jump to the code to see how these above steps look in practice 👇

Step 3: Get the replies from the message

In step 3, we need to get the replies from the message. Since we’ve successfully created a conversation history object in the above step and added the message and timestamp in it, now we need to generate the smart replies.

First, we need to create an Instance (object) of SmartReplyGenerator and then pass the conversation history object to its suggestReplies() method.

We also use the onSuccessListener. If the task succeeds, the SmartReplySuggestionResult object is passed to the success listener. The list generates up to three suggested replies.

We also add an OnFailureListener—if we can’t get the smart reply suggestion, we’ll be able to show the user an error.

Now let’s jump into some code to see how these above steps look in practice 👇

Result:

Let’s build and run the application to see our smart reply model in practice:

Conclusion

This article taught you the basics of how to implement ML Kit’s on-device Smart Reply API on Android. To do this, we learned how to create a conversation history object and then create an instance of Smart Reply. And finally, we passed the conversation list to the suggestReplies method.

If you want to explore smart reply in more detail, check out the official docs:

I hope this article was helpful. If you think something is missing, have questions, or would like to offer any thoughts or suggestions, go ahead and leave a comment below. I’d appreciate the feedback.

I’ve written some other Android-related content, and if you liked what you read here, you’ll probably also enjoy these:

Sharing (knowledge) is caring 😊 Thanks for reading this article. Be sure to clap or recommend this article if you found it helpful. It means a lot to me.

If you need any help then Join me on Twitter, LinkedIn, GitHub, and Subscribe to my YouTube Channel.

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square