From 1bebd1bb0729958a6ab6a844a6a10d344554cea7 Mon Sep 17 00:00:00 2001 From: Han Yin Date: Mon, 14 Apr 2025 22:53:25 -0700 Subject: [PATCH] util: extract file size formatting into ModelUtils --- .../llama/revamp/data/model/ModelInfo.kt | 19 ++----------------- .../example/llama/revamp/util/ModelUtils.kt | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/model/ModelInfo.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/model/ModelInfo.kt index 68ef17db3e..62e6295ca6 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/model/ModelInfo.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/model/ModelInfo.kt @@ -1,6 +1,6 @@ package com.example.llama.revamp.data.model -import java.util.Locale +import com.example.llama.revamp.util.formatSize /** * Data class containing information about an LLM model. @@ -17,22 +17,7 @@ data class ModelInfo( val lastUsed: Long? = null ) { val formattedSize: String - get() { - return when { - sizeInBytes >= 1_000_000_000 -> { - val sizeInGb = sizeInBytes / 1_000_000_000.0 - String.format(Locale.getDefault(), "%.2f GB", sizeInGb) - } - sizeInBytes >= 1_000_000 -> { - val sizeInMb = sizeInBytes / 1_000_000.0 - String.format(Locale.getDefault(), "%.2f MB", sizeInMb) - } - else -> { - val sizeInKb = sizeInBytes / 1_000.0 - String.format(Locale.getDefault(), "%.2f KB", sizeInKb) - } - } - } + get() = formatSize(sizeInBytes) companion object { /** diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/util/ModelUtils.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/util/ModelUtils.kt index 48ac961d87..f4ea74edd1 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/revamp/util/ModelUtils.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/util/ModelUtils.kt @@ -33,6 +33,24 @@ fun getFileSizeFromUri(context: Context, uri: Uri): Long? = } } +/** + * Convert bytes into human readable sizes + */ +fun formatSize(sizeInBytes: Long) = when { + sizeInBytes >= 1_000_000_000 -> { + val sizeInGb = sizeInBytes / 1_000_000_000.0 + String.format(Locale.getDefault(), "%.2f GB", sizeInGb) + } + sizeInBytes >= 1_000_000 -> { + val sizeInMb = sizeInBytes / 1_000_000.0 + String.format(Locale.getDefault(), "%.2f MB", sizeInMb) + } + else -> { + val sizeInKb = sizeInBytes / 1_000.0 + String.format(Locale.getDefault(), "%.2f KB", sizeInKb) + } +} + /** * Try to extract parameters by looking for patterns like 7B, 13B, etc. */