DungeonMap / app.py
WatchOutForMike's picture
D&D CSS
2927cdb
raw
history blame
4.91 kB
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
# Inference function
@spaces.GPU()
def infer(
prompt,
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)
generator = torch.Generator().manual_seed(seed)
image = pipe(
prompt=prompt,
width=width,
height=height,
num_inference_steps=num_inference_steps,
generator=generator,
guidance_scale=0.0
).images[0]
return image, seed
# Example prompts
examples = [
"A heroic adventurer wielding a flaming sword standing on a cliff overlooking a burning battlefield",
"A grand mystical library with ancient scrolls and a floating blue orb in the center of the room",
"A menacing dragon perched on a mountain peak as storm clouds gather around",
]
# Custom CSS for a Dungeons & Dragons theme
css = """
body {
background-color: #1b1b1b;
font-family: 'Cinzel', serif; /* Fantasy-style font */
color: #f5f5f5;
background-image: url('https://www.transparenttextures.com/patterns/dark-matter.png'); /* Subtle texture for a medieval touch */
}
#col-container {
margin: 0 auto;
max-width: 550px;
padding: 15px;
border: 4px solid #8b4513;
background: linear-gradient(145deg, #2e2b2a, #3a3433); /* Rustic parchment feel */
border-radius: 15px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
h1, h2 {
text-align: center;
color: #ffd700;
font-family: 'Uncial Antiqua', serif; /* Ancient script-like font */
text-shadow: 3px 3px #7c5200; /* Glow effect for magical appeal */
}
button {
background-color: #8b4513;
border: none;
color: #f0e6d2;
padding: 12px 20px;
border-radius: 8px;
cursor: pointer;
font-size: 18px;
text-shadow: 1px 1px #000;
transition: all 0.3s ease;
}
button:hover {
background-color: #a0522d;
box-shadow: 0 0 10px #ffd700;
}
"""
# 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 run button
with gr.Row():
prompt = gr.Textbox(
label="🎲 Enter Your Quest:",
lines=3,
placeholder="Describe your scene, hero, or epic landscape..."
)
run_button = gr.Button("Generate Image")
# Results
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="Number of Inference Steps",
minimum=1,
maximum=50,
step=1,
value=4,
)
# Examples
gr.Examples(
examples=examples,
inputs=[prompt],
outputs=[result],
fn=infer,
cache_examples="lazy",
)
# Interactivity
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
outputs=[result, seed]
)
# Launch the demo
demo.launch()