Skip to content

Commit

Permalink
add popup to display token generation rates and apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
vraspar committed Sep 27, 2024
1 parent 86c84ff commit f67f7e3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BottomSheet extends BottomSheetDialogFragment {
private SettingsListener settingsListener;

public interface SettingsListener {
void onSettingsApplied(int maxLength, int lengthPenalty);
void onSettingsApplied(int maxLength, float lengthPenalty);
}

public void setSettingsListener(SettingsListener listener) {
Expand All @@ -36,7 +36,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
applyButton.setOnClickListener(v -> {
if (settingsListener != null) {
int maxLength = Integer.parseInt(maxLengthEditText.getText().toString());
int lengthPenalty = Integer.parseInt(lengthPenaltyEditText.getText().toString());
float lengthPenalty = Float.parseFloat(lengthPenaltyEditText.getText().toString());
settingsListener.onSettingsApplied(maxLength, lengthPenalty);
dismiss();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
Expand Down Expand Up @@ -44,7 +46,7 @@ public class MainActivity extends AppCompatActivity implements Consumer<String>
private ImageButton settingsButton;
private static final String TAG = "genai.demo.MainActivity";
private int maxLength = 100;
private int lengthPenalty = 1000;
private float lengthPenalty = 1.0f;

private static boolean fileExists(Context context, String fileName) {
File file = new File(context.getFilesDir(), fileName);
Expand Down Expand Up @@ -77,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.setSettingsListener(new BottomSheet.SettingsListener() {
@Override
public void onSettingsApplied(int maxLength, int lengthPenalty) {
public void onSettingsApplied(int maxLength, float lengthPenalty) {
MainActivity.this.maxLength = maxLength;
MainActivity.this.lengthPenalty = lengthPenalty;
Log.i(TAG, "Setting max response length to: " + maxLength);
Expand Down Expand Up @@ -156,21 +158,34 @@ public void run() {
generator.generateNextToken();

int token = generator.getLastTokenInSequence(0);

tokenListener.accept(stream.decode(token));

if (numTokens == 0) { //first token
firstTokenTime = System.currentTimeMillis();
}

tokenListener.accept(stream.decode(token));


Log.i(TAG, "Generated token: " + token + ": " + stream.decode(token));
Log.i(TAG, "Time taken to generate token: " + (System.currentTimeMillis() - currentTime)/ 1000.0 + " seconds");
currentTime = System.currentTimeMillis();
numTokens++;
}
long totalTime = System.currentTimeMillis() - firstTokenTime;

Log.i(TAG, "Prompt processing time (first token): " + (firstTokenTime - startTime)/ 1000.0 + " seconds");
Log.i(TAG, "Tokens generated per second (excluding prompt processing): " + 1000 * ((numTokens -1) / totalTime));
float promptProcessingTime = (firstTokenTime - startTime)/ 1000.0f;
float tokensPerSecond = (1000 * (numTokens -1)) / totalTime;

runOnUiThread(() -> {
sendMsgIB.setEnabled(true);
sendMsgIB.setAlpha(1.0f);

// Display the token generation rate in a dialog popup
showTokenPopup(promptProcessingTime, tokensPerSecond);
});

Log.i(TAG, "Prompt processing time (first token): " + promptProcessingTime + " seconds");
Log.i(TAG, "Tokens generated per second (excluding prompt processing): " + tokensPerSecond);
}
catch (GenAIException e) {
Log.e(TAG, "Exception occurred during model query: " + e.getMessage());
Expand Down Expand Up @@ -295,4 +310,23 @@ public void setVisibility() {
TextView botView = (TextView) findViewById(R.id.sample_text);
botView.setVisibility(View.VISIBLE);
}

private void showTokenPopup(float promptProcessingTime, float tokenRate) {

final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.info_popup);

TextView promptProcessingTimeTv = dialog.findViewById(R.id.prompt_processing_time_tv);
TextView tokensPerSecondTv = dialog.findViewById(R.id.tokens_per_second_tv);
Button closeBtn = dialog.findViewById(R.id.close_btn);

promptProcessingTimeTv.setText(String.format("Prompt processing time: %.2f seconds", promptProcessingTime));
tokensPerSecondTv.setText(String.format("Tokens per second: %.2f", tokenRate));

closeBtn.setOnClickListener(v -> dialog.dismiss());

dialog.show();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
android:id="@+id/idEdtLengthPenalty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter length penalty"
android:text="1000"
android:padding="8dp"/>
android:inputType="number|numberDecimal"
android:padding="8dp"
android:text="1.0" />

<!-- Save Button -->
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/prompt_processing_time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prompt processing time: "
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/tokens_per_second_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tokens per second: "
android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
android:id="@+id/close_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_gravity="center" />
</LinearLayout>

0 comments on commit f67f7e3

Please sign in to comment.