Skip to content

Commit

Permalink
Enable prompt for basic Img2Img diffusion via text #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pramitchoudhary committed Oct 31, 2022
1 parent 4a46930 commit caf17c5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions img_styler/image_prompt/stable_diffusion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import torch
from torch import autocast
from PIL import Image
from io import BytesIO
from diffusers import StableDiffusionImg2ImgPipeline


def generate_image_with_prompt(input_img_path: str, prompt_txt: str = "Face portrait",
output_path: str=None, save: bool=True):
torch.cuda.empty_cache()
device = "cuda"
# License: https://huggingface.co/spaces/CompVis/stable-diffusion-license
model_path = "./models/stable_diffusion_v1_4"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_path, revision="fp16",
torch_dtype=torch.float16,)
pipe = pipe.to(device)

# Open image
image_input = Image.open(input_img_path).convert("RGB")
init_image = image_input.resize((512, 512))

with autocast(device):
images = pipe(prompt=prompt_txt, init_image=init_image, strength=0.5, guidance_scale=7.5)["sample"]

file_name = output_path + '/result.jpg'
images[0].save(file_name)
return file_name

0 comments on commit caf17c5

Please sign in to comment.