Spaces:
Sleeping
Sleeping
File size: 6,420 Bytes
02a7823 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
from PIL import Image
import spaces
# Initialize the model
MODEL_ID = "runwayml/stable-diffusion-v1-5"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Load the pipeline
@spaces.GPU
def load_pipeline():
pipe = StableDiffusionPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
safety_checker=None,
requires_safety_checker=False
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(DEVICE)
if torch.cuda.is_available():
pipe.enable_memory_efficient_attention()
pipe.enable_xformers_memory_efficient_attention()
return pipe
# Load pipeline at startup
pipe = load_pipeline()
@spaces.GPU
def generate_image(prompt, negative_prompt="", num_inference_steps=25, guidance_scale=7.5, width=512, height=512, seed=-1):
"""
Generate image from text prompt using Stable Diffusion
"""
try:
# Set seed for reproducibility
if seed != -1:
generator = torch.Generator(device=DEVICE).manual_seed(seed)
else:
generator = None
# Generate image
with torch.autocast(DEVICE):
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
width=width,
height=height,
generator=generator
).images[0]
return image
except Exception as e:
print(f"Error generating image: {e}")
return None
# Create Gradio interface
def create_interface():
with gr.Blocks(title="Text-to-Image Generator", theme=gr.themes.Soft()) as demo:
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px;">
<h1>🎨 Text-to-Image Generator</h1>
<p>Generate beautiful images from text descriptions using Stable Diffusion</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
# Input controls
prompt_input = gr.Textbox(
label="Prompt",
placeholder="Enter your image description here...",
lines=3,
value="A beautiful sunset over mountains, highly detailed, 8k resolution"
)
negative_prompt_input = gr.Textbox(
label="Negative Prompt (Optional)",
placeholder="What you don't want in the image...",
lines=2,
value="blurry, low quality, distorted"
)
with gr.Row():
steps_slider = gr.Slider(
minimum=10,
maximum=50,
value=25,
step=1,
label="Inference Steps"
)
guidance_slider = gr.Slider(
minimum=1.0,
maximum=20.0,
value=7.5,
step=0.5,
label="Guidance Scale"
)
with gr.Row():
width_slider = gr.Slider(
minimum=256,
maximum=1024,
value=512,
step=64,
label="Width"
)
height_slider = gr.Slider(
minimum=256,
maximum=1024,
value=512,
step=64,
label="Height"
)
seed_input = gr.Number(
label="Seed (-1 for random)",
value=-1,
precision=0
)
generate_btn = gr.Button(
"🎨 Generate Image",
variant="primary",
size="lg"
)
with gr.Column(scale=1):
# Output
output_image = gr.Image(
label="Generated Image",
type="pil",
height=500
)
# Example prompts
gr.HTML("<h3>💡 Example Prompts</h3>")
examples = gr.Examples(
examples=[
["A majestic lion in a savanna at golden hour, photorealistic, 8k"],
["Cyberpunk city at night with neon lights, futuristic, detailed"],
["A cozy cottage in a magical forest, fairy tale style, warm lighting"],
["Abstract art with vibrant colors, geometric shapes, modern"],
["A space explorer on an alien planet, sci-fi, dramatic lighting"],
],
inputs=prompt_input,
label="Click on an example to try it out!"
)
# Event handlers
generate_btn.click(
fn=generate_image,
inputs=[
prompt_input,
negative_prompt_input,
steps_slider,
guidance_slider,
width_slider,
height_slider,
seed_input
],
outputs=output_image,
show_progress=True
)
# Allow Enter key to generate
prompt_input.submit(
fn=generate_image,
inputs=[
prompt_input,
negative_prompt_input,
steps_slider,
guidance_slider,
width_slider,
height_slider,
seed_input
],
outputs=output_image,
show_progress=True
)
return demo
# Launch the app
if __name__ == "__main__":
demo = create_interface()
demo.queue(max_size=20)
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |