Spaces:
Sleeping
Sleeping
feat: add negative prompt support in Advanced Settings
Browse files
app.py
CHANGED
|
@@ -74,8 +74,16 @@ def load_pipeline():
|
|
| 74 |
return pipe
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
@spaces.GPU(duration=30)
|
| 78 |
-
def generate(cell_type, custom_prompt, cfg, steps, seed):
|
| 79 |
"""Generate a blood cell image. GPU is allocated for this function."""
|
| 80 |
import random
|
| 81 |
|
|
@@ -91,8 +99,14 @@ def generate(cell_type, custom_prompt, cfg, steps, seed):
|
|
| 91 |
|
| 92 |
generator = torch.Generator(device="cuda").manual_seed(actual_seed)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
result = pipeline(
|
| 95 |
prompt=prompt,
|
|
|
|
| 96 |
height=512,
|
| 97 |
width=512,
|
| 98 |
num_inference_steps=int(steps),
|
|
@@ -155,6 +169,21 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="red")) as demo:
|
|
| 155 |
info="Number of denoising steps. More steps = higher quality but slower generation. Recommended: 20-30."
|
| 156 |
)
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
btn = gr.Button("Generate Cell Image", variant="primary", elem_classes=["primary-btn"])
|
| 159 |
|
| 160 |
with gr.Column(scale=1):
|
|
@@ -180,7 +209,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="red")) as demo:
|
|
| 180 |
|
| 181 |
btn.click(
|
| 182 |
fn=generate,
|
| 183 |
-
inputs=[cell_dropdown, custom_box, cfg_slider, steps_slider, seed_box],
|
| 184 |
outputs=output_img
|
| 185 |
)
|
| 186 |
|
|
|
|
| 74 |
return pipe
|
| 75 |
|
| 76 |
|
| 77 |
+
# Default negative prompt to avoid common issues
|
| 78 |
+
DEFAULT_NEGATIVE_PROMPT = (
|
| 79 |
+
"white background, pale background, washed out, overexposed, "
|
| 80 |
+
"low contrast, blurry, out of focus, multiple cells, overlapping cells, "
|
| 81 |
+
"artifacts, noise, low quality, deformed"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
@spaces.GPU(duration=30)
|
| 86 |
+
def generate(cell_type, custom_prompt, cfg, steps, seed, negative_prompt, use_negative_prompt):
|
| 87 |
"""Generate a blood cell image. GPU is allocated for this function."""
|
| 88 |
import random
|
| 89 |
|
|
|
|
| 99 |
|
| 100 |
generator = torch.Generator(device="cuda").manual_seed(actual_seed)
|
| 101 |
|
| 102 |
+
# Prepare negative prompt
|
| 103 |
+
neg_prompt = None
|
| 104 |
+
if use_negative_prompt:
|
| 105 |
+
neg_prompt = negative_prompt.strip() if negative_prompt and negative_prompt.strip() else DEFAULT_NEGATIVE_PROMPT
|
| 106 |
+
|
| 107 |
result = pipeline(
|
| 108 |
prompt=prompt,
|
| 109 |
+
negative_prompt=neg_prompt,
|
| 110 |
height=512,
|
| 111 |
width=512,
|
| 112 |
num_inference_steps=int(steps),
|
|
|
|
| 169 |
info="Number of denoising steps. More steps = higher quality but slower generation. Recommended: 20-30."
|
| 170 |
)
|
| 171 |
|
| 172 |
+
gr.Markdown("---")
|
| 173 |
+
|
| 174 |
+
use_negative_checkbox = gr.Checkbox(
|
| 175 |
+
value=True,
|
| 176 |
+
label="Use Negative Prompt",
|
| 177 |
+
info="Enable to steer generation away from unwanted characteristics"
|
| 178 |
+
)
|
| 179 |
+
negative_prompt_box = gr.Textbox(
|
| 180 |
+
value=DEFAULT_NEGATIVE_PROMPT,
|
| 181 |
+
label="Negative Prompt",
|
| 182 |
+
placeholder="Describe what you DON'T want in the image...",
|
| 183 |
+
lines=2,
|
| 184 |
+
info="The model will avoid these characteristics. Helps prevent pale/washed out backgrounds and blurry images."
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
btn = gr.Button("Generate Cell Image", variant="primary", elem_classes=["primary-btn"])
|
| 188 |
|
| 189 |
with gr.Column(scale=1):
|
|
|
|
| 209 |
|
| 210 |
btn.click(
|
| 211 |
fn=generate,
|
| 212 |
+
inputs=[cell_dropdown, custom_box, cfg_slider, steps_slider, seed_box, negative_prompt_box, use_negative_checkbox],
|
| 213 |
outputs=output_img
|
| 214 |
)
|
| 215 |
|