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