Skip to content

Commit

Permalink
Update phi-3 Android app layout and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
vraspar committed Sep 23, 2024
1 parent f41e1ed commit 4eae3f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ protected void onCreate(Bundle savedInstanceState) {
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

sendMsgIB = findViewById(R.id.idIBSend);
userMsgEdt = findViewById(R.id.idEdtMessage);
generatedTV = findViewById(R.id.sample_text);
promptTV = findViewById(R.id.user_text);
progressText = findViewById(R.id.progress_text);

// Trigger the download operation when the application is created
try {
downloadModels(
Expand All @@ -63,10 +69,6 @@ protected void onCreate(Bundle savedInstanceState) {
throw new RuntimeException(e);
}

sendMsgIB = findViewById(R.id.idIBSend);
userMsgEdt = findViewById(R.id.idEdtMessage);
generatedTV = findViewById(R.id.sample_text);
promptTV = findViewById(R.id.user_text);

Consumer<String> tokenListener = this;

Expand Down Expand Up @@ -99,6 +101,7 @@ public void onClick(View v) {

// Disable send button while responding to prompt.
sendMsgIB.setEnabled(false);
sendMsgIB.setAlpha(0.5f);

promptTV.setText(promptQuestion);
// Clear Edit Text or prompt question.
Expand All @@ -125,14 +128,22 @@ public void run() {

generator = new Generator(model, generatorParams);

// try to measure average time taken to generate each token.
long startTime = System.currentTimeMillis();
int numTokens = 0;
while (!generator.isDone()) {
generator.computeLogits();
generator.generateNextToken();

int token = generator.getLastTokenInSequence(0);

tokenListener.accept(stream.decode(token));
Log.i(TAG, "Generated token: " + token + ": " + stream.decode(token));
numTokens++;
}
long totalTime = System.currentTimeMillis() - startTime;
Log.i(TAG, "Total time taken to generate + " + numTokens + "tokens: " + totalTime);
Log.i(TAG, "Average time taken to generate each token: " + totalTime / numTokens);
}
catch (GenAIException e) {
Log.e(TAG, "Exception occurred during model query: " + e.getMessage());
Expand All @@ -146,6 +157,7 @@ public void run() {

runOnUiThread(() -> {
sendMsgIB.setEnabled(true);
sendMsgIB.setAlpha(1.0f);
});
}
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="UseAppTint" />

<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="invisible" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 4eae3f5

Please sign in to comment.