Ensure reticulate is loaded

if (!requireNamespace("reticulate", quietly = TRUE)) {
install.packages("reticulate", repos = "https://cloud.r-project.org")
}
library(reticulate)

Install required packages dynamically

required_packages <- c("torch", "diffusers", "transformers", "accelerate", "omegaconf", "safetensors")
for (pkg in required_packages) {
if (!py_module_available(pkg)) {
message(paste("Installing missing package:", pkg))
reticulate::py_install(pkg, pip = TRUE)
}
}

Run the Python block in R

py_run_string('
import torch
from diffusers import StableDiffusionPipeline

Use CUDA for GPU acceleration if available

device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if torch.cuda.is_available() else torch.float32

print(f"Loading model on device: {device} with dtype: {dtype}")

Load using from_single_file with proper repo and file parameter routing

pipe = StableDiffusionPipeline.from_single_file(
"snwy/SD1.5-DALLE-2",
checkpoint_file="SD1.5-DALLE-2.safetensors",
torch_dtype=dtype
)
pipe = pipe.to(device)

prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
image = pipe(prompt).images[0]
image.save("dalle2_astronaut.png")
')
cat("Image generated and saved as dalle2_astronaut.png\n")

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment