# 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()