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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('node:path');
const fs = require('node:fs').promises;
const yaml = require('js-yaml');
const { spawn } = require('child_process');
const https = require('https');
const { CONFIG_SCHEMA, getDefaultConfig } = require('./config-schema.js');
const APP_ROOT = path.join(__dirname, '..');
const CONFIG_PATH = path.join(APP_ROOT, 'config.yaml');
let mainWindow;
let runningProcess = null; // Track the running Python process
// Required DLL files for CUDA/cuDNN support
const REQUIRED_DLLS = [
'cublas64_12.dll',
'cublasLt64_12.dll',
'cudnn64_9.dll',
'cudnn_adv64_9.dll',
'cudnn_cnn64_9.dll',
'cudnn_engines_precompiled64_9.dll',
'cudnn_engines_runtime_compiled64_9.dll',
'cudnn_graph64_9.dll',
'cudnn_heuristic64_9.dll',
'cudnn_ops64_9.dll'
];
// Helper function to get the correct Python executable from venv
function getVenvPython() {
const venvPath = path.join(APP_ROOT, 'venv');
const pythonPath = path.join(venvPath, 'Scripts', 'python.exe');
return pythonPath;
}
// Helper function to send Python output to renderer
function sendPythonOutput(message, type = 'stdout') {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('python-output', { message, type });
}
}
// Helper function to create environment with DLL path
function createPythonEnvironment() {
const dllPath = path.join(APP_ROOT, 'dll');
const binPath = path.join(APP_ROOT, 'bin');
const env = { ...process.env };
env.PATH = `${dllPath};${binPath};${env.PATH}`;
env.HF_HUB_DISABLE_SYMLINKS_WARNING = '1';
return env;
}
// Helper function to download a file from URL
function downloadFile(url, outputPath) {
return new Promise((resolve, reject) => {
const file = require('fs').createWriteStream(outputPath);
const request = https.get(url, (response) => {
if (response.statusCode === 200) {
response.pipe(file);
file.on('finish', () => {
file.close();
resolve();
});
file.on('error', (err) => {
fs.unlink(outputPath).catch(() => {}); // Clean up on error
reject(err);
});
} else {
file.close();
fs.unlink(outputPath).catch(() => {}); // Clean up on error
reject(new Error(`Failed to download: HTTP ${response.statusCode}`));
}
});
request.on('error', (err) => {
file.close();
fs.unlink(outputPath).catch(() => {}); // Clean up on error
reject(err);
});
});
}
function shouldFilterMessage(message) {
// Filter out pydub ffmpeg/avconv warning. It does not actually matter.
if (message.includes("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work")) {
return true;
}
return false;
}
// Helper function to setup process event handlers
function setupProcessHandlers(process) {
process.stdout.on('data', (data) => {
const text = data.toString();
sendPythonOutput(text.trimEnd(), 'stdout');
});
process.stderr.on('data', (data) => {
const text = data.toString();
if (!shouldFilterMessage(text)) {
sendPythonOutput(text.trimEnd(), 'stderr');
}
});
process.on('error', (error) => {
sendPythonOutput(`Process error: ${error.message}`, 'stderr');
runningProcess = null;
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('process-stopped');
}
});
process.on('close', (code) => {
sendPythonOutput(`Process exited with code ${code}`, 'info');
runningProcess = null;
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('process-stopped');
}
});
}
// Helper function to execute Python commands using venv
function executePythonCommand(args, options = {}) {
return new Promise((resolve, reject) => {
const pythonPath = getVenvPython();
const commandStr = `${path.basename(pythonPath)} ${args.join(' ')}`;
sendPythonOutput(`> ${commandStr}`, 'info');
const spawnOptions = {
...options,
env: createPythonEnvironment()
};
const pythonProcess = spawn(pythonPath, args, spawnOptions);
let stdout = '';
let stderr = '';
pythonProcess.stdout.on('data', (data) => {
const text = data.toString();
stdout += text;
sendPythonOutput(text.trimEnd(), 'stdout');
});
pythonProcess.stderr.on('data', (data) => {
const text = data.toString();
stderr += text;
// Filter out specific warning messages
if (!shouldFilterMessage(text)) {
sendPythonOutput(text.trimEnd(), 'stderr');
}
});
pythonProcess.on('error', (error) => {
sendPythonOutput(`Failed to start Python process: ${error.message}`, 'stderr');
reject({ error: error.message, stdout, stderr });
});
pythonProcess.on('close', (code) => {
if (code !== 0) {
sendPythonOutput(`Process exited with code ${code}`, 'stderr');
reject({ code, stdout, stderr });
} else {
resolve({ stdout, stderr });
}
});
});
}
function createWindow () {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
icon: path.join(APP_ROOT, 'Images', 'favicon.ico'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
mainWindow.loadFile('index.html');
}
// Replace the DEFAULT_CONFIG constant with:
const DEFAULT_CONFIG = getDefaultConfig();
// IPC handlers
ipcMain.handle('load-config', async () => {
try {
const fileContent = await fs.readFile(CONFIG_PATH, 'utf8');
return yaml.load(fileContent);
} catch (error) {
if (error.code === 'ENOENT') {
// Config file doesn't exist, create it with defaults
console.error('Config file not found, creating with defaults...');
try {
const yamlContent = yaml.dump(DEFAULT_CONFIG, { lineWidth: -1 });
await fs.writeFile(CONFIG_PATH, yamlContent, 'utf8');
console.error('Created config.yaml with default values');
return DEFAULT_CONFIG;
} catch (writeError) {
console.error('Error creating default config:', writeError);
// Return defaults even if we can't write the file
return DEFAULT_CONFIG;
}
}
console.error('Error loading config:', error);
throw error;
}
});
ipcMain.handle('save-config', async (event, config) => {
try {
const yamlContent = yaml.dump(config, { lineWidth: -1 });
await fs.writeFile(CONFIG_PATH, yamlContent, 'utf8');
return { success: true };
} catch (error) {
console.error('Error saving config:', error);
throw error;
}
});
ipcMain.handle('reset-config', async () => {
try {
// Check if the file exists first
try {
await fs.access(CONFIG_PATH);
// File exists, delete it
await fs.unlink(CONFIG_PATH);
console.error('Config file deleted successfully');
return { success: true, message: 'Configuration reset to defaults' };
} catch (error) {
if (error.code === 'ENOENT') {
// Config file doesn't exist, that's fine
return { success: true, message: 'Configuration already at defaults' };
}
throw error;
}
} catch (error) {
console.error('Error resetting config:', error);
throw new Error(`Failed to reset configuration: ${error.message}`);
}
});
ipcMain.handle('deleteVenvIndicatorFile', async () => {
const venvMarkerPath = path.join(APP_ROOT, '.venv_is_set_up');
try {
await fs.unlink(venvMarkerPath);
return { success: true, message: '.venv_is_set_up deleted successfully.' };
} catch (error) {
if (error.code === 'ENOENT') {
return { success: true, message: '.venv_is_set_up not found.' };
}
console.error('Error deleting .venv_is_set_up file:', error);
sendPythonOutput(`Error deleting .venv_is_set_up: ${error.message}`, 'stderr');
throw error;
}
});
// Generic function to ensure required files are present
async function ensureRequiredFiles(config) {
const {
directoryName,
requiredFiles,
downloadBaseUrl,
resourceType
} = config;
const targetPath = path.join(APP_ROOT, directoryName);
try {
// Check if target directory exists, create it if not
try {
await fs.access(targetPath);
sendPythonOutput(`${resourceType} directory exists`, 'info');
} catch (error) {
if (error.code === 'ENOENT') {
sendPythonOutput(`Creating ${resourceType} directory...`, 'info');
await fs.mkdir(targetPath, { recursive: true });
sendPythonOutput(`${resourceType} directory created`, 'info');
} else {
throw error;
}
}
// Check each required file
const missingFiles = [];
for (const fileName of requiredFiles) {
const filePath = path.join(targetPath, fileName);
try {
await fs.access(filePath);
sendPythonOutput(`✓ ${fileName} exists`, 'info');
} catch (error) {
if (error.code === 'ENOENT') {
missingFiles.push(fileName);
sendPythonOutput(`✗ ${fileName} missing`, 'info');
} else {
throw error;
}
}
}
// Download missing files
if (missingFiles.length > 0) {
sendPythonOutput(`Downloading ${missingFiles.length} missing ${resourceType} file${missingFiles.length > 1 ? 's' : ''}...`, 'info');
for (const fileName of missingFiles) {
const filePath = path.join(targetPath, fileName);
const downloadUrl = `${downloadBaseUrl}/${fileName}`;
try {
sendPythonOutput(`Downloading ${fileName}...`, 'info');
await downloadFile(downloadUrl, filePath);
sendPythonOutput(`✓ Downloaded ${fileName}`, 'info');
} catch (downloadError) {
sendPythonOutput(`✗ Failed to download ${fileName}: ${downloadError.message}`, 'stderr');
throw new Error(`Failed to download ${fileName}: ${downloadError.message}`);
}
}
sendPythonOutput(`All missing ${resourceType} files downloaded successfully`, 'info');
} else {
sendPythonOutput(`All required ${resourceType} files are present`, 'info');
}
return {
success: true,
message: `${resourceType} setup complete. ${missingFiles.length} file${missingFiles.length > 1 ? 's' : ''} downloaded.`,
downloadedFiles: missingFiles
};
} catch (error) {
console.error(`Error setting up ${resourceType} files:`, error);
throw new Error(`${resourceType} setup failed: ${error.message}`);
}
}
// Update the install-requirements handler
ipcMain.handle('install-requirements', async () => {
const requirementsPath = path.join(APP_ROOT, 'app', 'requirements.txt');
const venvMarkerPath = path.join(APP_ROOT, '.venv_is_set_up');
try {
// Check if venv is already set up
try {
await fs.access(venvMarkerPath);
return { success: true, message: 'Virtual environment already set up' };
} catch (error) {
// Marker doesn't exist, proceed with setup
}
// Check if requirements.txt exists
await fs.access(requirementsPath);
await executePythonCommand(['-m', 'pip', 'install', '-r', requirementsPath]);
await ensureRequiredFiles({
directoryName: 'dll',
requiredFiles: REQUIRED_DLLS,
downloadBaseUrl: 'https://yummers.dev/tastt/dll',
resourceType: 'DLL'
});
await fs.mkdir(path.join(APP_ROOT, 'Models'), { recursive: true });
await fs.writeFile(venvMarkerPath, new Date().toISOString(), 'utf8');
sendPythonOutput('Created .venv_is_set_up marker file', 'info');
return { success: true, message: 'Requirements and dependencies installed successfully' };
} catch (error) {
console.error('Error installing requirements:', error);
if (error.code === 'ENOENT') {
throw new Error('requirements.txt not found');
}
throw new Error(`Installation failed: ${error.stderr || error.error || 'Unknown error'}`);
}
});
ipcMain.handle('get-microphones', async () => {
const scriptPath = path.join(APP_ROOT, 'app', 'list_microphones.py');
try {
const result = await executePythonCommand([scriptPath]);
const microphones = JSON.parse(result.stdout.trim());
return microphones;
} catch (error) {
console.error('Failed to get microphones:', error);
throw new Error(`Failed to get microphones: ${error.stderr || error.error || 'Unknown error'}`);
}
});
// Helper function to safely delete directory contents
async function clearDirectory(dirPath, dirName) {
try {
await fs.access(dirPath);
sendPythonOutput(`Clearing ${dirName} directory...`, 'info');
const files = await fs.readdir(dirPath);
let deletedCount = 0;
for (const file of files) {
const filePath = path.join(dirPath, file);
try {
await fs.rm(filePath, { recursive: true, force: true });
sendPythonOutput(`✗ Deleted file ${file}`, 'info');
deletedCount++;
} catch (deleteError) {
sendPythonOutput(`Warning: Could not delete ${file}: ${deleteError.message}`, 'stderr');
// Continue with other files even if one fails
}
}
sendPythonOutput(`${dirName} directory cleared`, 'info');
return deletedCount;
} catch (error) {
if (error.code === 'ENOENT') {
sendPythonOutput(`${dirName} directory doesn't exist, skipping`, 'info');
return 0;
} else {
sendPythonOutput(`Error clearing ${dirName} directory: ${error.message}`, 'stderr');
throw error;
}
}
}
ipcMain.handle('reset-venv', async () => {
const venvMarkerPath = path.join(APP_ROOT, '.venv_is_set_up');
try {
sendPythonOutput('Starting virtual environment reset...', 'info');
// Delete the venv marker file first
try {
await fs.unlink(venvMarkerPath);
sendPythonOutput('Deleted .venv_is_set_up marker file', 'info');
} catch (error) {
if (error.code !== 'ENOENT') {
sendPythonOutput(`Warning: Could not delete marker file: ${error.message}`, 'stderr');
}
}
// Get list of installed packages
sendPythonOutput('Getting list of installed packages...', 'info');
const freezeResult = await executePythonCommand(['-m', 'pip', 'freeze']);
const installedPackages = freezeResult.stdout.trim();
let uninstalledPackages = [];
if (!installedPackages) {
sendPythonOutput('No packages found to uninstall', 'info');
} else {
// Parse package names and filter out core packages
const packageLines = installedPackages.split('\n').filter(line => line.trim());
const packageNames = packageLines
.map(line => line.split('==')[0].trim())
.filter(name => name && !name.startsWith('#'));
const corePackages = ['pip', 'setuptools', 'wheel'];
const packagesToUninstall = packageNames.filter(name => !corePackages.includes(name.toLowerCase()));
if (packagesToUninstall.length === 0) {
sendPythonOutput('Only core packages found, nothing to uninstall', 'info');
} else {
sendPythonOutput(`Uninstalling ${packagesToUninstall.length} packages...`, 'info');
const uninstallArgs = ['-m', 'pip', 'uninstall', '-y', ...packagesToUninstall];
await executePythonCommand(uninstallArgs);
uninstalledPackages = packagesToUninstall;
}
}
// Clear downloaded files
sendPythonOutput('Clearing downloaded files...', 'info');
const dllPath = path.join(APP_ROOT, 'dll');
const modelsPath = path.join(APP_ROOT, 'Models');
const binPath = path.join(APP_ROOT, 'bin');
const deletedDlls = await clearDirectory(dllPath, 'DLL');
const deletedModels = await clearDirectory(modelsPath, 'Models');
const deletedBins = await clearDirectory(binPath, 'Binary');
const totalDeletedFiles = deletedDlls + deletedModels + deletedBins;
sendPythonOutput('Virtual environment reset successfully!', 'info');
return {
success: true,
message: `Virtual environment reset complete. Uninstalled ${uninstalledPackages.length} packages and deleted ${totalDeletedFiles} downloaded files.`,
uninstalledPackages,
deletedFiles: {
dlls: deletedDlls,
models: deletedModels,
binaries: deletedBins,
total: totalDeletedFiles
}
};
} catch (error) {
console.error('Error resetting virtual environment:', error);
throw new Error(`Virtual environment reset failed: ${error.message}`);
}
});
// Add handlers for starting and stopping the process
ipcMain.handle('start-process', async () => {
if (runningProcess) {
throw new Error('Process is already running');
}
const scriptPath = path.join(APP_ROOT, 'app', 'hi.py');
const args = [scriptPath, '--config', CONFIG_PATH];
try {
const pythonPath = getVenvPython();
sendPythonOutput(`Starting process: ${path.basename(pythonPath)} ${args.join(' ')}`, 'info');
runningProcess = spawn(pythonPath, args, { env: createPythonEnvironment() });
setupProcessHandlers(runningProcess);
return { success: true };
} catch (error) {
runningProcess = null;
throw error;
}
});
ipcMain.handle('stop-process', async () => {
return new Promise((resolve) => {
let forcefullyKilled = false;
if (!runningProcess) {
resolve({ success: true, forcefullyKilled });
}
// Set up a timeout to force kill after 10 seconds
const killTimeout = setTimeout(() => {
if (runningProcess) {
sendPythonOutput('Process did not stop gracefully, forcing termination...', 'stderr');
forcefullyKilled = true;
runningProcess.kill();
}
}, 10000);
// Listen for the process to exit
runningProcess.once('exit', (code, signal) => {
clearTimeout(killTimeout);
runningProcess = null;
if (forcefullyKilled) {
sendPythonOutput('Process forcefully terminated', 'info');
} else {
sendPythonOutput('Process stopped gracefully', 'info');
}
resolve({ success: true, forcefullyKilled });
});
// Send termination signal
sendPythonOutput('Stopping process gracefully...', 'info');
runningProcess.kill();
});
});
// Clean up on app quit
app.on('before-quit', () => {
if (runningProcess) {
runningProcess.kill();
}
});
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', function () {
app.quit();
});
|