From cf929d0016d9b60a92341cf4b409eeb59271fc62 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Wed, 6 Mar 2024 16:00:33 -0800 Subject: [PATCH] Always use local resource (#2284) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/2284 Differential Revision: D54609685 --- .../example/executorchdemo/MainActivity.java | 39 +------------------ 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchdemo/MainActivity.java b/examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchdemo/MainActivity.java index 1d28676769..22f8dac080 100644 --- a/examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchdemo/MainActivity.java +++ b/examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchdemo/MainActivity.java @@ -10,17 +10,11 @@ import android.app.Activity; import android.app.AlertDialog; -import android.content.Context; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ListView; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import org.pytorch.executorch.LlamaCallback; import org.pytorch.executorch.LlamaModule; @@ -34,25 +28,6 @@ public class MainActivity extends Activity implements Runnable, LlamaCallback { private LlamaModule mModule = null; private Message mResultMessage = null; - private static String assetFilePath(Context context, String assetName) throws IOException { - File file = new File(context.getFilesDir(), assetName); - if (file.exists() && file.length() > 0) { - return file.getAbsolutePath(); - } - - try (InputStream is = context.getAssets().open(assetName)) { - try (OutputStream os = new FileOutputStream(file)) { - byte[] buffer = new byte[4 * 1024]; - int read; - while ((read = is.read(buffer)) != -1) { - os.write(buffer, 0, read); - } - os.flush(); - } - return file.getAbsolutePath(); - } - } - @Override public void onResult(String result) { System.out.println("onResult: " + result); @@ -60,16 +35,6 @@ public void onResult(String result) { run(); } - private void setModel(String modelPath, String tokenizerPath) { - try { - String model = MainActivity.assetFilePath(getApplicationContext(), modelPath); - String tokenizer = MainActivity.assetFilePath(getApplicationContext(), tokenizerPath); - mModule = new LlamaModule(model, tokenizer, 0.8f); - } catch (IOException e) { - finish(); - } - } - private void setLocalModel(String modelPath, String tokenizerPath) { mModule = new LlamaModule(modelPath, tokenizerPath, 0.8f); } @@ -84,7 +49,7 @@ private void modelDialog() { public void onClick(android.content.DialogInterface dialog, int item) { switch (item) { case 0: - setModel("stories110M.pte", "tokenizer.bin"); + setLocalModel("/data/local/tmp/stories110M.pte", "/data/local/tmp/tokenizer.bin"); break; case 1: setLocalModel("/data/local/tmp/language.pte", "/data/local/tmp/language.bin"); @@ -141,7 +106,7 @@ public void run() { modelDialog(); }); - setModel("stories110M.pte", "tokenizer.bin"); + setLocalModel("/data/local/tmp/stories110M.pte", "/data/local/tmp/tokenizer.bin"); } @Override