-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
94 lines (82 loc) · 2.61 KB
/
Copy pathindex.ts
File metadata and controls
94 lines (82 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { join } from "node:path";
import {
AssemblyAIProvider,
StarlingSTTProvider,
WhisperSTTProvider,
TranscriptionProvider,
convertFile,
getAudioFilePaths,
saveFileToS3,
} from "./utils";
import { exists, mkdir } from "node:fs/promises";
async function main() {
const language = "nl";
const audioPaths = await getAudioFilePaths("audio-" + language);
const providers: TranscriptionProvider[] = [
new AssemblyAIProvider(),
// new StarlingSTTProvider(),
new WhisperSTTProvider(language),
];
const results = await Promise.allSettled(
audioPaths.map(async (path) => {
try {
const converted = await convertFile(path);
const uploaded = await saveFileToS3(converted);
const transcripts = await Promise.allSettled(
providers.map((el) => el.transcribe(uploaded)),
);
const transcriptMap: any = {};
// add to the map both the name and the transcript
[...providers.map((p) => p.name)].forEach(
(p, idx) => (transcriptMap[p] = transcripts[idx]),
);
return {
path,
uploaded,
transcriptions: transcriptMap,
status: "success",
};
} catch (error) {
console.error(`Failed to process ${path}:`, error);
return { path, error, status: "failed" };
}
}),
);
// Separate successful and failed files
const successful = results.filter(
(r) => r.status === "fulfilled" && r.value.status === "success",
);
const failed = results.filter(
(r) =>
r.status === "rejected" ||
(r.status === "fulfilled" && r.value.status === "failed"),
);
if (failed.length > 0) {
console.warn(`${failed.length} files failed to process`);
}
console.log("success", successful);
console.log("failed", failed);
const outputDir = join(import.meta.dir, "outputs");
if (!(await exists(outputDir))) {
await mkdir(outputDir);
}
const file = join(outputDir, language + Date.now().toString() + ".json");
console.log();
await Bun.write(
file,
JSON.stringify(
{
successful,
failed,
},
null,
4,
),
);
}
main()
.then(() => console.log("✅ Done"))
.catch((e) => {
console.error("❌ Something went wrong:", e);
process.exit(1);
});