panchadip's picture
Create app.py
2c57eba verified
raw
history blame
1.03 kB
from diffusers import StableDiffusionPipeline # Import the StableDiffusionPipeline class from diffusers
import torch
# Define the model ID for Stable Diffusion
model_id = "CompVis/stable-diffusion-v1-4"
# Load the pre-trained model
pipe = StableDiffusionPipeline.from_pretrained(model_id)
# Move the model to GPU if available, otherwise use CPU
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
# Function to generate images based on text prompts
def generate_image(prompt, output_path="generated_image.png"):
# Generate the image
image = pipe(prompt).images[0]
# Save the generated image
image.save(output_path)
print(f"Image saved at: {output_path}")
# Example prompts for image generation
prompt1 = "A futuristic city skyline at sunset"
generate_image(prompt1, "image1.png")
prompt2 = "A beautiful landscape from mountains during sunrise"
generate_image(prompt2, "image2.png")
prompt3 = "A beautiful landscape from mountains during sunset"
generate_image(prompt3, "image3.png")