Skip to content

Commit 4df9ab2

Browse files
committed
Update Android app layout and add settings functionality
1 parent 4eae3f5 commit 4df9ab2

File tree

5 files changed

+157
-15
lines changed

5 files changed

+157
-15
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ai.onnxruntime.genai.demo;
2+
3+
import android.os.Bundle;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.Button;
8+
import android.widget.EditText;
9+
import androidx.annotation.NonNull;
10+
import androidx.annotation.Nullable;
11+
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
12+
13+
public class BottomSheet extends BottomSheetDialogFragment {
14+
private EditText maxLengthEditText;
15+
private EditText lengthPenaltyEditText;
16+
private SettingsListener settingsListener;
17+
18+
public interface SettingsListener {
19+
void onSettingsApplied(int maxLength, int lengthPenalty);
20+
}
21+
22+
public void setSettingsListener(SettingsListener listener) {
23+
this.settingsListener = listener;
24+
}
25+
26+
@Nullable
27+
@Override
28+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
29+
View view = inflater.inflate(R.layout.bottom_sheet, container, false);
30+
31+
maxLengthEditText = view.findViewById(R.id.idEdtMaxLength);
32+
lengthPenaltyEditText = view.findViewById(R.id.idEdtLengthPenalty);
33+
34+
Button applyButton = view.findViewById(R.id.applySettingsButton);
35+
36+
applyButton.setOnClickListener(v -> {
37+
if (settingsListener != null) {
38+
int maxLength = Integer.parseInt(maxLengthEditText.getText().toString());
39+
int lengthPenalty = Integer.parseInt(lengthPenaltyEditText.getText().toString());
40+
settingsListener.onSettingsApplied(maxLength, lengthPenalty);
41+
dismiss();
42+
}
43+
});
44+
45+
return view;
46+
}
47+
}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ public class MainActivity extends AppCompatActivity implements Consumer<String>
4141
private TextView generatedTV;
4242
private TextView promptTV;
4343
private TextView progressText;
44+
private ImageButton settingsButton;
4445
private static final String TAG = "genai.demo.MainActivity";
46+
private int maxLength = 100;
47+
private int lengthPenalty = 1000;
4548

4649
private static boolean fileExists(Context context, String fileName) {
4750
File file = new File(context.getFilesDir(), fileName);
@@ -60,6 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
6063
generatedTV = findViewById(R.id.sample_text);
6164
promptTV = findViewById(R.id.user_text);
6265
progressText = findViewById(R.id.progress_text);
66+
settingsButton = findViewById(R.id.idIBSettings);
6367

6468
// Trigger the download operation when the application is created
6569
try {
@@ -69,6 +73,20 @@ protected void onCreate(Bundle savedInstanceState) {
6973
throw new RuntimeException(e);
7074
}
7175

76+
settingsButton.setOnClickListener(v -> {
77+
BottomSheet bottomSheet = new BottomSheet();
78+
bottomSheet.setSettingsListener(new BottomSheet.SettingsListener() {
79+
@Override
80+
public void onSettingsApplied(int maxLength, int lengthPenalty) {
81+
MainActivity.this.maxLength = maxLength;
82+
MainActivity.this.lengthPenalty = lengthPenalty;
83+
Log.i(TAG, "Max Response length: " + maxLength);
84+
Log.i(TAG, "Length penalty: " + lengthPenalty);
85+
}
86+
});
87+
bottomSheet.show(getSupportFragmentManager(), "BottomSheet");
88+
});
89+
7290

7391
Consumer<String> tokenListener = this;
7492

@@ -120,8 +138,12 @@ public void run() {
120138

121139
generatorParams = model.createGeneratorParams();
122140
//examples for optional parameters to format AI response
123-
//generatorParams.setSearchOption("length_penalty", 1000);
124-
//generatorParams.setSearchOption("max_length", 500);
141+
generatorParams.setSearchOption("length_penalty", lengthPenalty);
142+
generatorParams.setSearchOption("max_length", maxLength);
143+
144+
Log.i(TAG, "Length penalty: " + lengthPenalty);
145+
Log.i(TAG, "Max Response length: " + maxLength);
146+
125147

126148
encodedPrompt = tokenizer.encode(promptQuestion_formatted);
127149
generatorParams.setInput(encodedPrompt);
@@ -143,7 +165,7 @@ public void run() {
143165
}
144166
long totalTime = System.currentTimeMillis() - startTime;
145167
Log.i(TAG, "Total time taken to generate + " + numTokens + "tokens: " + totalTime);
146-
Log.i(TAG, "Average time taken to generate each token: " + totalTime / numTokens);
168+
Log.i(TAG, "Tokens generated per second: " + 1000 * (numTokens / totalTime));
147169
}
148170
catch (GenAIException e) {
149171
Log.e(TAG, "Exception occurred during model query: " + e.getMessage());

mobile/examples/phi-3/android/app/src/main/res/drawable/rounded_corner2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
33

4-
<solid android:color="#828380" />
4+
<solid android:color="#03A9F4" />
55

66
<padding
77
android:left="1dp"

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
<!--recycler view to display our chats-->
1111

12+
1213
<TextView
13-
android:visibility="invisible"
1414
android:id="@+id/user_text"
1515
android:layout_width="0dp"
1616
android:layout_height="wrap_content"
@@ -22,39 +22,42 @@
2222
android:textAlignment="textEnd"
2323
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
2424
android:textSize="16sp"
25+
android:visibility="invisible"
2526
app:layout_constraintBottom_toBottomOf="parent"
2627
app:layout_constraintEnd_toEndOf="parent"
2728
app:layout_constraintHorizontal_bias="0.855"
2829
app:layout_constraintStart_toStartOf="parent"
2930
app:layout_constraintTop_toTopOf="parent"
30-
app:layout_constraintVertical_bias="0.0" />
31+
app:layout_constraintVertical_bias="0.0"
32+
tools:visibility="visible" />
3133

3234
<TextView
33-
android:visibility="invisible"
3435
android:id="@+id/sample_text"
35-
36-
android:background="@drawable/rounded_corner2"
3736
android:layout_width="0dp"
38-
android:layout_height="0dp"
37+
38+
android:layout_height="wrap_content"
3939
android:layout_marginStart="32dp"
4040
android:layout_marginTop="16dp"
4141
android:layout_marginEnd="64dp"
4242
android:layout_marginBottom="16dp"
43+
android:background="@drawable/rounded_corner2"
4344
android:padding="8dp"
4445
android:scrollbars="vertical"
4546
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
4647
android:textSize="16sp"
48+
android:visibility="invisible"
4749
app:layout_constraintBottom_toTopOf="@+id/idEdtMessage"
4850
app:layout_constraintEnd_toEndOf="parent"
4951
app:layout_constraintHorizontal_bias="0.144"
5052
app:layout_constraintStart_toStartOf="parent"
5153
app:layout_constraintTop_toBottomOf="@+id/user_text"
52-
app:layout_constraintVertical_bias="0.0" />
54+
app:layout_constraintVertical_bias="0.0"
55+
tools:visibility="visible" />
5356

5457
<EditText
5558
android:id="@+id/idEdtMessage"
56-
android:layout_width="300dp"
57-
android:layout_height="60dp"
59+
android:layout_width="249dp"
60+
android:layout_height="63dp"
5861
android:layout_marginStart="16dp"
5962
android:layout_marginBottom="16dp"
6063
android:layout_weight="4"
@@ -70,8 +73,8 @@
7073
android:layout_width="66dp"
7174
android:layout_height="60dp"
7275
android:layout_gravity="center_vertical"
73-
android:layout_marginEnd="16dp"
74-
android:layout_marginBottom="16dp"
76+
android:layout_marginEnd="68dp"
77+
android:layout_marginBottom="12dp"
7578
android:layout_weight="1"
7679
android:adjustViewBounds="false"
7780
android:background="#89CC04"
@@ -83,6 +86,21 @@
8386
app:layout_constraintEnd_toEndOf="parent"
8487
tools:ignore="UseAppTint" />
8588

89+
<ImageButton
90+
android:id="@+id/idIBSettings"
91+
android:layout_width="50dp"
92+
android:layout_height="60dp"
93+
android:layout_marginEnd="8dp"
94+
android:layout_marginBottom="12dp"
95+
android:src="@android:drawable/ic_menu_preferences"
96+
android:background="#00BCD4"
97+
android:backgroundTint="#009688"
98+
android:tint="@color/white"
99+
android:hapticFeedbackEnabled="true"
100+
app:layout_constraintBottom_toBottomOf="parent"
101+
app:layout_constraintEnd_toEndOf="parent"
102+
tools:ignore="UseAppTint" />
103+
86104
<TextView
87105
android:id="@+id/progress_text"
88106
android:layout_width="wrap_content"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="vertical"
6+
android:padding="16dp">
7+
8+
<!-- Max Response Length -->
9+
<TextView
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:text="Max Response Length"
13+
android:textSize="16sp"
14+
android:textColor="@color/black"
15+
android:layout_marginBottom="8dp"/>
16+
17+
<EditText
18+
android:id="@+id/idEdtMaxLength"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:inputType="number"
22+
android:hint="Enter max response length"
23+
android:text="100"
24+
android:padding="8dp" />
25+
26+
<!-- Length Penalty -->
27+
<TextView
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="Length Penalty"
31+
android:textSize="16sp"
32+
android:textColor="@color/black"
33+
android:layout_marginTop="16dp"
34+
android:layout_marginBottom="8dp"/>
35+
36+
<EditText
37+
android:id="@+id/idEdtLengthPenalty"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:inputType="number"
41+
android:hint="Enter length penalty"
42+
android:text="1000"
43+
android:padding="8dp"/>
44+
45+
<!-- Save Button -->
46+
<Button
47+
android:id="@+id/applySettingsButton"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:text="Apply"
51+
android:textColor="@android:color/white"
52+
android:padding="8dp"
53+
android:layout_gravity="center_horizontal"
54+
android:layout_marginTop="24dp"/>
55+
</LinearLayout>

0 commit comments

Comments
 (0)