dribnet / pixray

neural image generation
Other
402 stars 53 forks source link

Gracefully handle prompts with colons but no weights #46

Closed mitchellgordon95 closed 2 years ago

mitchellgordon95 commented 2 years ago

When a prompt contains a colon (i.e. "Star Wars: Return of the Sith"), it will currently crash generation. This is a small change to prompt parsing to gracefully handle this case. Before:

>>> parse_prompt("hello world:2")
('hello world', 2.0, -inf)
>>> parse_prompt("hello world:2:15")
('hello world', 2.0, 15.0)
>>> parse_prompt("hello world: my name is Mitchell.")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Volumes/Repos/pixray/pixray.py", line 254, in parse_prompt
    return vals[0], float(vals[1]), float(vals[2])
ValueError: could not convert string to float: ' my name is Mitchell.'

After:

>> parse_prompt("hello world")
('hello world', 1, -inf)
>>> parse_prompt("hello world:2")
('hello world', 2.0, -inf)
>>> parse_prompt("hello world:2:15")
('hello world', 2.0, 15.0)
>>> parse_prompt("hello world: my name is Mitchell")
Warning, failed to parse prompt weights, assuming prompt does not contain weights.
('hello world: my name is Mitchell', 1, -inf)
dribnet commented 2 years ago

This looks solid and I'm very stoked be able to enter text prompts with colons - thanks for the thought put into this and I hope to test it soon.

dribnet commented 2 years ago

I excited with this improvement, but then also reworked it a bit subsequently so that someone didn't have to choose between having a colon in the text part of the prompt or appending a weight - but in fact could flexibly have both. Here's your handy test cases with a couple of extras that show the other cases

>>> import pixray
>>> from pixray import parse_prompt
>>> parse_prompt("hello world")
('hello world', 1, -inf)
>>> parse_prompt("hello world:2")
('hello world', 2.0, -inf)
>>> parse_prompt("hello world:2:15")
('hello world', 2.0, 15.0)
>>> parse_prompt("hello world: my name is Mitchell")
('hello world: my name is Mitchell', 1, -inf)
>>> parse_prompt("hello world: my name is Mitchell:2")
('hello world: my name is Mitchell', 2.0, -inf)
>>> parse_prompt("hello world: my name is Mitchell:2:15")
('hello world: my name is Mitchell', 2.0, 15.0)

The code for that can be seen in this commit now in the pixray/future development branch. Thanks for instigating this change - I have often been frustrated with this myself but never thought to improve the parsing to fix it!