aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/ApiManagers/AssistantManager.ts147
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' };
- }
- });
+ });
+ }
}