Skip to content

Commit 79b6b79

Browse files
More robust boolean handling
1 parent 6ca163a commit 79b6b79

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

server/routes/document.routes.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ router.post('/analyze-documents', express.json(), async (req, res) => {
2121

2222
// Create a JSON payload to send via STDIN
2323
const inputData = JSON.stringify({ documents });
24+
logger.info(`Sending payload to Python: ${inputData}`);
2425

2526
// Spawn the Python process with piped stdio so we can write to STDIN and read STDOUT
2627
const pythonProcess = spawn(PYTHON_PATH, SCRIPT_ARGS, { stdio: ['pipe', 'pipe', 'pipe'] });
@@ -49,11 +50,15 @@ router.post('/analyze-documents', express.json(), async (req, res) => {
4950

5051
// When the Python process closes, process the output
5152
pythonProcess.on('close', (code) => {
53+
logger.info(`Python process exited with code: ${code}`);
54+
logger.info(`Raw Python output: ${stdout}`);
55+
5256
if (code !== 0) {
5357
const errorMsg = stderr || `Python process exited with code ${code}`;
5458
logger.error(`Python process failed: ${errorMsg}`);
5559
return res.status(500).json({ error: errorMsg });
5660
}
61+
5762
let result;
5863
try {
5964
result = JSON.parse(stdout);
@@ -62,11 +67,20 @@ router.post('/analyze-documents', express.json(), async (req, res) => {
6267
logger.error(`Raw output: ${stdout}`);
6368
return res.status(500).json({ error: 'Invalid response from Python process' });
6469
}
70+
71+
logger.info(`Parsed Python result: ${JSON.stringify(result)}`);
72+
6573
// Convert boolean predictions into "compliant" / "non-compliant" strings.
6674
const transformed = {};
6775
for (const fname in result.predictions) {
68-
transformed[fname] = result.predictions[fname] ? 'compliant' : 'non-compliant';
76+
const prediction = result.predictions[fname];
77+
logger.info(`Document: ${fname}, Prediction: ${prediction}`);
78+
// Convert string booleans to actual booleans if necessary
79+
const isCompliant = (prediction === true || prediction === 'True');
80+
transformed[fname] = isCompliant ? 'compliant' : 'non-compliant';
6981
}
82+
83+
logger.info(`Transformed result: ${JSON.stringify(transformed)}`);
7084
res.json(transformed);
7185
});
7286
} catch (error) {

0 commit comments

Comments
 (0)