Skip to content

Commit 4f17458

Browse files
authored
feat: add example tauri app (#59)
1 parent fbaec8a commit 4f17458

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6710
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[workspace]
22
members = ["crates/sherpa-rs", "crates/sherpa-rs-sys"]
3+
exclude = ["examples/tauri-app/src-tauri"]

crates/sherpa-rs/src/punctuate.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ impl Punctuation {
5757
}
5858
}
5959
}
60+
61+
unsafe impl Send for Punctuation {}
62+
unsafe impl Sync for Punctuation {}
63+
64+
impl Drop for Punctuation {
65+
fn drop(&mut self) {
66+
unsafe {
67+
sherpa_rs_sys::SherpaOnnxDestroyOfflinePunctuation(self.audio_punctuation);
68+
}
69+
}
70+
}

examples/punctuate.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/
66
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
77
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
88
9-
cargo run --example punctuate
9+
cargo run --example punctuate ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx
1010
*/
1111

1212
fn main() {
13-
let model = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx";
13+
let args: Vec<String> = std::env::args().collect();
14+
let model = args.get(1).expect("Please specify model path");
1415
let sentences = [
1516
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
1617
"我们都是木头人不会说话不会动",

examples/tauri-app/.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
schemas/
26+
autogenerated/
27+
src-tauri/icons/*
28+
!src-tauri/icons/icon.png
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"],
3+
}

examples/tauri-app/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Tauri + sherpa-rs
2+
3+
<img src="https://github.com/user-attachments/assets/4c0da66d-61b8-481d-b53a-a049fe0b914d" width=250>
4+
5+
## Prepare model
6+
7+
```console
8+
cd src-tauri
9+
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
10+
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
11+
mv sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx model.onnx
12+
rm -rf rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
13+
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
14+
adb push model.onnx /data/local/tmp/model.onnx # currently hardcoded in the APK
15+
```
16+
17+
## Build
18+
19+
See https://v2.tauri.app/start/prerequisites
20+
21+
See [Building](../../BUILDING.md)
22+
23+
```console
24+
# Setup environment variables
25+
export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
26+
export ANDROID_HOME="$HOME/Library/Android/sdk"
27+
export NDK_HOME="$HOME/Library/Android/sdk/ndk/27.0.12077973" # ls $HOME/Library/Android/sdk/ndk
28+
29+
# Setup UI
30+
bun install
31+
bunx tauri icon src-tauri/icons/icon.png
32+
33+
cd src-tauri
34+
export CARGO_TARGET_DIR="$(pwd)/target"
35+
cargo ndk -t arm64-v8a build
36+
mkdir -p gen/android/app/src/main/jniLibs/arm64-v8a
37+
ln -s $(pwd)/target/aarch64-linux-android/debug/libonnxruntime.so $(pwd)/gen/android/app/src/main/jniLibs/arm64-v8a/libonnxruntime.so
38+
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
39+
bun run tauri android dev
40+
```
41+
42+
## Debug
43+
44+
```console
45+
adb logcat -c && adb logcat | grep -i -E "tauri|rust|sherpa"
46+
```
47+
48+
## Debug webview
49+
50+
Open `chrome://inspect` in the chrome browser and click `inspect`

examples/tauri-app/bun.lockb

26.4 KB
Binary file not shown.

examples/tauri-app/index.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<!-- <link rel="stylesheet" href="/src/styles.css" /> -->
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>sherpa-rs with tauri</title>
8+
<script type="module" src="/src/main.ts" defer></script>
9+
<style>
10+
h1 {
11+
background-color: #8EC5FC;
12+
background-image: linear-gradient(62deg, #8EC5FC 0%, #E0C3FC 100%);
13+
-webkit-background-clip: text; /* Safari */
14+
background-clip: text; /* For other modern browsers */
15+
color: transparent; /* Make the text itself transparent */
16+
}
17+
18+
#input {
19+
width: 90%;
20+
height: 120px;
21+
border-radius: 12px;
22+
padding: 5px 5px;
23+
}
24+
25+
.action {
26+
display: flex;
27+
justify-content: center;
28+
margin-top: 10px;
29+
}
30+
31+
button {
32+
background: #8EC5FC;
33+
color: white;
34+
border: none;
35+
padding: 5px 5px;
36+
37+
font-size: 22px;
38+
border-radius: 12px;
39+
}
40+
41+
#output {
42+
margin-top: 20px;
43+
padding: 10px;
44+
border-radius: 12px;
45+
background-color: #f4f4f9;
46+
font-size: 18px;
47+
color: #333;
48+
word-wrap: break-word;
49+
}
50+
</style>
51+
</head>
52+
53+
<body>
54+
<main class="container">
55+
<h1 style="text-align: center;">sherpa-rs</h1>
56+
57+
<form class="row" id="input-form">
58+
<textarea id="input" placeholder="Type text to punctuate...">Hello how are you</textarea>
59+
<div class="action">
60+
<button type="submit">Punctuate</button>
61+
</div>
62+
</form>
63+
<p id="output"></p>
64+
</main>
65+
</body>
66+
</html>

examples/tauri-app/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "tauri-app",
3+
"private": true,
4+
"version": "0.1.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc && vite build",
9+
"preview": "vite preview",
10+
"tauri": "tauri"
11+
},
12+
"dependencies": {
13+
"@tauri-apps/api": "^2",
14+
"@tauri-apps/plugin-opener": "^2"
15+
},
16+
"devDependencies": {
17+
"@tauri-apps/cli": "^2",
18+
"vite": "^6.0.3",
19+
"typescript": "~5.6.2"
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
5+
# Generated by Tauri
6+
# will have schema files for capabilities auto-completion
7+
/gen/schemas

0 commit comments

Comments
 (0)