File size: 1,054 Bytes
b830766 2605fc4 b830766 | 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 | import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
# Load the text-to-image pipeline
# Using a smaller model for free tier compatibility; change model_id for better results (might need GPU)
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
# Define the prediction function
def generate_image(prompt):
# Ensure the model uses available device (CPU or GPU on HF Spaces)
if torch.cuda.is_available():
pipe.to("cuda")
image = pipe(prompt).images[0]
return image
# Create the Gradio interface
demo = gr.Interface(
fn=generate_image,
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
outputs=gr.Image(label="Generated Image"),
title="Text-to-Image Generator",
description="Generate images from text using a Stable Diffusion model.",
examples=["a a photo of a cat in a hat", "a painting of a starry night over a city"]
)
# Launch the app
if __name__ == "__main__":
demo.launch()
|