This document explains how to set up and use the Python integration features in Stream Diffusion RS.
Stream Diffusion RS includes Python interop capabilities that allow the Rust application to leverage Python-based AI/ML libraries such as:
- PyTorch for deep learning models
- Diffusers for diffusion models
- Transformers for NLP models
- MNE/SciPy for EEG processing
- NumPy for numerical computations
- Python 3.8 or later
- pip package manager
- Virtual environment support (venv module)
Run the setup script for your platform:
Linux/macOS:
./scripts/setup_python_env.shWindows:
scripts\setup_python_env.bat-
Create a virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
# Linux/macOS source venv/bin/activate # Windows venv\Scripts\activate.bat
-
Upgrade pip:
pip install --upgrade pip
-
Install dependencies:
pip install -r scripts/requirements.txt
After setting up the Python environment, you can test the integration:
-
Activate the virtual environment
-
Run the Python test script:
python scripts/test_python_integration.py
-
Run the Rust test binary:
cargo run --bin test_python_integration
The Python integration allows for real image generation using Stable Diffusion models:
from diffusers import StableDiffusionPipeline
import torch
# Load model
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
# Generate image
image = pipe("a beautiful landscape").images[0]The integration also supports EEG data processing:
import numpy as np
from scipy import signal
# Process EEG data
eeg_data = np.load("eeg_data.npy")
filtered_data = signal.butterworth(eeg_data, 4, [8, 13], btype='band')Manages the Python execution environment.
Methods:
new(python_path: &str)- Create new environmentinstall_dependencies()- Install required packagesexecute_script(script: &str, args: Option<&[&str]>)- Execute Python scriptexecute_script_file(script_path: &Path, args: Option<&[&str]>)- Execute Python script file
Interface for Python-based AI/ML models.
Methods:
new(model_path: &str, model_type: &str, python_env: PythonEnvironment)- Create new modelload_model()- Load modelgenerate_image(prompt: &str, steps: usize)- Generate image from promptprocess_eeg(eeg_data_path: &str)- Process EEG data
- Python not found: Ensure Python 3.8+ is installed and in PATH
- Import errors: Make sure all dependencies are installed
- CUDA issues: Verify CUDA drivers and PyTorch CUDA version compatibility
Enable verbose logging by setting the environment variable:
export RUST_LOG=debugTo extend the Python integration:
- Add new methods to the
PythonModelstruct insrc/python.rs - Create corresponding Python functions in
scripts/diffusion_model.py - Update the requirements.txt file if new dependencies are needed
- Add tests to verify new functionality
This project is licensed under the MIT License. See the LICENSE file for details.