How to generate smart reply using Firebase ML Kit Android Studio 2020 (Complete Guide) With Source Code
In this post, we're going to generate a reply on analyzing previous messages in android studio using the ml kit of the firebase.
This is the output after you are done with all step:
So, now make it happen :)
Step 1: Add Firebase to your android project:
I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step.
Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:
as shown below and then click on Sync Now:
Step 3: Design the layout of the activity:
as shown below:
Step 4: Generate Reply:
For every remote message by a remote user( user on the other side), we're going to generate three most appropriate reply and the local user can select one of them
Declare all variable and call generateReply() method on button click:
To generate smart replies, pass the ml kit a chronologically ordered List of FirebaseTextMessage objects, with the earliest timestamp first. When the user sends a message, add the message and its timestamp to the conversation history.
As a demo purpose, we generate a smart reply from only one message but in real application add every message of conversation to the list and not just the last one.
So now to generate smart replies,
1. Pass ML kit a chronologically ordered list of FirebaseTextMessage objects, with the earliest timestamp first (the first message first add)
2. Get an instance of FirebaseSmartReply and pass the conversation history to its suggestReplies() method
as shown below-
Note that ML Kit might not return results if
1. The model isn't confident in the relevance of the suggested replies,
2. The input conversation isn't in English,
3. The model detects sensitive subject matter
Now, run the app :)
If everything is done correctly then you see the excepted output.
You can see the full source code at GitHub.
If you face any problem or have any suggestion please comment it down we love to answer it.
This is the output after you are done with all step:
So, now make it happen :)
Step 1: Add Firebase to your android project:
I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step.
Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:
implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0' implementation 'com.google.firebase:firebase-ml-natural-language-smart-reply-model:20.0.7'
as shown below and then click on Sync Now:
Step 3: Design the layout of the activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/remote_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button" android:layout_centerInParent="true" android:layout_margin="30dp" android:padding="10dp" android:text="Hi there" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Make Reply" /> <TextView android:id="@+id/reply_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button" android:layout_centerInParent="true" android:layout_margin="30dp" android:textColor="@android:color/black" android:textSize="15sp" />
</RelativeLayout>
as shown below:
Step 4: Generate Reply:
For every remote message by a remote user( user on the other side), we're going to generate three most appropriate reply and the local user can select one of them
Declare all variable and call generateReply() method on button click:
To generate smart replies, pass the ml kit a chronologically ordered List of FirebaseTextMessage objects, with the earliest timestamp first. When the user sends a message, add the message and its timestamp to the conversation history.
As a demo purpose, we generate a smart reply from only one message but in real application add every message of conversation to the list and not just the last one.
So now to generate smart replies,
1. Pass ML kit a chronologically ordered list of FirebaseTextMessage objects, with the earliest timestamp first (the first message first add)
2. Get an instance of FirebaseSmartReply and pass the conversation history to its suggestReplies() method
as shown below-
private void generateReply() { // when the user sends a message(local user) add the sender message and its timestamp to the list//conversation.add(FirebaseTextMessage.createForLocalUser("heading out now", System.currentTimeMillis())); // when user receives a message(remote user) add the message, its timestamp,// and the sender's user ID to the list conversation.add(FirebaseTextMessage.createForRemoteUser(receiverMessage.getText().toString(), System.currentTimeMillis(), "user0"));
//To generate smart replies to a message, get an instance of FirebaseSmartReply and// pass the conversation history to its suggestReplies() method: FirebaseSmartReply smartReply = FirebaseNaturalLanguage.getInstance().getSmartReply(); smartReply.suggestReplies(conversation) .addOnSuccessListener(new OnSuccessListener<SmartReplySuggestionResult>() { @SuppressLint("SetTextI18n") @Override public void onSuccess(SmartReplySuggestionResult result) { if (result.getStatus() == SmartReplySuggestionResult.STATUS_NOT_SUPPORTED_LANGUAGE) { replyText.setText("Language not supported"); } else if (result.getStatus() == SmartReplySuggestionResult.STATUS_SUCCESS) {
//If the operation succeeds, a SmartReplySuggestionResult object is passed to the success handler.// This object contains a list of up to 3 suggested replies, which you can present to your userreplyText.setText("Suggestions: " + "\n\n"); for (SmartReplySuggestion suggestion : result.getSuggestions()) { String replyMessage = suggestion.getText(); replyText.append("*" + replyMessage + ",\n\n"); } } } }); }
1. The model isn't confident in the relevance of the suggested replies,
2. The input conversation isn't in English,
3. The model detects sensitive subject matter
Now, run the app :)
If everything is done correctly then you see the excepted output.
You can see the full source code at GitHub.
If you face any problem or have any suggestion please comment it down we love to answer it.
Comment down what next topic you need a guide on? or Drop a message on our social media handle
Happy coding and designing : )
Comments
Post a Comment