Skip to content

armgit / Edge0

Public Imported from GitHub
0

Run 35B MoE models in ~2.5 GB of RAM. Weights stream from SSD; a trained router predicts loads a step ahead. Mac, iPhone, Android, Windows supported

Python

fix(android): import a GGUF from the system document picker

Jiahaoadd535f3f667
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index deab95e..c6a906f 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -19,8 +19,8 @@ android {
applicationId = "dev.edge0.runtime.app"
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.compileSdk.get().toInt()
- versionCode = 1
- versionName = "0.1.0"
+ versionCode = 2
+ versionName = "0.1.1"
ndk { abiFilters += "arm64-v8a" } // arm64 only
externalNativeBuild {
cmake {
diff --git a/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelDocumentImport.kt b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelDocumentImport.kt
new file mode 100644
index 0000000..32b7eac
--- /dev/null
+++ b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelDocumentImport.kt
@@ -0,0 +1,61 @@
+// runtime/ModelDocumentImport.kt - Storage Access Framework entry for Models.
+// The system document picker grants a temporary read URI. No storage permission
+// and no adb: the bytes are copied into the app-private models directory.
+package dev.edge0.runtime.app.runtime
+
+import android.content.Context
+import android.net.Uri
+import android.provider.OpenableColumns
+import java.io.File
+
+object ModelDocumentImport {
+
+ /**
+ * Copy one picked document into [modelsDir]. [blocked] may reject a
+ * destination (for example the model that is currently loaded).
+ */
+ suspend fun import(
+ context: Context,
+ uri: Uri,
+ modelsDir: File,
+ blocked: (File) -> String? = { null },
+ onProgress: suspend (name: String, copied: Long, total: Long?) -> Unit = { _, _, _ -> },
+ ): File {
+ val (display, size) = readMeta(context, uri)
+ val safe = ModelImport.fileName(display)
+ ?: throw ModelImport.ImportException(
+ "Choose a file whose name ends in .gguf" +
+ (display?.let { " ($it)" } ?: ""))
+ val dest = File(modelsDir, safe)
+ blocked(dest)?.let { throw ModelImport.ImportException(it) }
+ val input = try {
+ context.contentResolver.openInputStream(uri)
+ } catch (e: SecurityException) {
+ throw ModelImport.ImportException("Cannot open the selected file")
+ } ?: throw ModelImport.ImportException("Cannot open the selected file")
+ onProgress(safe, 0L, size)
+ return ModelImport.copy(input, modelsDir, safe, size) { copied, total ->
+ onProgress(safe, copied, total)
+ }
+ }
+
+ private fun readMeta(context: Context, uri: Uri): Pair<String?, Long?> {
+ var name: String? = null
+ var size: Long? = null
+ context.contentResolver.query(
+ uri,
+ arrayOf(OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE),
+ null, null, null,
+ )?.use { c ->
+ if (!c.moveToFirst()) return@use
+ val ni = c.getColumnIndex(OpenableColumns.DISPLAY_NAME)
+ val si = c.getColumnIndex(OpenableColumns.SIZE)
+ if (ni >= 0) name = c.getString(ni)
+ if (si >= 0 && !c.isNull(si)) {
+ val s = c.getLong(si)
+ if (s >= 0) size = s
+ }
+ }
+ return name to size
+ }
+}
diff --git a/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelImport.kt b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelImport.kt
new file mode 100644
index 0000000..87f3f89
--- /dev/null
+++ b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelImport.kt
@@ -0,0 +1,101 @@
+// runtime/ModelImport.kt - copy a caller-supplied .gguf stream into files/models.
+// The document picker lives in the UI; this class only accepts a flat file name
+// and a stream, so a release install can import without adb or storage permission.
+// Bytes land in a sibling "*.gguf.partial" and are renamed after the copy, so a
+// killed transfer is never scanned as a model.
+package dev.edge0.runtime.app.runtime
+
+import java.io.File
+import java.io.FileOutputStream
+import java.io.InputStream
+import kotlin.coroutines.coroutineContext
+import kotlinx.coroutines.ensureActive
+
+object ModelImport {
+ const val PARTIAL_SUFFIX = ".partial"
+ private const val REPORT_EVERY = 32L * 1024 * 1024
+
+ class ImportException(message: String) : Exception(message)
+
+ /** Flat `*.gguf` basename, or null when the display name is not one. */
+ fun fileName(displayName: String?): String? {
+ if (displayName.isNullOrBlank()) return null
+ val base = displayName.trim().substringAfterLast('/').substringAfterLast('\\')
+ if (base.isEmpty() || base == "." || base == "..") return null
+ if (base.any { it == '/' || it == '\\' || it == '\u0000' }) return null
+ if (!base.endsWith(".gguf", ignoreCase = true)) return null
+ if (base.length > 180) return null
+ val stem = base.dropLast(".gguf".length)
+ if (stem.isEmpty()) return null
+ return stem + ".gguf"
+ }
+
+ /** Drop a partial left behind when the process died mid-copy. */
+ fun deleteStalePartials(modelsDir: File) {
+ modelsDir.listFiles()?.forEach { f ->
+ if (f.isFile && f.name.endsWith(".gguf$PARTIAL_SUFFIX")) f.delete()
+ }
+ }
+
+ /**
+ * Copy [input] into [modelsDir]. [sizeBytes] is the picker-reported length
+ * when known; a mismatch or a short disk is an [ImportException] and leaves
+ * no partial file behind. [availableBytes] defaults to the volume free space.
+ */
+ suspend fun copy(
+ input: InputStream,
+ modelsDir: File,
+ displayName: String,
+ sizeBytes: Long?,
+ availableBytes: Long = modelsDir.usableSpace,
+ onProgress: suspend (copied: Long, total: Long?) -> Unit = { _, _ -> },
+ ): File = input.use { inp ->
+ val safe = fileName(displayName)
+ ?: throw ImportException("Choose a file whose name ends in .gguf")
+ if (sizeBytes != null && sizeBytes < 0) throw ImportException("invalid size")
+ if (!modelsDir.exists() && !modelsDir.mkdirs()) {
+ throw ImportException("Could not create ${modelsDir.path}")
+ }
+ if (sizeBytes != null && sizeBytes > availableBytes) {
+ throw ImportException("Not enough free space for $safe")
+ }
+ val dest = File(modelsDir, safe)
+ val partial = File(modelsDir, safe + PARTIAL_SUFFIX)
+ if (partial.exists() && !partial.delete()) {
+ throw ImportException("Could not clear a previous partial copy of $safe")
+ }
+ var finished = false
+ try {
+ FileOutputStream(partial).use { out ->
+ val buf = ByteArray(1024 * 1024)
+ var copied = 0L
+ var lastReport = -1L
+ while (true) {
+ coroutineContext.ensureActive()
+ val n = inp.read(buf)
+ if (n < 0) break
+ out.write(buf, 0, n)
+ copied += n
+ if (lastReport < 0 || copied - lastReport >= REPORT_EVERY) {
+ lastReport = copied
+ onProgress(copied, sizeBytes)
+ }
+ }
+ out.fd.sync()
+ if (copied != lastReport) onProgress(copied, sizeBytes)
+ if (copied == 0L) throw ImportException("$safe is empty")
+ if (sizeBytes != null && copied != sizeBytes) {
+ throw ImportException("$safe was truncated ($copied of $sizeBytes bytes)")
+ }
+ }
+ if (dest.exists() && !dest.delete()) {
+ throw ImportException("Could not replace $safe")
+ }
+ if (!partial.renameTo(dest)) throw ImportException("Could not finish importing $safe")
+ finished = true
+ return dest
+ } finally {
+ if (!finished) partial.delete()
+ }
+ }
+}