@bendyline/gezel-capacitor 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Package.swift +26 -0
- package/README.md +230 -2
- package/android/build.gradle +30 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/GezelKnowledgePlugin.java +153 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/GezelOfflineSpeechPlugin.java +216 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/GezelRuntimePlugin.java +84 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/KnowledgeFiles.java +101 -0
- package/android/src/main/java/com/bendyline/gezel/capacitor/SpeechModels.java +230 -0
- package/android/src/main/java/com/bendyline/gezel/speech/SpeechRuntime.java +38 -0
- package/dist/index.d.ts +561 -0
- package/dist/index.js +7512 -0
- package/dist/knowledge-worker.js +211 -0
- package/dist/licenses/npm/manifest.json +76 -0
- package/dist/licenses/npm/texts/1583809bf1155510d2961f94aa9bca6dc4b44b6f59fd773de33bd6c20860d781.txt +29 -0
- package/dist/licenses/npm/texts/1cd39aa4a566eba49eb206b969ef423295e79b50503df567bbdb2e8877d78892.txt +8 -0
- package/dist/licenses/npm/texts/3f1189b28e3866e0d979968d466b78f813f76827cfdca1fbb124cc0a5c8841f8.txt +21 -0
- package/dist/licenses/npm/texts/5b37224c080cdcc97c871ada971c224e9926370fe74f11b539aa1cf9f3b1aca1.txt +21 -0
- package/dist/licenses/npm/texts/c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4.txt +201 -0
- package/dist/sqlite3.wasm +0 -0
- package/embedding-manifest.json +210 -0
- package/ios/Sources/GezelCapacitor/GezelKnowledgePlugin.swift +200 -0
- package/ios/Sources/GezelCapacitor/GezelOfflineSpeechPlugin.swift +484 -0
- package/ios/Sources/GezelCapacitor/GezelRuntimePlugin.swift +102 -0
- package/kokoro-lexicon/LICENSE +202 -0
- package/kokoro-lexicon/lexicon-gb-en.txt.gz +0 -0
- package/kokoro-lexicon/lexicon-us-en.txt.gz +0 -0
- package/native/android/LICENSE-gezel.txt +21 -0
- package/native/android/PUBLIC-DISTRIBUTION.md +97 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
- package/native/android/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
- package/native/android/THIRD_PARTY_LICENSES/manifest.json +21 -0
- package/native/android/engine-manifest.json +88 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar +0 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.aar.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom +8 -0
- package/native/android/maven/com/bendyline/gezel/gezel-llama/0.1.57/gezel-llama-0.1.57.pom.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar +0 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57-sources.jar.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar +0 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.aar.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module +118 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.module.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom +51 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/0.1.57/gezel-runtime-0.1.57.pom.sha512 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml +13 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.md5 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha1 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha256 +1 -0
- package/native/android/maven/com/bendyline/gezel/gezel-runtime/maven-metadata.xml.sha512 +1 -0
- package/native/android/sdk-manifest.json +98 -0
- package/native/ios/GezelLlama.xcframework/Info.plist +47 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/gezel_llama.h +290 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64/Headers/module.modulemap +9 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64/libGezelLlama.a +0 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/gezel_llama.h +290 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/Headers/module.modulemap +9 -0
- package/native/ios/GezelLlama.xcframework/ios-arm64-simulator/libGezelLlama.a +0 -0
- package/native/ios/LICENSE-gezel.txt +21 -0
- package/native/ios/LICENSE-ggml.txt +21 -0
- package/native/ios/LICENSE-llama-cpp.txt +21 -0
- package/native/ios/PUBLIC-DISTRIBUTION.md +97 -0
- package/native/ios/Package.swift +17 -0
- package/native/ios/Sources/GezelModelStorage/MobileModelStore.swift +286 -0
- package/native/ios/Sources/GezelModelStorage/ModelDownloadHTTP.swift +55 -0
- package/native/ios/Sources/GezelModelStorage/ModelDownloadSource.swift +46 -0
- package/native/ios/Sources/GezelModelStorage/ModelDownloads.swift +289 -0
- package/native/ios/Sources/GezelModelStorage/PrivacyInfo.xcprivacy +32 -0
- package/native/ios/Sources/GezelRuntime/AppleFoundationProvider.swift +292 -0
- package/native/ios/Sources/GezelRuntime/AppleNativeTools.swift +152 -0
- package/native/ios/Sources/GezelRuntime/GezelNativeRuntime.swift +1349 -0
- package/native/ios/Sources/GezelRuntime/NativeCall.swift +35 -0
- package/native/ios/Sources/GezelRuntime/PrivacyInfo.xcprivacy +31 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-arm-optimized-routines-MIT-OR-Apache-2.0.txt +249 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-cpp-httplib-MIT.txt +22 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-fp16-MIT.txt +11 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-ggml-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-llamafile-sgemm-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-openvino-APACHE.txt +201 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-pillow-MIT-CMU.txt +30 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-rotate-bits-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-llama-cpp-unicode-data-Unicode-3.0.txt +39 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nlohmann-json-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-nvidia-cub-BSD-3-Clause.txt +24 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-headers-Apache-2.0-OR-MIT.txt +33 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-APACHE.txt +201 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-vulkan-loader-cjson-MIT.txt +32 -0
- package/native/ios/THIRD_PARTY_LICENSES/LICENSE-yarn-MIT.txt +21 -0
- package/native/ios/THIRD_PARTY_LICENSES/manifest.json +21 -0
- package/native/ios/engine-manifest.json +102 -0
- package/native/ios/sdk-manifest.json +89 -0
- package/package.json +94 -3
- package/scripts/embedding-package.d.mts +23 -0
- package/scripts/embedding-package.mjs +185 -0
- package/scripts/stage-native.mjs +168 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
package com.bendyline.gezel.capacitor;
|
|
2
|
+
|
|
3
|
+
import android.app.ActivityManager;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.os.Build;
|
|
6
|
+
import android.os.PowerManager;
|
|
7
|
+
import android.os.SystemClock;
|
|
8
|
+
import android.util.Base64;
|
|
9
|
+
import com.bendyline.gezel.speech.SpeechRuntime;
|
|
10
|
+
import com.getcapacitor.*;
|
|
11
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
12
|
+
import java.io.File;
|
|
13
|
+
import java.nio.charset.StandardCharsets;
|
|
14
|
+
import java.util.*;
|
|
15
|
+
import java.util.concurrent.*;
|
|
16
|
+
import org.json.JSONArray;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Offline speech: Whisper dictation and Kokoro narration on the optional
|
|
20
|
+
* gezel-speech runtime, with models the host downloads on request. One
|
|
21
|
+
* request runs at a time; models stay loaded between requests and are
|
|
22
|
+
* released after a quiet spell or when the app leaves the foreground.
|
|
23
|
+
*/
|
|
24
|
+
@CapacitorPlugin(name = "GezelOfflineSpeech")
|
|
25
|
+
public final class GezelOfflineSpeechPlugin extends Plugin {
|
|
26
|
+
private static final long IDLE_RELEASE_MS = 120_000;
|
|
27
|
+
private final ExecutorService worker = Executors.newSingleThreadExecutor();
|
|
28
|
+
private final ExecutorService downloads = Executors.newSingleThreadExecutor();
|
|
29
|
+
private final ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
|
|
30
|
+
private SpeechModels models;
|
|
31
|
+
private long engine;
|
|
32
|
+
private String activeId, downloading;
|
|
33
|
+
private boolean stopped, backgrounded, downloadPaused;
|
|
34
|
+
private ScheduledFuture<?> idle;
|
|
35
|
+
private final List<PluginCall> waiters = new ArrayList<>();
|
|
36
|
+
|
|
37
|
+
@Override public void load() { models = new SpeechModels(getContext()); }
|
|
38
|
+
|
|
39
|
+
@PluginMethod public void status(PluginCall call) {
|
|
40
|
+
try {
|
|
41
|
+
JSObject result = new JSObject().put("available", SpeechRuntime.available()).put("models", models.list());
|
|
42
|
+
if (!SpeechRuntime.available()) result.put("reason", SpeechRuntime.unavailableReason());
|
|
43
|
+
synchronized (this) { if (downloading != null) result.put("downloading", downloading); }
|
|
44
|
+
call.resolve(result);
|
|
45
|
+
} catch (Exception error) { call.reject("Speech models could not be read", "failed"); }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@PluginMethod public void install(PluginCall call) {
|
|
49
|
+
String id;
|
|
50
|
+
try { id = SpeechModels.id(call.getString("id")); }
|
|
51
|
+
catch (Exception invalid) { call.reject("Invalid speech model", "invalid-input"); return; }
|
|
52
|
+
JSONArray files = call.getArray("files");
|
|
53
|
+
synchronized (this) {
|
|
54
|
+
if (downloading != null) { call.reject("Another speech model is downloading", "busy"); return; }
|
|
55
|
+
if (backgrounded) { call.reject("Speech downloads run in the foreground", "paused"); return; }
|
|
56
|
+
downloading = id; downloadPaused = false;
|
|
57
|
+
}
|
|
58
|
+
downloads.execute(() -> {
|
|
59
|
+
try {
|
|
60
|
+
models.install(id, files, (received, total) -> notifyListeners("speechDownload",
|
|
61
|
+
new JSObject().put("id", id).put("receivedBytes", received).put("totalBytes", total)));
|
|
62
|
+
call.resolve(new JSObject().put("id", id));
|
|
63
|
+
} catch (CancellationException cancelled) {
|
|
64
|
+
boolean paused; synchronized (this) { paused = downloadPaused; }
|
|
65
|
+
call.reject(paused ? "Download paused when the app left the foreground" : "Download cancelled", paused ? "paused" : "cancelled");
|
|
66
|
+
} catch (SpeechModels.Failure failure) { call.reject(failure.getMessage(), failure.code); }
|
|
67
|
+
catch (Exception error) { call.reject(error.getMessage() == null ? "The speech model could not be saved" : error.getMessage(), "failed"); }
|
|
68
|
+
finally { synchronized (this) { downloading = null; } }
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@PluginMethod public void cancelInstall(PluginCall call) { models.cancel(); call.resolve(); }
|
|
73
|
+
|
|
74
|
+
@PluginMethod public void remove(PluginCall call) {
|
|
75
|
+
String id = call.getString("id");
|
|
76
|
+
synchronized (this) {
|
|
77
|
+
if (id != null && id.equals(downloading)) { call.reject("Cancel the download before removing this model", "busy"); return; }
|
|
78
|
+
if (activeId != null) { call.reject("Speech is busy", "busy"); return; }
|
|
79
|
+
}
|
|
80
|
+
worker.execute(() -> {
|
|
81
|
+
try {
|
|
82
|
+
synchronized (this) { if (engine != 0) SpeechRuntime.release(engine); }
|
|
83
|
+
models.remove(id);
|
|
84
|
+
call.resolve();
|
|
85
|
+
} catch (SpeechModels.Failure failure) { call.reject(failure.getMessage(), failure.code); }
|
|
86
|
+
catch (Exception error) { call.reject("The speech model could not be removed", "failed"); }
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@PluginMethod public void transcribe(PluginCall call) { run(call, false); }
|
|
91
|
+
@PluginMethod public void synthesize(PluginCall call) { run(call, true); }
|
|
92
|
+
|
|
93
|
+
private void run(PluginCall call, boolean synthesis) {
|
|
94
|
+
String id = call.getString("requestId");
|
|
95
|
+
try { UUID.fromString(id); } catch (Exception invalid) { call.reject("A speech request identity is required", "invalid-input"); return; }
|
|
96
|
+
if (!SpeechRuntime.available()) { call.reject(SpeechRuntime.unavailableReason(), "unavailable"); return; }
|
|
97
|
+
synchronized (this) {
|
|
98
|
+
if (activeId != null) { call.reject("Speech is busy", "busy"); return; }
|
|
99
|
+
if (backgrounded) { call.reject("Speech runs while the app is in the foreground", "cancelled"); return; }
|
|
100
|
+
activeId = id; stopped = false;
|
|
101
|
+
if (idle != null) idle.cancel(false);
|
|
102
|
+
}
|
|
103
|
+
worker.execute(() -> {
|
|
104
|
+
JSObject result = null; String failure = null, code = "failed";
|
|
105
|
+
long started = SystemClock.uptimeMillis();
|
|
106
|
+
try {
|
|
107
|
+
File model = models.file(call.getString("model"), call.getString("file"));
|
|
108
|
+
checkResources();
|
|
109
|
+
long handle = begin();
|
|
110
|
+
if (synthesis) {
|
|
111
|
+
File voices = models.file(call.getString("model"), call.getString("voices"));
|
|
112
|
+
JSONArray encoded = call.getArray("tokens");
|
|
113
|
+
if (encoded == null || encoded.length() < 3 || encoded.length() > 511)
|
|
114
|
+
throw new SpeechModels.Failure("invalid-input", "Speech phonemes are missing or too long");
|
|
115
|
+
int[] tokens = new int[encoded.length()];
|
|
116
|
+
for (int i = 0; i < tokens.length; i++) {
|
|
117
|
+
tokens[i] = encoded.getInt(i);
|
|
118
|
+
if (tokens[i] < 0 || tokens[i] > 177) throw new SpeechModels.Failure("invalid-input", "Invalid speech phoneme");
|
|
119
|
+
}
|
|
120
|
+
float speed = (float) Math.min(2, Math.max(.5, call.getDouble("speed", 1.0)));
|
|
121
|
+
float[][] output = SpeechRuntime.kokoro(handle, model.getPath(), voices.getPath(), call.getInt("voice", 0), tokens, speed);
|
|
122
|
+
result = new JSObject().put("pcm", Base64.encodeToString(pcm16(output[0]), Base64.NO_WRAP))
|
|
123
|
+
.put("sampleRate", 24000).put("durations", new JSONArray(output[1]));
|
|
124
|
+
} else {
|
|
125
|
+
String encoded = call.getString("audio", "");
|
|
126
|
+
if (encoded.isEmpty() || encoded.length() > 5_120_000) throw new SpeechModels.Failure("invalid-input", "Use up to two minutes of speech");
|
|
127
|
+
byte[] pcm = Base64.decode(encoded, Base64.NO_WRAP);
|
|
128
|
+
if (pcm.length == 0 || pcm.length % 2 != 0) throw new SpeechModels.Failure("invalid-input", "Invalid speech recording");
|
|
129
|
+
byte[] json = SpeechRuntime.transcribe(handle, model.getPath(), pcm, call.getString("language"),
|
|
130
|
+
call.getString("prompt"), Boolean.TRUE.equals(call.getBoolean("words", false)));
|
|
131
|
+
result = new JSObject(new String(json, StandardCharsets.UTF_8));
|
|
132
|
+
}
|
|
133
|
+
result.put("durationMs", SystemClock.uptimeMillis() - started);
|
|
134
|
+
} catch (CancellationException cancelled) { failure = "Speech stopped"; code = "cancelled"; }
|
|
135
|
+
catch (SpeechModels.Failure error) { failure = error.getMessage(); code = error.code; }
|
|
136
|
+
catch (OutOfMemoryError error) { failure = "Not enough memory for speech"; code = "resources"; }
|
|
137
|
+
catch (Exception | LinkageError error) { failure = error.getMessage(); }
|
|
138
|
+
finally {
|
|
139
|
+
synchronized (this) {
|
|
140
|
+
if (stopped) { failure = "Speech stopped"; code = "cancelled"; }
|
|
141
|
+
activeId = null;
|
|
142
|
+
if (failure == null && result != null) call.resolve(result); else call.reject(failure == null ? "Speech failed" : failure, code);
|
|
143
|
+
for (PluginCall waiter : waiters) waiter.resolve();
|
|
144
|
+
waiters.clear();
|
|
145
|
+
scheduleRelease(backgrounded ? 0 : IDLE_RELEASE_MS);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The long-lived engine, cleared of any earlier cancel; serialized with stop(). */
|
|
152
|
+
private synchronized long begin() {
|
|
153
|
+
if (stopped || backgrounded) throw new CancellationException();
|
|
154
|
+
if (engine == 0) engine = SpeechRuntime.create();
|
|
155
|
+
if (engine == 0) throw new OutOfMemoryError();
|
|
156
|
+
SpeechRuntime.begin(engine);
|
|
157
|
+
return engine;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private static byte[] pcm16(float[] samples) {
|
|
161
|
+
byte[] out = new byte[samples.length * 2];
|
|
162
|
+
for (int i = 0; i < samples.length; i++) {
|
|
163
|
+
float sample = Float.isFinite(samples[i]) ? Math.max(-1f, Math.min(1f, samples[i])) : 0f;
|
|
164
|
+
int value = Math.round(sample * (sample < 0 ? 32768f : 32767f));
|
|
165
|
+
out[2 * i] = (byte) value;
|
|
166
|
+
out[2 * i + 1] = (byte) (value >> 8);
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private void checkResources() throws SpeechModels.Failure {
|
|
172
|
+
ActivityManager manager = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
|
|
173
|
+
ActivityManager.MemoryInfo memory = new ActivityManager.MemoryInfo();
|
|
174
|
+
manager.getMemoryInfo(memory);
|
|
175
|
+
if (memory.lowMemory || memory.availMem < memory.threshold + 512L * 1024 * 1024)
|
|
176
|
+
throw new SpeechModels.Failure("resources", "Not enough memory for speech. Close other apps and try again.");
|
|
177
|
+
if (Build.VERSION.SDK_INT >= 29 && ((PowerManager) getContext().getSystemService(Context.POWER_SERVICE)).getCurrentThermalStatus() >= PowerManager.THERMAL_STATUS_SEVERE)
|
|
178
|
+
throw new SpeechModels.Failure("resources", "Let this device cool before running speech");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private synchronized void scheduleRelease(long delay) {
|
|
182
|
+
if (idle != null) idle.cancel(false);
|
|
183
|
+
idle = timer.schedule(() -> worker.execute(() -> {
|
|
184
|
+
synchronized (this) { if (activeId == null && engine != 0) SpeechRuntime.release(engine); }
|
|
185
|
+
}), delay, TimeUnit.MILLISECONDS);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private synchronized void stop() {
|
|
189
|
+
stopped = true;
|
|
190
|
+
if (engine != 0) SpeechRuntime.cancel(engine);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@PluginMethod public synchronized void cancel(PluginCall call) {
|
|
194
|
+
if (activeId != null && activeId.equals(call.getString("requestId"))) { waiters.add(call); stop(); }
|
|
195
|
+
else call.resolve();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
@PluginMethod public void release(PluginCall call) { scheduleRelease(0); call.resolve(); }
|
|
199
|
+
|
|
200
|
+
@Override protected void handleOnStop() {
|
|
201
|
+
synchronized (this) {
|
|
202
|
+
backgrounded = true;
|
|
203
|
+
if (activeId != null) stop();
|
|
204
|
+
if (downloading != null) downloadPaused = true;
|
|
205
|
+
scheduleRelease(0);
|
|
206
|
+
}
|
|
207
|
+
models.cancel();
|
|
208
|
+
}
|
|
209
|
+
@Override protected synchronized void handleOnStart() { backgrounded = false; }
|
|
210
|
+
@Override protected void handleOnDestroy() {
|
|
211
|
+
synchronized (this) { backgrounded = true; if (activeId != null) stop(); }
|
|
212
|
+
models.cancel();
|
|
213
|
+
worker.execute(() -> { synchronized (this) { if (engine != 0) SpeechRuntime.destroy(engine); engine = 0; } });
|
|
214
|
+
worker.shutdown(); downloads.shutdown(); timer.shutdown();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
package com.bendyline.gezel.capacitor;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.content.Intent;
|
|
5
|
+
import android.net.Uri;
|
|
6
|
+
import androidx.activity.result.ActivityResult;
|
|
7
|
+
import com.getcapacitor.*;
|
|
8
|
+
import com.getcapacitor.annotation.ActivityCallback;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
import com.bendyline.gezel.runtime.GezelNativeRuntime;
|
|
11
|
+
import com.bendyline.gezel.runtime.NativeCall;
|
|
12
|
+
import org.json.JSONObject;
|
|
13
|
+
|
|
14
|
+
@CapacitorPlugin(name = "GezelRuntime")
|
|
15
|
+
public final class GezelRuntimePlugin extends Plugin {
|
|
16
|
+
private GezelNativeRuntime runtime;
|
|
17
|
+
private AutoCloseable listener;
|
|
18
|
+
private boolean pickingModel;
|
|
19
|
+
private boolean foreground;
|
|
20
|
+
// Registration must not initialize engines or model storage in opt-in hosts.
|
|
21
|
+
private synchronized GezelNativeRuntime runtime() {
|
|
22
|
+
if (runtime == null) {
|
|
23
|
+
runtime = GezelNativeRuntime.shared(getContext());
|
|
24
|
+
listener = runtime.listen((name, data) -> notifyListeners(name, toJS(data)));
|
|
25
|
+
if (foreground) runtime.onForeground();
|
|
26
|
+
}
|
|
27
|
+
return runtime;
|
|
28
|
+
}
|
|
29
|
+
public static JSObject toJS(JSONObject value) {
|
|
30
|
+
JSObject copy = new JSObject();
|
|
31
|
+
for (var keys = value.keys(); keys.hasNext();) { String key = keys.next(); copy.put(key, value.opt(key)); }
|
|
32
|
+
return copy;
|
|
33
|
+
}
|
|
34
|
+
public static NativeCall adapt(PluginCall call) {
|
|
35
|
+
return new NativeCall(call.getData(), new NativeCall.Reply() {
|
|
36
|
+
public void resolve(JSONObject value) { call.resolve(toJS(value)); }
|
|
37
|
+
public void reject(String message, String code) { call.reject(message, code); }
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
@PluginMethod public void providers(PluginCall call) { runtime().providers(adapt(call)); }
|
|
41
|
+
@PluginMethod public void prepareProvider(PluginCall call) { runtime().prepareProvider(adapt(call)); }
|
|
42
|
+
@PluginMethod public void cancelProviderPreparation(PluginCall call) { runtime().cancelProviderPreparation(adapt(call)); }
|
|
43
|
+
@PluginMethod public void generate(PluginCall call) { runtime().generate(adapt(call)); }
|
|
44
|
+
@PluginMethod public void chat(PluginCall call) { runtime().chat(adapt(call)); }
|
|
45
|
+
@PluginMethod public void describeImage(PluginCall call) { runtime().describeImage(adapt(call)); }
|
|
46
|
+
@PluginMethod public void cancel(PluginCall call) { runtime().cancel(adapt(call)); }
|
|
47
|
+
@PluginMethod public void releaseModel(PluginCall call) { runtime().releaseModel(adapt(call)); }
|
|
48
|
+
@PluginMethod public void listModels(PluginCall call) { runtime().listModels(adapt(call)); }
|
|
49
|
+
@PluginMethod public void selectModel(PluginCall call) { runtime().selectModel(adapt(call)); }
|
|
50
|
+
@PluginMethod public void removeModel(PluginCall call) { runtime().removeModel(adapt(call)); }
|
|
51
|
+
@PluginMethod public void resolveModelSource(PluginCall call) { runtime().resolveModelSource(adapt(call)); }
|
|
52
|
+
@PluginMethod public void cancelModelSourceResolution(PluginCall call) { runtime().cancelModelSourceResolution(adapt(call)); }
|
|
53
|
+
@PluginMethod public void listModelDownloads(PluginCall call) { runtime().listModelDownloads(adapt(call)); }
|
|
54
|
+
@PluginMethod public void startModelDownload(PluginCall call) { runtime().startModelDownload(adapt(call)); }
|
|
55
|
+
@PluginMethod public void resumeModelDownload(PluginCall call) { runtime().resumeModelDownload(adapt(call)); }
|
|
56
|
+
@PluginMethod public void cancelModelDownload(PluginCall call) { runtime().cancelModelDownload(adapt(call)); }
|
|
57
|
+
@PluginMethod public void removeModelDownload(PluginCall call) { runtime().removeModelDownload(adapt(call)); }
|
|
58
|
+
@PluginMethod public void importModel(PluginCall call) {
|
|
59
|
+
if (!runtime().reserveModelMutation()) { call.reject("Finish the current operation before importing", "BUSY"); return; }
|
|
60
|
+
pickingModel = true;
|
|
61
|
+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
62
|
+
intent.setType("application/octet-stream");
|
|
63
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
64
|
+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
65
|
+
try { startActivityForResult(call, intent, "modelPicked"); }
|
|
66
|
+
catch (Exception error) { pickingModel = false; runtime.releaseModelMutation(); call.reject("Document picker unavailable", error); }
|
|
67
|
+
}
|
|
68
|
+
@ActivityCallback private void modelPicked(PluginCall call, ActivityResult result) {
|
|
69
|
+
if (!pickingModel) { if (call != null) call.reject("Document picker closed", "CANCELLED"); return; }
|
|
70
|
+
pickingModel = false;
|
|
71
|
+
runtime.releaseModelMutation();
|
|
72
|
+
if (call == null) return;
|
|
73
|
+
Uri uri = result.getData() == null ? null : result.getData().getData();
|
|
74
|
+
if (result.getResultCode() != Activity.RESULT_OK || uri == null) { call.resolve(new JSObject().put("model", JSONObject.NULL)); return; }
|
|
75
|
+
runtime.importModel(adapt(call), uri);
|
|
76
|
+
}
|
|
77
|
+
// Start and stop pair per activity; the runtime counts them.
|
|
78
|
+
@Override protected synchronized void handleOnStart() { foreground = true; if (runtime != null) runtime.onForeground(); }
|
|
79
|
+
@Override protected synchronized void handleOnStop() { foreground = false; if (runtime != null) runtime.onBackground(); }
|
|
80
|
+
@Override protected void handleOnDestroy() {
|
|
81
|
+
if (pickingModel) { pickingModel = false; runtime.releaseModelMutation(); }
|
|
82
|
+
try { if (listener != null) listener.close(); } catch (Exception ignored) {}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package com.bendyline.gezel.capacitor;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.net.Uri;
|
|
5
|
+
import android.webkit.WebResourceRequest;
|
|
6
|
+
import android.webkit.WebResourceResponse;
|
|
7
|
+
import java.io.ByteArrayInputStream;
|
|
8
|
+
import java.io.File;
|
|
9
|
+
import java.io.FileInputStream;
|
|
10
|
+
import java.io.FilterInputStream;
|
|
11
|
+
import java.io.IOException;
|
|
12
|
+
import java.io.InputStream;
|
|
13
|
+
import java.nio.charset.StandardCharsets;
|
|
14
|
+
import java.util.HashMap;
|
|
15
|
+
import java.util.Map;
|
|
16
|
+
import java.util.regex.Matcher;
|
|
17
|
+
import java.util.regex.Pattern;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Byte ranges of installed knowledge catalogs for the reader that runs in the
|
|
21
|
+
* app's WebView, where SQLite reads catalog pages with synchronous range
|
|
22
|
+
* requests. Capacitor's own file server serves any app file and sizes ranges
|
|
23
|
+
* with InputStream.available(), an int; this one is confined to the catalog
|
|
24
|
+
* store and sized from the file. Apps call {@link #intercept} first from their
|
|
25
|
+
* BridgeWebViewClient's shouldInterceptRequest.
|
|
26
|
+
*
|
|
27
|
+
* Read-only and confined to the catalog store; catalogs are public reference
|
|
28
|
+
* data, never the person's documents.
|
|
29
|
+
*/
|
|
30
|
+
public final class KnowledgeFiles {
|
|
31
|
+
public static final String PREFIX = "/_gezel_knowledge/";
|
|
32
|
+
private static final Pattern SEGMENT = Pattern.compile("[A-Za-z0-9][A-Za-z0-9._-]{0,127}");
|
|
33
|
+
private static final Pattern RANGE = Pattern.compile("bytes=(\\d+)-(\\d*)");
|
|
34
|
+
private static final int MAX_RANGE = 8 * 1024 * 1024;
|
|
35
|
+
private KnowledgeFiles() {}
|
|
36
|
+
|
|
37
|
+
public static File root(Context context) { return new File(context.getNoBackupFilesDir(), "gezel-knowledge/catalogs"); }
|
|
38
|
+
|
|
39
|
+
/** The response for a catalog byte range, or null when the request is not one. */
|
|
40
|
+
public static WebResourceResponse intercept(Context context, WebResourceRequest request) {
|
|
41
|
+
Uri url = request.getUrl();
|
|
42
|
+
String path = url.getPath();
|
|
43
|
+
if (!"https".equals(url.getScheme()) || !"localhost".equals(url.getHost()) || path == null || !path.startsWith(PREFIX)) return null;
|
|
44
|
+
if (!"GET".equals(request.getMethod())) return status(405, "Method Not Allowed");
|
|
45
|
+
String[] parts = path.substring(PREFIX.length()).split("/", -1);
|
|
46
|
+
if (parts.length == 0 || parts.length > 12) return status(404, "Not Found");
|
|
47
|
+
for (String part : parts) if (!SEGMENT.matcher(part).matches()) return status(404, "Not Found");
|
|
48
|
+
try {
|
|
49
|
+
File root = root(context).getCanonicalFile();
|
|
50
|
+
File file = new File(root, String.join("/", parts)).getCanonicalFile();
|
|
51
|
+
if (!file.getPath().startsWith(root.getPath() + File.separator) || !file.isFile()) return status(404, "Not Found");
|
|
52
|
+
long size = file.length();
|
|
53
|
+
String header = request.getRequestHeaders().get("Range");
|
|
54
|
+
if (header == null) header = request.getRequestHeaders().get("range");
|
|
55
|
+
Matcher range = header == null ? null : RANGE.matcher(header);
|
|
56
|
+
if (range == null || !range.matches()) return status(416, "Range Not Satisfiable");
|
|
57
|
+
long start = Long.parseLong(range.group(1));
|
|
58
|
+
long end = range.group(2).isEmpty() ? size - 1 : Long.parseLong(range.group(2));
|
|
59
|
+
end = Math.min(end, size - 1);
|
|
60
|
+
if (start > end || end - start + 1 > MAX_RANGE) return status(416, "Range Not Satisfiable");
|
|
61
|
+
Map<String, String> headers = new HashMap<>();
|
|
62
|
+
headers.put("Content-Range", "bytes " + start + "-" + end + "/" + size);
|
|
63
|
+
headers.put("Accept-Ranges", "bytes");
|
|
64
|
+
headers.put("Cache-Control", "no-store");
|
|
65
|
+
// WebView skips to the range's first byte itself but reads on to the end,
|
|
66
|
+
// so the stream starts at byte 0 like the file and ends after the range.
|
|
67
|
+
return new WebResourceResponse("application/octet-stream", null, 206, "Partial Content", headers, new Bounded(new FileInputStream(file), end + 1));
|
|
68
|
+
} catch (Exception error) {
|
|
69
|
+
return status(500, "Internal Server Error");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A file stream that ends at `limit`, counting the bytes WebView skips. */
|
|
74
|
+
private static final class Bounded extends FilterInputStream {
|
|
75
|
+
private long position;
|
|
76
|
+
private final long limit;
|
|
77
|
+
Bounded(InputStream input, long limit) { super(input); this.limit = limit; }
|
|
78
|
+
@Override public int read() throws IOException {
|
|
79
|
+
if (position >= limit) return -1;
|
|
80
|
+
int value = super.read();
|
|
81
|
+
if (value >= 0) position++;
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
@Override public int read(byte[] buffer, int offset, int length) throws IOException {
|
|
85
|
+
if (position >= limit) return -1;
|
|
86
|
+
int read = super.read(buffer, offset, (int) Math.min(length, limit - position));
|
|
87
|
+
if (read > 0) position += read;
|
|
88
|
+
return read;
|
|
89
|
+
}
|
|
90
|
+
@Override public long skip(long count) throws IOException {
|
|
91
|
+
long skipped = super.skip(Math.min(count, Math.max(0, limit - position)));
|
|
92
|
+
position += skipped;
|
|
93
|
+
return skipped;
|
|
94
|
+
}
|
|
95
|
+
@Override public int available() throws IOException { return (int) Math.min(super.available(), Math.max(0, limit - position)); }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private static WebResourceResponse status(int code, String reason) {
|
|
99
|
+
return new WebResourceResponse("text/plain", "utf-8", code, reason, new HashMap<>(), new ByteArrayInputStream(reason.getBytes(StandardCharsets.UTF_8)));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
package com.bendyline.gezel.capacitor;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.util.AtomicFile;
|
|
5
|
+
import java.io.*;
|
|
6
|
+
import java.net.HttpURLConnection;
|
|
7
|
+
import java.net.URL;
|
|
8
|
+
import java.nio.charset.StandardCharsets;
|
|
9
|
+
import java.nio.file.Files;
|
|
10
|
+
import java.security.MessageDigest;
|
|
11
|
+
import java.util.*;
|
|
12
|
+
import java.util.concurrent.CancellationException;
|
|
13
|
+
import java.util.regex.Pattern;
|
|
14
|
+
import org.json.JSONArray;
|
|
15
|
+
import org.json.JSONObject;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Downloaded model files in app-private, no-backup storage (speech models and
|
|
19
|
+
* knowledge catalogs, each in its own store): one folder per model,
|
|
20
|
+
* declared files only, each verified against its SHA-256 before the model
|
|
21
|
+
* counts as installed. JavaScript names models and files; it never supplies
|
|
22
|
+
* paths, and every path is checked to stay inside the store.
|
|
23
|
+
*/
|
|
24
|
+
final class SpeechModels {
|
|
25
|
+
static final class Failure extends IOException {
|
|
26
|
+
final String code;
|
|
27
|
+
Failure(String code, String message) { super(message); this.code = code; }
|
|
28
|
+
}
|
|
29
|
+
interface Progress { void update(long received, long total); }
|
|
30
|
+
|
|
31
|
+
private static final Pattern ID = Pattern.compile("[a-z0-9][a-z0-9._-]{0,63}");
|
|
32
|
+
private static final Pattern SEGMENT = Pattern.compile("[A-Za-z0-9][A-Za-z0-9._-]{0,127}");
|
|
33
|
+
private static final Pattern SHA256 = Pattern.compile("[0-9a-f]{64}");
|
|
34
|
+
private static final String MARKER = "installed.json";
|
|
35
|
+
private final File root;
|
|
36
|
+
private final long maxFile;
|
|
37
|
+
private volatile boolean cancelled;
|
|
38
|
+
private volatile HttpURLConnection connection;
|
|
39
|
+
|
|
40
|
+
SpeechModels(Context context) { this(new File(context.getNoBackupFilesDir(), "gezel-speech/models"), 1L << 30); }
|
|
41
|
+
SpeechModels(File root, long maxFile) { this.root = root; this.maxFile = maxFile; }
|
|
42
|
+
File root() { return root; }
|
|
43
|
+
|
|
44
|
+
static String id(String value) throws IOException {
|
|
45
|
+
if (value == null || !ID.matcher(value).matches()) throw new Failure("invalid-input", "Invalid speech model");
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
static String name(String value) throws IOException {
|
|
49
|
+
if (value == null || value.length() > 256) throw new Failure("invalid-input", "Invalid speech model file");
|
|
50
|
+
String[] parts = value.split("/", -1);
|
|
51
|
+
if (parts.length > 3) throw new Failure("invalid-input", "Invalid speech model file");
|
|
52
|
+
for (String part : parts) if (!SEGMENT.matcher(part).matches() || part.endsWith(".part")) throw new Failure("invalid-input", "Invalid speech model file");
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
private File inside(File folder, String relative) throws IOException {
|
|
56
|
+
File file = new File(folder, relative);
|
|
57
|
+
if (!file.getCanonicalPath().startsWith(folder.getCanonicalPath() + File.separator)) throw new Failure("invalid-input", "Invalid speech model file");
|
|
58
|
+
return file;
|
|
59
|
+
}
|
|
60
|
+
private File folder(String model) throws IOException {
|
|
61
|
+
File folder = new File(root, id(model));
|
|
62
|
+
if (Files.isSymbolicLink(folder.toPath())) throw new Failure("failed", "Invalid speech model storage");
|
|
63
|
+
return folder;
|
|
64
|
+
}
|
|
65
|
+
private static JSONObject read(File file) throws Exception {
|
|
66
|
+
try (FileInputStream input = new AtomicFile(file).openRead()) {
|
|
67
|
+
long size = input.getChannel().size();
|
|
68
|
+
if (size > 64 * 1024) throw new IOException("Speech model record exceeds its limit");
|
|
69
|
+
byte[] bytes = new byte[(int) size];
|
|
70
|
+
new DataInputStream(input).readFully(bytes);
|
|
71
|
+
return new JSONObject(new String(bytes, StandardCharsets.UTF_8));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Installed files by name, or null when the model is not installed. */
|
|
75
|
+
private Map<String, String> installed(String model) throws Exception {
|
|
76
|
+
File marker = new File(folder(model), MARKER);
|
|
77
|
+
if (!marker.isFile()) return null;
|
|
78
|
+
JSONObject files = read(marker).getJSONObject("files");
|
|
79
|
+
Map<String, String> result = new TreeMap<>();
|
|
80
|
+
for (Iterator<String> names = files.keys(); names.hasNext();) { String name = names.next(); result.put(name(name), files.getString(name)); }
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
/** A verified file of an installed model. */
|
|
84
|
+
synchronized File file(String model, String relative) throws Exception {
|
|
85
|
+
Map<String, String> files = installed(model);
|
|
86
|
+
if (files == null) throw new Failure("model-missing", "Download this speech model first");
|
|
87
|
+
if (!files.containsKey(name(relative))) throw new Failure("invalid-input", "This file is not part of the installed model");
|
|
88
|
+
File file = inside(folder(model), relative);
|
|
89
|
+
if (!file.isFile()) throw new Failure("model-missing", "The speech model is incomplete; download it again");
|
|
90
|
+
return file;
|
|
91
|
+
}
|
|
92
|
+
synchronized JSONArray list() throws Exception {
|
|
93
|
+
JSONArray models = new JSONArray();
|
|
94
|
+
File[] folders = root.listFiles();
|
|
95
|
+
if (folders == null) return models;
|
|
96
|
+
for (File folder : folders) {
|
|
97
|
+
if (!folder.isDirectory() || !ID.matcher(folder.getName()).matches()) continue;
|
|
98
|
+
Map<String, String> files = installed(folder.getName());
|
|
99
|
+
if (files != null) models.put(new JSONObject().put("id", folder.getName()).put("files", new JSONArray(files.keySet())));
|
|
100
|
+
}
|
|
101
|
+
return models;
|
|
102
|
+
}
|
|
103
|
+
synchronized void remove(String model) throws Exception {
|
|
104
|
+
File folder = folder(model);
|
|
105
|
+
if (folder.exists()) delete(folder);
|
|
106
|
+
}
|
|
107
|
+
private static void delete(File file) throws IOException {
|
|
108
|
+
if (file.isDirectory() && !Files.isSymbolicLink(file.toPath())) for (File child : Objects.requireNonNull(file.listFiles())) delete(child);
|
|
109
|
+
Files.deleteIfExists(file.toPath());
|
|
110
|
+
}
|
|
111
|
+
void cancel() { cancelled = true; HttpURLConnection current = connection; if (current != null) current.disconnect(); }
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Download and verify every file, resuming any partial file from where it
|
|
115
|
+
* stopped. A file that fails verification is deleted so the next attempt
|
|
116
|
+
* starts clean. Runs on the caller's thread; {@link #cancel()} stops it.
|
|
117
|
+
*/
|
|
118
|
+
void install(String model, JSONArray specs, Progress progress) throws Exception {
|
|
119
|
+
cancelled = false;
|
|
120
|
+
File folder = folder(model);
|
|
121
|
+
long total = 0;
|
|
122
|
+
List<JSONObject> files = new ArrayList<>();
|
|
123
|
+
Map<String, String> expected = new TreeMap<>();
|
|
124
|
+
if (specs == null || specs.length() == 0 || specs.length() > 64) throw new Failure("invalid-input", "Invalid speech model files");
|
|
125
|
+
for (int i = 0; i < specs.length(); i++) {
|
|
126
|
+
JSONObject spec = specs.getJSONObject(i);
|
|
127
|
+
String name = name(spec.getString("name")), sha = spec.getString("sha256"), url = spec.getString("url");
|
|
128
|
+
long size = spec.getLong("size");
|
|
129
|
+
if (!SHA256.matcher(sha).matches() || size < 1 || size > maxFile || !url.startsWith("https://huggingface.co/") || expected.put(name, sha) != null)
|
|
130
|
+
throw new Failure("invalid-input", "Invalid speech model files");
|
|
131
|
+
files.add(spec);
|
|
132
|
+
total += size;
|
|
133
|
+
}
|
|
134
|
+
final long all = total;
|
|
135
|
+
synchronized (this) {
|
|
136
|
+
if (!folder.isDirectory() && !folder.mkdirs()) throw new Failure("storage", "Cannot create speech model storage");
|
|
137
|
+
Files.deleteIfExists(new File(folder, MARKER).toPath());
|
|
138
|
+
}
|
|
139
|
+
long missing = 0;
|
|
140
|
+
for (JSONObject spec : files) {
|
|
141
|
+
File target = inside(folder, spec.getString("name")), partial = new File(target.getPath() + ".part");
|
|
142
|
+
missing += spec.getLong("size") - (target.isFile() ? target.length() : partial.isFile() ? partial.length() : 0);
|
|
143
|
+
}
|
|
144
|
+
if (folder.getUsableSpace() < missing + 64L * 1024 * 1024) throw new Failure("storage", "Not enough storage for this speech model");
|
|
145
|
+
long done = 0;
|
|
146
|
+
for (JSONObject spec : files) {
|
|
147
|
+
String name = spec.getString("name"), sha = spec.getString("sha256");
|
|
148
|
+
long size = spec.getLong("size");
|
|
149
|
+
File target = inside(folder, name), partial = new File(target.getPath() + ".part");
|
|
150
|
+
File parent = target.getParentFile();
|
|
151
|
+
if (parent != null && !parent.isDirectory() && !parent.mkdirs()) throw new Failure("storage", "Cannot create speech model storage");
|
|
152
|
+
if (!(target.isFile() && target.length() == size && sha.equals(digest(target)))) {
|
|
153
|
+
Files.deleteIfExists(target.toPath());
|
|
154
|
+
final long before = done;
|
|
155
|
+
transfer(new URL(spec.getString("url")), partial, size, received -> progress.update(before + received, all));
|
|
156
|
+
if (cancelled) throw new CancellationException();
|
|
157
|
+
if (!sha.equals(digest(partial))) {
|
|
158
|
+
Files.deleteIfExists(partial.toPath());
|
|
159
|
+
throw new Failure("verification", "The downloaded speech model was damaged, so it was not installed");
|
|
160
|
+
}
|
|
161
|
+
if (!partial.renameTo(target)) throw new Failure("storage", "Cannot save the speech model");
|
|
162
|
+
}
|
|
163
|
+
done += size;
|
|
164
|
+
progress.update(done, all);
|
|
165
|
+
}
|
|
166
|
+
synchronized (this) {
|
|
167
|
+
AtomicFile marker = new AtomicFile(new File(folder, MARKER));
|
|
168
|
+
FileOutputStream output = null;
|
|
169
|
+
try {
|
|
170
|
+
output = marker.startWrite();
|
|
171
|
+
output.write(new JSONObject().put("files", new JSONObject(expected)).toString().getBytes(StandardCharsets.UTF_8));
|
|
172
|
+
marker.finishWrite(output);
|
|
173
|
+
} catch (Exception error) { marker.failWrite(output); throw error; }
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
private interface Received { void update(long received); }
|
|
177
|
+
private void transfer(URL url, File partial, long size, Received progress) throws Exception {
|
|
178
|
+
long have = partial.isFile() ? partial.length() : 0;
|
|
179
|
+
if (have > size) { Files.deleteIfExists(partial.toPath()); have = 0; }
|
|
180
|
+
if (have == size) return;
|
|
181
|
+
HttpURLConnection http = (HttpURLConnection) url.openConnection();
|
|
182
|
+
connection = http;
|
|
183
|
+
try {
|
|
184
|
+
if (cancelled) throw new CancellationException();
|
|
185
|
+
http.setConnectTimeout(30_000);
|
|
186
|
+
http.setReadTimeout(60_000);
|
|
187
|
+
http.setRequestProperty("Accept-Encoding", "identity");
|
|
188
|
+
if (have > 0) http.setRequestProperty("Range", "bytes=" + have + "-");
|
|
189
|
+
int status;
|
|
190
|
+
try { status = http.getResponseCode(); }
|
|
191
|
+
catch (IOException error) { if (cancelled) throw new CancellationException(); throw new Failure("interrupted", error.getMessage() == null ? "The connection failed" : error.getMessage()); }
|
|
192
|
+
boolean append = status == 206 && have > 0;
|
|
193
|
+
if (status != 200 && !append) {
|
|
194
|
+
if (status == 416) { Files.deleteIfExists(partial.toPath()); throw new Failure("interrupted", "The download must restart"); }
|
|
195
|
+
throw new Failure(status == 408 || status == 429 || status >= 500 ? "interrupted" : "failed", "Speech model download failed (HTTP " + status + ")");
|
|
196
|
+
}
|
|
197
|
+
if (!append) have = 0;
|
|
198
|
+
byte[] buffer = new byte[1 << 20];
|
|
199
|
+
long last = 0;
|
|
200
|
+
try (InputStream input = http.getInputStream(); FileOutputStream output = new FileOutputStream(partial, append)) {
|
|
201
|
+
for (int read; (read = input.read(buffer)) > 0;) {
|
|
202
|
+
if (cancelled) throw new CancellationException();
|
|
203
|
+
have += read;
|
|
204
|
+
if (have > size) throw new Failure("verification", "The speech model is larger than expected");
|
|
205
|
+
output.write(buffer, 0, read);
|
|
206
|
+
long now = System.nanoTime();
|
|
207
|
+
if (now - last > 250_000_000L) { last = now; progress.update(have); }
|
|
208
|
+
}
|
|
209
|
+
output.getFD().sync();
|
|
210
|
+
} catch (IOException error) {
|
|
211
|
+
if (cancelled) throw new CancellationException();
|
|
212
|
+
if (error instanceof Failure) throw error;
|
|
213
|
+
throw new Failure("interrupted", error.getMessage() == null ? "The download stopped" : error.getMessage());
|
|
214
|
+
}
|
|
215
|
+
if (have != size) throw new Failure("interrupted", "The download ended early");
|
|
216
|
+
progress.update(have);
|
|
217
|
+
} finally {
|
|
218
|
+
connection = null;
|
|
219
|
+
http.disconnect();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
private static String digest(File file) throws Exception {
|
|
223
|
+
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
|
224
|
+
byte[] buffer = new byte[1 << 20];
|
|
225
|
+
try (InputStream input = new FileInputStream(file)) { for (int read; (read = input.read(buffer)) > 0;) sha.update(buffer, 0, read); }
|
|
226
|
+
StringBuilder hex = new StringBuilder();
|
|
227
|
+
for (byte value : sha.digest()) hex.append(String.format("%02x", value));
|
|
228
|
+
return hex.toString();
|
|
229
|
+
}
|
|
230
|
+
}
|