| |
|
|
| import os |
| import sys |
| import torch |
| from datetime import datetime |
| from diffusers import StableDiffusionPipeline |
| from PIL import Image |
|
|
| |
| sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) |
|
|
| |
|
|
| |
| if torch.cuda.is_available(): |
| device = "cuda" |
| print("CUDA GPU detected. Running on GPU for best performance.") |
| else: |
| device = "cpu" |
| print("No CUDA GPU detected. Running on CPU. Generation will be slow.") |
|
|
| |
| OUTPUT_DIR = os.path.join(os.path.dirname(__file__), '..', '..', "outputs") |
| os.makedirs(OUTPUT_DIR, exist_ok=True) |
|
|
| |
| def log(msg): |
| now = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]") |
| print(f"{now} {msg}") |
|
|
| |
|
|
| MODEL_NAME = "runwayml/stable-diffusion-v1-5" |
| log(f"Loading model: {MODEL_NAME} (this may take a minute on first run)") |
|
|
| |
| def dummy_safety_checker(images, **kwargs): |
| return images, [False] * len(images) |
|
|
| try: |
| pipe = StableDiffusionPipeline.from_pretrained( |
| MODEL_NAME, |
| torch_dtype=torch.float16 if device == "cuda" else torch.float32, |
| safety_checker=dummy_safety_checker, |
| ) |
| except Exception as e: |
| log(f"Error loading model: {e}") |
| sys.exit(1) |
|
|
| pipe = pipe.to(device) |
| pipe.enable_attention_slicing() |
|
|
| log("Model loaded successfully.") |
|
|
| |
|
|
| def main(): |
| """Main function for command-line execution""" |
| if len(sys.argv) > 1: |
| prompt = " ".join(sys.argv[1:]) |
| log(f"Prompt taken from command line: {prompt}") |
| else: |
| prompt = input("Enter your prompt (e.g. 'A magical forest, digital art'): ").strip() |
| log(f"Prompt entered: {prompt}") |
|
|
| if not prompt: |
| log("No prompt provided. Exiting.") |
| sys.exit(0) |
|
|
| |
|
|
| SEED = torch.seed() |
| generator = torch.manual_seed(SEED) if device == "cpu" else torch.Generator(device).manual_seed(torch.seed()) |
|
|
| num_inference_steps = 30 |
| guidance_scale = 7.5 |
|
|
| |
| height = 512 |
| width = 512 |
|
|
| |
|
|
| log(f"Generating image for prompt: {prompt}") |
| log(f"Params: steps={num_inference_steps}, guidance_scale={guidance_scale}, seed={SEED}") |
|
|
| with torch.autocast(device) if device == "cuda" else torch.no_grad(): |
| result = pipe( |
| prompt, |
| height=height, |
| width=width, |
| num_inference_steps=num_inference_steps, |
| guidance_scale=guidance_scale, |
| generator=generator, |
| ) |
|
|
| image: Image.Image = result.images[0] |
|
|
| |
|
|
| |
| prompt_slug = "_".join(prompt.lower().split()[:6]) |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
| filename = f"{prompt_slug[:40]}_{timestamp}_seed{SEED}.png" |
| filepath = os.path.join(OUTPUT_DIR, filename) |
| image.save(filepath) |
| log(f"Image saved to {filepath}") |
|
|
| |
| |
|
|
| |
| log("Generation complete.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|