File size: 1,038 Bytes
679fdcd
 
 
 
 
04b274e
679fdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
04b274e
 
 
 
 
679fdcd
04b274e
679fdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
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()