Skip to content

Commit a3b0e0e

Browse files
Merge pull request #19 from dev-diaries41/refactor/providers
Update IEmbeddingProvider interface
2 parents 2a7a4a3 + e81e931 commit a3b0e0e

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
### v1.1.1 - 02/11/2025
1+
### v1.1.1 - 04/11/2025
22

33
### Added
44
* Added new text embedding provider, Mini-LM
5+
* Add `initialized` and `isInitialized` to `IEmbeddingProvider`"
6+
57

68
### Changed
79
* IEmbeddingProvider is require to provider `embeddingDim` variable (used to be optional)

core/src/main/java/com/fpf/smartscansdk/core/embeddings/IEmbeddingProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import android.graphics.Bitmap
55

66
interface IEmbeddingProvider<T> {
77
val embeddingDim: Int
8+
suspend fun initialize()
9+
fun isInitialized(): Boolean
810
fun closeSession() = Unit
911
suspend fun embed(data: T): FloatArray
1012
suspend fun embedBatch(data: List<T>): List<FloatArray>

ml/src/main/java/com/fpf/smartscansdk/ml/models/providers/embeddings/clip/ClipImageEmbedder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ class ClipImageEmbedder(
2929
override val embeddingDim: Int = 512
3030
private var closed = false
3131

32-
suspend fun initialize() = model.loadModel()
32+
override suspend fun initialize() = model.loadModel()
3333

34-
fun isInitialized() = model.isLoaded()
34+
override fun isInitialized() = model.isLoaded()
3535

36-
override suspend fun embed(bitmap: Bitmap): FloatArray = withContext(Dispatchers.Default) {
36+
override suspend fun embed(data: Bitmap): FloatArray = withContext(Dispatchers.Default) {
3737
if (!isInitialized()) throw IllegalStateException("Model not initialized")
3838

3939
val inputShape = longArrayOf(ClipConfig.DIM_BATCH_SIZE.toLong(), ClipConfig.DIM_PIXEL_SIZE.toLong(), ClipConfig.IMAGE_SIZE_X.toLong(), ClipConfig.IMAGE_SIZE_Y.toLong())
40-
val imgData: FloatBuffer = preProcess(bitmap)
40+
val imgData: FloatBuffer = preProcess(data)
4141
val inputName = model.getInputNames()?.firstOrNull() ?: throw IllegalStateException("Model inputs not available")
4242
val output = model.run(mapOf(inputName to TensorData.FloatBufferTensor(imgData, inputShape)))
4343
normalizeL2((output.values.first() as Array<FloatArray>)[0])

ml/src/main/java/com/fpf/smartscansdk/ml/models/providers/embeddings/clip/ClipTextEmbedder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class ClipTextEmbedder(
4242
override val embeddingDim: Int = 512
4343
private var closed = false
4444

45-
suspend fun initialize() = model.loadModel()
45+
override suspend fun initialize() = model.loadModel()
4646

47-
fun isInitialized() = model.isLoaded()
47+
override fun isInitialized() = model.isLoaded()
4848

4949
override suspend fun embed(data: String): FloatArray = withContext(Dispatchers.Default) {
5050
if (!isInitialized()) throw IllegalStateException("Model not initialized")

ml/src/main/java/com/fpf/smartscansdk/ml/models/providers/embeddings/minilm/MiniLmTextEmbedder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class MiniLMTextEmbedder(
2929
private var closed = false
3030
override val embeddingDim: Int = 384 // MiniLM-L6-v2 dimension
3131

32-
suspend fun initialize() {
32+
override suspend fun initialize() {
3333
model.loadModel()
3434
}
3535

36-
fun isInitialized() = model.isLoaded()
36+
override fun isInitialized() = model.isLoaded()
3737

3838
override suspend fun embed(data: String): FloatArray = withContext(Dispatchers.Default) {
3939
if (!isInitialized()) throw IllegalStateException("Model not initialized")

0 commit comments

Comments
 (0)