Skip to content

Commit

Permalink
Fix incorrect multiplier format error (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgirin authored Oct 13, 2023
1 parent b88e9af commit 4f86496
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
18 changes: 12 additions & 6 deletions src/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/junte/stable-diffusion-prompt-parser/src/reader"
)

const DefaultMultiplier = 0.5

func (parser *PromptParser) escapeToken(token string) string {
return regexp.MustCompile(`\\(.)`).ReplaceAllString(token, "$1")
}
Expand Down Expand Up @@ -188,6 +190,11 @@ func (parser *PromptParser) parseNumber(reader *reader.TokenReader, name string)
}
}

switch reader.GetToken() {
case ")", ">", ":":
return DefaultMultiplier, nil
}

token, err := parser.parseContentToken(reader, name)
if err != nil {
return 0, err
Expand All @@ -203,7 +210,7 @@ func (parser *PromptParser) parseNumber(reader *reader.TokenReader, name string)

number, err = strconv.ParseFloat(token, 64)
if err != nil {
return 0, fmt.Errorf(fmt.Sprintf("Incorrect %s format: %s", strings.ToLower(name), token))
number = DefaultMultiplier
}

reader.NextToken()
Expand Down Expand Up @@ -244,8 +251,9 @@ func (parser *PromptParser) parseAnglePrompt(reader *reader.TokenReader, kind st
if reader.GetToken() == ">" {
reader.NextToken()
return &prompt{
kind: kind,
filename: filename,
kind: kind,
filename: filename,
multiplier: DefaultMultiplier,
}, nil
}
reader.NextToken()
Expand Down Expand Up @@ -335,9 +343,7 @@ func (parser *PromptParser) parse(input string) (*prompt, error) {
reader := reader.NewTokenReader(input)

for {
token := reader.GetToken()

switch token {
switch reader.GetToken() {
case ")", "]", ">", ":":
reader.NextToken()
continue
Expand Down
67 changes: 57 additions & 10 deletions src/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ func TestParseAnglePrompt(t *testing.T) {
"<lora:file>",
"lora",
prompt{
kind: "lora",
filename: "file",
kind: "lora",
filename: "file",
multiplier: 0.5,
},
},
{
Expand Down Expand Up @@ -325,6 +326,24 @@ func TestParseAnglePrompt(t *testing.T) {
multiplier: 0.5,
},
},
{
"<lora:file.name-v.1.5:.>",
"lora",
prompt{
kind: "lora",
filename: "file.name-v.1.5",
multiplier: 0.5,
},
},
{
"<lora:file.name-v.1.5:>",
"lora",
prompt{
kind: "lora",
filename: "file.name-v.1.5",
multiplier: 0.5,
},
},
{
"< lora : ?file,name[v.1] : .5 >",
"lora",
Expand Down Expand Up @@ -358,8 +377,9 @@ func TestParsePromptContent(t *testing.T) {
"<lora:file>",
true,
prompt{
kind: "lora",
filename: "file",
kind: "lora",
filename: "file",
multiplier: 0.5,
},
},
{
Expand Down Expand Up @@ -483,10 +503,37 @@ func TestParsePrompt(t *testing.T) {
},
},
},
{
"(abc: xyz)",
ParsedPrompt{
Tags: []*PromptTag{
{
Tag: "abc",
Weight: 1.1,
},
{
Tag: "xyz",
Weight: 1.1,
},
},
},
},
{
"<lora:file>",
ParsedPrompt{
Loras: []*PromptModel{{Filename: "file", Multiplier: 1}},
Loras: []*PromptModel{{Filename: "file", Multiplier: 0.5}},
},
},
{
"<lora:file:>",
ParsedPrompt{
Loras: []*PromptModel{{Filename: "file", Multiplier: 0.5}},
},
},
{
"<lora:file:.>",
ParsedPrompt{
Loras: []*PromptModel{{Filename: "file", Multiplier: 0.5}},
},
},
{
Expand All @@ -504,7 +551,7 @@ func TestParsePrompt(t *testing.T) {
{
"<hypernet:file>",
ParsedPrompt{
Hypernets: []*PromptModel{{Filename: "file", Multiplier: 1}},
Hypernets: []*PromptModel{{Filename: "file", Multiplier: 0.5}},
},
},
{
Expand Down Expand Up @@ -539,7 +586,7 @@ func TestParsePrompt(t *testing.T) {
Hypernets: []*PromptModel{
{
Filename: "file",
Multiplier: 1,
Multiplier: 0.5,
},
},
},
Expand Down Expand Up @@ -585,9 +632,9 @@ func TestPromptToString(t *testing.T) {
{"(abc) [xyz]", "(abc), [xyz]"},
{"(abc)mno[xyz]", "(abc), mno, [xyz]"},
{"< lora : file name : .5 >", "<lora:file name:.5>"},
{"< hypernet : file name>", "<hypernet:file name>"},
{"abc <lora:filename> mno xyz", "abc, <lora:filename>, mno xyz"},
{"(abc,, <lora:filename>,, <lora:filename>)", "(abc, <lora:filename>, <lora:filename>)"},
{"< hypernet : file name>", "<hypernet:file name:.5>"},
{"abc <lora:filename> mno xyz", "abc, <lora:filename:.5>, mno xyz"},
{"(abc,, <lora:filename>,, <lora:filename>)", "(abc, <lora:filename:.5>, <lora:filename:.5>)"},
}

parser := NewPromptParser()
Expand Down

0 comments on commit 4f86496

Please sign in to comment.