Skip to content

Commit

Permalink
Upload TTS APKs to huggingface (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Oct 29, 2023
1 parent 4115f97 commit 1544a57
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 92 deletions.
103 changes: 103 additions & 0 deletions .github/workflows/apk-tts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: apk-tts

on:
push:
branches:
- apk-tts
- apk
tags:
- '*'

workflow_dispatch:

concurrency:
group: apk-tts-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: write

jobs:
apk_tts:
if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa'
runs-on: ${{ matrix.os }}
name: apk for tts ${{ matrix.index }}/${{ matrix.total }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
total: ["12"]
index: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Display NDK HOME
shell: bash
run: |
echo "ANDROID_NDK_LATEST_HOME: ${ANDROID_NDK_LATEST_HOME}"
ls -lh ${ANDROID_NDK_LATEST_HOME}
- name: Install Python dependencies
shell: bash
run: |
python3 -m pip install --upgrade pip jinja2
- name: Generate build script
shell: bash
run: |
cd scripts/apk
total=${{ matrix.total }}
index=${{ matrix.index }}
./generate-tts-apk-script.py --total $total --index $index
chmod +x build-apk-tts.sh
mv -v ./build-apk-tts.sh ../..
- name: build APK
shell: bash
run: |
export ANDROID_NDK=$ANDROID_NDK_LATEST_HOME
./build-apk-tts.sh
- name: Display APK
shell: bash
run: |
ls -lh ./apks/
du -h -d1 .
# - uses: actions/upload-artifact@v3
# with:
# name: tts-apk
# path: ./apks/*.apk

- name: Publish to huggingface
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
uses: nick-fields/retry@v2
with:
max_attempts: 20
timeout_seconds: 200
shell: bash
command: |
git config --global user.email "[email protected]"
git config --global user.name "Fangjun Kuang"
rm -rf huggingface
export GIT_LFS_SKIP_SMUDGE=1
git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface
cd huggingface
git pull
mkdir -p tts
cp -v ../apks/*.apk ./tts/
git status
git lfs track "*.apk"
git add .
git commit -m "add more apks"
git push https://csukuangfj:[email protected]/csukuangfj/sherpa-onnx-apk main
1 change: 0 additions & 1 deletion .github/workflows/apk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ jobs:
shell: bash
run: |
export ANDROID_NDK=$ANDROID_NDK_LATEST_HOME
./build-apk-tts.sh
./build-apk-vad.sh
./build-apk-two-pass.sh
./build-apk.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class MainActivity : AppCompatActivity() {
val ok = audio.samples.size > 0 && audio.save(filename)
if (ok) {
play.isEnabled = true
// Play automatically after generation
onClickPlay()
}
}

Expand All @@ -97,10 +99,24 @@ class MainActivity : AppCompatActivity() {
}

fun initTts() {
// 0 - vits-vctk (multi-speaker, English)
// 1 - vits-zh-aishell3 (multi-speaker, Chinese)
val type = 0
val config = getOfflineTtsConfig(type = type, debug = true)!!
var modelDir :String?
var modelName :String?

// The purpose of such a design is to make the CI test easier
// Please see
// https://github.com/k2-fsa/sherpa-onnx/blob/master/scripts/apk/generate-tts-apk-script.py
modelDir = null
modelName = null

// Example 1:
// modelDir = "vits-vctk"
// modelName = "vits-vctk.onnx"

// Example 2:
// modelDir = "vits-piper-en_US-lessac-medium"
// modelName = "en_US-lessac-medium.onnx"

val config = getOfflineTtsConfig(modelDir = modelDir!!, modelName = modelName!!)!!
tts = OfflineTts(assetManager = application.assets, config = config)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,45 +116,17 @@ class OfflineTts(
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/index.html
// to download models
//
// You can change the type as you wish
fun getOfflineTtsConfig(type: Int, debug: Boolean = false): OfflineTtsConfig? {
when (type) {
0 -> {
val modelDir = "vits-vctk"
return OfflineTtsConfig(
model = OfflineTtsModelConfig(
vits = OfflineTtsVitsModelConfig(
model = "$modelDir/vits-vctk.onnx",
lexicon = "$modelDir/lexicon.txt",
tokens = "$modelDir/tokens.txt"
),
numThreads = 2,
debug = debug,
provider = "cpu",
)
)
}

1 -> {
val modelDir = "vits-zh-aishell3"
return OfflineTtsConfig(
model = OfflineTtsModelConfig(
vits = OfflineTtsVitsModelConfig(
model = "$modelDir/vits-aishell3.onnx",
lexicon = "$modelDir/lexicon.txt",
tokens = "$modelDir/tokens.txt"
),
numThreads = 2,
debug = debug,
provider = "cpu",
)
)
}
}

println("Unsupported type $type")

return null

fun getOfflineTtsConfig(modelDir: String, modelName: String): OfflineTtsConfig? {
return OfflineTtsConfig(
model = OfflineTtsModelConfig(
vits = OfflineTtsVitsModelConfig(
model = "$modelDir/$modelName",
lexicon = "$modelDir/lexicon.txt",
tokens = "$modelDir/tokens.txt"
),
numThreads = 2,
debug = false,
provider = "cpu",
)
)
}
1 change: 1 addition & 0 deletions scripts/apk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build-apk-tts.sh
3 changes: 3 additions & 0 deletions scripts/apk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

This folder contains scripts for building Android APKs.
65 changes: 19 additions & 46 deletions build-apk-tts.sh → scripts/apk/build-apk-tts.sh.in
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
#
# Auto generated! Please DO NOT EDIT!

# Please set the environment variable ANDROID_NDK
# before running this script
Expand Down Expand Up @@ -27,61 +29,31 @@ log "====================x86-64===================="
log "====================x86===================="
./build-android-x86.sh


mkdir -p apks

# Download the model
{% for tts_model in tts_model_list %}
pushd ./android/SherpaOnnxTts/app/src/main/assets/
mkdir vits-vctk

cd vits-vctk
wget -qq https://huggingface.co/csukuangfj/vits-vctk/resolve/main/vits-vctk.onnx
wget -qq https://huggingface.co/csukuangfj/vits-vctk/resolve/main/lexicon.txt
wget -qq https://huggingface.co/csukuangfj/vits-vctk/resolve/main/tokens.txt
popd

for arch in arm64-v8a armeabi-v7a x86_64 x86; do
log "------------------------------------------------------------"
log "build tts apk for $arch"
log "------------------------------------------------------------"
src_arch=$arch
if [ $arch == "armeabi-v7a" ]; then
src_arch=armv7-eabi
elif [ $arch == "x86_64" ]; then
src_arch=x86-64
fi
model_dir={{ tts_model.model_dir }}
model_name={{ tts_model.model_name }}
lang={{ tts_model.lang }}

ls -lh ./build-android-$src_arch/install/lib/*.so
mkdir $model_dir
cd $model_dir
wget -qq https://huggingface.co/csukuangfj/$model_dir/resolve/main/$model_name
wget -qq https://huggingface.co/csukuangfj/$model_dir/resolve/main/lexicon.txt
wget -qq https://huggingface.co/csukuangfj/$model_dir/resolve/main/tokens.txt
wget -qq https://huggingface.co/csukuangfj/$model_dir/resolve/main/MODEL_CARD 2>/dev/null || true

cp -v ./build-android-$src_arch/install/lib/*.so ./android/SherpaOnnxTts/app/src/main/jniLibs/$arch/

pushd ./android/SherpaOnnxTts
./gradlew build
popd

mv android/SherpaOnnxTts/app/build/outputs/apk/debug/app-debug.apk ./apks/sherpa-onnx-${SHERPA_ONNX_VERSION}-$arch-en-tts-multi-speaker-vctk.apk
ls -lh apks
rm -v ./android/SherpaOnnxTts/app/src/main/jniLibs/$arch/*.so
done

rm -rf ./android/SherpaOnnxTts/app/src/main/assets/vits-vctk
popd
# Now we are at the project root directory

git checkout .
pushd android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx
sed -i.bak s/"type = 0"/"type = 1"/ ./MainActivity.kt
sed -i.bak s/"modelDir = null"/"modelDir = \"$model_dir\""/ ./MainActivity.kt
sed -i.bak s/"modelName = null"/"modelName = \"$model_name\""/ ./MainActivity.kt
git diff
popd

pushd ./android/SherpaOnnxTts/app/src/main/assets/
mkdir vits-zh-aishell3
cd vits-zh-aishell3

wget -qq https://huggingface.co/csukuangfj/vits-zh-aishell3/resolve/main/vits-aishell3.onnx
wget -qq https://huggingface.co/csukuangfj/vits-zh-aishell3/resolve/main/lexicon.txt
wget -qq https://huggingface.co/csukuangfj/vits-zh-aishell3/resolve/main/tokens.txt

popd

for arch in arm64-v8a armeabi-v7a x86_64 x86; do
log "------------------------------------------------------------"
log "build tts apk for $arch"
Expand All @@ -101,12 +73,13 @@ for arch in arm64-v8a armeabi-v7a x86_64 x86; do
./gradlew build
popd

mv android/SherpaOnnxTts/app/build/outputs/apk/debug/app-debug.apk ./apks/sherpa-onnx-${SHERPA_ONNX_VERSION}-$arch-zh-tts-multi-speaker-aishell3.apk
mv android/SherpaOnnxTts/app/build/outputs/apk/debug/app-debug.apk ./apks/sherpa-onnx-${SHERPA_ONNX_VERSION}-$arch-$lang-tts-$model_dir.apk
ls -lh apks
rm -v ./android/SherpaOnnxTts/app/src/main/jniLibs/$arch/*.so
done

rm -rf ./android/SherpaOnnxTts/app/src/main/assets/vits-vctk
rm -rf ./android/SherpaOnnxTts/app/src/main/assets/$model_dir
{% endfor %}

git checkout .

Expand Down
Loading

0 comments on commit 1544a57

Please sign in to comment.