Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/example tauri app #59

Merged
merged 1 commit into from
Dec 25, 2024
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
members = ["crates/sherpa-rs", "crates/sherpa-rs-sys"]
exclude = ["examples/tauri-app/src-tauri"]
11 changes: 11 additions & 0 deletions crates/sherpa-rs/src/punctuate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ impl Punctuation {
}
}
}

unsafe impl Send for Punctuation {}
unsafe impl Sync for Punctuation {}

impl Drop for Punctuation {
fn drop(&mut self) {
unsafe {
sherpa_rs_sys::SherpaOnnxDestroyOfflinePunctuation(self.audio_punctuation);
}
}
}
5 changes: 3 additions & 2 deletions examples/punctuate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2

cargo run --example punctuate
cargo run --example punctuate ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx
*/

fn main() {
let model = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx";
let args: Vec<String> = std::env::args().collect();
let model = args.get(1).expect("Please specify model path");
let sentences = [
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
"我们都是木头人不会说话不会动",
Expand Down
28 changes: 28 additions & 0 deletions examples/tauri-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
schemas/
autogenerated/
src-tauri/icons/*
!src-tauri/icons/icon.png
3 changes: 3 additions & 0 deletions examples/tauri-app/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"],
}
50 changes: 50 additions & 0 deletions examples/tauri-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Tauri + sherpa-rs

<img src="https://github.com/user-attachments/assets/4c0da66d-61b8-481d-b53a-a049fe0b914d" width=250>

## Prepare model

```console
cd src-tauri
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
mv sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx model.onnx
rm -rf rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
adb push model.onnx /data/local/tmp/model.onnx # currently hardcoded in the APK
```

## Build

See https://v2.tauri.app/start/prerequisites

See [Building](../../BUILDING.md)

```console
# Setup environment variables
export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
export ANDROID_HOME="$HOME/Library/Android/sdk"
export NDK_HOME="$HOME/Library/Android/sdk/ndk/27.0.12077973" # ls $HOME/Library/Android/sdk/ndk

# Setup UI
bun install
bunx tauri icon src-tauri/icons/icon.png

cd src-tauri
export CARGO_TARGET_DIR="$(pwd)/target"
cargo ndk -t arm64-v8a build
mkdir -p gen/android/app/src/main/jniLibs/arm64-v8a
ln -s $(pwd)/target/aarch64-linux-android/debug/libonnxruntime.so $(pwd)/gen/android/app/src/main/jniLibs/arm64-v8a/libonnxruntime.so
ln -s $(pwd)/target/aarch64-linux-android/debug/libsherpa-onnx-c-api.so $(pwd)/gen/android/app/src/main/jniLibs/arm64-v8a/libsherpa-onnx-c-api.so
bun run tauri android dev
```

## Debug

```console
adb logcat -c && adb logcat | grep -i -E "tauri|rust|sherpa"
```

## Debug webview

Open `chrome://inspect` in the chrome browser and click `inspect`
Binary file added examples/tauri-app/bun.lockb
Binary file not shown.
66 changes: 66 additions & 0 deletions examples/tauri-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<!-- <link rel="stylesheet" href="/src/styles.css" /> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>sherpa-rs with tauri</title>
<script type="module" src="/src/main.ts" defer></script>
<style>
h1 {
background-color: #8EC5FC;
background-image: linear-gradient(62deg, #8EC5FC 0%, #E0C3FC 100%);
-webkit-background-clip: text; /* Safari */
background-clip: text; /* For other modern browsers */
color: transparent; /* Make the text itself transparent */
}

#input {
width: 90%;
height: 120px;
border-radius: 12px;
padding: 5px 5px;
}

.action {
display: flex;
justify-content: center;
margin-top: 10px;
}

button {
background: #8EC5FC;
color: white;
border: none;
padding: 5px 5px;

font-size: 22px;
border-radius: 12px;
}

#output {
margin-top: 20px;
padding: 10px;
border-radius: 12px;
background-color: #f4f4f9;
font-size: 18px;
color: #333;
word-wrap: break-word;
}
</style>
</head>

<body>
<main class="container">
<h1 style="text-align: center;">sherpa-rs</h1>

<form class="row" id="input-form">
<textarea id="input" placeholder="Type text to punctuate...">Hello how are you</textarea>
<div class="action">
<button type="submit">Punctuate</button>
</div>
</form>
<p id="output"></p>
</main>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/tauri-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "tauri-app",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "^2",
"@tauri-apps/plugin-opener": "^2"
},
"devDependencies": {
"@tauri-apps/cli": "^2",
"vite": "^6.0.3",
"typescript": "~5.6.2"
}
}
7 changes: 7 additions & 0 deletions examples/tauri-app/src-tauri/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Generated by Tauri
# will have schema files for capabilities auto-completion
/gen/schemas
Loading