    def predict(
        self,
        video: Path = Input(description="Input video for segmentation"),
        instruction: str = Input(description="Text instruction for the model"),
        frame_interval: int = Input(description="Frame interval for processing", default=6, ge=1, le=30),
    ) -> Output:
        """Run a single prediction on the model"""
        # clean up past runs remove /tmp/output folder
        if os.path.exists("/tmp/output"):
            shutil.rmtree("/tmp/output")
        
        os.makedirs("/tmp/output")

        # Process video frames
        vid_frames, image_paths = read_video(str(video), frame_interval)
        
        # Get video properties for output
        cap = cv2.VideoCapture(str(video))
        if not cap.isOpened():
            raise ValueError("Failed to open video file")
        original_fps = cap.get(cv2.CAP_PROP_FPS) 
        if original_fps == 0:
            original_fps = 30.0  # Default to 30fps if unable to read
        new_fps = original_fps / frame_interval if frame_interval > 1 else original_fps
        cap.release()

        # Prepare the input
        question = f"<image>{instruction}"
        result = self.model.predict_forward(
            video=vid_frames,
            text=question,
            tokenizer=self.tokenizer,
        )
        prediction = result['prediction']

        output_video_path = None
        masked_video_path = None

        if '[SEG]' in prediction:
            _seg_idx = 0
            pred_masks = result['prediction_masks'][_seg_idx]
            seg_frames = []
            masked_only_frames = []

            temp_dir = tempfile.mkdtemp()
            os.makedirs(temp_dir, exist_ok=True)

            # Process each frame
            for frame_idx in range(len(vid_frames)):
                pred_mask = pred_masks[frame_idx]
                
                # Create visualized frame with segmentation overlay
                seg_frame = visualize(pred_mask, image_paths[frame_idx], temp_dir)
                seg_frames.append(seg_frame)

                # Create binary mask frame
                binary_mask = (pred_mask.astype('uint8') * 255)
                binary_mask_path = os.path.join(temp_dir, f"binary_mask_{frame_idx}.png")
                cv2.imwrite(binary_mask_path, binary_mask)
                masked_only_frames.append(binary_mask_path)

            # Read first frame for dimensions
            frame = cv2.imread(seg_frames[0])
            height, width, layers = frame.shape

            # Create output video files
            masked_video_path = "/tmp/output/masked_video.mp4"
            temp_masked_path = "/tmp/output/temp_masked.avi"

            # Define video writer using a more basic codec
            fourcc = cv2.VideoWriter_fourcc(*'MJPG')
            masked_video_writer = cv2.VideoWriter(temp_masked_path, fourcc, new_fps, (width, height), isColor=False)

            # Write frames to video
            for mask_frame_path in masked_only_frames:
                mask_frame = cv2.imread(mask_frame_path, cv2.IMREAD_GRAYSCALE)
                masked_video_writer.write(mask_frame)

            # Release video writer
            masked_video_writer.release()

            # Convert to web-compatible MP4 using ffmpeg
            subprocess.run([
                'ffmpeg', '-i', temp_masked_path, '-c:v', 'libx264',
                '-preset', 'fast', '-pix_fmt', 'yuv420p', masked_video_path
            ], check=True)

            # Clean up temporary file
            os.remove(temp_masked_path)

        return Output(
            masked_video=Path(masked_video_path) if masked_video_path else None,
            response=str(prediction)
        ) 
Run a single prediction on the model
demo/predict-vid.py