-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMainActivity.kt
More file actions
149 lines (131 loc) · 5.62 KB
/
MainActivity.kt
File metadata and controls
149 lines (131 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.runanywhere.kotlin_starter_example
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.runanywhere.kotlin_starter_example.services.ModelService
import com.runanywhere.kotlin_starter_example.ui.screens.ChatScreen
import com.runanywhere.kotlin_starter_example.ui.screens.HomeScreen
import com.runanywhere.kotlin_starter_example.ui.screens.SpeechToTextScreen
import com.runanywhere.kotlin_starter_example.ui.screens.SplashScreen
import com.runanywhere.kotlin_starter_example.ui.screens.TextToSpeechScreen
import com.runanywhere.kotlin_starter_example.ui.screens.ToolCallingScreen
import com.runanywhere.kotlin_starter_example.ui.screens.LoraScreen
import com.runanywhere.kotlin_starter_example.ui.screens.VisionScreen
import com.runanywhere.kotlin_starter_example.ui.screens.VoicePipelineScreen
import com.runanywhere.kotlin_starter_example.ui.theme.KotlinStarterTheme
import android.util.Log
import com.runanywhere.sdk.core.onnx.ONNX
import com.runanywhere.sdk.foundation.bridge.extensions.CppBridgeModelPaths
import com.runanywhere.sdk.llm.llamacpp.LlamaCPP
import com.runanywhere.sdk.public.RunAnywhere
import com.runanywhere.sdk.public.SDKEnvironment
import com.runanywhere.sdk.storage.AndroidPlatformContext
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
// Initialize Android platform context FIRST - this sets up storage paths
// The SDK requires this before RunAnywhere.initialize() on Android
AndroidPlatformContext.initialize(this)
// Initialize RunAnywhere SDK for development
RunAnywhere.initialize(environment = SDKEnvironment.DEVELOPMENT)
// Set the base directory for model storage
val runanywherePath = java.io.File(filesDir, "runanywhere").absolutePath
CppBridgeModelPaths.setBaseDirectory(runanywherePath)
// Register backends FIRST - these must be registered before loading any models
// They provide the inference capabilities (TEXT_GENERATION, STT, TTS, VLM)
try {
LlamaCPP.register(priority = 100) // For LLM + VLM (GGUF models)
} catch (e: Throwable) {
// VLM native registration may fail if .so doesn't include nativeRegisterVlm;
// LLM text generation still works since it was registered before VLM in register()
Log.w("MainActivity", "LlamaCPP.register partial failure (VLM may be unavailable): ${e.message}")
}
ONNX.register(priority = 100) // For STT/TTS (ONNX models)
// Register default models
ModelService.registerDefaultModels()
setContent {
KotlinStarterTheme {
RunAnywhereApp()
}
}
}
}
@Composable
fun RunAnywhereApp() {
val navController = rememberNavController()
val modelService: ModelService = viewModel()
NavHost(
navController = navController,
startDestination = "splash"
) {
composable("splash") {
SplashScreen(
onSplashComplete = {
navController.navigate("home") {
popUpTo("splash") { inclusive = true }
}
}
)
}
composable("home") {
HomeScreen(
onNavigateToChat = { navController.navigate("chat") },
onNavigateToSTT = { navController.navigate("stt") },
onNavigateToTTS = { navController.navigate("tts") },
onNavigateToVoicePipeline = { navController.navigate("voice_pipeline") },
onNavigateToToolCalling = { navController.navigate("tool_calling") },
onNavigateToVision = { navController.navigate("vision") },
onNavigateToLora = { navController.navigate("lora") }
)
}
composable("chat") {
ChatScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
composable("stt") {
SpeechToTextScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
composable("tts") {
TextToSpeechScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
composable("voice_pipeline") {
VoicePipelineScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
composable("tool_calling") {
ToolCallingScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
composable("vision") {
VisionScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
composable("lora") {
LoraScreen(
onNavigateBack = { navController.popBackStack() },
modelService = modelService
)
}
}
}