From 183300472e537456081c3a14ebadbc2bc43a9e71 Mon Sep 17 00:00:00 2001 From: Masonmason Date: Thu, 17 Jul 2025 09:46:31 +0800 Subject: [PATCH] fix: Resolve array comparison error and add inference stop functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix ambiguous truth value error in InferencePipeline result handling - Add stop inference button to deployment dialog with proper UI state management - Improve error handling for tuple vs dict result types 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../core/functions/InferencePipeline.py | 14 ++++-- cluster4npu_ui/ui/dialogs/deployment.py | 45 ++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/cluster4npu_ui/core/functions/InferencePipeline.py b/cluster4npu_ui/core/functions/InferencePipeline.py index 83d8914..4a9d639 100644 --- a/cluster4npu_ui/core/functions/InferencePipeline.py +++ b/cluster4npu_ui/core/functions/InferencePipeline.py @@ -215,10 +215,16 @@ class PipelineStage: timeout_start = time.time() while time.time() - timeout_start < 5.0: # 5 second timeout result = self.multidongle.get_latest_inference_result(timeout=0.1) - # Check if result is not None and not an empty dict - if result is not None and (not isinstance(result, dict) or result): - inference_result = result - break + # Check if result is not None and not an empty dict/tuple + if result is not None: + if isinstance(result, dict): + if result: # Non-empty dict + inference_result = result + break + else: + # For tuple results like (probability, result_string) + inference_result = result + break time.sleep(0.01) if not inference_result: diff --git a/cluster4npu_ui/ui/dialogs/deployment.py b/cluster4npu_ui/ui/dialogs/deployment.py index 5660e73..242e063 100644 --- a/cluster4npu_ui/ui/dialogs/deployment.py +++ b/cluster4npu_ui/ui/dialogs/deployment.py @@ -220,6 +220,12 @@ class DeploymentDialog(QDialog): self.deploy_button.setEnabled(False) button_layout.addWidget(self.deploy_button) + self.stop_button = QPushButton("Stop Inference") + self.stop_button.clicked.connect(self.stop_deployment) + self.stop_button.setEnabled(False) + self.stop_button.setVisible(False) + button_layout.addWidget(self.stop_button) + button_layout.addStretch() self.close_button = QPushButton("Close") @@ -518,6 +524,29 @@ Stage Configurations: self.deployment_worker.start() + def stop_deployment(self): + """Stop the current deployment/inference.""" + if self.deployment_worker and self.deployment_worker.isRunning(): + reply = QMessageBox.question(self, "Stop Inference", + "Are you sure you want to stop the inference?", + QMessageBox.Yes | QMessageBox.No) + if reply == QMessageBox.Yes: + self.deployment_log.append("Stopping inference...") + self.status_label.setText("Stopping inference...") + self.deployment_worker.stop() + self.deployment_worker.wait(3000) # Wait up to 3 seconds + + # Update UI + self.stop_button.setEnabled(False) + self.stop_button.setVisible(False) + self.deploy_button.setEnabled(True) + self.close_button.setText("Close") + self.progress_bar.setVisible(False) + + self.deployment_log.append("Inference stopped.") + self.status_label.setText("Inference stopped") + self.dongle_status.setText("Pipeline stopped") + def update_progress(self, value: int, message: str): """Update deployment progress.""" self.progress_bar.setValue(value) @@ -536,6 +565,11 @@ Stage Configurations: """Handle deployment start.""" self.deployment_log.append("Connecting to dongles...") self.dongle_status.setText("Initializing dongles...") + + # Show stop button and hide deploy button + self.stop_button.setEnabled(True) + self.stop_button.setVisible(True) + self.deploy_button.setEnabled(False) def on_deployment_completed(self, success: bool, message: str): """Handle deployment completion.""" @@ -545,12 +579,18 @@ Stage Configurations: self.deployment_log.append(f"SUCCESS: {message}") self.status_label.setText("Deployment completed successfully!") self.dongle_status.setText("Pipeline running on dongles") + # Keep stop button visible for successful deployment + self.stop_button.setEnabled(True) + self.stop_button.setVisible(True) QMessageBox.information(self, "Deployment Success", message) else: self.deployment_log.append(f"FAILED: {message}") self.status_label.setText("Deployment failed") + # Hide stop button for failed deployment + self.stop_button.setEnabled(False) + self.stop_button.setVisible(False) + self.deploy_button.setEnabled(True) - self.deploy_button.setEnabled(True) self.close_button.setText("Close") self.progress_bar.setVisible(False) @@ -560,6 +600,9 @@ Stage Configurations: self.status_label.setText("Deployment failed") QMessageBox.critical(self, "Deployment Error", error) + # Hide stop button and show deploy button on error + self.stop_button.setEnabled(False) + self.stop_button.setVisible(False) self.deploy_button.setEnabled(True) self.close_button.setText("Close") self.progress_bar.setVisible(False)