-
Notifications
You must be signed in to change notification settings - Fork 368
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
Changes from 4 commits
4eae3f5
4df9ab2
d276879
86c84ff
f67f7e3
14ebe59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ai.onnxruntime.genai.demo; | ||
vraspar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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,39 @@ 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 firstTokenTime = startTime; | ||
long currentTime = startTime; | ||
int numTokens = 0; | ||
while (!generator.isDone()) { | ||
generator.computeLogits(); | ||
generator.generateNextToken(); | ||
|
||
int token = generator.getLastTokenInSequence(0); | ||
|
||
tokenListener.accept(stream.decode(token)); | ||
|
||
if (numTokens == 0) { //first token | ||
firstTokenTime = System.currentTimeMillis(); | ||
} | ||
vraspar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() - firstTokenTime; | ||
|
||
Log.i(TAG, "Prompt processing time (first token): " + (firstTokenTime - startTime)/ 1000.0 + " seconds"); | ||
Log.i(TAG, "Tokens generated per second (excluding prompt processing): " + 1000 * ((numTokens -1) / totalTime)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be much work to add a setting for 'display timing' and if true we add this info at the end of the response that is displayed to the user? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added small pop up that shows the rates after prompt generation is done |
||
} | ||
catch (GenAIException e) { | ||
Log.e(TAG, "Exception occurred during model query: " + e.getMessage()); | ||
|
@@ -146,6 +184,7 @@ public void run() { | |
|
||
runOnUiThread(() -> { | ||
sendMsgIB.setEnabled(true); | ||
sendMsgIB.setAlpha(1.0f); | ||
}); | ||
} | ||
}).start(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
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:layout_gravity="center_horizontal" | ||
android:layout_marginTop="24dp" | ||
android:background="#89CC04" | ||
android:backgroundTint="#89CC04" | ||
android:tint="@color/white" | ||
android:padding="8dp" | ||
android:text="Apply" | ||
android:textAppearance="@style/TextAppearance.AppCompat.Body1" | ||
android:visibility="visible" | ||
tools:visibility="visible" /> | ||
</LinearLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add info on the new configuration params and a link to the genai doco for them? 'length penalty' is somewhat obtuse.