Skip to content

Commit

Permalink
Tweak smart reply demo.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 414839941
Change-Id: I8e8cf83d0ecda0ced8b453a1a0caacfd19c41c20
  • Loading branch information
Google ML Kit authored and zhouyiself committed Jan 11, 2022
1 parent 70e92f5 commit c8b8578
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 41 deletions.
8 changes: 4 additions & 4 deletions android/smartreply/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 30
compileSdkVersion 31
defaultConfig {
applicationId "com.google.mlkit.samples.nl.smartreply"
minSdkVersion 16
targetSdkVersion 30
minSdkVersion 19
targetSdkVersion 31
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary true
Expand Down Expand Up @@ -39,7 +39,7 @@ dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

implementation 'com.google.mlkit:smart-reply:16.2.0'
implementation 'com.google.mlkit:smart-reply:17.0.0'

implementation 'com.google.guava:guava:27.0.1-android'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.mlkit.samples.nl.smartreply.java.chat;

import android.annotation.SuppressLint;
import androidx.lifecycle.ViewModelProvider;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
Expand All @@ -35,6 +34,7 @@
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import com.google.mlkit.samples.nl.smartreply.R;
import com.google.mlkit.samples.nl.smartreply.java.model.Message;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package com.google.mlkit.samples.nl.smartreply.java.chat;

import android.app.Application;
import android.widget.Toast;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import android.widget.Toast;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.common.collect.Iterables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import com.google.mlkit.samples.nl.smartreply.R;

/** Represents a chat message. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package com.google.mlkit.samples.nl.smartreply.kotlin.chat

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import android.widget.Toast
import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks
import com.google.mlkit.nl.smartreply.SmartReply
Expand Down Expand Up @@ -94,8 +94,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
return@Observer
}

generateReplies(list, isEmulatingRemoteUser!!)
.addOnSuccessListener { result -> suggestions.postValue(result) }
generateReplies(list, isEmulatingRemoteUser!!).addOnSuccessListener { result ->
suggestions.postValue(result)
}
}
)

Expand Down Expand Up @@ -129,45 +130,36 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
val chatHistory = ArrayList<TextMessage>()
for (message in messages) {
if (message.isLocalUser != isEmulatingRemoteUser) {
chatHistory.add(
TextMessage.createForLocalUser(
message.text,
message.timestamp
)
)
chatHistory.add(TextMessage.createForLocalUser(message.text, message.timestamp))
} else {
chatHistory.add(
TextMessage.createForRemoteUser(
message.text,
message.timestamp, remoteUserId
)
TextMessage.createForRemoteUser(message.text, message.timestamp, remoteUserId)
)
}
}

return smartReply.suggestReplies(chatHistory)
.continueWith { task ->
val result = task.result
when (result.status) {
SmartReplySuggestionResult.STATUS_NOT_SUPPORTED_LANGUAGE ->
// This error happens when the detected language is not English, as that is the
// only supported language in Smart Reply.
Toast.makeText(
return smartReply.suggestReplies(chatHistory).continueWith { task ->
val result = task.result
when (result.status) {
SmartReplySuggestionResult.STATUS_NOT_SUPPORTED_LANGUAGE ->
// This error happens when the detected language is not English, as that is the
// only supported language in Smart Reply.
Toast.makeText(
getApplication(),
R.string.error_not_supported_language,
Toast.LENGTH_SHORT
).show()
SmartReplySuggestionResult.STATUS_NO_REPLY ->
// This error happens when the inference completed successfully, but no replies
// were returned.
Toast.makeText(getApplication(), R.string.error_no_reply, Toast.LENGTH_SHORT)
.show()
else -> {
// Do nothing.
}
)
.show()
SmartReplySuggestionResult.STATUS_NO_REPLY ->
// This error happens when the inference completed successfully, but no replies
// were returned.
Toast.makeText(getApplication(), R.string.error_no_reply, Toast.LENGTH_SHORT).show()
else -> {
// Do nothing.
}
result!!.suggestions
}
result!!.suggestions
}
}

override fun onCleared() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ package com.google.mlkit.samples.nl.smartreply.kotlin.model
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.google.mlkit.samples.nl.smartreply.R

class Message(val text: String, val isLocalUser: Boolean, val timestamp: Long) {

fun getIcon(context: Context): Drawable {
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_tag_faces_black_24dp)
?: throw IllegalStateException("Could not get drawable ic_tag_faces_black_24dp")
val drawable =
ContextCompat.getDrawable(context, R.drawable.ic_tag_faces_black_24dp)
?: throw IllegalStateException("Could not get drawable ic_tag_faces_black_24dp")

if (isLocalUser) {
DrawableCompat.setTint(drawable, Color.BLUE)
Expand Down

0 comments on commit c8b8578

Please sign in to comment.