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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import AutoPipelineForText2Image
|
| 3 |
+
import torch
|
| 4 |
+
import gc
|
| 5 |
+
|
| 6 |
+
# --------------------------------------------------------
|
| 7 |
+
# 1. Loading the Turbo Model for CPU Inference
|
| 8 |
+
# --------------------------------------------------------
|
| 9 |
+
print("🔄 Loading the Text-to-Image AI Model (SD Turbo - Fast Inference)...")
|
| 10 |
+
|
| 11 |
+
# We use the AutoPipelineForText2Image from diffusers.
|
| 12 |
+
# 'stabilityai/sd-turbo' allows acceptable quality in just 1 to 4 steps!
|
| 13 |
+
# This makes it viable for CPU-only execution in HuggingFace free spaces.
|
| 14 |
+
try:
|
| 15 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 16 |
+
"stabilityai/sd-turbo",
|
| 17 |
+
torch_dtype=torch.float32,
|
| 18 |
+
variant="fp16" # use fp16 weights where possible to save memory
|
| 19 |
+
)
|
| 20 |
+
pipe = pipe.to("cpu")
|
| 21 |
+
|
| 22 |
+
# Optional performance tweaks for CPU:
|
| 23 |
+
pipe.set_progress_bar_config(disable=True)
|
| 24 |
+
|
| 25 |
+
print("✅ Model loaded successfully!")
|
| 26 |
+
print(f"Pipeline components: {list(pipe.components.keys())}")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"❌ Error loading model: {e}")
|
| 29 |
+
# Fallback in case fp16 variant fails to download on CPU
|
| 30 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 31 |
+
"stabilityai/sd-turbo",
|
| 32 |
+
torch_dtype=torch.float32
|
| 33 |
+
)
|
| 34 |
+
pipe = pipe.to("cpu")
|
| 35 |
+
print("✅ Model loaded via fallback!")
|
| 36 |
+
|
| 37 |
+
# --------------------------------------------------------
|
| 38 |
+
# 2. Generation Logic
|
| 39 |
+
# --------------------------------------------------------
|
| 40 |
+
def generate_image(prompt, num_steps, guidance_scale, seed):
|
| 41 |
+
if not prompt or not prompt.strip():
|
| 42 |
+
raise gr.Error("⚠️ Please enter a text prompt.")
|
| 43 |
+
|
| 44 |
+
print(f"Generating: '{prompt}' (Steps: {num_steps}, Seed: {seed})")
|
| 45 |
+
|
| 46 |
+
# Manage seed reproducibility
|
| 47 |
+
generator = torch.Generator("cpu").manual_seed(int(seed))
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Generate the image
|
| 51 |
+
# SD-Turbo performs best around 1-4 steps
|
| 52 |
+
result = pipe(
|
| 53 |
+
prompt=prompt,
|
| 54 |
+
num_inference_steps=int(num_steps),
|
| 55 |
+
guidance_scale=float(guidance_scale), # Usually 0.0 for SD-Turbo
|
| 56 |
+
generator=generator
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Free up memory
|
| 60 |
+
gc.collect()
|
| 61 |
+
|
| 62 |
+
return result.images[0]
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
import traceback
|
| 66 |
+
traceback.print_exc()
|
| 67 |
+
raise gr.Error(f"❌ Error during generation: {str(e)}")
|
| 68 |
+
|
| 69 |
+
# --------------------------------------------------------
|
| 70 |
+
# 3. Custom UI Styling
|
| 71 |
+
# --------------------------------------------------------
|
| 72 |
+
custom_css = """
|
| 73 |
+
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;800&display=swap');
|
| 74 |
+
|
| 75 |
+
* {
|
| 76 |
+
font-family: 'Outfit', sans-serif !important;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.gradio-container {
|
| 80 |
+
max-width: 1000px !important;
|
| 81 |
+
margin: auto !important;
|
| 82 |
+
background: radial-gradient(circle at 50% 0%, #1e293b 0%, #0f172a 100%) !important;
|
| 83 |
+
min-height: 100vh;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.header-container {
|
| 87 |
+
text-align: center;
|
| 88 |
+
padding: 30px;
|
| 89 |
+
margin-bottom: 20px;
|
| 90 |
+
border-radius: 20px;
|
| 91 |
+
background: rgba(255, 255, 255, 0.03);
|
| 92 |
+
border: 1px solid rgba(255, 255, 255, 0.1);
|
| 93 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
|
| 94 |
+
backdrop-filter: blur(10px);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.title-text {
|
| 98 |
+
font-size: 3rem !important;
|
| 99 |
+
font-weight: 800 !important;
|
| 100 |
+
background: linear-gradient(135deg, #00f2fe 0%, #4facfe 100%) !important;
|
| 101 |
+
-webkit-background-clip: text !important;
|
| 102 |
+
-webkit-text-fill-color: transparent !important;
|
| 103 |
+
margin-bottom: 15px !important;
|
| 104 |
+
letter-spacing: -1px;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
.subtitle-text {
|
| 108 |
+
color: #94a3b8 !important;
|
| 109 |
+
font-size: 1.2rem !important;
|
| 110 |
+
font-weight: 300 !important;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
.generate-btn {
|
| 114 |
+
background: linear-gradient(135deg, #00f2fe 0%, #4facfe 100%) !important;
|
| 115 |
+
border: none !important;
|
| 116 |
+
box-shadow: 0 4px 15px rgba(79, 172, 254, 0.4) !important;
|
| 117 |
+
color: white !important;
|
| 118 |
+
font-weight: 800 !important;
|
| 119 |
+
font-size: 1.2rem !important;
|
| 120 |
+
border-radius: 12px !important;
|
| 121 |
+
transition: all 0.3s ease !important;
|
| 122 |
+
padding: 15px !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.generate-btn:hover {
|
| 126 |
+
transform: translateY(-2px) !important;
|
| 127 |
+
box-shadow: 0 8px 25px rgba(79, 172, 254, 0.6) !important;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
/* Make image display beautiful */
|
| 131 |
+
.image-output img {
|
| 132 |
+
border-radius: 12px !important;
|
| 133 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.5) !important;
|
| 134 |
+
}
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
# --------------------------------------------------------
|
| 138 |
+
# 4. Gradio Application Construction
|
| 139 |
+
# --------------------------------------------------------
|
| 140 |
+
with gr.Blocks(css=custom_css, title="🎨 Fast Text-to-Image AI", theme=gr.themes.Monochrome()) as demo:
|
| 141 |
+
|
| 142 |
+
# Header Section
|
| 143 |
+
gr.HTML("""
|
| 144 |
+
<div class="header-container">
|
| 145 |
+
<h1 class="title-text">⚡ Fast Text-to-Image AI</h1>
|
| 146 |
+
<p class="subtitle-text">Powered by SD-Turbo. Generates beautiful images on CPU in seconds.</p>
|
| 147 |
+
</div>
|
| 148 |
+
""")
|
| 149 |
+
|
| 150 |
+
with gr.Row():
|
| 151 |
+
# Left Column - Controls
|
| 152 |
+
with gr.Column(scale=1):
|
| 153 |
+
prompt = gr.Textbox(
|
| 154 |
+
label="🔮 Your Prompt",
|
| 155 |
+
placeholder="A futuristic city at sunset, highly detailed, cyberpunk style...",
|
| 156 |
+
lines=3
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 160 |
+
num_steps = gr.Slider(
|
| 161 |
+
label="Steps (Quality vs Speed)",
|
| 162 |
+
minimum=1,
|
| 163 |
+
maximum=10,
|
| 164 |
+
value=2,
|
| 165 |
+
step=1,
|
| 166 |
+
info="SD-Turbo is designed for 1-4 steps!"
|
| 167 |
+
)
|
| 168 |
+
guidance_scale = gr.Slider(
|
| 169 |
+
label="Guidance Scale",
|
| 170 |
+
minimum=0.0,
|
| 171 |
+
maximum=5.0,
|
| 172 |
+
value=0.0,
|
| 173 |
+
step=0.1,
|
| 174 |
+
info="Must be 0.0 for SD-Turbo!"
|
| 175 |
+
)
|
| 176 |
+
seed = gr.Slider(
|
| 177 |
+
label="Random Seed",
|
| 178 |
+
minimum=1,
|
| 179 |
+
maximum=999999,
|
| 180 |
+
value=1337,
|
| 181 |
+
step=1,
|
| 182 |
+
info="Change to get different images"
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
generate_btn = gr.Button("🚀 Generate Image", elem_classes=["generate-btn"])
|
| 186 |
+
|
| 187 |
+
gr.Examples(
|
| 188 |
+
examples=[
|
| 189 |
+
["A cute corgi dog in a spacesuit on Mars", 2, 0.0, 42],
|
| 190 |
+
["A hyper-realistic photograph of a juicy hamburger with melted cheese", 3, 0.0, 100],
|
| 191 |
+
["Cinematic shot of an ancient mechanical dragon sleeping in a cave", 4, 0.0, 999]
|
| 192 |
+
],
|
| 193 |
+
inputs=[prompt, num_steps, guidance_scale, seed],
|
| 194 |
+
label="💡 Try examples"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# Right Column - Output
|
| 198 |
+
with gr.Column(scale=1):
|
| 199 |
+
output_image = gr.Image(
|
| 200 |
+
label="✨ Generated Result",
|
| 201 |
+
type="pil",
|
| 202 |
+
elem_classes=["image-output"]
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
# Connect UI to logic
|
| 206 |
+
generate_btn.click(
|
| 207 |
+
fn=generate_image,
|
| 208 |
+
inputs=[prompt, num_steps, guidance_scale, seed],
|
| 209 |
+
outputs=output_image
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
# Launch app
|
| 213 |
+
demo.launch()
|