File size: 943 Bytes
0e1ce81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # Import the required libraries
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image
import gradio as gr
# Set up the model and device
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
# Move the model to the GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
pipe.to(device)
# Define a function to generate an image from text
def generate_image(prompt):
# Generate the image
image = pipe(prompt).images[0]
# Return the image
return image
# Create a Gradio interface
demo = gr.Interface(
fn=generate_image,
inputs=[gr.Textbox(label="Text Prompt", placeholder="Enter a text prompt")],
outputs=[gr.Image(label="Generated Image")],
title="Text-to-Image AI",
description="Enter a text prompt to generate an image",
)
# Launch the Gradio interface
demo.launch() |