forked from google-ai-edge/mediapipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google-ai-edge#35 from googlesamples/implement_tex…
…t_embedder Implement text embedder
- Loading branch information
Showing
39 changed files
with
1,516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# MediaPipe Tasks Text Embedder Android Demo | ||
|
||
### Overview | ||
|
||
This is an app that compare the similarity between two texts, with the | ||
option to use a [Mobile Bert](https://storage.googleapis.com/mediapipe-tasks/text_embedder/mobilebert_embedding_with_metadata.tflite), | ||
or [Average word-embedding](https://storage.googleapis.com/mediapipe-tasks/text_embedder/regex_embedder_with_metadata.tflite) | ||
model. The model files are downloaded by a Gradle script when you build and run | ||
the app. You don't need to do any steps to download TFLite models into the | ||
project explicitly unless you wish to use your own models. If you do use your | ||
own models, place them into the app's *assets* directory. | ||
|
||
This application should be run on a physical Android device to take advantage of | ||
the physical camera, though the gallery tab will enable you to use an emulator | ||
for opening locally stored files. | ||
|
||
![Text Embedder Demo](textembedder.png?raw=true "Text Embedder Demo") | ||
|
||
## Build the demo using Android Studio | ||
|
||
### Prerequisites | ||
|
||
* The **[Android Studio](https://developer.android.com/studio/index.html)** | ||
IDE. This sample has been tested on Android Studio Dolphin. | ||
|
||
* A physical Android device with a minimum OS version of SDK 24 (Android 7.0 - | ||
Nougat) with developer mode enabled. The process of enabling developer mode | ||
may vary by device. You may also use an Android emulator with more limited | ||
functionality. | ||
|
||
### Building | ||
|
||
* Open Android Studio. From the Welcome screen, select Open an existing Android | ||
Studio project. | ||
|
||
* From the Open File or Project window that appears, navigate to and select the | ||
mediapipe/examples/text_embedder/android directory. Click OK. You may be | ||
asked if you trust the project. Select Trust. | ||
|
||
* If it asks you to do a Gradle Sync, click OK. | ||
|
||
* With your Android device connected to your computer and developer mode | ||
enabled, click on the green Run arrow in Android Studio. | ||
|
||
### Models used | ||
|
||
Downloading, extraction, and placing the models into the *assets* folder is | ||
managed automatically by the **download.gradle** file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'org.jetbrains.kotlin.android' | ||
id 'de.undercouch.download' | ||
} | ||
|
||
android { | ||
namespace 'com.google.mediapipe.examples.textembedder' | ||
compileSdk 32 | ||
|
||
defaultConfig { | ||
applicationId "com.google.mediapipe.examples.textembedder" | ||
minSdk 24 | ||
targetSdk 32 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
buildFeatures { | ||
viewBinding true | ||
} | ||
} | ||
|
||
// import DownloadModels task | ||
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets' | ||
project.ext.TEST_ASSETS_DIR = projectDir.toString() + '/src/androidTest/assets' | ||
|
||
// Download default models; if you wish to use your own models then | ||
// place them in the "assets" directory and comment out this line. | ||
apply from: 'download_models.gradle' | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.core:core-ktx:1.7.0' | ||
implementation 'androidx.appcompat:appcompat:1.5.1' | ||
implementation 'com.google.android.material:material:1.7.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.4' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' | ||
|
||
implementation 'com.google.mediapipe:tasks-text:0.1.0-alpha-4' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
task downloadMobileBertModel(type: Download) { | ||
src 'https://storage.googleapis.com/mediapipe-tasks/text_embedder/mobilebert_embedding_with_metadata.tflite' | ||
dest project.ext.ASSET_DIR + '/mobile_bert.tflite' | ||
overwrite false | ||
} | ||
|
||
task downloadAverageWordModel(type: Download) { | ||
src 'https://storage.googleapis.com/mediapipe-tasks/text_embedder/regex_embedder_with_metadata.tflite' | ||
dest project.ext.ASSET_DIR + '/average_word.tflite' | ||
overwrite false | ||
} | ||
|
||
preBuild.dependsOn downloadMobileBertModel, downloadAverageWordModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
56 changes: 56 additions & 0 deletions
56
...src/androidTest/java/com/google/mediapipe/examples/textembedder/TextEmbedderHelperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.mediapipe.examples.textembedder | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class TextEmbedderHelperTest { | ||
companion object { | ||
private const val TEXT_TEST_ONE = | ||
"Da Nang is a coastal city with a 60-km long shoreline. With smooth and sandy beaches running across the coast, Da Nang beach is voted by Forbes (U.S.) to be 1 of 6 most beautiful beaches in the world." | ||
private const val TEXT_TEST_TWO = | ||
"Da Nang is the commercial and educational centre of Central Vietnam and is the largest city in the region. It has a well-sheltered, easily accessible port, and its location on National Route 1 and the North–South Railway makes it a transport hub." | ||
private const val EXPECTED_SIMILARITY = 0.9 | ||
} | ||
|
||
private lateinit var embedderHelper: TextEmbedderHelper | ||
|
||
@Before | ||
fun setup() { | ||
embedderHelper = | ||
TextEmbedderHelper(ApplicationProvider.getApplicationContext()) | ||
} | ||
|
||
@Test | ||
fun embeddingResultsWithinAcceptedRange() { | ||
val bundleResult = embedderHelper.compare(TEXT_TEST_ONE, TEXT_TEST_TWO) | ||
assertEquals(EXPECTED_SIMILARITY, bundleResult?.similarity ?: 0.0, 0.09) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
examples/text_embedder/android/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
<meta-data | ||
android:name="android.app.lib_name" | ||
android:value="" /> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
Oops, something went wrong.