Spaces:
Runtime error
Runtime error
Commit
·
2927cdb
1
Parent(s):
86f96a4
D&D CSS
Browse files
app.py
CHANGED
@@ -5,66 +5,117 @@ import spaces
|
|
5 |
import torch
|
6 |
from diffusers import DiffusionPipeline
|
7 |
|
|
|
8 |
dtype = torch.bfloat16
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
|
11 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
12 |
|
|
|
13 |
MAX_SEED = np.iinfo(np.int32).max
|
14 |
MAX_IMAGE_SIZE = 2048
|
15 |
|
|
|
16 |
@spaces.GPU()
|
17 |
-
def infer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if randomize_seed:
|
19 |
seed = random.randint(0, MAX_SEED)
|
20 |
generator = torch.Generator().manual_seed(seed)
|
21 |
image = pipe(
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
).images[0]
|
29 |
return image, seed
|
30 |
-
|
|
|
31 |
examples = [
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
]
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
#col-container {
|
39 |
margin: 0 auto;
|
40 |
-
max-width:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
}
|
42 |
"""
|
43 |
|
|
|
44 |
with gr.Blocks(css=css) as demo:
|
45 |
-
|
46 |
with gr.Column(elem_id="col-container"):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
with gr.Row():
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
max_lines=1,
|
58 |
-
placeholder="Enter your prompt",
|
59 |
-
container=False,
|
60 |
)
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
result = gr.Image(label="
|
65 |
|
66 |
-
|
67 |
-
|
68 |
seed = gr.Slider(
|
69 |
label="Seed",
|
70 |
minimum=0,
|
@@ -72,11 +123,9 @@ with gr.Blocks(css=css) as demo:
|
|
72 |
step=1,
|
73 |
value=0,
|
74 |
)
|
75 |
-
|
76 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
77 |
|
78 |
with gr.Row():
|
79 |
-
|
80 |
width = gr.Slider(
|
81 |
label="Width",
|
82 |
minimum=256,
|
@@ -84,7 +133,6 @@ with gr.Blocks(css=css) as demo:
|
|
84 |
step=32,
|
85 |
value=1024,
|
86 |
)
|
87 |
-
|
88 |
height = gr.Slider(
|
89 |
label="Height",
|
90 |
minimum=256,
|
@@ -93,30 +141,30 @@ with gr.Blocks(css=css) as demo:
|
|
93 |
value=1024,
|
94 |
)
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
)
|
106 |
-
|
107 |
gr.Examples(
|
108 |
-
examples
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
cache_examples="lazy"
|
113 |
)
|
114 |
|
|
|
115 |
gr.on(
|
116 |
triggers=[run_button.click, prompt.submit],
|
117 |
-
fn
|
118 |
-
inputs
|
119 |
-
outputs
|
120 |
)
|
121 |
|
122 |
-
demo
|
|
|
|
5 |
import torch
|
6 |
from diffusers import DiffusionPipeline
|
7 |
|
8 |
+
# Load the model
|
9 |
dtype = torch.bfloat16
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
11 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
12 |
|
13 |
+
# Constants
|
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,
|
24 |
+
height=1024,
|
25 |
+
num_inference_steps=4,
|
26 |
+
progress=gr.Progress(track_tqdm=True),
|
27 |
+
):
|
28 |
if randomize_seed:
|
29 |
seed = random.randint(0, MAX_SEED)
|
30 |
generator = torch.Generator().manual_seed(seed)
|
31 |
image = pipe(
|
32 |
+
prompt=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 overlooking a burning battlefield",
|
44 |
+
"A grand mystical library with ancient scrolls and a floating blue orb in the center of the room",
|
45 |
+
"A menacing dragon perched on a mountain peak as storm clouds gather around",
|
46 |
]
|
47 |
|
48 |
+
# Custom CSS for a Dungeons & Dragons theme
|
49 |
+
css = """
|
50 |
+
body {
|
51 |
+
background-color: #1b1b1b;
|
52 |
+
font-family: 'Cinzel', serif; /* Fantasy-style font */
|
53 |
+
color: #f5f5f5;
|
54 |
+
background-image: url('https://www.transparenttextures.com/patterns/dark-matter.png'); /* Subtle texture for a medieval touch */
|
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); /* Rustic parchment feel */
|
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
|
93 |
with gr.Blocks(css=css) as demo:
|
|
|
94 |
with gr.Column(elem_id="col-container"):
|
95 |
+
# Title and Description
|
96 |
+
gr.Markdown(
|
97 |
+
"""
|
98 |
+
# 🛡️ ChatDnD.net Dungeons & Dragons Image Generator ⚔️
|
99 |
+
**Unleash Your Imagination!** Create heroes, maps, quests, and epic scenes to bring your campaigns to life.
|
100 |
+
Tailored for adventurers seeking inspiration or Dungeon Masters constructing their next grand story. <br>
|
101 |
+
[Visit Our Website](https://chatdnd.net) | [Support Us](https://buymeacoffee.com/watchoutformike)
|
102 |
+
"""
|
103 |
+
)
|
104 |
+
|
105 |
+
# Prompt input and run button
|
106 |
with gr.Row():
|
107 |
+
prompt = gr.Textbox(
|
108 |
+
label="🎲 Enter Your Quest:",
|
109 |
+
lines=3,
|
110 |
+
placeholder="Describe your scene, hero, or epic landscape..."
|
|
|
|
|
|
|
111 |
)
|
112 |
+
run_button = gr.Button("Generate Image")
|
113 |
+
|
114 |
+
# Results
|
115 |
+
result = gr.Image(label="🖼️ Your Legendary Vision")
|
116 |
|
117 |
+
# Advanced settings
|
118 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
119 |
seed = gr.Slider(
|
120 |
label="Seed",
|
121 |
minimum=0,
|
|
|
123 |
step=1,
|
124 |
value=0,
|
125 |
)
|
|
|
126 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
127 |
|
128 |
with gr.Row():
|
|
|
129 |
width = gr.Slider(
|
130 |
label="Width",
|
131 |
minimum=256,
|
|
|
133 |
step=32,
|
134 |
value=1024,
|
135 |
)
|
|
|
136 |
height = gr.Slider(
|
137 |
label="Height",
|
138 |
minimum=256,
|
|
|
141 |
value=1024,
|
142 |
)
|
143 |
|
144 |
+
num_inference_steps = gr.Slider(
|
145 |
+
label="Number of Inference Steps",
|
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",
|
159 |
)
|
160 |
|
161 |
+
# Interactivity
|
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
|
170 |
+
demo.launch()
|