Skip to content

Commit

Permalink
Chat-like UX (#2282)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #2282

Reviewed By: larryliu0820

Differential Revision: D54607005
  • Loading branch information
kirklandsign authored and facebook-github-bot committed Mar 7, 2024
1 parent 0570294 commit 5916639
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ImageButton;
import android.widget.ListView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -25,12 +26,13 @@

public class MainActivity extends Activity implements Runnable, LlamaCallback {
private EditText mEditTextMessage;
private TextView mTextViewChat;
private Button mSendButton;
private Button mStopButton;
private Button mModelButton;
private ImageButton mModelButton;
private ListView mMessagesView;
private MessageAdapter mMessageAdapter;
private LlamaModule mModule = null;
private String mResult = null;
private Message mResultMessage = null;

private static String assetFilePath(Context context, String assetName) throws IOException {
File file = new File(context.getFilesDir(), assetName);
Expand All @@ -54,7 +56,7 @@ private static String assetFilePath(Context context, String assetName) throws IO
@Override
public void onResult(String result) {
System.out.println("onResult: " + result);
mResult = result;
mResultMessage.appendText(result);
run();
}

Expand Down Expand Up @@ -89,7 +91,6 @@ public void onClick(android.content.DialogInterface dialog, int item) {
break;
}
mEditTextMessage.setText("");
mTextViewChat.setText("");
dialog.dismiss();
}
});
Expand All @@ -103,16 +104,20 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

mEditTextMessage = findViewById(R.id.editTextMessage);
mTextViewChat = findViewById(R.id.textViewChat);
mSendButton = findViewById(R.id.sendButton);
mStopButton = findViewById(R.id.stopButton);
mModelButton = findViewById(R.id.modelButton);

mMessagesView = findViewById(R.id.messages_view);
mMessageAdapter = new MessageAdapter(this, R.layout.sent_message);
mMessagesView.setAdapter(mMessageAdapter);
mSendButton.setOnClickListener(
view -> {
String prompt = mEditTextMessage.getText().toString();
mTextViewChat.append(prompt);
mMessageAdapter.add(new Message(prompt, true));
mMessageAdapter.notifyDataSetChanged();
mEditTextMessage.setText("");
mResultMessage = new Message("", false);
mMessageAdapter.add(mResultMessage);
Runnable runnable =
new Runnable() {
@Override
Expand All @@ -131,6 +136,8 @@ public void run() {
mModelButton.setOnClickListener(
view -> {
mModule.stop();
mMessageAdapter.clear();
mMessageAdapter.notifyDataSetChanged();
modelDialog();
});

Expand All @@ -143,7 +150,7 @@ public void run() {
new Runnable() {
@Override
public void run() {
mTextViewChat.append(mResult);
mMessageAdapter.notifyDataSetChanged();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.example.executorchllamademo;

public class Message {
private String text;
private boolean isSent;

public Message(String text, boolean isSent) {
this.text = text;
this.isSent = isSent;
}

public String getText() {
return text;
}

public void appendText(String text) {
this.text += text;
}

public boolean getIsSent() {
return isSent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.example.executorchllamademo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MessageAdapter extends ArrayAdapter<Message> {
public MessageAdapter(android.content.Context context, int resource) {
super(context, resource);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message currentMessage = getItem(position);
int layoutIdForListItem =
currentMessage.getIsSent() ? R.layout.sent_message : R.layout.received_message;
View listItemView =
LayoutInflater.from(getContext()).inflate(layoutIdForListItem, parent, false);
TextView messageTextView = listItemView.findViewById(R.id.message_text);
messageTextView.setText(currentMessage.getText());
return listItemView;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff" />
<corners android:radius="10dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
<corners android:radius="10dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM18,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextMessage"

<ListView
android:layout_width="match_parent"
android:id="@+id/messages_view"
android:layout_weight="2"
android:divider="#fff"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textSize="20sp"
android:hint="Type a prompt" />
<LinearLayout
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="right"
tools:ignore="RtlHardcoded">
<Button
android:background="#fff"
android:orientation="horizontal">
<ImageButton
android:id="@+id/modelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Model..." />
android:src="@drawable/three_dots" />
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="Prompt"
android:inputType="text"
android:paddingHorizontal="10dp"
android:text="" />
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
Expand All @@ -34,11 +46,4 @@
android:layout_height="wrap_content"
android:text="Generate" />
</LinearLayout>
<TextView
android:id="@+id/textViewChat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/editTextMessage"
android:textSize="24sp"
android:scrollbars="vertical" />
</RelativeLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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="wrap_content"
android:paddingVertical="10dp"
android:paddingLeft="15dp"
android:paddingRight="60dp"
android:clipToPadding="false">

<TextView
android:id="@+id/name"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:text="LLaMA"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message_text"
android:layout_below="@+id/name"
android:layout_alignLeft="@+id/name"
android:background="@drawable/received_message"
android:paddingVertical="12dp"
android:paddingHorizontal="16dp"
android:elevation="2dp"
android:textSize="18dp"
android:text="Generated text"
/>
</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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="wrap_content"
android:paddingVertical="10dp"
android:paddingRight="15dp"
android:paddingLeft="60dp"
android:clipToPadding="false">

<TextView
android:id="@+id/name"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:layout_alignParentRight="true"
android:text="Prompt"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message_text"
android:layout_below="@+id/name"
android:layout_alignRight="@+id/name"
android:background="@drawable/sent_message"
android:textColor="#fff"
android:padding="10dp"
android:elevation="2dp"
android:textSize="18dp"
android:layout_alignParentRight="true"
android:text="My prompt"
/>

</RelativeLayout>

0 comments on commit 5916639

Please sign in to comment.