forked from Expl0dingCat/Ame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
164 lines (163 loc) · 5.06 KB
/
Copy pathcontroller.js
File metadata and controls
164 lines (163 loc) · 5.06 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// deps
import LLM from "./components/llm.js";
import STT from "./components/stt.js";
import TTS from "./components/tts.js";
import config from "./config.json" with { type: "json" };
// class
export default class Controller {
// constructor
constructor() {
this.llm = new LLM();
if (config.stt) this.stt = new STT();
if (config.tts) this.tts = new TTS();
}
// init function
async init() {
console.log("[CONTROLLER] Initializing LLM...");
await this.llm.init();
if (this.tts) {
console.log("[CONTROLLER] Initializing TTS...");
await this.tts.init();
}
if (this.stt) {
console.log("[CONTROLLER] Initializing STT...");
await this.stt.init();
}
}
// audio pipeline
async audio(audioPath) {
// if stt is disabled
if (!this.stt)
return {
input: "N/A",
output: "STT is disabled. Please enable STT to use the audio pipeline.",
};
// log
console.log(
"[CONTROLLER] Audio pipeline called. Requesting transcription..."
);
// get the transcription
const input = await this.stt.transcribe(audioPath);
// log
console.log(`[CONTROLLER] Transcription finished: ${input}`);
// get output from the llm
let output = await this.llm.generate(input);
// log the output
console.log(`[CONTROLLER] Output generated: ${output}`);
// if tts is enabled, speak the output
if (this.tts) await this.tts.speak(output);
// log on output spoken
console.log("[CONTROLLER] Output processing done!");
// return
return {
input,
output,
tts: !!this.tts,
};
}
// text pipeline
async text(input) {
// log
console.log(`[CONTROLLER] Generation requested: ${input}`);
// generate output with the llm
let output = await this.llm.generate(input);
// log the output
console.log(`[CONTROLLER] Output generated: ${output}`);
// if tts is enabled, speak the output
if (this.tts) await this.tts.speak(output);
// log on output spoken
console.log("[CONTROLLER] Output processing done!");
// return
return {
output,
tts: !!this.tts,
};
}
// vision pipeline
async vision(input, imagePath) {
// if vision is off
if (!config.vision) {
return {
output: "Vision is disabled. Please enable vision to use the vision pipeline.",
tts: false,
};
}
// log
console.log(
`[CONTROLLER] Generation through vision endpoint requested. Text input: ${input}`
);
// get output from the llm
let output = await this.llm.generate(input, imagePath);
// log the output
console.log(`[CONTROLLER] Output generated: ${output}`);
// if tts is enabled, speak the output
if (this.tts) await this.tts.speak(output);
// log on output spoken
console.log("[CONTROLLER] Output processing done!");
// return
return {
output,
tts: !!this.tts,
};
}
// see
async see(imagePath) {
// if vision is off
if (!config.vision)
return "Vision is disabled. Please enable vision to use the see function.";
// log
console.log("[CONTROLLER] Vision requested.");
// see the image
let caption = await this.llm.vision.caption(imagePath);
// log again
console.log(`[CONTROLLER] Caption generated: ${caption}`);
// return
return caption;
}
// speak
async speak(text) {
// if tts is off
if (!this.tts) return false;
// log
console.log(`[CONTROLLER] TTS requested: ${text}`);
// generate
await this.tts.speak(text);
// log again
console.log("[CONTROLLER] TTS generated.");
// return
return true;
}
// listen
async listen(audioPath) {
// if stt is off
if (!this.stt)
return "STT is disabled. Please enable STT to use the listen function.";
// log
console.log("[CONTROLLER] Transcription requested.");
// transcribe the audio
let transcription = await this.stt.transcribe(audioPath);
// log again
console.log(`[CONTROLLER] Transcription generated: ${transcription}`);
// return
return transcription;
}
// generate
async generate(input) {
// log
console.log(`[CONTROLLER] Generation requested: ${input}`);
// get output from the llm
let output = await this.llm.generate(input);
// log it
console.log(`[CONTROLLER] Generation finished: ${output}`);
// return
return output;
}
// emitter
get emitter() {
return this.llm.emitter;
}
// model
get model() {
return this.llm.model;
}
}