aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA.J. Shulman <Shulman.aj@gmail.com>2024-09-20 11:55:24 -0400
committerA.J. Shulman <Shulman.aj@gmail.com>2024-09-20 11:55:24 -0400
commit34a828b2820439b6f7a17a55bca80a8212ba3dca (patch)
tree773c760337051d144ef21d287788f9ae26aadbe1
parent2d61b3b0d00c239f05615c691ffbf4b98f3054e9 (diff)
added creating of the venv
-rw-r--r--src/server/ApiManagers/AssistantManager.ts147
-rw-r--r--startup.sh30
2 files changed, 87 insertions, 90 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' };
- }
- });
+ });
+ }
}
diff --git a/startup.sh b/startup.sh
index c0ee53a4f..d741658ad 100644
--- a/startup.sh
+++ b/startup.sh
@@ -1,34 +1,4 @@
#!/bin/bash
-cd /c/Users/dash/Documents/GitHub/Dash-Web/src/server # cd /c/Users/dash/Documents/Dash-Web instead for dash-release
-echo "Navigating to the Python chunker directory..."
-cd chunker/
-
-# Step 3: Set up Python virtual environment (if it doesn't exist, create it)
-if [ ! -d "venv" ]; then
- echo "Creating Python virtual environment..."
- python3 -m venv venv
-fi
-
-# Step 4: Activate the Python virtual environment
-echo "Activating Python virtual environment..."
-source venv/bin/activate
-
-# Step 5: Install Python dependencies
-echo "Installing Python dependencies..."
-pip install -r requirements.txt
-
-# Step 6: Go back to the main directory where the Node.js app will run
-cd ..
-
-# Step 7: Load environment variables from .env file (if it exists)
-if [ -f .env ]; then
- echo "Loading environment variables from .env file..."
- export $(grep -v '^#' .env | xargs)
-else
- echo ".env file not found, skipping environment variable loading."
-fi
-
-
cd /c/Users/dash/Documents/GitHub/Dash-Web # cd /c/Users/dash/Documents/Dash-Web instead for dash-release
npm run start-release
# works for browndash \ No newline at end of file