-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for Text-to-Image diffusion #5
- Loading branch information
1 parent
0d2acdd
commit 1d43df0
Showing
3 changed files
with
36 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
import gc | ||
from io import BytesIO | ||
from typing import Optional | ||
|
||
import torch | ||
from torch import autocast | ||
from diffusers import StableDiffusionImg2ImgPipeline, StableDiffusionPipeline | ||
from PIL import Image | ||
from io import BytesIO | ||
from diffusers import StableDiffusionImg2ImgPipeline | ||
from torch import autocast | ||
|
||
|
||
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" | ||
def generate_image_with_prompt(input_img_path: Optional[str]=None, prompt_txt: str = "Face portrait", | ||
output_path: str=None): | ||
# License: https://huggingface.co/spaces/CompVis/stable-diffusion-license | ||
torch.cuda.empty_cache() | ||
device = 'cuda' if torch.cuda.is_available() else 'cpu' | ||
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)) | ||
if input_img_path: | ||
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_path, revision="fp16", | ||
torch_dtype=torch.float16).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"] | ||
else: # Default prompt | ||
pipe = StableDiffusionPipeline.from_pretrained(model_path, revision="fp16", | ||
torch_dtype=torch.float16).to(device) | ||
|
||
with autocast(device): | ||
images = pipe(prompt=prompt_txt).images | ||
|
||
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) | ||
if output_path: | ||
images[0].save(file_name) | ||
gc.collect() | ||
torch.cuda.empty_cache() | ||
return file_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters