hyperman12's picture
Upload 5 files
c86726f verified
Raw History Blame Contribute Delete
10.4 kB
import gradio as gr
import numpy as np
import random
import spaces
from diffusers import DiffusionPipeline
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
if torch.cuda.is_available():
torch_dtype = torch.bfloat16
else:
torch_dtype = torch.float32
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
pipe = pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024
@spaces.GPU(duration=65)
def infer(
prompt,
negative_prompt="",
seed=42,
randomize_seed=False,
width=1024,
height=1024,
guidance_scale=4.5,
num_inference_steps=40,
progress=gr.Progress(track_tqdm=True),
):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
width=width,
height=height,
generator=generator,
).images[0]
return image, seed
examples = [
"A serene mountain landscape at sunset with crystal clear lake reflections, photorealistic, 8k quality",
"Modern minimalist office space with floor-to-ceiling windows, natural lighting, architectural photography",
"Portrait of a woman in elegant evening dress, studio lighting, fashion photography, high detail",
"Futuristic city skyline at night with neon lights, cyberpunk aesthetic, ultra detailed",
"Cozy coffee shop interior with warm ambient lighting, bokeh effect, professional photography",
]
css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
* {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
#main-container {
max-width: 1400px;
margin: 0 auto;
padding: 2rem;
background: #ffffff;
}
.header-section {
text-align: center;
padding: 2rem 0;
border-bottom: 1px solid #e5e7eb;
margin-bottom: 2rem;
}
.header-section h1 {
font-size: 2.5rem;
font-weight: 700;
color: #111827;
margin-bottom: 0.5rem;
letter-spacing: -0.025em;
}
.header-section p {
font-size: 1.125rem;
color: #6b7280;
font-weight: 400;
}
.input-section {
background: #f9fafb;
padding: 2rem;
border-radius: 12px;
border: 1px solid #e5e7eb;
margin-bottom: 1.5rem;
}
.output-section {
background: #ffffff;
padding: 1.5rem;
border-radius: 12px;
border: 1px solid #e5e7eb;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
}
button.primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
border: none !important;
color: white !important;
font-weight: 600 !important;
padding: 0.75rem 2rem !important;
border-radius: 8px !important;
font-size: 1rem !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
}
button.primary:hover {
transform: translateY(-2px);
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1) !important;
}
.accordion {
background: #ffffff !important;
border: 1px solid #e5e7eb !important;
border-radius: 8px !important;
margin-top: 1rem;
}
label {
font-weight: 500 !important;
color: #374151 !important;
font-size: 0.875rem !important;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 0.5rem !important;
}
input, textarea {
border: 1px solid #d1d5db !important;
border-radius: 8px !important;
padding: 0.75rem !important;
font-size: 1rem !important;
transition: border-color 0.2s ease !important;
}
input:focus, textarea:focus {
border-color: #667eea !important;
outline: none !important;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
}
.slider {
margin: 1rem 0;
}
.examples {
margin-top: 2rem;
padding-top: 2rem;
border-top: 1px solid #e5e7eb;
}
.footer {
text-align: center;
padding: 2rem 0;
color: #9ca3af;
font-size: 0.875rem;
margin-top: 3rem;
border-top: 1px solid #e5e7eb;
}
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
}
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #555;
}
"""
with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")) as demo:
with gr.Column(elem_id="main-container"):
# Header
with gr.Row(elem_classes="header-section"):
gr.HTML("""
<div>
<h1>🎨 Stable Diffusion 3.5 Large</h1>
<p>Professional AI Image Generation - 8B Parameter Model</p>
</div>
""")
# Main content in two columns
with gr.Row():
# Left column - Input
with gr.Column(scale=1):
with gr.Group(elem_classes="input-section"):
prompt = gr.Textbox(
label="✍️ Your Prompt",
placeholder="Describe the image you want to create in detail...",
lines=4,
max_lines=8,
)
run_button = gr.Button(
"🚀 Generate Image",
variant="primary",
size="lg",
elem_classes="primary"
)
# Advanced Settings
with gr.Accordion("⚙️ Advanced Settings", open=False, elem_classes="accordion"):
negative_prompt = gr.Textbox(
label="🚫 Negative Prompt",
placeholder="What to avoid in the image...",
lines=2,
value="blurry, low quality, distorted, artifacts"
)
with gr.Row():
width = gr.Slider(
label="Width (px)",
minimum=512,
maximum=MAX_IMAGE_SIZE,
step=64,
value=1024,
)
height = gr.Slider(
label="Height (px)",
minimum=512,
maximum=MAX_IMAGE_SIZE,
step=64,
value=1024,
)
with gr.Row():
guidance_scale = gr.Slider(
label="Guidance Scale",
minimum=1.0,
maximum=15.0,
step=0.5,
value=4.5,
info="Higher = more adherence to prompt"
)
num_inference_steps = gr.Slider(
label="Quality Steps",
minimum=20,
maximum=75,
step=5,
value=40,
info="More steps = higher quality"
)
with gr.Row():
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=42,
info="For reproducible results"
)
randomize_seed = gr.Checkbox(
label="🎲 Randomize Seed",
value=True
)
# Right column - Output
with gr.Column(scale=1):
with gr.Group(elem_classes="output-section"):
result = gr.Image(
label="Generated Image",
show_label=True,
height=600,
show_download_button=True,
show_share_button=True
)
seed_output = gr.Number(
label="Used Seed",
interactive=False,
info="Save this to reproduce the exact image"
)
# Example prompts
with gr.Row(elem_classes="examples"):
gr.Examples(
examples=examples,
inputs=[prompt],
outputs=[result, seed_output],
fn=infer,
cache_examples=False,
label="💡 Example Prompts - Click to try"
)
# Footer
gr.HTML("""
<div class="footer">
<p>Powered by Stable Diffusion 3.5 Large | Built with Gradio</p>
<p style="margin-top: 0.5rem; font-size: 0.75rem;">Model by Stability AI • Interface by Gradio</p>
</div>
""")
# Event handlers
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[
prompt,
negative_prompt,
seed,
randomize_seed,
width,
height,
guidance_scale,
num_inference_steps,
],
outputs=[result, seed_output],
)
if __name__ == "__main__":
demo.launch(
share=True, # Creates a public URL
server_name="0.0.0.0",
server_port=7860,
show_error=True,
)