Skip to content

Commit 356556a

Browse files
committed
Refactor model configuration in MainActivity and update README
1 parent 3b7b379 commit 356556a

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

mobile/examples/phi-3/android/README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1-
# Local Chatbot on Android with Phi-3, ONNX Runtime Mobile and ONNX Runtime Generate() API
1+
# Local Chatbot on Android with ONNX Runtime Mobile and ONNX Runtime Generate() API
22

33
## Overview
44

5-
This is a basic [Phi-3](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct) Android example application with [ONNX Runtime mobile](https://onnxruntime.ai/docs/tutorials/mobile/) and [ONNX Runtime Generate() API](https://github.com/microsoft/onnxruntime-genai) with support for efficiently running generative AI models. This app demonstrates the usage of phi-3 model in a simple question answering chatbot mode.
5+
This is a flexible Android chatbot application with [ONNX Runtime mobile](https://onnxruntime.ai/docs/tutorials/mobile/) and [ONNX Runtime Generate() API](https://github.com/microsoft/onnxruntime-genai) that supports efficiently running generative AI models. While it uses [Phi-3](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct) by default, **it can work with any ONNX Runtime GenAI compatible model** by simply updating the model configuration in the code.
66

77
### Model
8-
The model used here is [ONNX Phi-3 model on HuggingFace](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/tree/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4) with INT4 quantization and optimizations for mobile usage.
9-
10-
You can also optimize your fine-tuned PyTorch Phi-3 model for mobile usage following this example [Phi3 optimization with Olive](https://github.com/microsoft/Olive/tree/main/examples/phi3).
8+
By default, this app uses the [ONNX Phi-3 model on HuggingFace](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/tree/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4) with INT4 quantization and optimizations for mobile usage.
9+
10+
### Using Different Models
11+
**The app is designed to work with any ONNX Runtime GenAI compatible model.** To use a different model:
12+
13+
1. Open `MainActivity.java` in Android Studio
14+
2. Locate the model configuration section at the top of the class (marked with comments)
15+
3. Update the `MODEL_BASE_URL` to point to your model's download location
16+
4. Update the `MODEL_FILES` list to include all required files for your model
17+
18+
Example for a different model:
19+
```java
20+
// Base URL for downloading model files (ensure it ends with '/')
21+
private static final String MODEL_BASE_URL = "https://your-model-host.com/path/to/model/";
22+
23+
// List of required model files to download
24+
private static final List<String> MODEL_FILES = Arrays.asList(
25+
"config.json",
26+
"genai_config.json",
27+
"your-model.onnx",
28+
"your-model.onnx.data",
29+
"tokenizer.json",
30+
"tokenizer_config.json"
31+
// Add other required files...
32+
);
33+
```
34+
35+
**Note:** The model files will be downloaded to `/data/data/ai.onnxruntime.genai.demo/files` on the Android device.
1136

1237
### Requirements
1338
- Android Studio Giraffe | 2022.3.1 or later (installed on Mac/Windows/Linux)
@@ -30,7 +55,7 @@ The current set up supports downloading Phi-3-mini model directly from Huggingfa
3055
You can also follow this link to download **Phi-3-mini**: https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/tree/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4
3156
and manually copy to the android device file directory following the below instructions:
3257

33-
#### Steps for manual copying models to android device directory:
58+
#### Steps for manual copying model files to android device directory:
3459
From Android Studio:
3560
- create (if necessary) and run your emulator/device
3661
- make sure it has at least 8GB of internal storage
@@ -40,7 +65,8 @@ From Android Studio:
4065
- Open Device Explorer in Android Studio
4166
- Navigate to `/data/data/ai.onnxruntime.genai.demo/files`
4267
- adjust as needed if the value returned by getFilesDir() differs for your emulator or device
43-
- copy the whole [phi-3](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/tree/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4) model folder to the `files` directory
68+
- copy all the required model files (as specified in `MODEL_FILES` in MainActivity.java) directly to the `files` directory
69+
- For the default Phi-3 model, copy files from [here](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/tree/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4)
4470

4571
### Step 3: Connect Android Device and Run the app
4672
Connect your Android Device to your computer or select the Android Emulator in Android Studio Device manager.

mobile/examples/phi-3/android/app/src/main/java/ai/onnxruntime/genai/demo/MainActivity.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@
3030

3131
public class MainActivity extends AppCompatActivity implements Consumer<String> {
3232

33+
// ===== MODEL CONFIGURATION - MODIFY THESE FOR DIFFERENT MODELS =====
34+
// Base URL for downloading model files (ensure it ends with '/')
35+
private static final String MODEL_BASE_URL = "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/resolve/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/";
36+
37+
// List of required model files to download
38+
private static final List<String> MODEL_FILES = Arrays.asList(
39+
"added_tokens.json",
40+
"config.json",
41+
"configuration_phi3.py",
42+
"genai_config.json",
43+
"phi3-mini-4k-instruct-cpu-int4-rtn-block-32-acc-level-4.onnx",
44+
"phi3-mini-4k-instruct-cpu-int4-rtn-block-32-acc-level-4.onnx.data",
45+
"special_tokens_map.json",
46+
"tokenizer.json",
47+
"tokenizer.model",
48+
"tokenizer_config.json"
49+
);
50+
// ===== END MODEL CONFIGURATION =====
51+
3352
private EditText userMsgEdt;
3453
private SimpleGenAI genAI;
3554
private ImageButton sendMsgIB;
@@ -188,24 +207,11 @@ protected void onDestroy() {
188207

189208
private void downloadModels(Context context) throws GenAIException {
190209

191-
final String baseUrl = "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/resolve/main/cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/";
192-
List<String> files = Arrays.asList(
193-
"added_tokens.json",
194-
"config.json",
195-
"configuration_phi3.py",
196-
"genai_config.json",
197-
"phi3-mini-4k-instruct-cpu-int4-rtn-block-32-acc-level-4.onnx",
198-
"phi3-mini-4k-instruct-cpu-int4-rtn-block-32-acc-level-4.onnx.data",
199-
"special_tokens_map.json",
200-
"tokenizer.json",
201-
"tokenizer.model",
202-
"tokenizer_config.json");
203-
204210
List<Pair<String, String>> urlFilePairs = new ArrayList<>();
205-
for (String file : files) {
211+
for (String file : MODEL_FILES) {
206212
if (!fileExists(context, file)) {
207213
urlFilePairs.add(new Pair<>(
208-
baseUrl + file,
214+
MODEL_BASE_URL + file,
209215
file));
210216
}
211217
}

0 commit comments

Comments
 (0)