@openspecui/server 3.9.0 → 3.10.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/dist/index.mjs +2 -2
- package/dist/src-DBFY1eQK.mjs +121 -0
- package/dist/src-DuQ_-3Kn.mjs +13440 -0
- package/package.json +4 -6
package/dist/index.mjs
CHANGED
|
@@ -6616,12 +6616,12 @@ var TranslationEngineService = class {
|
|
|
6616
6616
|
}
|
|
6617
6617
|
async loadFactory(engineId, model) {
|
|
6618
6618
|
const globalSettings = await this.globalSettingsManager.readSettings();
|
|
6619
|
-
if (engineId === "local") return (await import("
|
|
6619
|
+
if (engineId === "local") return (await import("./src-DBFY1eQK.mjs")).createLocalTranslatorFactory({
|
|
6620
6620
|
defaultModel: model ?? globalSettings.translationEngines.local.model,
|
|
6621
6621
|
cacheDir: this.localCacheDir,
|
|
6622
6622
|
localOnly: true
|
|
6623
6623
|
});
|
|
6624
|
-
return (await import("
|
|
6624
|
+
return (await import("./src-DuQ_-3Kn.mjs")).createOpenAICompletionTranslatorFactory({
|
|
6625
6625
|
baseUrl: globalSettings.translationEngines.openai.baseUrl,
|
|
6626
6626
|
token: globalSettings.translationEngines.openai.token,
|
|
6627
6627
|
model: model ?? globalSettings.translationEngines.openai.model
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import "@openspecui/core/local-download-profiles";
|
|
4
|
+
|
|
5
|
+
//#region ../local-translator/src/index.ts
|
|
6
|
+
const DEFAULT_MODEL = "Xenova/nllb-200-distilled-600M";
|
|
7
|
+
var LocalTranslatorFactory = class {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
async prepare(options) {
|
|
12
|
+
await (await loadTranslationPipeline(options.model || this.options.defaultModel || DEFAULT_MODEL, options.monitor, this.options.cacheDir, options.dtype ?? this.options.dtype, this.options.localOnly, options.runtimeConfig)).dispose?.();
|
|
13
|
+
}
|
|
14
|
+
async create(options) {
|
|
15
|
+
return new LocalTranslator(await loadTranslationPipeline(options.model || this.options.defaultModel || DEFAULT_MODEL, options.monitor, this.options.cacheDir, options.dtype ?? this.options.dtype, this.options.localOnly, options.runtimeConfig), {
|
|
16
|
+
sourceLanguage: options.sourceLanguage,
|
|
17
|
+
targetLanguage: options.targetLanguage
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
function createLocalTranslatorFactory(options = {}) {
|
|
22
|
+
return new LocalTranslatorFactory(options);
|
|
23
|
+
}
|
|
24
|
+
var LocalTranslator = class {
|
|
25
|
+
constructor(pipeline, languages) {
|
|
26
|
+
this.pipeline = pipeline;
|
|
27
|
+
this.languages = languages;
|
|
28
|
+
}
|
|
29
|
+
async *batchTranslate(inputs, options) {
|
|
30
|
+
for (const [index, input] of inputs.entries()) {
|
|
31
|
+
throwIfAborted(options?.signal);
|
|
32
|
+
const result = await this.pipeline(input, {
|
|
33
|
+
src_lang: this.languages.sourceLanguage,
|
|
34
|
+
tgt_lang: this.languages.targetLanguage
|
|
35
|
+
});
|
|
36
|
+
throwIfAborted(options?.signal);
|
|
37
|
+
yield {
|
|
38
|
+
index,
|
|
39
|
+
output: readTranslatedText(result)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
destroy() {
|
|
44
|
+
this.pipeline.dispose?.();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
async function loadTranslationPipeline(model, monitor, cacheDir, dtype, localOnly = false, runtimeConfig) {
|
|
48
|
+
monitor?.setStatus({ message: `Loading local model ${model}.` });
|
|
49
|
+
const transformers = await import("@huggingface/transformers");
|
|
50
|
+
if (cacheDir && transformers.env) {
|
|
51
|
+
transformers.env.cacheDir = cacheDir;
|
|
52
|
+
transformers.env.localModelPath = join(cacheDir, "models");
|
|
53
|
+
}
|
|
54
|
+
if (transformers.env) {
|
|
55
|
+
transformers.env.allowLocalModels = true;
|
|
56
|
+
transformers.env.allowRemoteModels = !localOnly;
|
|
57
|
+
}
|
|
58
|
+
const runtimeModel = localOnly && cacheDir ? join(cacheDir, "models", model) : model;
|
|
59
|
+
const progressCallback = monitor ? (event) => {
|
|
60
|
+
const progress = readProgress(event);
|
|
61
|
+
monitor.setStatus({
|
|
62
|
+
message: progress === void 0 ? `Downloading local model ${model}.` : `Downloading local model ${model} ${Math.round(progress * 100)}%.`,
|
|
63
|
+
...progress === void 0 ? {} : { progress }
|
|
64
|
+
});
|
|
65
|
+
} : void 0;
|
|
66
|
+
const pipeline = await transformers.pipeline("translation", runtimeModel, {
|
|
67
|
+
config: runtimeConfig ?? await readLocalRuntimeConfig({
|
|
68
|
+
cacheDir,
|
|
69
|
+
model,
|
|
70
|
+
localOnly
|
|
71
|
+
}),
|
|
72
|
+
...dtype ? { dtype } : {},
|
|
73
|
+
...localOnly ? { local_files_only: true } : {},
|
|
74
|
+
...progressCallback ? { progress_callback: progressCallback } : {}
|
|
75
|
+
});
|
|
76
|
+
monitor?.setStatus({
|
|
77
|
+
message: `Local model ${model} is ready.`,
|
|
78
|
+
progress: 1
|
|
79
|
+
});
|
|
80
|
+
return pipeline;
|
|
81
|
+
}
|
|
82
|
+
async function readLocalRuntimeConfig(input) {
|
|
83
|
+
if (!input.localOnly || !input.cacheDir) return void 0;
|
|
84
|
+
const configPath = join(input.cacheDir, "models", input.model, "config.json");
|
|
85
|
+
try {
|
|
86
|
+
return JSON.parse(await readFile(configPath, "utf8"));
|
|
87
|
+
} catch {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function readTranslatedText(value) {
|
|
92
|
+
if (typeof value === "string") return value;
|
|
93
|
+
if (Array.isArray(value)) {
|
|
94
|
+
const first = value[0];
|
|
95
|
+
if (first && typeof first === "object") {
|
|
96
|
+
const text = first.translation_text;
|
|
97
|
+
if (typeof text === "string") return text;
|
|
98
|
+
const generated = first.generated_text;
|
|
99
|
+
if (typeof generated === "string") return generated;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (value && typeof value === "object") {
|
|
103
|
+
const text = value.translation_text;
|
|
104
|
+
if (typeof text === "string") return text;
|
|
105
|
+
const generated = value.generated_text;
|
|
106
|
+
if (typeof generated === "string") return generated;
|
|
107
|
+
}
|
|
108
|
+
return String(value);
|
|
109
|
+
}
|
|
110
|
+
function readProgress(event) {
|
|
111
|
+
if (!event || typeof event !== "object") return void 0;
|
|
112
|
+
const record = event;
|
|
113
|
+
if (typeof record.progress === "number") return Math.max(0, Math.min(1, record.progress / (record.progress > 1 ? 100 : 1)));
|
|
114
|
+
if (typeof record.loaded === "number" && typeof record.total === "number" && record.total > 0) return Math.max(0, Math.min(1, record.loaded / record.total));
|
|
115
|
+
}
|
|
116
|
+
function throwIfAborted(signal) {
|
|
117
|
+
if (signal?.aborted) throw new DOMException("The operation was aborted.", "AbortError");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { createLocalTranslatorFactory };
|