Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,33 +8,59 @@ from diffusers import DiffusionPipeline
|
|
8 |
dtype = torch.bfloat16
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
MAX_SEED = np.iinfo(np.int32).max
|
14 |
MAX_IMAGE_SIZE = 2048
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
@spaces.GPU()
|
17 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
|
|
|
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 |
-
css="""
|
38 |
#col-container {
|
39 |
margin: 0 auto;
|
40 |
max-width: 520px;
|
@@ -42,29 +68,28 @@ css="""
|
|
42 |
"""
|
43 |
|
44 |
with gr.Blocks(css=css) as demo:
|
45 |
-
|
46 |
with gr.Column(elem_id="col-container"):
|
47 |
-
gr.Markdown(
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
""")
|
51 |
|
52 |
with gr.Row():
|
53 |
-
|
54 |
prompt = gr.Text(
|
55 |
-
label="
|
56 |
show_label=False,
|
57 |
max_lines=1,
|
58 |
-
placeholder="
|
59 |
container=False,
|
60 |
)
|
61 |
-
|
62 |
-
run_button = gr.Button("Run", scale=0)
|
63 |
|
64 |
-
result = gr.Image(label="
|
65 |
|
66 |
with gr.Accordion("Advanced Settings", open=False):
|
67 |
-
|
68 |
seed = gr.Slider(
|
69 |
label="Seed",
|
70 |
minimum=0,
|
@@ -72,11 +97,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 +107,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,
|
@@ -94,8 +116,6 @@ with gr.Blocks(css=css) as demo:
|
|
94 |
)
|
95 |
|
96 |
with gr.Row():
|
97 |
-
|
98 |
-
|
99 |
num_inference_steps = gr.Slider(
|
100 |
label="Number of inference steps",
|
101 |
minimum=1,
|
@@ -105,18 +125,18 @@ with gr.Blocks(css=css) as demo:
|
|
105 |
)
|
106 |
|
107 |
gr.Examples(
|
108 |
-
examples
|
109 |
-
fn
|
110 |
-
inputs
|
111 |
-
outputs
|
112 |
cache_examples="lazy"
|
113 |
)
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
|
122 |
demo.launch()
|
|
|
8 |
dtype = torch.bfloat16
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
+
# Initialize the model
|
12 |
+
pipe = DiffusionPipeline.from_pretrained(
|
13 |
+
"black-forest-labs/FLUX.1-schnell",
|
14 |
+
torch_dtype=dtype
|
15 |
+
).to(device)
|
16 |
|
17 |
MAX_SEED = np.iinfo(np.int32).max
|
18 |
MAX_IMAGE_SIZE = 2048
|
19 |
|
20 |
+
# Pattern-specific prompt engineering
|
21 |
+
def enhance_prompt_for_pattern(prompt):
|
22 |
+
"""Add specific terms to ensure seamless, tileable patterns."""
|
23 |
+
pattern_terms = [
|
24 |
+
"seamless pattern",
|
25 |
+
"tileable textile design",
|
26 |
+
"repeating pattern",
|
27 |
+
"high-quality fabric design",
|
28 |
+
"continuous pattern",
|
29 |
+
]
|
30 |
+
enhanced_prompt = f"{prompt}, {random.choice(pattern_terms)}, suitable for textile printing, high-quality fabric design, seamless edges"
|
31 |
+
return enhanced_prompt
|
32 |
+
|
33 |
@spaces.GPU()
|
34 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
|
35 |
+
num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
+
|
39 |
+
# Enhance the prompt for pattern generation
|
40 |
+
enhanced_prompt = enhance_prompt_for_pattern(prompt)
|
41 |
+
|
42 |
generator = torch.Generator().manual_seed(seed)
|
43 |
image = pipe(
|
44 |
+
prompt=enhanced_prompt,
|
45 |
+
width=width,
|
46 |
+
height=height,
|
47 |
+
num_inference_steps=num_inference_steps,
|
48 |
+
generator=generator,
|
49 |
+
guidance_scale=0.0
|
50 |
+
).images[0]
|
51 |
+
|
52 |
return image, seed
|
53 |
+
|
54 |
+
# Example prompts specifically for pattern generation
|
55 |
examples = [
|
56 |
+
"geometric Art Deco shapes in gold and navy",
|
57 |
+
"delicate floral motifs with small roses and leaves",
|
58 |
+
"abstract watercolor spots in pastel colors",
|
59 |
+
"traditional paisley design in earth tones",
|
60 |
+
"modern minimalist lines and circles",
|
61 |
]
|
62 |
|
63 |
+
css = """
|
64 |
#col-container {
|
65 |
margin: 0 auto;
|
66 |
max-width: 520px;
|
|
|
68 |
"""
|
69 |
|
70 |
with gr.Blocks(css=css) as demo:
|
|
|
71 |
with gr.Column(elem_id="col-container"):
|
72 |
+
gr.Markdown("""
|
73 |
+
# Deradh's AI Pattern Master
|
74 |
+
### Create seamless, tileable patterns for high-quality textile designs
|
75 |
+
|
76 |
+
This tool specializes in generating patterns that can be used for fabric printing and textile design.
|
77 |
+
Each pattern is optimized to be seamless and repeatable.
|
78 |
""")
|
79 |
|
80 |
with gr.Row():
|
|
|
81 |
prompt = gr.Text(
|
82 |
+
label="Pattern Description",
|
83 |
show_label=False,
|
84 |
max_lines=1,
|
85 |
+
placeholder="Describe your desired pattern (e.g., 'geometric Art Deco shapes in gold and navy')",
|
86 |
container=False,
|
87 |
)
|
88 |
+
run_button = gr.Button("Generate Pattern", scale=0)
|
|
|
89 |
|
90 |
+
result = gr.Image(label="Generated Pattern", show_label=True)
|
91 |
|
92 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
93 |
seed = gr.Slider(
|
94 |
label="Seed",
|
95 |
minimum=0,
|
|
|
97 |
step=1,
|
98 |
value=0,
|
99 |
)
|
|
|
100 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
|
102 |
with gr.Row():
|
|
|
103 |
width = gr.Slider(
|
104 |
label="Width",
|
105 |
minimum=256,
|
|
|
107 |
step=32,
|
108 |
value=1024,
|
109 |
)
|
|
|
110 |
height = gr.Slider(
|
111 |
label="Height",
|
112 |
minimum=256,
|
|
|
116 |
)
|
117 |
|
118 |
with gr.Row():
|
|
|
|
|
119 |
num_inference_steps = gr.Slider(
|
120 |
label="Number of inference steps",
|
121 |
minimum=1,
|
|
|
125 |
)
|
126 |
|
127 |
gr.Examples(
|
128 |
+
examples=examples,
|
129 |
+
fn=infer,
|
130 |
+
inputs=[prompt],
|
131 |
+
outputs=[result, seed],
|
132 |
cache_examples="lazy"
|
133 |
)
|
134 |
+
|
135 |
+
gr.on(
|
136 |
+
triggers=[run_button.click, prompt.submit],
|
137 |
+
fn=infer,
|
138 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
139 |
+
outputs=[result, seed]
|
140 |
+
)
|
141 |
|
142 |
demo.launch()
|