Skip to content

Phi 3 Android app Update #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ai.onnxruntime.genai.demo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class BottomSheet extends BottomSheetDialogFragment {
private EditText maxLengthEditText;
private EditText lengthPenaltyEditText;
private SettingsListener settingsListener;

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

public void setSettingsListener(SettingsListener listener) {
this.settingsListener = listener;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_sheet, container, false);

maxLengthEditText = view.findViewById(R.id.idEdtMaxLength);
lengthPenaltyEditText = view.findViewById(R.id.idEdtLengthPenalty);

Button applyButton = view.findViewById(R.id.applySettingsButton);

applyButton.setOnClickListener(v -> {
if (settingsListener != null) {
int maxLength = Integer.parseInt(maxLengthEditText.getText().toString());
int lengthPenalty = Integer.parseInt(lengthPenaltyEditText.getText().toString());
settingsListener.onSettingsApplied(maxLength, lengthPenalty);
dismiss();
}
});

return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public class MainActivity extends AppCompatActivity implements Consumer<String>
private TextView generatedTV;
private TextView promptTV;
private TextView progressText;
private ImageButton settingsButton;
private static final String TAG = "genai.demo.MainActivity";
private int maxLength = 100;
private int lengthPenalty = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this doco the default for length penalty is 1, with values < 1 encouraging shorter responses and > 1 longer. Is that doco out of date? Or if not, should this be a float and start at 1.0?


private static boolean fileExists(Context context, String fileName) {
File file = new File(context.getFilesDir(), fileName);
Expand All @@ -55,6 +58,13 @@ 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);
settingsButton = findViewById(R.id.idIBSettings);

// Trigger the download operation when the application is created
try {
downloadModels(
Expand All @@ -63,10 +73,20 @@ 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);
settingsButton.setOnClickListener(v -> {
BottomSheet bottomSheet = new BottomSheet();
bottomSheet.setSettingsListener(new BottomSheet.SettingsListener() {
@Override
public void onSettingsApplied(int maxLength, int lengthPenalty) {
MainActivity.this.maxLength = maxLength;
MainActivity.this.lengthPenalty = lengthPenalty;
Log.i(TAG, "Setting max response length to: " + maxLength);
Log.i(TAG, "Setting length penalty to: " + lengthPenalty);
}
});
bottomSheet.show(getSupportFragmentManager(), "BottomSheet");
});


Consumer<String> tokenListener = this;

Expand Down Expand Up @@ -99,6 +119,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 @@ -117,22 +138,34 @@ public void run() {

generatorParams = model.createGeneratorParams();
//examples for optional parameters to format AI response
//generatorParams.setSearchOption("length_penalty", 1000);
//generatorParams.setSearchOption("max_length", 500);
generatorParams.setSearchOption("length_penalty", lengthPenalty);
generatorParams.setSearchOption("max_length", maxLength);

encodedPrompt = tokenizer.encode(promptQuestion_formatted);
generatorParams.setInput(encodedPrompt);

generator = new Generator(model, generatorParams);

// try to measure average time taken to generate each token.
long startTime = System.currentTimeMillis();
long currentTime = startTime;
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));
Log.i(TAG, "Time taken to generate token: " + (System.currentTimeMillis() - currentTime)/ 1000.0 + " seconds");
currentTime = System.currentTimeMillis();
numTokens++;
}
long totalTime = System.currentTimeMillis() - startTime;
Log.i(TAG, "Total time taken to generate + " + numTokens + "tokens: " + totalTime);
Log.i(TAG, "Tokens generated per second: " + 1000 * (numTokens / totalTime));
}
catch (GenAIException e) {
Log.e(TAG, "Exception occurred during model query: " + e.getMessage());
Expand All @@ -146,6 +179,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
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#828380" />
<solid android:color="#03A9F4" />

<padding
android:left="1dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

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


<TextView
android:visibility="invisible"
android:id="@+id/user_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand All @@ -22,39 +22,42 @@
android:textAlignment="textEnd"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="16sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.855"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
app:layout_constraintVertical_bias="0.0"
tools:visibility="visible" />

<TextView
android:visibility="invisible"
android:id="@+id/sample_text"

android:background="@drawable/rounded_corner2"
android:layout_width="0dp"
android:layout_height="0dp"

android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="64dp"
android:layout_marginBottom="16dp"
android:background="@drawable/rounded_corner2"
android:padding="8dp"
android:scrollbars="vertical"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="16sp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/idEdtMessage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.144"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_text"
app:layout_constraintVertical_bias="0.0" />
app:layout_constraintVertical_bias="0.0"
tools:visibility="visible" />

<EditText
android:id="@+id/idEdtMessage"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_width="249dp"
android:layout_height="63dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_weight="4"
Expand All @@ -70,8 +73,8 @@
android:layout_width="66dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="68dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:adjustViewBounds="false"
android:background="#89CC04"
Expand All @@ -83,4 +86,31 @@
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="UseAppTint" />

<ImageButton
android:id="@+id/idIBSettings"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="12dp"
android:src="@android:drawable/ic_menu_preferences"
android:background="#00BCD4"
android:backgroundTint="#009688"
android:tint="@color/white"
android:hapticFeedbackEnabled="true"
app:layout_constraintBottom_toBottomOf="parent"
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<!-- Max Response Length -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Response Length"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_marginBottom="8dp"/>

<EditText
android:id="@+id/idEdtMaxLength"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter max response length"
android:text="100"
android:padding="8dp" />

<!-- Length Penalty -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Length Penalty"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"/>

<EditText
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"/>

<!-- Save Button -->
<Button
android:id="@+id/applySettingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apply"
android:textColor="@android:color/white"
android:padding="8dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="24dp"/>
</LinearLayout>
Loading