-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ShyVortex/dev-sd3
Integrate stable-diffusion-3 pipeline
- Loading branch information
Showing
16 changed files
with
222 additions
and
59 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import sys | ||
from diffusers import StableDiffusion3Pipeline | ||
from PIL import Image | ||
from io import BytesIO | ||
import torch | ||
import os | ||
import base64 | ||
|
||
|
||
def main(): | ||
# Check if the correct number of command-line arguments is provided | ||
if len(sys.argv) != 4: | ||
print("Usage: python generate_sd3.py <prompt> <tags> <date>") | ||
sys.exit(1) | ||
|
||
# Get the prompt and date from the command-line arguments passed from Java | ||
prompt = sys.argv[1] | ||
tags = sys.argv[2] | ||
date = sys.argv[3] | ||
|
||
# Model initialization and processing | ||
repo_id = "stabilityai/stable-diffusion-3-medium-diffusers" | ||
pipe = StableDiffusion3Pipeline.from_pretrained( | ||
repo_id, | ||
|
||
# removes memory-intensive text encoder to decrease memory requirements | ||
text_encoder_3=None, | ||
tokenizer_3=None, | ||
|
||
torch_dtype=torch.float16, | ||
) | ||
|
||
# offload components to CPU during inference to save memory | ||
pipe.enable_model_cpu_offload() | ||
|
||
# Process the prompt and set the output path | ||
with torch.cuda.amp.autocast(): | ||
image = pipe( | ||
prompt=prompt, | ||
negative_prompt=tags, | ||
num_inference_steps=25, | ||
guidance_scale=6.5 | ||
).images[0] | ||
output_folder = os.path.abspath("result/generated/sd3") | ||
output_filename = f"generated_image_{date}.png" | ||
output_filepath = os.path.join(output_folder, output_filename) | ||
|
||
# Check if the output folder exists, and create it if not, then save the image | ||
if not os.path.exists(output_folder): | ||
os.makedirs(output_folder) | ||
image.save(output_filepath) | ||
|
||
# Encode the image as a base64 string | ||
with open(output_filepath, "rb") as image_file: | ||
encoded_image = base64.b64encode(image_file.read()).decode('utf-8') | ||
|
||
# Print image as string | ||
print(encoded_image) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.