Spaces:
Runtime error
Runtime error
| 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 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": "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": "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 Map" | |
| # 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 | |
| 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: #f4a261; /* Updated font color */ | |
| 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; /* A deep brown border for a medieval feel */ | |
| background: linear-gradient(145deg, #2e2b2a, #3a3433); /* Subtle texture effect */ | |
| border-radius: 15px; | |
| box-shadow: 0 0 20px rgba(0, 0, 0, 0.8); | |
| } | |
| h1, h2, h3 { | |
| color: #f4a261; /* Ensure headers also use this color */ | |
| text-align: center; | |
| font-family: 'Cinzel', serif; | |
| } | |
| button, .gr-button { | |
| background-color: #3a3433; | |
| color: #f4a261; /* Matches the overall theme */ | |
| border: 2px solid #8b4513; | |
| border-radius: 10px; | |
| padding: 10px; | |
| font-family: 'Cinzel', serif; | |
| font-weight: bold; | |
| box-shadow: 0 4px 15px rgba(139, 69, 19, 0.5); | |
| } | |
| button:hover, .gr-button:hover { | |
| background-color: #8b4513; /* Deep brown hover effect */ | |
| color: #1b1b1b; | |
| } | |
| label { | |
| color: #f4a261; /* Form labels use the same themed color */ | |
| font-family: 'Cinzel', serif; | |
| } | |
| textarea, input, select { | |
| background-color: #2e2b2a; /* Matches the overall form styling */ | |
| color: #f4a261; | |
| border: 1px solid #8b4513; | |
| border-radius: 5px; | |
| font-family: 'Cinzel', serif; | |
| padding: 8px; | |
| } | |
| """ | |
| # Interface | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| # Title and Description | |
| gr.Markdown( | |
| """ | |
| # π‘οΈ ChatDnD.net 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() | |