|
| 1 | +# ONNX Runtime Mobile Phi-3 Android sample application with ORT GenAI library support |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This is a basic [Phi-3](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx) example application for [ONNX Runtime](https://github.com/microsoft/onnxruntime) on Android with [Ort-GenAI](https://github.com/microsoft/onnxruntime-genai) support for running generative AI models. This app demonstrates the usage of phi-3 model in a simple question answering chatbot mode. |
| 6 | + |
| 7 | +### Model |
| 8 | +The model used here is from the source: [HuggingFace](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx#phi-3-mini-4k-instruct-onnx-models) |
| 9 | +and optimized for int4 CPU and Mobile usage. |
| 10 | + |
| 11 | +### Requirements |
| 12 | +- Android Studio Giraffe | 2022.3.1 or later (installed on Mac/Windows/Linux) |
| 13 | +- Android SDK 29+ |
| 14 | +- Android NDK r22+ |
| 15 | +- An Android device or an Android Emulator |
| 16 | + |
| 17 | +## Build And Run |
| 18 | + |
| 19 | +### Step 1: Clone the ONNX runtime mobile examples source code |
| 20 | + |
| 21 | +Clone this repository to get the sample application. |
| 22 | + |
| 23 | +`[email protected]:microsoft/onnxruntime-inference-examples.git` |
| 24 | + |
| 25 | +### [Optional] Step 2: Prepare the model |
| 26 | + |
| 27 | +The current set up supports downloading Phi-3-mini model directly from Huggingface repo to the android device folder. However, it takes time since the model data is >2.5G. |
| 28 | + |
| 29 | +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 |
| 30 | +and manually copy to the android device file directory following the below instructions: |
| 31 | + |
| 32 | +#### Steps for manual copying models to android device directory: |
| 33 | +From Android Studio: |
| 34 | + - create (if necessary) and run your emulator/device |
| 35 | + - make sure it has at least 8GB of internal storage |
| 36 | + - debug/run the app so it's deployed to the device and creates it's `files` directory |
| 37 | + - expected to be `/data/data/ai.onnxruntime.genai.demo/files` |
| 38 | + - this is the path returned by `getFilesDir()` |
| 39 | + - Open Device Explorer in Android Studio |
| 40 | + - Navigate to `/data/data/ai.onnxruntime.genai.demo/files` |
| 41 | + - adjust as needed if the value returned by getFilesDir() differs for your emulator or device |
| 42 | + - 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 |
| 43 | + |
| 44 | +### Step 3: Connect Android Device and Run the app |
| 45 | + Connect your Android Device to your computer or select the Android Emulator in Android Studio Device manager. |
| 46 | + |
| 47 | + Then select `Run -> Run app` and this will prompt the app to be built and installed on your device or emulator. |
| 48 | + |
| 49 | + Now you can try giving some sample prompt questions and test the chatbot android app by clicking the ">" action button. |
| 50 | + |
| 51 | +# |
| 52 | +Here are some sample example screenshots of the app. |
| 53 | + |
| 54 | +<img width=20% src="images/Local_LLM_1.jpg" alt="App Screenshot 1" /> |
| 55 | + |
| 56 | +<img width=20% src="images/Local_LLM_2.jpg" alt="App Screenshot 2" /> |
| 57 | + |
| 58 | +<img width=20% src="images/Local_LLM_3.jpg" alt="App Screenshot 3" /> |
| 59 | + |
0 commit comments