Spaces:
Runtime error
Runtime error
Commit
·
3f9a584
1
Parent(s):
2927cdb
D&D CSS
Browse files
app.py
CHANGED
@@ -14,10 +14,29 @@ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", tor
|
|
14 |
MAX_SEED = np.iinfo(np.int32).max
|
15 |
MAX_IMAGE_SIZE = 2048
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Inference function
|
18 |
@spaces.GPU()
|
19 |
def infer(
|
20 |
prompt,
|
|
|
21 |
seed=42,
|
22 |
randomize_seed=False,
|
23 |
width=1024,
|
@@ -27,66 +46,45 @@ def infer(
|
|
27 |
):
|
28 |
if randomize_seed:
|
29 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
30 |
generator = torch.Generator().manual_seed(seed)
|
31 |
image = pipe(
|
32 |
-
prompt=
|
33 |
width=width,
|
34 |
height=height,
|
35 |
num_inference_steps=num_inference_steps,
|
36 |
generator=generator,
|
37 |
-
guidance_scale=0.0
|
|
|
38 |
).images[0]
|
39 |
return image, seed
|
40 |
|
41 |
# Example prompts
|
42 |
examples = [
|
43 |
-
"A heroic adventurer wielding a flaming sword standing on a cliff
|
44 |
-
"A
|
45 |
-
"A
|
46 |
]
|
47 |
|
48 |
# Custom CSS for a Dungeons & Dragons theme
|
49 |
css = """
|
50 |
body {
|
51 |
background-color: #1b1b1b;
|
52 |
-
font-family: 'Cinzel', serif;
|
53 |
color: #f5f5f5;
|
54 |
-
background-image: url('https://www.transparenttextures.com/patterns/dark-matter.png');
|
55 |
}
|
56 |
-
|
57 |
#col-container {
|
58 |
margin: 0 auto;
|
59 |
max-width: 550px;
|
60 |
padding: 15px;
|
61 |
border: 4px solid #8b4513;
|
62 |
-
background: linear-gradient(145deg, #2e2b2a, #3a3433);
|
63 |
border-radius: 15px;
|
64 |
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
|
65 |
}
|
66 |
-
|
67 |
-
h1, h2 {
|
68 |
-
text-align: center;
|
69 |
-
color: #ffd700;
|
70 |
-
font-family: 'Uncial Antiqua', serif; /* Ancient script-like font */
|
71 |
-
text-shadow: 3px 3px #7c5200; /* Glow effect for magical appeal */
|
72 |
-
}
|
73 |
-
|
74 |
-
button {
|
75 |
-
background-color: #8b4513;
|
76 |
-
border: none;
|
77 |
-
color: #f0e6d2;
|
78 |
-
padding: 12px 20px;
|
79 |
-
border-radius: 8px;
|
80 |
-
cursor: pointer;
|
81 |
-
font-size: 18px;
|
82 |
-
text-shadow: 1px 1px #000;
|
83 |
-
transition: all 0.3s ease;
|
84 |
-
}
|
85 |
-
|
86 |
-
button:hover {
|
87 |
-
background-color: #a0522d;
|
88 |
-
box-shadow: 0 0 10px #ffd700;
|
89 |
-
}
|
90 |
"""
|
91 |
|
92 |
# Interface
|
@@ -102,16 +100,22 @@ with gr.Blocks(css=css) as demo:
|
|
102 |
"""
|
103 |
)
|
104 |
|
105 |
-
# Prompt input and
|
106 |
with gr.Row():
|
107 |
prompt = gr.Textbox(
|
108 |
-
label="🎲
|
109 |
lines=3,
|
110 |
-
placeholder="Describe your
|
|
|
|
|
|
|
|
|
|
|
111 |
)
|
|
|
|
|
|
|
112 |
run_button = gr.Button("Generate Image")
|
113 |
-
|
114 |
-
# Results
|
115 |
result = gr.Image(label="🖼️ Your Legendary Vision")
|
116 |
|
117 |
# Advanced settings
|
@@ -123,7 +127,7 @@ with gr.Blocks(css=css) as demo:
|
|
123 |
step=1,
|
124 |
value=0,
|
125 |
)
|
126 |
-
randomize_seed = gr.Checkbox(label="Randomize
|
127 |
|
128 |
with gr.Row():
|
129 |
width = gr.Slider(
|
@@ -142,17 +146,17 @@ with gr.Blocks(css=css) as demo:
|
|
142 |
)
|
143 |
|
144 |
num_inference_steps = gr.Slider(
|
145 |
-
label="
|
146 |
minimum=1,
|
147 |
maximum=50,
|
148 |
step=1,
|
149 |
value=4,
|
150 |
)
|
151 |
|
152 |
-
# Examples
|
153 |
gr.Examples(
|
154 |
examples=examples,
|
155 |
-
inputs=[prompt],
|
156 |
outputs=[result],
|
157 |
fn=infer,
|
158 |
cache_examples="lazy",
|
@@ -162,8 +166,8 @@ with gr.Blocks(css=css) as demo:
|
|
162 |
gr.on(
|
163 |
triggers=[run_button.click, prompt.submit],
|
164 |
fn=infer,
|
165 |
-
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
166 |
-
outputs=[result, seed]
|
167 |
)
|
168 |
|
169 |
# Launch the demo
|
|
|
14 |
MAX_SEED = np.iinfo(np.int32).max
|
15 |
MAX_IMAGE_SIZE = 2048
|
16 |
|
17 |
+
# Style list for prompt customization
|
18 |
+
style_list = [
|
19 |
+
{"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"},
|
20 |
+
{"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"},
|
21 |
+
{"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"},
|
22 |
+
# Add additional styles as needed
|
23 |
+
{"name": "(No style)", "prompt": "{prompt}", "negative_prompt": ""},
|
24 |
+
]
|
25 |
+
|
26 |
+
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
27 |
+
STYLE_NAMES = list(styles.keys())
|
28 |
+
DEFAULT_STYLE_NAME = "D&D Art"
|
29 |
+
|
30 |
+
# Function to apply selected style
|
31 |
+
def apply_style(style_name: str, positive: str, negative: str = ""):
|
32 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
33 |
+
return p.replace("{prompt}", positive), n + (negative or "")
|
34 |
+
|
35 |
# Inference function
|
36 |
@spaces.GPU()
|
37 |
def infer(
|
38 |
prompt,
|
39 |
+
style,
|
40 |
seed=42,
|
41 |
randomize_seed=False,
|
42 |
width=1024,
|
|
|
46 |
):
|
47 |
if randomize_seed:
|
48 |
seed = random.randint(0, MAX_SEED)
|
49 |
+
|
50 |
+
# Apply style to prompt
|
51 |
+
styled_prompt, negative_prompt = apply_style(style, prompt)
|
52 |
generator = torch.Generator().manual_seed(seed)
|
53 |
image = pipe(
|
54 |
+
prompt=styled_prompt,
|
55 |
width=width,
|
56 |
height=height,
|
57 |
num_inference_steps=num_inference_steps,
|
58 |
generator=generator,
|
59 |
+
guidance_scale=0.0,
|
60 |
+
negative_prompt=negative_prompt,
|
61 |
).images[0]
|
62 |
return image, seed
|
63 |
|
64 |
# Example prompts
|
65 |
examples = [
|
66 |
+
["A heroic adventurer wielding a flaming sword standing on a cliff", "D&D Art"],
|
67 |
+
["A mystical library with ancient scrolls and glowing runes", "Dark Fantasy"],
|
68 |
+
["A ferocious dragon breathing fire in a dark cavern", "Epic Battle"],
|
69 |
]
|
70 |
|
71 |
# Custom CSS for a Dungeons & Dragons theme
|
72 |
css = """
|
73 |
body {
|
74 |
background-color: #1b1b1b;
|
75 |
+
font-family: 'Cinzel', serif;
|
76 |
color: #f5f5f5;
|
77 |
+
background-image: url('https://www.transparenttextures.com/patterns/dark-matter.png');
|
78 |
}
|
|
|
79 |
#col-container {
|
80 |
margin: 0 auto;
|
81 |
max-width: 550px;
|
82 |
padding: 15px;
|
83 |
border: 4px solid #8b4513;
|
84 |
+
background: linear-gradient(145deg, #2e2b2a, #3a3433);
|
85 |
border-radius: 15px;
|
86 |
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
|
87 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
"""
|
89 |
|
90 |
# Interface
|
|
|
100 |
"""
|
101 |
)
|
102 |
|
103 |
+
# Prompt input and style selector
|
104 |
with gr.Row():
|
105 |
prompt = gr.Textbox(
|
106 |
+
label="🎲 Describe Your Vision:",
|
107 |
lines=3,
|
108 |
+
placeholder="Describe your hero, monster, or legendary landscape..."
|
109 |
+
)
|
110 |
+
style = gr.Dropdown(
|
111 |
+
label="🎨 Select a Style",
|
112 |
+
choices=STYLE_NAMES,
|
113 |
+
value=DEFAULT_STYLE_NAME,
|
114 |
)
|
115 |
+
|
116 |
+
# Run button and result display
|
117 |
+
with gr.Row():
|
118 |
run_button = gr.Button("Generate Image")
|
|
|
|
|
119 |
result = gr.Image(label="🖼️ Your Legendary Vision")
|
120 |
|
121 |
# Advanced settings
|
|
|
127 |
step=1,
|
128 |
value=0,
|
129 |
)
|
130 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
131 |
|
132 |
with gr.Row():
|
133 |
width = gr.Slider(
|
|
|
146 |
)
|
147 |
|
148 |
num_inference_steps = gr.Slider(
|
149 |
+
label="Inference Steps",
|
150 |
minimum=1,
|
151 |
maximum=50,
|
152 |
step=1,
|
153 |
value=4,
|
154 |
)
|
155 |
|
156 |
+
# Examples with styles
|
157 |
gr.Examples(
|
158 |
examples=examples,
|
159 |
+
inputs=[prompt, style],
|
160 |
outputs=[result],
|
161 |
fn=infer,
|
162 |
cache_examples="lazy",
|
|
|
166 |
gr.on(
|
167 |
triggers=[run_button.click, prompt.submit],
|
168 |
fn=infer,
|
169 |
+
inputs=[prompt, style, seed, randomize_seed, width, height, num_inference_steps],
|
170 |
+
outputs=[result, seed],
|
171 |
)
|
172 |
|
173 |
# Launch the demo
|