-
Notifications
You must be signed in to change notification settings - Fork 397
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4eae3f5
Update phi-3 Android app layout and add logs
vraspar 4df9ab2
Update Android app layout and add settings functionality
vraspar d276879
Update logging in MainActivity
vraspar 86c84ff
Log prompt processing time and add screenshot
vraspar f67f7e3
add popup to display token generation rates and apply feedback
vraspar 14ebe59
add link to gen ai reference docs
vraspar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
mobile/examples/phi-3/android/app/src/main/java/ai/onnxruntime/genai/demo/BottomSheet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
||
|
|
||
| private static boolean fileExists(Context context, String fileName) { | ||
| File file = new File(context.getFilesDir(), fileName); | ||
|
|
@@ -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( | ||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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. | ||
|
|
@@ -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)); | ||
vraspar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| catch (GenAIException e) { | ||
| Log.e(TAG, "Exception occurred during model query: " + e.getMessage()); | ||
|
|
@@ -146,6 +179,7 @@ public void run() { | |
|
|
||
| runOnUiThread(() -> { | ||
| sendMsgIB.setEnabled(true); | ||
| sendMsgIB.setAlpha(1.0f); | ||
| }); | ||
| } | ||
| }).start(); | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
mobile/examples/phi-3/android/app/src/main/res/drawable/rounded_corner2.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
mobile/examples/phi-3/android/app/src/main/res/layout/bottom_sheet.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.