diff options
author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-09-20 11:55:24 -0400 |
---|---|---|
committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-09-20 11:55:24 -0400 |
commit | 34a828b2820439b6f7a17a55bca80a8212ba3dca (patch) | |
tree | 773c760337051d144ef21d287788f9ae26aadbe1 /src/server/ApiManagers/AssistantManager.ts | |
parent | 2d61b3b0d00c239f05615c691ffbf4b98f3054e9 (diff) |
added creating of the venv
Diffstat (limited to 'src/server/ApiManagers/AssistantManager.ts')
-rw-r--r-- | src/server/ApiManagers/AssistantManager.ts | 147 |
1 files changed, 87 insertions, 60 deletions
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts index 224d47d3b..42e544e1d 100644 --- a/src/server/ApiManagers/AssistantManager.ts +++ b/src/server/ApiManagers/AssistantManager.ts @@ -442,69 +442,96 @@ export default class AssistantManager extends ApiManager { } function spawnPythonProcess(jobId: string, file_name: string, file_data: string) { - const pythonPath = - process.platform === 'win32' - ? path.join(__dirname, '../chunker/venv/Scripts/python') // Windows path - : path.join(__dirname, '../chunker/venv/bin/python3'); // Linux/Mac path - - const pythonProcess = spawn(pythonPath, [ - path.join(__dirname, '../chunker/pdf_chunker.py'), // Correct path to Python script - jobId, - file_name, - file_data, - ]); - - let pythonOutput = ''; // Accumulate stdout data - let stderrOutput = ''; // For stderr logs and progress - - // Handle stdout data (final result in JSON format) - pythonProcess.stdout.on('data', data => { - pythonOutput += data.toString(); // Accumulate data from stdout - }); - - // Handle stderr (progress logs or errors) - pythonProcess.stderr.on('data', data => { - stderrOutput += data.toString(); - const lines = stderrOutput.split('\n'); - lines.forEach(line => { - if (line.trim()) { - try { - // Progress and warnings are printed as JSON to stderr - const parsedOutput = JSON.parse(line); - - // Handle progress updates - if (parsedOutput.job_id && parsedOutput.progress !== undefined) { - jobProgress[parsedOutput.job_id] = { - step: parsedOutput.step, - progress: parsedOutput.progress, - }; - } else if (parsedOutput.progress !== undefined) { - jobProgress[jobId] = { - step: parsedOutput.step, - progress: parsedOutput.progress, - }; + const venvPath = path.join(__dirname, '../chunker/venv'); + const requirementsPath = path.join(__dirname, '../chunker/requirements.txt'); + const pythonScriptPath = path.join(__dirname, '../chunker/pdf_chunker.py'); + + // Check if venv exists + if (!fs.existsSync(venvPath)) { + console.log('Virtual environment not found. Creating and setting up...'); + + // Create venv + const createVenvProcess = spawn('python', ['-m', 'venv', venvPath]); + + createVenvProcess.on('close', code => { + if (code !== 0) { + console.error(`Failed to create virtual environment. Exit code: ${code}`); + return; + } + + console.log('Virtual environment created. Installing requirements...'); + + // Determine the pip path based on the OS + const pipPath = process.platform === 'win32' ? path.join(venvPath, 'Scripts', 'pip') : path.join(venvPath, 'bin', 'pip'); + + // Install requirements + const installRequirementsProcess = spawn(pipPath, ['install', '-r', requirementsPath]); + + installRequirementsProcess.on('close', code => { + if (code !== 0) { + console.error(`Failed to install requirements. Exit code: ${code}`); + return; + } + + console.log('Requirements installed. Running Python script...'); + runPythonScript(); + }); + }); + } else { + console.log('Virtual environment found. Running Python script...'); + runPythonScript(); + } + + function runPythonScript() { + const pythonPath = process.platform === 'win32' ? path.join(venvPath, 'Scripts', 'python') : path.join(venvPath, 'bin', 'python3'); + + const pythonProcess = spawn(pythonPath, [pythonScriptPath, jobId, file_name, file_data]); + + let pythonOutput = ''; + let stderrOutput = ''; + + pythonProcess.stdout.on('data', data => { + pythonOutput += data.toString(); + }); + + pythonProcess.stderr.on('data', data => { + stderrOutput += data.toString(); + const lines = stderrOutput.split('\n'); + lines.forEach(line => { + if (line.trim()) { + try { + const parsedOutput = JSON.parse(line); + if (parsedOutput.job_id && parsedOutput.progress !== undefined) { + jobProgress[parsedOutput.job_id] = { + step: parsedOutput.step, + progress: parsedOutput.progress, + }; + } else if (parsedOutput.progress !== undefined) { + jobProgress[jobId] = { + step: parsedOutput.step, + progress: parsedOutput.progress, + }; + } + } catch (err) { + console.error('Progress log from Python:', line); } - } catch (err) { - console.error('Progress log from Python:', line); } - } + }); }); - }); - // Handle process exit - pythonProcess.on('close', code => { - if (code === 0) { - // Parse final JSON output (stdout) - try { - const finalResult = JSON.parse(pythonOutput); // Parse JSON from stdout - jobResults[jobId] = finalResult; - jobProgress[jobId] = { step: 'Complete', progress: 100 }; - } catch (err) { - console.error('Error parsing final JSON result:', err); + pythonProcess.on('close', code => { + if (code === 0) { + try { + const finalResult = JSON.parse(pythonOutput); + jobResults[jobId] = finalResult; + jobProgress[jobId] = { step: 'Complete', progress: 100 }; + } catch (err) { + console.error('Error parsing final JSON result:', err); + } + } else { + console.error(`Python process exited with code ${code}`); + jobResults[jobId] = { error: 'Python process failed' }; } - } else { - console.error(`Python process exited with code ${code}`); - jobResults[jobId] = { error: 'Python process failed' }; - } - }); + }); + } } |