Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Update to Gradle 7/AGP 7/Kotlin 1.8/Android 13 #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions bert/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
compileSdkVersion 33
namespace "co.huggingface.android_transformers.bertqa"

defaultConfig {
applicationId "co.huggingface.android_transformers.bert"
minSdkVersion 26
targetSdkVersion 29
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -21,8 +22,8 @@ android {
noCompress "tflite"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
// If you find lint problem like:
// * What went wrong:
Expand Down Expand Up @@ -53,7 +54,7 @@ dependencies {
implementation 'com.google.guava:guava:28.1-android'
// implementation 'org.tensorflow:tensorflow-lite:2.0.0'
// implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly'
compile(name: 'tensorflow-lite-with-select-tf-ops-0.0.0-nightly', ext: 'aar')
implementation files('libs/tensorflow-lite-with-select-tf-ops-0.0.0-nightly.aar')

testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:core:1.2.0'
Expand All @@ -62,10 +63,3 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

repositories {
flatDir {
dirs 'libs'
}
mavenCentral()
}
2 changes: 1 addition & 1 deletion bert/download.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'de.undercouch.download'

task downloadLiteModel {
task downloadLiteModel(type: Download) {
def downloadFiles = [
'https://s3.amazonaws.com/models.huggingface.co/bert/distilbert-base-uncased-distilled-squad-384.tflite': 'model.tflite',
// 'https://s3.amazonaws.com/models.huggingface.co/bert/distilbert-base-uncased-distilled-squad-384-fp16.tflite': 'model.tflite', // FP16 quantization version
Expand Down
6 changes: 3 additions & 3 deletions bert/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="co.huggingface.android_transformers">
package="co.huggingface.android_transformers.bertqa">

<application
android:allowBackup="true"
Expand All @@ -12,14 +12,14 @@
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".bertqa.ui.QaActivity"
android:name=".ui.QaActivity"
android:exported="false">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="co.huggingface.android_transformers.bertqa.ui.DatasetListActivity" />
</activity>
<activity
android:name=".bertqa.ui.DatasetListActivity"
android:name=".ui.DatasetListActivity"
android:exported="true"
android:theme="@style/AppTheme.NoTitleActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import android.widget.ArrayAdapter;
import android.widget.ListView;
import co.huggingface.android_transformers.R;

import co.huggingface.android_transformers.bertqa.R;
import co.huggingface.android_transformers.bertqa.ml.LoadDatasetClient;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import com.google.android.material.textfield.TextInputEditText;
import java.util.List;
import java.util.Locale;
import co.huggingface.android_transformers.R;

import co.huggingface.android_transformers.bertqa.R;
import co.huggingface.android_transformers.bertqa.ml.LoadDatasetClient;
import co.huggingface.android_transformers.bertqa.ml.QaAnswer;
import co.huggingface.android_transformers.bertqa.ml.QaClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.chip.Chip;
import co.huggingface.android_transformers.R;

import co.huggingface.android_transformers.bertqa.R;

/** Adapter class to show question suggestion chips. */
public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.MyViewHolder> {
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.61'
ext.kotlin_version = '1.8.20'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'de.undercouch:gradle-download-task:4.0.0'
classpath 'com.android.tools.build:gradle:7.2.2'
classpath 'de.undercouch:gradle-download-task:5.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -18,7 +18,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
16 changes: 7 additions & 9 deletions gpt2/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"

compileSdkVersion 33
namespace "co.huggingface.android_transformers.gpt2"

defaultConfig {
applicationId "co.huggingface.android_transformers.gpt2"
namespace "co.huggingface.android_transformers.gpt2"
minSdkVersion 26
targetSdkVersion 29
targetSdkVersion 33
versionCode 1
versionName "1.0"

Expand All @@ -22,12 +20,12 @@ android {
noCompress "tflite"
}

dataBinding {
enabled = true
buildFeatures {
viewBinding true
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
jvmTarget = JavaVersion.VERSION_11
}

buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion gpt2/download.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'de.undercouch.download'

task downloadLiteModel {
task downloadLiteModel(type: Download) {
def downloadFiles = [
"https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-vocab.json": "gpt2-vocab.json",
"https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-merges.txt": "gpt2-merges.txt",
Expand Down
3 changes: 2 additions & 1 deletion gpt2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
package co.huggingface.android_transformers.gpt2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.widget.TextView
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import co.huggingface.android_transformers.gpt2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private val gpt2: co.huggingface.android_transformers.gpt2.ml.GPT2Client by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val binding: ActivityMainBinding
= DataBindingUtil.setContentView(this, R.layout.activity_main)

// Bind layout with ViewModel
binding.vm = gpt2
binding.autocompleteButton.setOnClickListener {
gpt2.launchAutocomplete()
}
binding.shuffleButton.setOnClickListener {
gpt2.refreshPrompt()
}
gpt2.completion.observe(this) { completion ->
gpt2.prompt.observe(this) { prompt ->
binding.prompt.formatCompletion(prompt, completion)
}
}
}

// LiveData needs the lifecycle owner
binding.lifecycleOwner = this
private fun TextView.formatCompletion(prompt: String, completion: String) {
text = when {
completion.isEmpty() -> prompt
else -> {
val str = SpannableStringBuilder(prompt + completion)
val bgCompletionColor =
ResourcesCompat.getColor(resources, R.color.colorPrimary, context.theme)
str.apply {
setSpan(
android.text.style.BackgroundColorSpan(bgCompletionColor),
prompt.length,
str.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package co.huggingface.android_transformers.gpt2.ml

import android.app.Application
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.util.JsonReader
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.databinding.BindingAdapter
import androidx.lifecycle.*
import co.huggingface.android_transformers.gpt2.R
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import co.huggingface.android_transformers.gpt2.tokenization.GPT2Tokenizer
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import org.tensorflow.lite.Interpreter
import java.io.BufferedReader
import java.io.FileInputStream
Expand Down Expand Up @@ -107,7 +109,7 @@ class GPT2Client(application: Application) : AndroidViewModel(application) {

// Softmax computation on filtered logits
val filteredLogits = filteredLogitsWithIndexes.map { it.second }
val maxLogitValue = filteredLogits.max()!!
val maxLogitValue = filteredLogits.maxOrNull()!!
val logitsExp = filteredLogits.map { exp(it - maxLogitValue) }
val sumExp = logitsExp.sum()
val probs = logitsExp.map { it.div(sumExp) }
Expand Down Expand Up @@ -200,17 +202,3 @@ private fun FloatArray.argmax(): Int {

return bestIndex
}

@BindingAdapter("prompt", "completion")
fun TextView.formatCompletion(prompt: String, completion: String): Unit {
text = when {
completion.isEmpty() -> prompt
else -> {
val str = SpannableStringBuilder(prompt + completion)
val bgCompletionColor = ResourcesCompat.getColor(resources, R.color.colorPrimary, context.theme)
str.setSpan(android.text.style.BackgroundColorSpan(bgCompletionColor), prompt.length, str.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

str
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GPT2Tokenizer(

while (true) {
if (!pairs.any { bpeRanks.containsKey(it) }) break
val (first, second) = pairs.minBy { bpeRanks.getOrDefault(it, Int.MAX_VALUE) } ?: break
val (first, second) = pairs.minByOrNull { bpeRanks.getOrDefault(it, Int.MAX_VALUE) } ?: break

var i = 0
val newWord = mutableListOf<String>()
Expand Down
Loading