yum-slop/TaSTT

Free self-hosted STT for VRChat.

git clone https://git.yummers.dev/yum-slop/TaSTT

yumClean up python process environmentdcafd88

v1.0.0-beta02
19.4 KiB618 linesraw
1const { app, BrowserWindow, ipcMain } = require('electron');
2const path = require('node:path');
3const fs = require('node:fs').promises;
4const yaml = require('js-yaml');
5const { spawn } = require('child_process');
6const https = require('https');
7const { CONFIG_SCHEMA, getDefaultConfig } = require('./config-schema.js');
8
9// Detect if we're running in development or production
10const isDev = !app.isPackaged;
11const APP_ROOT = isDev
12  ? path.join(__dirname, '..')  // Development: go up from ui/ to project root
13  : process.resourcesPath;       // Production: use Electron's resource path
14
15const CONFIG_PATH = path.join(APP_ROOT, 'config.yaml');
16
17let mainWindow;
18let runningProcess = null; // Track the running Python process
19
20// Required DLL files for CUDA/cuDNN support
21const REQUIRED_DLLS = [
22  'cublas64_12.dll',
23  'cublasLt64_12.dll',
24  'cudnn64_9.dll',
25  'cudnn_adv64_9.dll',
26  'cudnn_cnn64_9.dll',
27  'cudnn_engines_precompiled64_9.dll',
28  'cudnn_engines_runtime_compiled64_9.dll',
29  'cudnn_graph64_9.dll',
30  'cudnn_heuristic64_9.dll',
31  'cudnn_ops64_9.dll'
32];
33
34// Helper function to get the correct Python executable from venv
35function getVenvPython() {
36  const venvPath = path.join(APP_ROOT, 'venv');
37  const pythonPath = path.join(venvPath, 'Scripts', 'python.exe');
38  return pythonPath;
39}
40
41// Helper function to send Python output to renderer
42function sendPythonOutput(message, type = 'stdout') {
43  if (mainWindow && !mainWindow.isDestroyed()) {
44    mainWindow.webContents.send('python-output', { message, type });
45  }
46}
47
48// Helper function to create environment with DLL path
49function createPythonEnvironment() {
50  const dllPath = path.join(APP_ROOT, 'dll');
51  const binPath = path.join(APP_ROOT, 'venv', 'Scripts');
52  const env = {};
53  env.PATH = `${dllPath};${binPath}`;
54  env.HF_HUB_DISABLE_SYMLINKS_WARNING = '1';
55  return env;
56}
57
58// Helper function to download a file from URL with progress
59function downloadFile(url, outputPath) {
60  return new Promise((resolve, reject) => {
61    const file = require('fs').createWriteStream(outputPath);
62    const fileName = path.basename(outputPath);
63
64    const request = https.get(url, (response) => {
65      if (response.statusCode === 200) {
66        const totalSize = parseInt(response.headers['content-length'], 10);
67        let downloadedSize = 0;
68        let lastProgressTime = Date.now();
69
70        response.on('data', (chunk) => {
71          downloadedSize += chunk.length;
72
73          // Log progress every 5 seconds
74          const now = Date.now();
75          if (totalSize && (now - lastProgressTime >= 5000)) {
76            const progress = Math.round((downloadedSize / totalSize) * 100);
77            const mb = (downloadedSize / 1024 / 1024).toFixed(1);
78            const totalMb = (totalSize / 1024 / 1024).toFixed(1);
79            sendPythonOutput(`Downloading ${fileName}: ${mb}/${totalMb} MB (${progress}%)`, 'info');
80            lastProgressTime = now;
81          }
82        });
83
84        response.pipe(file);
85
86        file.on('finish', () => {
87          file.close();
88          resolve();
89        });
90
91        file.on('error', (err) => {
92          fs.unlink(outputPath).catch(() => {}); // Clean up on error
93          reject(err);
94        });
95      } else {
96        file.close();
97        fs.unlink(outputPath).catch(() => {}); // Clean up on error
98        reject(new Error(`Failed to download: HTTP ${response.statusCode}`));
99      }
100    });
101
102    request.on('error', (err) => {
103      file.close();
104      fs.unlink(outputPath).catch(() => {}); // Clean up on error
105      reject(err);
106    });
107  });
108}
109
110function shouldFilterMessage(message) {
111  // Filter out pydub ffmpeg/avconv warning. It does not actually matter.
112  if (message.includes("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work")) {
113    return true;
114  }
115  return false;
116}
117
118// Helper function to setup process event handlers
119function setupProcessHandlers(process) {
120  process.stdout.on('data', (data) => {
121    const text = data.toString();
122    sendPythonOutput(text.trimEnd(), 'stdout');
123  });
124
125  process.stderr.on('data', (data) => {
126    const text = data.toString();
127    if (!shouldFilterMessage(text)) {
128      sendPythonOutput(text.trimEnd(), 'stderr');
129    }
130  });
131
132  process.on('error', (error) => {
133    sendPythonOutput(`Process error: ${error.message}`, 'stderr');
134    runningProcess = null;
135    if (mainWindow && !mainWindow.isDestroyed()) {
136      mainWindow.webContents.send('process-stopped');
137    }
138  });
139
140  process.on('close', (code) => {
141    sendPythonOutput(`Process exited with code ${code}`, 'info');
142    runningProcess = null;
143    if (mainWindow && !mainWindow.isDestroyed()) {
144      mainWindow.webContents.send('process-stopped');
145    }
146  });
147}
148
149// Helper function to execute Python commands using venv
150function executePythonCommand(args, options = {}) {
151  return new Promise((resolve, reject) => {
152    const pythonPath = getVenvPython();
153    const commandStr = `${pythonPath} ${args.join(' ')}`;
154    sendPythonOutput(`> ${commandStr}`, 'info');
155
156    const spawnOptions = {
157      ...options,
158      env: createPythonEnvironment()
159    };
160
161    const pythonProcess = spawn(pythonPath, args, spawnOptions);
162
163    let stdout = '';
164    let stderr = '';
165
166    pythonProcess.stdout.on('data', (data) => {
167      const text = data.toString();
168      stdout += text;
169      sendPythonOutput(text.trimEnd(), 'stdout');
170    });
171
172    pythonProcess.stderr.on('data', (data) => {
173      const text = data.toString();
174      stderr += text;
175      // Filter out specific warning messages
176      if (!shouldFilterMessage(text)) {
177        sendPythonOutput(text.trimEnd(), 'stderr');
178      }
179    });
180
181    pythonProcess.on('error', (error) => {
182      sendPythonOutput(`Failed to start Python process: ${error.message}`, 'stderr');
183      reject({ error: error.message, stdout, stderr });
184    });
185
186    pythonProcess.on('close', (code) => {
187      if (code !== 0) {
188        sendPythonOutput(`Process exited with code ${code}`, 'stderr');
189        reject({ code, stdout, stderr });
190      } else {
191        resolve({ stdout, stderr });
192      }
193    });
194  });
195}
196
197function createWindow () {
198  mainWindow = new BrowserWindow({
199    width: 1000,
200    height: 800,
201    icon: path.join(APP_ROOT, 'Images', 'favicon.ico'),
202    webPreferences: {
203      preload: path.join(__dirname, 'preload.js'),
204      contextIsolation: true,
205      nodeIntegration: false
206    }
207  });
208
209  mainWindow.loadFile('index.html');
210}
211
212// Replace the DEFAULT_CONFIG constant with:
213const DEFAULT_CONFIG = getDefaultConfig();
214
215// IPC handlers
216ipcMain.handle('load-config', async () => {
217  try {
218    const fileContent = await fs.readFile(CONFIG_PATH, 'utf8');
219    return yaml.load(fileContent);
220  } catch (error) {
221    if (error.code === 'ENOENT') {
222      // Config file doesn't exist, create it with defaults
223      console.error('Config file not found, creating with defaults...');
224      try {
225        const yamlContent = yaml.dump(DEFAULT_CONFIG, { lineWidth: -1 });
226        await fs.writeFile(CONFIG_PATH, yamlContent, 'utf8');
227        console.error('Created config.yaml with default values');
228        return DEFAULT_CONFIG;
229      } catch (writeError) {
230        console.error('Error creating default config:', writeError);
231        // Return defaults even if we can't write the file
232        return DEFAULT_CONFIG;
233      }
234    }
235    console.error('Error loading config:', error);
236    throw error;
237  }
238});
239
240ipcMain.handle('save-config', async (event, config) => {
241  try {
242    const yamlContent = yaml.dump(config, { lineWidth: -1 });
243    await fs.writeFile(CONFIG_PATH, yamlContent, 'utf8');
244    return { success: true };
245  } catch (error) {
246    console.error('Error saving config:', error);
247    throw error;
248  }
249});
250
251ipcMain.handle('reset-config', async () => {
252  try {
253    // Check if the file exists first
254    try {
255      await fs.access(CONFIG_PATH);
256      // File exists, delete it
257      await fs.unlink(CONFIG_PATH);
258      console.error('Config file deleted successfully');
259      return { success: true, message: 'Configuration reset to defaults' };
260    } catch (error) {
261      if (error.code === 'ENOENT') {
262        // Config file doesn't exist, that's fine
263        return { success: true, message: 'Configuration already at defaults' };
264      }
265      throw error;
266    }
267  } catch (error) {
268    console.error('Error resetting config:', error);
269    throw new Error(`Failed to reset configuration: ${error.message}`);
270  }
271});
272
273ipcMain.handle('deleteVenvIndicatorFile', async () => {
274  const venvMarkerPath = path.join(APP_ROOT, '.venv_is_set_up');
275  try {
276    await fs.unlink(venvMarkerPath);
277    return { success: true, message: '.venv_is_set_up deleted successfully.' };
278  } catch (error) {
279    if (error.code === 'ENOENT') {
280      return { success: true, message: '.venv_is_set_up not found.' };
281    }
282    console.error('Error deleting .venv_is_set_up file:', error);
283    sendPythonOutput(`Error deleting .venv_is_set_up: ${error.message}`, 'stderr');
284    throw error;
285  }
286});
287
288// Generic function to ensure required files are present
289async function ensureRequiredFiles(config) {
290  const {
291    directoryName,
292    requiredFiles,
293    downloadBaseUrl,
294    resourceType
295  } = config;
296
297  const targetPath = path.join(APP_ROOT, directoryName);
298
299  try {
300    // Check if target directory exists, create it if not
301    try {
302      await fs.access(targetPath);
303      sendPythonOutput(`${resourceType} directory exists`, 'info');
304    } catch (error) {
305      if (error.code === 'ENOENT') {
306        sendPythonOutput(`Creating ${resourceType} directory...`, 'info');
307        await fs.mkdir(targetPath, { recursive: true });
308        sendPythonOutput(`${resourceType} directory created`, 'info');
309      } else {
310        throw error;
311      }
312    }
313
314    // Check each required file
315    const missingFiles = [];
316    for (const fileName of requiredFiles) {
317      const filePath = path.join(targetPath, fileName);
318      try {
319        await fs.access(filePath);
320        sendPythonOutput(`✓ ${fileName} exists`, 'info');
321      } catch (error) {
322        if (error.code === 'ENOENT') {
323          missingFiles.push(fileName);
324          sendPythonOutput(`✗ ${fileName} missing`, 'info');
325        } else {
326          throw error;
327        }
328      }
329    }
330
331    // Download missing files
332    if (missingFiles.length > 0) {
333      sendPythonOutput(`Downloading ${missingFiles.length} missing ${resourceType} file${missingFiles.length > 1 ? 's' : ''}...`, 'info');
334
335      for (const fileName of missingFiles) {
336        const filePath = path.join(targetPath, fileName);
337        const downloadUrl = `${downloadBaseUrl}/${fileName}`;
338
339        try {
340          sendPythonOutput(`Downloading ${fileName}...`, 'info');
341          await downloadFile(downloadUrl, filePath);
342          sendPythonOutput(`✓ Downloaded ${fileName}`, 'info');
343        } catch (downloadError) {
344          sendPythonOutput(`✗ Failed to download ${fileName}: ${downloadError.message}`, 'stderr');
345          throw new Error(`Failed to download ${fileName}: ${downloadError.message}`);
346        }
347      }
348
349      sendPythonOutput(`All missing ${resourceType} files downloaded successfully`, 'info');
350    } else {
351      sendPythonOutput(`All required ${resourceType} files are present`, 'info');
352    }
353
354    return {
355      success: true,
356      message: `${resourceType} setup complete. ${missingFiles.length} file${missingFiles.length > 1 ? 's' : ''} downloaded.`,
357      downloadedFiles: missingFiles
358    };
359  } catch (error) {
360    console.error(`Error setting up ${resourceType} files:`, error);
361    throw new Error(`${resourceType} setup failed: ${error.message}`);
362  }
363}
364
365// Update the install-requirements handler
366ipcMain.handle('install-requirements', async () => {
367  const requirementsPath = path.join(APP_ROOT, 'app', 'requirements.txt');
368  const venvMarkerPath = path.join(APP_ROOT, '.venv_is_set_up');
369
370  try {
371    // Check if venv is already set up
372    try {
373      await fs.access(venvMarkerPath);
374      return { success: true, message: 'Virtual environment already set up' };
375    } catch (error) {
376      // Marker doesn't exist, proceed with setup
377    }
378
379    // Check if requirements.txt exists
380    await fs.access(requirementsPath);
381
382    await executePythonCommand(['-m', 'pip', 'install', '-r', requirementsPath]);
383
384    await ensureRequiredFiles({
385      directoryName: 'dll',
386      requiredFiles: REQUIRED_DLLS,
387      downloadBaseUrl: 'https://yummers.dev/tastt/dll',
388      resourceType: 'DLL'
389    });
390
391    await fs.mkdir(path.join(APP_ROOT, 'Models'), { recursive: true });
392
393    await fs.writeFile(venvMarkerPath, new Date().toISOString(), 'utf8');
394    sendPythonOutput('Created .venv_is_set_up marker file', 'info');
395
396    return { success: true, message: 'Requirements and dependencies installed successfully' };
397  } catch (error) {
398    console.error('Error installing requirements:', error);
399    if (error.code === 'ENOENT') {
400      throw new Error('requirements.txt not found');
401    }
402
403    const errorDetails = error.stderr || error.stdout || error.message || error.error || 'Unknown error';
404    throw new Error(`Installation failed: ${errorDetails}`);
405  }
406});
407
408ipcMain.handle('get-microphones', async () => {
409  const scriptPath = path.join(APP_ROOT, 'app', 'list_microphones.py');
410
411  try {
412    const result = await executePythonCommand([scriptPath]);
413    const microphones = JSON.parse(result.stdout.trim());
414    return microphones;
415  } catch (error) {
416    console.error('Failed to get microphones:', error);
417    throw new Error(`Failed to get microphones: ${error.stderr || error.error || 'Unknown error'}`);
418  }
419});
420
421// Helper function to safely delete directory contents
422async function clearDirectory(dirPath, dirName) {
423  try {
424    await fs.access(dirPath);
425    sendPythonOutput(`Clearing ${dirName} directory...`, 'info');
426
427    const files = await fs.readdir(dirPath);
428    let deletedCount = 0;
429
430    for (const file of files) {
431      const filePath = path.join(dirPath, file);
432
433      try {
434        await fs.rm(filePath, { recursive: true, force: true });
435        sendPythonOutput(`✗ Deleted file ${file}`, 'info');
436
437        deletedCount++;
438      } catch (deleteError) {
439        sendPythonOutput(`Warning: Could not delete ${file}: ${deleteError.message}`, 'stderr');
440        // Continue with other files even if one fails
441      }
442    }
443
444    sendPythonOutput(`${dirName} directory cleared`, 'info');
445    return deletedCount;
446  } catch (error) {
447    if (error.code === 'ENOENT') {
448      sendPythonOutput(`${dirName} directory doesn't exist, skipping`, 'info');
449      return 0;
450    } else {
451      sendPythonOutput(`Error clearing ${dirName} directory: ${error.message}`, 'stderr');
452      throw error;
453    }
454  }
455}
456
457ipcMain.handle('reset-venv', async () => {
458  const venvMarkerPath = path.join(APP_ROOT, '.venv_is_set_up');
459
460  try {
461    sendPythonOutput('Starting virtual environment reset...', 'info');
462
463    // Delete the venv marker file first
464    try {
465      await fs.unlink(venvMarkerPath);
466      sendPythonOutput('Deleted .venv_is_set_up marker file', 'info');
467    } catch (error) {
468      if (error.code !== 'ENOENT') {
469        sendPythonOutput(`Warning: Could not delete marker file: ${error.message}`, 'stderr');
470      }
471    }
472
473    // Get list of installed packages
474    sendPythonOutput('Getting list of installed packages...', 'info');
475    const freezeResult = await executePythonCommand(['-m', 'pip', 'freeze']);
476    const installedPackages = freezeResult.stdout.trim();
477
478    let uninstalledPackages = [];
479
480    if (!installedPackages) {
481      sendPythonOutput('No packages found to uninstall', 'info');
482    } else {
483      // Parse package names and filter out core packages
484      const packageLines = installedPackages.split('\n').filter(line => line.trim());
485      const packageNames = packageLines
486        .map(line => line.split('==')[0].trim())
487        .filter(name => name && !name.startsWith('#'));
488
489      const corePackages = ['pip', 'setuptools', 'wheel'];
490      const packagesToUninstall = packageNames.filter(name => !corePackages.includes(name.toLowerCase()));
491
492      if (packagesToUninstall.length === 0) {
493        sendPythonOutput('Only core packages found, nothing to uninstall', 'info');
494      } else {
495        sendPythonOutput(`Uninstalling ${packagesToUninstall.length} packages...`, 'info');
496
497        const uninstallArgs = ['-m', 'pip', 'uninstall', '-y', ...packagesToUninstall];
498        await executePythonCommand(uninstallArgs);
499        uninstalledPackages = packagesToUninstall;
500      }
501    }
502
503    // Clear downloaded files
504    sendPythonOutput('Clearing downloaded files...', 'info');
505
506    const dllPath = path.join(APP_ROOT, 'dll');
507    const modelsPath = path.join(APP_ROOT, 'Models');
508    const binPath = path.join(APP_ROOT, 'bin');
509
510    const deletedDlls = await clearDirectory(dllPath, 'DLL');
511    const deletedModels = await clearDirectory(modelsPath, 'Models');
512    const deletedBins = await clearDirectory(binPath, 'Binary');
513
514    const totalDeletedFiles = deletedDlls + deletedModels + deletedBins;
515
516    sendPythonOutput('Virtual environment reset successfully!', 'info');
517
518    return {
519      success: true,
520      message: `Virtual environment reset complete. Uninstalled ${uninstalledPackages.length} packages and deleted ${totalDeletedFiles} downloaded files.`,
521      uninstalledPackages,
522      deletedFiles: {
523        dlls: deletedDlls,
524        models: deletedModels,
525        binaries: deletedBins,
526        total: totalDeletedFiles
527      }
528    };
529  } catch (error) {
530    console.error('Error resetting virtual environment:', error);
531    throw new Error(`Virtual environment reset failed: ${error.message}`);
532  }
533});
534
535// Add handlers for starting and stopping the process
536ipcMain.handle('start-process', async () => {
537  if (runningProcess) {
538    throw new Error('Process is already running');
539  }
540
541  const scriptPath = path.join(APP_ROOT, 'app', 'hi.py');
542  const args = [scriptPath, '--config', CONFIG_PATH];
543
544  try {
545    const pythonPath = getVenvPython();
546    sendPythonOutput(`Starting process: ${path.basename(pythonPath)} ${args.join(' ')}`, 'info');
547
548    runningProcess = spawn(pythonPath, args, { env: createPythonEnvironment() });
549    setupProcessHandlers(runningProcess);
550
551    return { success: true };
552  } catch (error) {
553    runningProcess = null;
554    throw error;
555  }
556});
557
558ipcMain.handle('stop-process', async () => {
559  if (!runningProcess) {
560    sendPythonOutput('No process to stop', 'info');
561    return { success: true, forcefullyKilled: false };
562  }
563
564  return new Promise((resolve) => {
565    let forcefullyKilled = false;
566
567    // Set up a timeout to force kill after 10 seconds
568    const killTimeout = setTimeout(() => {
569      if (runningProcess) {
570        sendPythonOutput('Process did not stop gracefully, forcing termination...', 'stderr');
571        forcefullyKilled = true;
572        runningProcess.kill('SIGKILL');
573      }
574    }, 10000);
575
576    // Listen for the process to exit
577    runningProcess.once('exit', (code, signal) => {
578      clearTimeout(killTimeout);
579      runningProcess = null;
580
581      if (forcefullyKilled) {
582        sendPythonOutput('Process forcefully terminated', 'info');
583      } else {
584        sendPythonOutput('Process stopped gracefully', 'info');
585      }
586
587      resolve({ success: true, forcefullyKilled });
588    });
589
590    // Send termination signal
591    sendPythonOutput('Stopping process gracefully...', 'info');
592    runningProcess.kill('SIGTERM');
593  });
594});
595
596ipcMain.handle('get-process-state', () => {
597  return { isRunning: runningProcess !== null };
598});
599
600// Clean up on app quit
601app.on('before-quit', () => {
602  if (runningProcess) {
603    runningProcess.kill();
604  }
605});
606
607app.whenReady().then(() => {
608  createWindow();
609
610  app.on('activate', function () {
611    if (BrowserWindow.getAllWindows().length === 0) createWindow();
612  });
613});
614
615app.on('window-all-closed', function () {
616  app.quit();
617});
618