Skip to content

Commit 4eae3f5

Browse files
committed
Update phi-3 Android app layout and add logs
1 parent f41e1ed commit 4eae3f5

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

mobile/examples/phi-3/android/app/src/main/java/ai/onnxruntime/genai/demo/MainActivity.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ protected void onCreate(Bundle savedInstanceState) {
5555
binding = ActivityMainBinding.inflate(getLayoutInflater());
5656
setContentView(binding.getRoot());
5757

58+
sendMsgIB = findViewById(R.id.idIBSend);
59+
userMsgEdt = findViewById(R.id.idEdtMessage);
60+
generatedTV = findViewById(R.id.sample_text);
61+
promptTV = findViewById(R.id.user_text);
62+
progressText = findViewById(R.id.progress_text);
63+
5864
// Trigger the download operation when the application is created
5965
try {
6066
downloadModels(
@@ -63,10 +69,6 @@ protected void onCreate(Bundle savedInstanceState) {
6369
throw new RuntimeException(e);
6470
}
6571

66-
sendMsgIB = findViewById(R.id.idIBSend);
67-
userMsgEdt = findViewById(R.id.idEdtMessage);
68-
generatedTV = findViewById(R.id.sample_text);
69-
promptTV = findViewById(R.id.user_text);
7072

7173
Consumer<String> tokenListener = this;
7274

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

100102
// Disable send button while responding to prompt.
101103
sendMsgIB.setEnabled(false);
104+
sendMsgIB.setAlpha(0.5f);
102105

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

126129
generator = new Generator(model, generatorParams);
127130

131+
// try to measure average time taken to generate each token.
132+
long startTime = System.currentTimeMillis();
133+
int numTokens = 0;
128134
while (!generator.isDone()) {
129135
generator.computeLogits();
130136
generator.generateNextToken();
131137

132138
int token = generator.getLastTokenInSequence(0);
133139

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

147158
runOnUiThread(() -> {
148159
sendMsgIB.setEnabled(true);
160+
sendMsgIB.setAlpha(1.0f);
149161
});
150162
}
151163
}).start();

mobile/examples/phi-3/android/app/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,16 @@
8383
app:layout_constraintEnd_toEndOf="parent"
8484
tools:ignore="UseAppTint" />
8585

86+
<TextView
87+
android:id="@+id/progress_text"
88+
android:layout_width="wrap_content"
89+
android:layout_height="wrap_content"
90+
android:text="TextView"
91+
android:visibility="invisible"
92+
app:layout_constraintBottom_toBottomOf="parent"
93+
app:layout_constraintEnd_toEndOf="parent"
94+
app:layout_constraintStart_toStartOf="parent"
95+
app:layout_constraintTop_toTopOf="parent"
96+
tools:visibility="invisible" />
97+
8698
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)