Skip to content

Commit 18ff82f

Browse files
authored
[LlamaDemo] Support more tokenizer suffix (#10664)
1 parent 4cc6532 commit 18ff82f

File tree

1 file changed

+11
-9
lines changed
  • examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo

1 file changed

+11
-9
lines changed

examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/SettingsActivity.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.gson.Gson;
2828
import java.io.File;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
3031
import java.util.List;
3132

3233
public class SettingsActivity extends AppCompatActivity {
@@ -324,7 +325,7 @@ private void setupBackendSelectorDialog() {
324325
}
325326

326327
private void setupModelSelectorDialog() {
327-
String[] pteFiles = listLocalFile("/data/local/tmp/llama/", ".pte");
328+
String[] pteFiles = listLocalFile("/data/local/tmp/llama/", new String[] {".pte"});
328329
AlertDialog.Builder modelPathBuilder = new AlertDialog.Builder(this);
329330
modelPathBuilder.setTitle("Select model path");
330331

@@ -341,13 +342,17 @@ private void setupModelSelectorDialog() {
341342
modelPathBuilder.create().show();
342343
}
343344

344-
private static String[] listLocalFile(String path, String suffix) {
345+
private static boolean fileHasExtension(String file, String[] suffix) {
346+
return Arrays.stream(suffix).anyMatch(entry -> file.endsWith(entry));
347+
}
348+
349+
private static String[] listLocalFile(String path, String[] suffix) {
345350
File directory = new File(path);
346351
if (directory.exists() && directory.isDirectory()) {
347-
File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(suffix));
352+
File[] files = directory.listFiles((dir, name) -> (fileHasExtension(name, suffix)));
348353
String[] result = new String[files.length];
349354
for (int i = 0; i < files.length; i++) {
350-
if (files[i].isFile() && files[i].getName().endsWith(suffix)) {
355+
if (files[i].isFile() && fileHasExtension(files[i].getName(), suffix)) {
351356
result[i] = files[i].getAbsolutePath();
352357
}
353358
}
@@ -380,11 +385,8 @@ private void setupModelTypeSelectorDialog() {
380385
}
381386

382387
private void setupTokenizerSelectorDialog() {
383-
String[] binFiles = listLocalFile("/data/local/tmp/llama/", ".bin");
384-
String[] modelFiles = listLocalFile("/data/local/tmp/llama/", ".model");
385-
String[] tokenizerFiles = new String[binFiles.length + modelFiles.length];
386-
System.arraycopy(binFiles, 0, tokenizerFiles, 0, binFiles.length);
387-
System.arraycopy(modelFiles, 0, tokenizerFiles, binFiles.length, modelFiles.length);
388+
String[] tokenizerFiles =
389+
listLocalFile("/data/local/tmp/llama/", new String[] {".bin", ".json", ".model"});
388390
AlertDialog.Builder tokenizerPathBuilder = new AlertDialog.Builder(this);
389391
tokenizerPathBuilder.setTitle("Select tokenizer path");
390392
tokenizerPathBuilder.setSingleChoiceItems(

0 commit comments

Comments
 (0)