Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Negative Prompt Supported. #244

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
- streamlit>=0.73.1
- einops==0.3.0
- torch-fidelity==0.3.0
- transformers==4.19.2
- transformers==4.33.2
- torchmetrics==0.6.0
- kornia==0.6
- -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
Expand Down
15 changes: 14 additions & 1 deletion optimizedSD/optimized_txt2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def load_model_from_config(ckpt, verbose=False):
type=str,
help="if specified, load prompts from this file",
)
parser.add_argument(
"--negative-prompt-file",
type=str,
help="if specified, load negative prompts from this file",
)
parser.add_argument(
"--seed",
type=int,
Expand Down Expand Up @@ -235,6 +240,7 @@ def load_model_from_config(ckpt, verbose=False):

batch_size = opt.n_samples
n_rows = opt.n_rows if opt.n_rows > 0 else batch_size

if not opt.from_file:
assert opt.prompt is not None
prompt = opt.prompt
Expand All @@ -250,6 +256,13 @@ def load_model_from_config(ckpt, verbose=False):
data = batch_size * list(data)
data = list(chunk(sorted(data), batch_size))

if not opt.negative_prompt_file:
negative_prompt = ""
else:
print(f"reading negative prompts from {opt.negative_prompt_file}")
with open(opt.negative_prompt_file, "r") as f:
negative_prompt = f.read().splitlines()
negative_prompt = ' '.join(negative_prompt)

if opt.precision == "autocast" and opt.device != "cpu":
precision_scope = autocast
Expand All @@ -271,7 +284,7 @@ def load_model_from_config(ckpt, verbose=False):
modelCS.to(opt.device)
uc = None
if opt.scale != 1.0:
uc = modelCS.get_learned_conditioning(batch_size * [""])
uc = modelCS.get_learned_conditioning(batch_size * [negative_prompt])
if isinstance(prompts, tuple):
prompts = list(prompts)

Expand Down