Spaces:
Runtime error
Runtime error
File size: 17,301 Bytes
8ccf632 51c0dc0 8ccf632 81b26b5 06f0278 2927cdb 51c0dc0 8ccf632 4ea3b6f 8ccf632 2927cdb 8ccf632 06f0278 8ccf632 3f9a584 51c0dc0 eaf5b7f 51c0dc0 3f9a584 2927cdb 51c0dc0 2927cdb 3f9a584 2927cdb 54192f0 51c0dc0 2927cdb 8ccf632 3f9a584 8ccf632 2927cdb 3f9a584 2927cdb 3f9a584 2927cdb 8ccf632 2927cdb 3f9a584 2927cdb 8ccf632 2927cdb 8ccf632 2927cdb 3d0bcbc 2927cdb 3f9a584 8ccf632 2927cdb 3f9a584 2927cdb 3f9a584 8ccf632 3f9a584 2927cdb 51c0dc0 8ccf632 2927cdb 8ccf632 3f9a584 8ccf632 2927cdb 3f9a584 2927cdb 3f9a584 8ccf632 2927cdb 3f9a584 51c0dc0 2927cdb 8ccf632 2927cdb 51c0dc0 2927cdb 51c0dc0 8ccf632 2927cdb 51c0dc0 |
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
import gradio as gr
import numpy as np
import random
import spaces
import torch
from diffusers import DiffusionPipeline
# Load the model
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
# Constants
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
# Style list for prompt customization
style_list = [
{"name": "D&D Art", "prompt": "dungeons & dragons style artwork {prompt}. d&d style, key visual, vibrant, studio anime, highly detailed", "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast"},
{"name": "Dark Fantasy", "prompt": "dark and moody dungeons & dragons artwork of {prompt}. gothic ruins, shadowy figures, haunting atmospheres, grim villains, muted colors, intricate textures, sinister undertones", "negative_prompt": "bright, cheerful, cartoonish, lighthearted, futuristic, deformed"},
{"name": "Epic Battle", "prompt": "dynamic dungeons & dragons artwork of {prompt}. epic battle scene, legendary heroes, fierce monsters, intense action, dramatic lighting, high-detail environment, magical effects, vibrant colors", "negative_prompt": "peaceful, mundane, low energy, modern, sci-fi, simplistic, cartoonish, low contrast"},
# Add additional styles as needed
{ "name": "Character Portrait",
"prompt": "dungeons & dragons character portrait of {prompt}. intricate details, expressive faces, heroic poses, rich textures, elaborate costumes, iconic weapons, fantasy aesthetic, studio-quality rendering",
"negative_prompt": "generic, overly stylized, blurry, simplistic, futuristic, deformed"
},
{
"name": "Magical Realm",
"prompt": "mystical dungeons & dragons artwork of {prompt}. enchanted forests, glowing runes, floating islands, otherworldly landscapes, magical auras, vibrant fantasy colors, ultra-detailed",
"negative_prompt": "urban, dull, realistic, futuristic, lifeless, overly muted, minimalistic"
},
{
"name": "Legendary Creatures",
"prompt": "dungeons & dragons artwork of legendary creatures {prompt}. awe-inspiring dragons, ferocious beasts, mythical monsters, intricate scales, detailed textures, dynamic poses, magical auras, cinematic quality",
"negative_prompt": "simplistic, dull, mundane creatures, low-detail, modern animals, cartoony"
},
{
"name": "Ancient Ruins",
"prompt": "dungeons & dragons artwork of {prompt}. ancient temples, crumbling ruins, arcane symbols, epic scale, mystical atmosphere, grand designs, dramatic perspective, ultra-detailed",
"negative_prompt": "modern structures, low-detail, dull, untextured, non-fantasy elements, minimalistic"
},
{
"name": "Spellcasting Scene",
"prompt": "dungeons & dragons artwork of {prompt}. dramatic spellcasting, glowing magic effects, dynamic poses, swirling energy, vibrant light contrasts, powerful sorcerers, rich fantasy aesthetic",
"negative_prompt": "low energy, non-magical, mundane, modern day, generic designs, dull colors"
},
{
"name": "Tavern Life",
"prompt": "dungeons & dragons artwork of {prompt}. lively medieval tavern scene, detailed character interactions, warm firelight, wooden interiors, mugs of ale, bard performances, rich textures, inviting atmosphere",
"negative_prompt": "cold, empty, futuristic, lifeless, overly simplistic, boring"
},
{
"name": "D&D Quest Art",
"prompt": "dungeons & dragons quest artwork of {prompt}. heroic adventurers, perilous journeys, hidden treasures, legendary quests, immersive fantasy landscapes, intricate details, atmospheric lighting, grand scale, narrative-driven composition",
"negative_prompt": "modern, futuristic, low-detail, overly abstract, mundane settings, simplistic, sci-fi, cartoony, lifeless, uninspired"
},
{
"name": "D&D Anime Art",
"prompt": "dungeons & dragons anime-style artwork of {prompt}. stylized characters, vibrant colors, expressive designs, dynamic action poses, magical effects, intricate fantasy backgrounds, studio-quality anime detailing",
"negative_prompt": "photo-realistic, overly gritty, mundane, futuristic, overly dark, simplistic, deformed"
},
{
"name": "D&D Map",
"prompt": "dungeons & dragons map artwork of {prompt}. In the styel of Dungeon Tiles, Intricate fantasy map with a precise 1-inch visible grid layout adhering to D&D rules (5 feet per square), ancient cartography style, clearly labeled locations, key landmarks, navigational markers, topographic textures, detailed compass rose, parchment-like texture, vibrant rich colors, immersive design, optimized for tabletop gameplay and tactical use.",
"negative_prompt": "modern maps, bland, plain, low detail, inaccurate grids (non-5-foot alignment), futuristic, chaotic, unlabeled locations, minimalist, non-immersive, disproportionate landmarks."
},
{
"name": "Arcane Duel",
"prompt": "dungeons & dragons artwork of {prompt}. two powerful sorcerers battling with swirling magical energy, dramatic lighting, glowing spell effects, mystical atmosphere, ultra-detailed fantasy elements",
"negative_prompt": "calm, peaceful, dull colors, modern setting, non-fantasy, low-detail"
},
{
"name": "Treasure Hoard",
"prompt": "dungeons & dragons artwork of {prompt}. a cavern filled with glittering gold coins, ancient relics, glowing gemstones, treasure chests, detailed textures, vibrant fantasy lighting",
"negative_prompt": "empty, dull, modern day, futuristic, sci-fi, bland atmosphere"
},
{
"name": "Underdark",
"prompt": "dark and moody dungeons & dragons artwork of {prompt}. sprawling underground caverns, glowing fungi, shadowy creatures, bioluminescent details, ominous tones, intricate fantasy detailing",
"negative_prompt": "bright and sunny, surface world, futuristic, sci-fi, minimal detail, lifeless"
},
{
"name": "Celestial Realm",
"prompt": "dungeons & dragons artwork of {prompt}. divine celestial realms, golden light, angelic beings, floating clouds, radiant auras, epic divine architecture, detailed textures",
"negative_prompt": "dark, demonic, mundane, low-detail, futuristic, simplistic"
},
{
"name": "D&D Heist Scene",
"prompt": "dungeons & dragons artwork of {prompt}. a group of adventurers sneaking into a heavily guarded treasure vault, dramatic lighting, intricate lock mechanisms, tense atmosphere, ultra-detailed environment",
"negative_prompt": "carefree, bland setting, modern day, low-detail, simplistic designs, deformed"
},
{
"name": "Enchanted Forest",
"prompt": "dungeons & dragons artwork of {prompt}. magical forest with glowing flora, towering ancient trees, mystical wildlife, detailed textures, serene but vibrant fantasy tones",
"negative_prompt": "modern city, mundane, lifeless, futuristic, abstract designs, overly simplistic"
},
{
"name": "Battle at the Gates",
"prompt": "dungeons & dragons artwork of {prompt}. an epic battle unfolding at massive fortress gates, heavily armored soldiers, dramatic clashes, powerful war machines, intense action, atmospheric lighting",
"negative_prompt": "peaceful, bland, empty fields, low energy, futuristic, deformed, overly simplistic"
},
{
"name": "Stormy Seas",
"prompt": "dungeons & dragons artwork of {prompt}. a ship caught in a raging storm, enormous waves, dark clouds, flashes of lightning, detailed ocean textures, dynamic lighting",
"negative_prompt": "calm water, lifeless, plain scenery, futuristic, minimalist, abstract"
},
{
"name": "Desert Wasteland",
"prompt": "dungeons & dragons artwork of {prompt}. endless desert landscapes, ruins of ancient civilizations, glowing sun on the horizon, swirling sands, intricate details, mystical atmosphere",
"negative_prompt": "lush forests, futuristic, cityscapes, low-detail, lifeless"
},
{
"name": "Dwarven Forge",
"prompt": "dungeons & dragons artwork of {prompt}. a massive underground forge with molten lava streams, glowing furnaces, dwarf blacksmiths, intricate metalworks, rugged and vibrant tones",
"negative_prompt": "modern machinery, minimalistic, clean, futuristic, deformed"
},
{
"name": "Ethereal Spires",
"prompt": "dungeons & dragons artwork of {prompt}. floating crystalline towers above the clouds, glowing lights, otherworldly designs, vibrant celestial colors, ultra-detailed mystical atmosphere",
"negative_prompt": "mundane, low-detail, dark and grim, simplistic, lifeless"
},
{
"name": "Feywild Meadow",
"prompt": "dungeons & dragons artwork of {prompt}. a surreal and magical meadow filled with sparkling lights, dancing fey creatures, vibrant blooming flowers, whimsical details, fantasy colors",
"negative_prompt": "plain forest, dark and brooding, lifeless, realistic, dull tones"
},
{
"name": "Dungeon Crawl",
"prompt": "dungeons & dragons artwork of {prompt}. adventurers exploring a dark dungeon with traps, treasure chests, cobwebbed corners, torch-lit corridors, richly detailed stone textures",
"negative_prompt": "bright outdoor scenes, futuristic, modern, low detail"
},
{
"name": "Frosted Peaks",
"prompt": "dungeons & dragons artwork of {prompt}. icy mountaintops, adventurers climbing through harsh snowstorms, frozen castles, glowing icy runes, cold tones with vivid details",
"negative_prompt": "warm beaches, futuristic, cartoony, deformed, dull colors"
},
{
"name": "Cyberpunk Cityscape",
"prompt": "futuristic cyberpunk artwork of {prompt}. neon-lit streets, towering skyscrapers, flying vehicles, cybernetic details, vibrant colors, cinematic atmosphere, ultra-detailed",
"negative_prompt": "fantasy, medieval, dull colors, empty city, overly simplistic, bland"
},
{
"name": "Steampunk World",
"prompt": "steampunk-style artwork of {prompt}. intricate clockwork mechanisms, steam-powered inventions, Victorian-inspired characters, vibrant but rustic tones, rich textures, detailed environments",
"negative_prompt": "futuristic, sleek designs, dull, modern cityscapes, simplistic, minimalistic"
},
{
"name": "Wild West Scene",
"prompt": "cinematic wild west-style artwork of {prompt}. vast desert landscapes, rugged cowboys, wooden saloons, vintage rifles, atmospheric lighting, detailed textures",
"negative_prompt": "modern vehicles, futuristic elements, overly vibrant, simplistic, low energy"
},
{
"name": "Mythological Greece",
"prompt": "mythological artwork of {prompt}. ancient Greek gods and heroes, marble temples, epic battles, radiant divine light, ultra-detailed settings, rich classical tones",
"negative_prompt": "sci-fi, modern city, low-detail, lifeless atmosphere, generic textures"
},
{
"name": "Sci-Fi Space Adventure",
"prompt": "sci-fi style artwork of {prompt}. massive starships, alien planets, swirling galaxies, futuristic cities, neon highlights, ultra-detailed space themes",
"negative_prompt": "fantasy elements, medieval setting, dull or muted tones, overly simple designs"
},
{
"name": "Retro Pixel Art",
"prompt": "8-bit retro-style pixel art of {prompt}. classic video game aesthetics, vibrant and bold pixel colors, stylized backgrounds, clean and defined sprites",
"negative_prompt": "realistic textures, photorealistic, overly smooth, detailed textures"
},
{
"name": "Pop Art Aesthetic",
"prompt": "vivid pop art illustration of {prompt}. bold colors, high contrast, retro style, comic book-like details, playful and energetic compositions",
"negative_prompt": "realism, dull tones, blurred effects, abstract"
},
{
"name": "Noir Detective",
"prompt": "cinematic noir-style artwork of {prompt}. black and white aesthetics, shadowy figures, fog-filled streets, dramatic lighting, vintage details, atmospheric tension",
"negative_prompt": "colorful, futuristic, mundane, cheerful, low-detail"
},
{
"name": "Studio Ghibli-Inspired Fantasy",
"prompt": "anime-style artwork inspired by Studio Ghibli of {prompt}. magical landscapes, intricate details, vibrant colors, fantastical creatures, peaceful yet surreal atmosphere",
"negative_prompt": "dark and brooding, sci-fi, plain, minimalist, harsh contrasts"
},
{
"name": "Art Deco City",
"prompt": "art deco-inspired cityscape of {prompt}. geometric designs, golden accents, intricate and modern architecture, glowing lights, detailed and luxurious atmosphere",
"negative_prompt": "medieval settings, fantasy, low-detail, dull textures, minimalism"
},
{"name": "(No style)", "prompt": "{prompt}", "negative_prompt": ""},
]
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
STYLE_NAMES = list(styles.keys())
DEFAULT_STYLE_NAME = "D&D Art"
# Function to apply selected style
def apply_style(style_name: str, positive: str, negative: str = ""):
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
return p.replace("{prompt}", positive), n + (negative or "")
# Inference function
@spaces.GPU()
def infer(
prompt,
style,
seed=42,
randomize_seed=False,
width=1024,
height=1024,
num_inference_steps=4,
progress=gr.Progress(track_tqdm=True),
):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
# Apply style to prompt
styled_prompt, negative_prompt = apply_style(style, prompt)
generator = torch.Generator().manual_seed(seed)
image = pipe(
prompt=styled_prompt,
width=width,
height=height,
num_inference_steps=num_inference_steps,
generator=generator,
guidance_scale=0.0,
negative_prompt=negative_prompt,
).images[0]
return image, seed
# Example prompts
examples = [
["A heroic adventurer wielding a flaming sword standing on a cliff", "D&D Art"],
["A mystical library with ancient scrolls and glowing runes", "Dark Fantasy"],
["A ferocious dragon breathing fire in a dark cavern", "Epic Battle"],
]
# Custom CSS for a Dungeons & Dragons theme
css = """
body {
background-color: #1b1b1b;
font-family: 'Cinzel', serif;
color: #f5f5f5;
background-image: url('https://www.transparenttextures.com/patterns/dark-matter.png');
}
#col-container {
margin: 0 auto;
max-width: 550px;
padding: 15px;
border: 4px solid #8b4513;
background: linear-gradient(145deg, #2e2b2a, #3a3433);
border-radius: 15px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
"""
# Interface
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
# Title and Description
gr.Markdown(
"""
# 🛡️ ChatDnD.net 🛡️
# ⚔️ Dungeons & Dragons Image Generator ⚔️
**Unleash Your Imagination!** Create heroes, maps, quests, and epic scenes to bring your campaigns to life.
Tailored for adventurers seeking inspiration or Dungeon Masters constructing their next grand story. <br>
[Visit Our Website](https://chatdnd.net) | [Support Us](https://buymeacoffee.com/watchoutformike)
"""
)
# Prompt input and style selector
with gr.Row():
prompt = gr.Textbox(
label="🎲 Describe Your Vision:",
lines=3,
placeholder="Describe your hero, monster, or legendary landscape..."
)
style = gr.Dropdown(
label="🎨 Select a Style",
choices=STYLE_NAMES,
value=DEFAULT_STYLE_NAME,
)
# Run button and result display
with gr.Row():
run_button = gr.Button("Generate Image")
result = gr.Image(label="🖼️ Your Legendary Vision")
# Advanced settings
with gr.Accordion("⚙️ Advanced Settings", open=False):
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
num_inference_steps = gr.Slider(
label="Inference Steps",
minimum=1,
maximum=50,
step=1,
value=4,
)
# Examples with styles
gr.Examples(
examples=examples,
inputs=[prompt, style],
outputs=[result],
fn=infer,
cache_examples="lazy",
)
# Interactivity
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, style, seed, randomize_seed, width, height, num_inference_steps],
outputs=[result, seed],
)
# Launch the demo
demo.launch() |