- Регистрация
- 9 Май 2015
- Сообщения
- 1,549
- Баллы
- 155
Create virtual environment
- Create a project folder
- Cd to the project folder
- Create virtual environment: conda create -n ai_painting python=3.10
- Activate virtual environment: conda activate ai_painting
- Install CPU torch pip install torch trochvision trochaudio
- Install AI painting libraries pip install diffusers["torch"] transformers accelerate
- Install web framework:pip install "fastapi[standard]"
Create a python file:
import torch
from diffusers import StableDiffusionPipeline
import gc # Garbage collector
print("
# Force CPU and clear memory
device = "cpu"
torch.cuda.empty_cache() if torch.cuda.is_available() else None
gc.collect()
print("
try:
# Load with memory optimizations
model_id = "runwayml/stable-diffusion-v1-5"
# Load to CPU with specific settings for stability
pipe = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float32, # Use float32 for CPU stability
use_safetensors=True
)
pipe = pipe.to(device)
# Disable the safety checker to save memory
pipe.safety_checker = None
pipe.requires_safety_checker = False
print("
# Generate a smaller image to save memory
prompt = "a simple red apple on a table"
with torch.no_grad():
image = pipe(
prompt,
num_inference_steps=20, # Fewer steps = less memory
guidance_scale=7.5,
width=256, # Smaller image
height=256 # Smaller image
).images[0]
image.save("test_small.jpg")
print("
except Exception as e:
print(f"
Источник: