chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="ai.mlc.mlcengineexample">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission
|
||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||
android:maxSdkVersion="32"
|
||||
tools:ignore="ScopedStorage" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@drawable/mlc_logo_108"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@drawable/mlc_logo_108"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.MLCEngineExample"
|
||||
tools:targetApi="31">
|
||||
<uses-native-library
|
||||
android:name="libOpenCL.so"
|
||||
android:required="false"/>
|
||||
|
||||
<uses-native-library
|
||||
android:name="libOpenCL-pixel.so"
|
||||
android:required="false" />
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@android:style/Theme.Material.NoActionBar">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
@@ -0,0 +1,81 @@
|
||||
package ai.mlc.mlcengineexample
|
||||
|
||||
import ai.mlc.mlcengineexample.ui.theme.MLCEngineExampleTheme
|
||||
import ai.mlc.mlcllm.MLCEngine
|
||||
import ai.mlc.mlcllm.OpenAIProtocol
|
||||
import ai.mlc.mlcllm.OpenAIProtocol.*
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Modifier
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
@SuppressLint("CoroutineCreationDuringComposition")
|
||||
@ExperimentalMaterial3Api
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val modelName = "phi-2-q4f16_1-MLC"
|
||||
var modelPath = File(application.getExternalFilesDir(""), modelName).toString()
|
||||
Log.i("MLC", "model path: $modelPath")
|
||||
// need to be changed to the custom system lib prefix used while compiling the model
|
||||
val modelLib = "phi_msft_q4f16_1_686d8979c6ebf05d142d9081f1b87162"
|
||||
Log.i("MLC", "engine loaded")
|
||||
|
||||
setContent {
|
||||
val responseText = remember { mutableStateOf("") }
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val engine = MLCEngine()
|
||||
engine.unload()
|
||||
engine.reload(modelPath, modelLib)
|
||||
coroutineScope.launch {
|
||||
var channel = engine.chat.completions.create(
|
||||
messages = listOf(
|
||||
ChatCompletionMessage(
|
||||
role = OpenAIProtocol.ChatCompletionRole.user,
|
||||
content = "What is the meaning of life?"
|
||||
)
|
||||
),
|
||||
stream_options = OpenAIProtocol.StreamOptions(include_usage = true)
|
||||
)
|
||||
|
||||
|
||||
for (response in channel) {
|
||||
val finalusage = response.usage
|
||||
if (finalusage != null) {
|
||||
responseText.value += "\n" + (finalusage.extra?.asTextLabel() ?: "")
|
||||
} else {
|
||||
if (response.choices.size > 0) {
|
||||
responseText.value += response.choices[0].delta.content?.asText()
|
||||
.orEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Surface(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
) {
|
||||
MLCEngineExampleTheme {
|
||||
Text(text = responseText.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ai.mlc.mlcengineexample.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val Blue10 = Color(0xFF000F5E)
|
||||
val Blue20 = Color(0xFF001E92)
|
||||
val Blue30 = Color(0xFF002ECC)
|
||||
val Blue40 = Color(0xFF1546F6)
|
||||
val Blue80 = Color(0xFFB8C3FF)
|
||||
val Blue90 = Color(0xFFDDE1FF)
|
||||
|
||||
val DarkBlue10 = Color(0xFF00036B)
|
||||
val DarkBlue20 = Color(0xFF000BA6)
|
||||
val DarkBlue30 = Color(0xFF1026D3)
|
||||
val DarkBlue40 = Color(0xFF3648EA)
|
||||
val DarkBlue80 = Color(0xFFBBC2FF)
|
||||
val DarkBlue90 = Color(0xFFDEE0FF)
|
||||
|
||||
val Yellow10 = Color(0xFF261900)
|
||||
val Yellow20 = Color(0xFF402D00)
|
||||
val Yellow30 = Color(0xFF5C4200)
|
||||
val Yellow40 = Color(0xFF7A5900)
|
||||
val Yellow80 = Color(0xFFFABD1B)
|
||||
val Yellow90 = Color(0xFFFFDE9C)
|
||||
|
||||
val Red10 = Color(0xFF410001)
|
||||
val Red20 = Color(0xFF680003)
|
||||
val Red30 = Color(0xFF930006)
|
||||
val Red40 = Color(0xFFBA1B1B)
|
||||
val Red80 = Color(0xFFFFB4A9)
|
||||
val Red90 = Color(0xFFFFDAD4)
|
||||
|
||||
val Grey10 = Color(0xFF191C1D)
|
||||
val Grey20 = Color(0xFF2D3132)
|
||||
val Grey80 = Color(0xFFC4C7C7)
|
||||
val Grey90 = Color(0xFFE0E3E3)
|
||||
val Grey95 = Color(0xFFEFF1F1)
|
||||
val Grey99 = Color(0xFFFBFDFD)
|
||||
|
||||
val BlueGrey30 = Color(0xFF45464F)
|
||||
val BlueGrey50 = Color(0xFF767680)
|
||||
val BlueGrey60 = Color(0xFF90909A)
|
||||
val BlueGrey80 = Color(0xFFC6C5D0)
|
||||
val BlueGrey90 = Color(0xFFE2E1EC)
|
||||
@@ -0,0 +1,107 @@
|
||||
package ai.mlc.mlcengineexample.ui.theme
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.material3.dynamicDarkColorScheme
|
||||
import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.core.view.WindowCompat
|
||||
|
||||
private val DarkColorScheme = darkColorScheme(
|
||||
primary = Blue80,
|
||||
onPrimary = Blue20,
|
||||
primaryContainer = Blue30,
|
||||
onPrimaryContainer = Blue90,
|
||||
inversePrimary = Blue40,
|
||||
secondary = DarkBlue80,
|
||||
onSecondary = DarkBlue20,
|
||||
secondaryContainer = DarkBlue30,
|
||||
onSecondaryContainer = DarkBlue90,
|
||||
tertiary = Yellow80,
|
||||
onTertiary = Yellow20,
|
||||
tertiaryContainer = Yellow30,
|
||||
onTertiaryContainer = Yellow90,
|
||||
error = Red80,
|
||||
onError = Red20,
|
||||
errorContainer = Red30,
|
||||
onErrorContainer = Red90,
|
||||
background = Grey10,
|
||||
onBackground = Grey90,
|
||||
surface = Grey10,
|
||||
onSurface = Grey80,
|
||||
inverseSurface = Grey90,
|
||||
inverseOnSurface = Grey20,
|
||||
surfaceVariant = BlueGrey30,
|
||||
onSurfaceVariant = BlueGrey80,
|
||||
outline = BlueGrey60
|
||||
)
|
||||
|
||||
private val LightColorScheme = lightColorScheme(
|
||||
primary = Blue40,
|
||||
onPrimary = Color.White,
|
||||
primaryContainer = Blue90,
|
||||
onPrimaryContainer = Blue10,
|
||||
inversePrimary = Blue80,
|
||||
secondary = DarkBlue40,
|
||||
onSecondary = Color.White,
|
||||
secondaryContainer = DarkBlue90,
|
||||
onSecondaryContainer = DarkBlue10,
|
||||
tertiary = Yellow40,
|
||||
onTertiary = Color.White,
|
||||
tertiaryContainer = Yellow90,
|
||||
onTertiaryContainer = Yellow10,
|
||||
error = Red40,
|
||||
onError = Color.White,
|
||||
errorContainer = Red90,
|
||||
onErrorContainer = Red10,
|
||||
background = Grey99,
|
||||
onBackground = Grey10,
|
||||
surface = Grey99,
|
||||
onSurface = Grey10,
|
||||
inverseSurface = Grey20,
|
||||
inverseOnSurface = Grey95,
|
||||
surfaceVariant = BlueGrey90,
|
||||
onSurfaceVariant = BlueGrey30,
|
||||
outline = BlueGrey50
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun MLCEngineExampleTheme(
|
||||
darkTheme: Boolean = isSystemInDarkTheme(),
|
||||
// Dynamic color is available on Android 12+
|
||||
dynamicColor: Boolean = true,
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val colorScheme = when {
|
||||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||
val context = LocalContext.current
|
||||
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
|
||||
}
|
||||
|
||||
darkTheme -> DarkColorScheme
|
||||
else -> LightColorScheme
|
||||
}
|
||||
val view = LocalView.current
|
||||
if (!view.isInEditMode) {
|
||||
SideEffect {
|
||||
val window = (view.context as Activity).window
|
||||
window.statusBarColor = colorScheme.primary.toArgb()
|
||||
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
|
||||
}
|
||||
}
|
||||
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = Typography,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ai.mlc.mlcengineexample.ui.theme
|
||||
|
||||
import androidx.compose.material3.Typography
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
// Set of Material typography styles to start with
|
||||
val Typography = Typography(
|
||||
bodyLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 24.sp,
|
||||
letterSpacing = 0.5.sp
|
||||
)
|
||||
/* Other default text styles to override
|
||||
titleLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 22.sp,
|
||||
lineHeight = 28.sp,
|
||||
letterSpacing = 0.sp
|
||||
),
|
||||
labelSmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 11.sp,
|
||||
lineHeight = 16.sp,
|
||||
letterSpacing = 0.5.sp
|
||||
)
|
||||
*/
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#000000"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#FF000000" android:pathData="M17.6,11.48 L19.44,8.3a0.63,0.63 0,0 0,-1.09 -0.63l-1.88,3.24a11.43,11.43 0,0 0,-8.94 0L5.65,7.67a0.63,0.63 0,0 0,-1.09 0.63L6.4,11.48A10.81,10.81 0,0 0,1 20L23,20A10.81,10.81 0,0 0,17.6 11.48ZM7,17.25A1.25,1.25 0,1 1,8.25 16,1.25 1.25,0 0,1 7,17.25ZM17,17.25A1.25,1.25 0,1 1,18.25 16,1.25 1.25,0 0,1 17,17.25Z"/>
|
||||
</vector>
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="purple_200">#FFBB86FC</color>
|
||||
<color name="purple_500">#FF6200EE</color>
|
||||
<color name="purple_700">#FF3700B3</color>
|
||||
<color name="teal_200">#FF03DAC5</color>
|
||||
<color name="teal_700">#FF018786</color>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
</resources>
|
||||
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">MLCEngineExample</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Theme.MLCEngineExample" parent="android:Theme.Material.Light" />
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
Sample backup rules file; uncomment and customize as necessary.
|
||||
See https://developer.android.com/guide/topics/data/autobackup
|
||||
for details.
|
||||
Note: This file is ignored for devices older that API 31
|
||||
See https://developer.android.com/about/versions/12/backup-restore
|
||||
-->
|
||||
<full-backup-content>
|
||||
<!--
|
||||
<include domain="sharedpref" path="."/>
|
||||
<exclude domain="sharedpref" path="device.xml"/>
|
||||
-->
|
||||
</full-backup-content>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
Sample data extraction rules file; uncomment and customize as necessary.
|
||||
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
|
||||
for details.
|
||||
-->
|
||||
<data-extraction-rules>
|
||||
<cloud-backup>
|
||||
<!-- TODO: Use <include> and <exclude> to control what is backed up.
|
||||
<include .../>
|
||||
<exclude .../>
|
||||
-->
|
||||
</cloud-backup>
|
||||
<!--
|
||||
<device-transfer>
|
||||
<include .../>
|
||||
<exclude .../>
|
||||
</device-transfer>
|
||||
-->
|
||||
</data-extraction-rules>
|
||||
Reference in New Issue
Block a user