from diffusers import StableDiffusionPipeline import torch def…
- User
from diffusers import StableDiffusionPipeline import torch
def generate_image_from_text(prompt_text: str): # Загрузка оптимизированной модели model_id = "runwayml/stable-diffusion-v1-5" # можно заменить на "stabilityai/stable-diffusion-2" pipe = StableDiffusionPipeline.from_pretrained(model_id)
# Переводим модель на GPU (если доступен) pipe = pipe.to("cuda") # Включаем оптимизацию для ускорения torch.backends.cudnn.benchmark = True # Генерация изображения image = pipe(prompt_text).images[0] print('Image generated') return image # возвращаем изображение в формате PILПример текста для генерации изображения
prompt_text = "An inspiring scene showing perseverance and strength, a journey towards a glowing distant goal." image = generate_image_from_text(prompt_text)
Сохранение изображения
image.save("generated_image.png") print("Image saved as generated_image.png")
Cannot initialize model with low cpu memory usage because
acceleratewas not found in the environment. Defaulting tolow_cpu_mem_usage=False. It is strongly recommended to installacceleratefor faster and less memory-intense model loading. You can do so with:pip install accelerate - Assistant
The warning message you're encountering suggests that the model could be loaded more efficiently with the
acceleratelibrary. This library can help reduce memory usage and speed up the model loading process, especially useful if you're working with limited computational resources. Here's what you can do to address this issue:-
Install
accelerate:
To install theacceleratelibrary, run the following command in your environment:pip install accelerate -
Update Your Code:
Once is installed, the same code should automatically leverage it to optimize the loading process of the model, reducing CPU memory usage.
-