Skip to content

Commit

Permalink
update default text for text embedder
Browse files Browse the repository at this point in the history
  • Loading branch information
duy-maimanh committed Jan 16, 2023
1 parent 6b41e61 commit 35d3532
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

package com.google.mediapipe.examples.textembedder

import android.content.res.AssetManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Before
import java.io.InputStream

/**
* Instrumented test, which will execute on an Android device.
Expand All @@ -55,7 +50,7 @@ class TextEmbedderHelperTest {

@Test
fun embeddingResultsWithinAcceptedRange() {
val bundleResult = embedderHelper.embed(TEXT_TEST_ONE, TEXT_TEST_TWO)
val bundleResult = embedderHelper.compare(TEXT_TEST_ONE, TEXT_TEST_TWO)
assertEquals(EXPECTED_SIMILARITY, bundleResult?.similarity ?: 0.0, 0.09)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class MainActivity : AppCompatActivity(), TextEmbedderHelper.EmbedderListener {
binding.btnCompare.setOnClickListener {
// Compare two texts here
val firstText =
if (binding.imgOne.text.isNullOrEmpty()) getString(R.string.default_compared_text) else binding.imgOne.text.toString()
if (binding.imgOne.text.isNullOrEmpty()) getString(R.string.default_hint_first_text) else binding.imgOne.text.toString()
val secondText =
if (binding.imgTwo.text.isNullOrEmpty()) getString(R.string.default_compared_text) else binding.imgTwo.text.toString()
if (binding.imgTwo.text.isNullOrEmpty()) getString(R.string.default_hint_second_text) else binding.imgTwo.text.toString()

textEmbedderHelper.embed(firstText, secondText)
textEmbedderHelper.compare(firstText, secondText)
?.let { resultBundle ->
updateResult(resultBundle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ class TextEmbedderHelper(
// If both the vectors are aligned, the angle between them
// will be 0. cos 0 = 1. So, mathematically, this distance metric will
// be used to find the most similar text.
fun embed(firstBitmap: String, secondBitmap: String): ResultBundle? {
fun compare(firstText: String, secondText: String): ResultBundle? {
// Inference time is the difference between the system time at the start and finish of the
// process
val startTime = SystemClock.uptimeMillis()

textEmbedder?.let {
val firstEmbed =
it.embed(firstBitmap).embeddingResult().embeddings().first()
it.embed(firstText).embeddingResult().embeddings().first()
val secondEmbed =
it.embed(secondBitmap).embeddingResult().embeddings().first()
it.embed(secondText).embeddingResult().embeddings().first()
val inferenceTimeMs = SystemClock.uptimeMillis() - startTime
return ResultBundle(
TextEmbedder.cosineSimilarity(firstEmbed, secondEmbed),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
android:layout_height="0dp"
android:background="@color/bottom_sheet_background"
android:gravity="top"
android:hint="@string/default_compared_text"
android:hint="@string/default_hint_first_text"
android:inputType="textMultiLine"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -88,7 +88,7 @@
android:layout_height="0dp"
android:background="@color/bottom_sheet_background"
android:gravity="top"
android:hint="@string/default_compared_text"
android:hint="@string/default_hint_second_text"
android:inputType="textMultiLine"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<string name="tv_title">Input two texts to compare</string>
<string name="tv_similarity">Similarity:</string>
<string name="btn_compare">Compare</string>
<string name="default_compared_text">MediaPipe offers cross-platform, customizable ML solutions for live and streaming media.</string>
<string name="default_hint_first_text">MediaPipe offers cross-platform, customizable ML solutions for live and streaming media.</string>
<string name="default_hint_second_text">I use MediaPipe to create amazing things with text, audio, and visual content.</string>

<string-array name="delegate_spinner_titles">
<item>CPU</item>
Expand Down

0 comments on commit 35d3532

Please sign in to comment.