Skip to content

Commit d379c85

Browse files
Merge pull request #316 from thatfiredev/llm_inference
Add LLM Inference sample android app
2 parents 0fc6e2b + 5cc561e commit d379c85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1517
-0
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# MediaPipe LLM Inference Android Demo
2+
3+
### Overview
4+
5+
This is a sample app that demonstrates how to use the LLM Inference API to run common text-to-text generation tasks like information retrieval, email drafting, and document summarization.
6+
7+
This application must be run on a physical Android device to take advantage of the device GPU.
8+
9+
![LLM Inference Demo](llm_inference.png)
10+
11+
## Build the demo using Android Studio
12+
13+
### Prerequisites
14+
15+
* The **[Android Studio](https://developer.android.com/studio/index.html)**
16+
IDE. This sample has been tested on Android Studio Hedgehog.
17+
18+
* A physical Android device with a minimum OS version of SDK 24 (Android 7.0 -
19+
Nougat) with developer mode enabled.
20+
21+
### Building
22+
23+
* Open Android Studio. From the Welcome screen, select Open an existing
24+
Android Studio project.
25+
26+
* From the Open File or Project window that appears, navigate to and select
27+
the mediapipe/examples/llm_inference/android directory. Click OK. You may
28+
be asked if you trust the project. Select Trust.
29+
30+
* If it asks you to do a Gradle Sync, click OK.
31+
32+
* With your Android device connected to your computer and developer mode
33+
enabled, click on the green Run arrow in Android Studio.
34+
35+
### Models used
36+
37+
You can download one of the [compatible models](https://developers.google.com/mediapipe/solutions/genai/llm_inference#models).
38+
39+
Once you download it, place it under the path defined as MODEL_PATH in InferenceModel on the Android device
40+
(eg. /data/local/tmp/llm/model.tflite).
41+
42+
The easiest way to do that would be to use Android Studio's [Device Explorer](https://developer.android.com/studio/debug/device-file-explorer).
43+
44+
For more details, see the [models section](https://developers.google.com/mediapipe/solutions/genai/llm_inference/android#model) in the LLM Inference guide for Android.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
plugins {
2+
id("com.android.application")
3+
id("org.jetbrains.kotlin.android")
4+
}
5+
6+
android {
7+
namespace = "com.google.mediapipe.examples.llminference"
8+
compileSdk = 34
9+
10+
defaultConfig {
11+
applicationId = "com.google.mediapipe.examples.llminference"
12+
minSdk = 24
13+
targetSdk = 34
14+
versionCode = 1
15+
versionName = "1.0"
16+
17+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary = true
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility = JavaVersion.VERSION_1_8
34+
targetCompatibility = JavaVersion.VERSION_1_8
35+
}
36+
kotlinOptions {
37+
jvmTarget = "1.8"
38+
}
39+
buildFeatures {
40+
compose = true
41+
}
42+
composeOptions {
43+
kotlinCompilerExtensionVersion = "1.5.9"
44+
}
45+
packaging {
46+
resources {
47+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
48+
}
49+
}
50+
}
51+
52+
dependencies {
53+
54+
implementation("androidx.core:core-ktx:1.12.0")
55+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
56+
implementation("androidx.activity:activity-compose:1.8.2")
57+
implementation("androidx.navigation:navigation-compose:2.7.7")
58+
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
59+
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
60+
61+
implementation(platform("androidx.compose:compose-bom:2024.02.00"))
62+
implementation("androidx.compose.ui:ui")
63+
implementation("androidx.compose.ui:ui-graphics")
64+
implementation("androidx.compose.ui:ui-tooling-preview")
65+
implementation("androidx.compose.material3:material3")
66+
67+
implementation ("com.google.mediapipe:tasks-genai:0.10.11")
68+
69+
testImplementation("junit:junit:4.13.2")
70+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
71+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
72+
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
73+
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
74+
debugImplementation("androidx.compose.ui:ui-tooling")
75+
debugImplementation("androidx.compose.ui:ui-test-manifest")
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.LLMInference"
14+
tools:targetApi="31">
15+
<activity
16+
android:name="com.google.mediapipe.examples.llminference.MainActivity"
17+
android:exported="true"
18+
android:label="@string/app_name"
19+
android:theme="@style/Theme.LLMInference">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
27+
<!-- Required to initialize the LlmInference -->
28+
<uses-native-library
29+
android:name="libOpenCL.so"
30+
android:required="false"/>
31+
<uses-native-library android:name="libOpenCL-car.so" android:required="false"/>
32+
<uses-native-library android:name="libOpenCL-pixel.so" android:required="false"/>
33+
</application>
34+
35+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.google.mediapipe.examples.llminference
2+
3+
import java.util.UUID
4+
5+
/**
6+
* Used to represent a ChatMessage
7+
*/
8+
data class ChatMessage(
9+
val id: String = UUID.randomUUID().toString(),
10+
val message: String = "",
11+
val author: String,
12+
val isLoading: Boolean = false
13+
) {
14+
val isFromUser: Boolean
15+
get() = author == USER_PREFIX
16+
}

0 commit comments

Comments
 (0)