Spaces:
Paused
Paused
| import torch | |
| from diffusers import StableDiffusionPipeline | |
| import gradio as gr | |
| # Load model (optimized for Hugging Face Spaces) | |
| model_id = "stabilityai/sd-turbo" | |
| pipe = StableDiffusionPipeline.from_pretrained( | |
| model_id, | |
| torch_dtype=torch.float16 | |
| ) | |
| # Use GPU if available | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| pipe = pipe.to(device) | |
| # Enable memory optimization | |
| pipe.enable_attention_slicing() | |
| # Image generation function | |
| def generate_image(prompt): | |
| image = pipe( | |
| prompt, | |
| num_inference_steps=20, # reduce from default (~50) | |
| guidance_scale=7.5 | |
| ).images[0] | |
| return image | |
| # Gradio UI | |
| interface = gr.Interface( | |
| fn=generate_image, | |
| inputs=gr.Textbox( | |
| lines=2, | |
| placeholder="Enter your prompt (e.g., A futuristic car in neon city)" | |
| ), | |
| outputs="image", | |
| title="🎨 AI Image Generator (Stable Diffusion)", | |
| description="Type a prompt and generate AI images instantly 🚀" | |
| ) | |
| # Launch app | |
| if __name__ == "__main__": | |
| interface.launch() |