transl8-srt 1.0.1 → 1.0.2
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/README.md +17 -0
- package/dist/index.cjs +29 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -3
- package/dist/index.d.ts +20 -3
- package/dist/index.js +29 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -131,6 +131,23 @@ const client = new OpenAI({
|
|
|
131
131
|
const result = await translate(content, {targetLang: "es"}, client);
|
|
132
132
|
```
|
|
133
133
|
|
|
134
|
+
### Bring custom models - like Deepseek
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import OpenAI from "openai";
|
|
138
|
+
import { translate } from "transl8-srt";
|
|
139
|
+
|
|
140
|
+
const client = new OpenAI({
|
|
141
|
+
baseURL: "https://api.deepseek.com",
|
|
142
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const result = await translate(content, {
|
|
146
|
+
targetLang: "fr",
|
|
147
|
+
model: "deepseek-chat", // ← explicitly set — no auto-selection for custom providers
|
|
148
|
+
}, client);
|
|
149
|
+
```
|
|
150
|
+
|
|
134
151
|
---
|
|
135
152
|
|
|
136
153
|
## Options reference
|
package/dist/index.cjs
CHANGED
|
@@ -7154,11 +7154,27 @@ Do not explain metaphors or idioms \u2014 translate them faithfully.`,
|
|
|
7154
7154
|
Preserve the speaker's natural register and sentence rhythm.
|
|
7155
7155
|
Do not add formality that isn't in the source.`
|
|
7156
7156
|
};
|
|
7157
|
-
var
|
|
7158
|
-
|
|
7159
|
-
|
|
7160
|
-
|
|
7161
|
-
|
|
7157
|
+
var NEEDS_FULL_MODEL = /* @__PURE__ */ new Set([
|
|
7158
|
+
"ar",
|
|
7159
|
+
"am",
|
|
7160
|
+
"zh",
|
|
7161
|
+
"ja",
|
|
7162
|
+
"ko",
|
|
7163
|
+
"hi",
|
|
7164
|
+
"ru",
|
|
7165
|
+
"uk",
|
|
7166
|
+
"el",
|
|
7167
|
+
"yo",
|
|
7168
|
+
"ig",
|
|
7169
|
+
"ha",
|
|
7170
|
+
"sw",
|
|
7171
|
+
"zu"
|
|
7172
|
+
]);
|
|
7173
|
+
function pickModel(opts) {
|
|
7174
|
+
if (opts.model) return opts.model;
|
|
7175
|
+
const needsFull = NEEDS_FULL_MODEL.has(opts.targetLang);
|
|
7176
|
+
if (needsFull) return "gpt-4o";
|
|
7177
|
+
return opts.modelFallback ?? "gpt-4o-mini";
|
|
7162
7178
|
}
|
|
7163
7179
|
function buildGlossaryBlock(meta) {
|
|
7164
7180
|
const lines = [];
|
|
@@ -7173,7 +7189,7 @@ function buildGlossaryBlock(meta) {
|
|
|
7173
7189
|
}
|
|
7174
7190
|
if (meta.glossary) {
|
|
7175
7191
|
for (const [src, tgt] of Object.entries(meta.glossary)) {
|
|
7176
|
-
lines.push(`"${src}"
|
|
7192
|
+
lines.push(`"${src}" -> "${tgt}"`);
|
|
7177
7193
|
}
|
|
7178
7194
|
}
|
|
7179
7195
|
return lines.length ? `GLOSSARY (must follow exactly):
|
|
@@ -7189,7 +7205,7 @@ function buildSystemPrompt(opts) {
|
|
|
7189
7205
|
domainBlock,
|
|
7190
7206
|
glossaryBlock,
|
|
7191
7207
|
`RULES:
|
|
7192
|
-
- Preserve ALL-CAPS emphasis, ellipses (...), dashes
|
|
7208
|
+
- Preserve ALL-CAPS emphasis, ellipses (...), dashes, and line-ending punctuation exactly.
|
|
7193
7209
|
- Never merge or split sentences \u2014 one input item = one output item.
|
|
7194
7210
|
- Never add explanatory text, parentheticals, or translator notes.
|
|
7195
7211
|
- Output ONLY valid JSON. No markdown, no code fences, no preamble.`,
|
|
@@ -7304,7 +7320,7 @@ async function executeTranslation(client, sentences, opts) {
|
|
|
7304
7320
|
const batchSize = opts.batchSize ?? 25;
|
|
7305
7321
|
const overlap = opts.contextOverlap ?? 4;
|
|
7306
7322
|
const concurrency = opts.concurrency ?? 8;
|
|
7307
|
-
const model = pickModel(opts
|
|
7323
|
+
const model = pickModel(opts);
|
|
7308
7324
|
const systemPrompt = buildSystemPrompt(opts);
|
|
7309
7325
|
const payloads = buildBatches(sentences, batchSize, overlap);
|
|
7310
7326
|
const tasks = payloads.map(
|
|
@@ -7417,12 +7433,13 @@ function emitVTT(cues) {
|
|
|
7417
7433
|
// src/index.ts
|
|
7418
7434
|
var import_promises = require("fs/promises");
|
|
7419
7435
|
var import_path37 = require("path");
|
|
7420
|
-
var
|
|
7436
|
+
var KNOWN_COSTS = {
|
|
7421
7437
|
"gpt-4o": { input: 2.5, output: 10 },
|
|
7422
7438
|
"gpt-4o-mini": { input: 0.15, output: 0.6 }
|
|
7423
7439
|
};
|
|
7424
7440
|
function estimateCost(model, inputTokens, outputTokens) {
|
|
7425
|
-
const rates =
|
|
7441
|
+
const rates = KNOWN_COSTS[model];
|
|
7442
|
+
if (!rates) return 0;
|
|
7426
7443
|
return inputTokens / 1e6 * rates.input + outputTokens / 1e6 * rates.output;
|
|
7427
7444
|
}
|
|
7428
7445
|
async function translate(fileContent, opts, client) {
|
|
@@ -7433,14 +7450,14 @@ async function translate(fileContent, opts, client) {
|
|
|
7433
7450
|
const sentences = reconstructSentences(cues);
|
|
7434
7451
|
const { translations, totalInputTokens, totalOutputTokens, totalBatches } = await executeTranslation(oi, sentences, opts);
|
|
7435
7452
|
const translatedCues = reanchorCues(cues, sentences, translations);
|
|
7436
|
-
const
|
|
7453
|
+
const resolvedModel = pickModel(opts);
|
|
7437
7454
|
const stats = {
|
|
7438
7455
|
totalCues: cues.length,
|
|
7439
7456
|
totalSentences: sentences.length,
|
|
7440
7457
|
totalBatches,
|
|
7441
7458
|
inputTokens: totalInputTokens,
|
|
7442
7459
|
outputTokens: totalOutputTokens,
|
|
7443
|
-
estimatedCostUsd: estimateCost(
|
|
7460
|
+
estimatedCostUsd: estimateCost(resolvedModel, totalInputTokens, totalOutputTokens),
|
|
7444
7461
|
durationMs: Date.now() - startTime
|
|
7445
7462
|
};
|
|
7446
7463
|
return {
|