Skip to content

Commit

Permalink
show txt2img info,and support some default setting like cfg,height,wi…
Browse files Browse the repository at this point in the history
…dth,negative prompt
  • Loading branch information
SpenserCai committed Aug 26, 2023
1 parent ae08ca5 commit f281b7e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Date: 2023-08-17 18:23:21
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-08-23 17:58:04
* @LastEditTime: 2023-08-27 00:37:58
* @Description: file content
-->
<div align="center">
Expand Down Expand Up @@ -146,6 +146,22 @@ Edit the `config.json` file and fill in the token and other information.
}
```

If you want set default value with sd-webui
```json
{
"sd_webui":{
"servers":[...],
"default_setting": {
"cfg_scale": 8,
"negative_prompt": "bad,text,watermask",
"height":1024,
"width":1024
}
}
...
}
```

Start The Bot
```bash
# if you can't connect discord,you need use proxy and run this command:
Expand Down
12 changes: 10 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Date: 2023-08-16 11:05:40
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-08-17 13:39:42
* @LastEditTime: 2023-08-27 00:01:34
* @Description: file content
*/
package config
Expand All @@ -18,10 +18,18 @@ type ServerItem struct {

type Config struct {
SDWebUi struct {
Servers []ServerItem `json:"servers"`
Servers []ServerItem `json:"servers"`
DefaultSetting DefaultSetting `json:"default_setting"`
} `json:"sd_webui"`
Discord struct {
Token string `json:"token"`
ServerId string `json:"server_id"`
} `json:"discord"`
}

type DefaultSetting struct {
CfgScale float64 `json:"cfg_scale"`
NegativePrompt string `json:"negative_prompt"`
Height int64 `json:"height"`
Width int64 `json:"width"`
}
28 changes: 27 additions & 1 deletion dbot/slash_handler/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
* @Date: 2023-08-17 09:52:25
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-08-23 15:49:22
* @LastEditTime: 2023-08-27 00:29:05
* @Description: file content
*/
package slash_handler

import (
"encoding/json"
"fmt"
"reflect"

"github.com/SpenserCai/sd-webui-discord/global"
"github.com/SpenserCai/sd-webui-discord/utils"
"github.com/SpenserCai/sd-webui-go/intersvc"

Expand Down Expand Up @@ -80,3 +82,27 @@ func (shdl SlashHandler) GetControlNetScript(jsonStr string) (*intersvc.Controln
return script, nil

}

// Only Step 1,will be change to support every user every setting
func (shdl SlashHandler) GetSdDefaultSetting(key string, defaultValue interface{}) interface{} {
// 把global.Config.SDWebUi.DefaultSetting转成map[string]interface{}
defaultSettingMap := make(map[string]interface{})
defaultSettingJson, _ := json.Marshal(global.Config.SDWebUi.DefaultSetting)
json.Unmarshal(defaultSettingJson, &defaultSettingMap)

// 判断key是否被赋值如果没有返回defaultValue
keyValue, ok := defaultSettingMap[key]
if ok && utils.IsZeroValue(keyValue) {
return defaultValue
} else {
defaultValueType := reflect.TypeOf(defaultValue)
if defaultValueType.Kind() == reflect.Ptr {
defaultValueType = defaultValueType.Elem()
}
if keyValue != nil && reflect.TypeOf(keyValue) != defaultValueType {
convertedValue := reflect.ValueOf(keyValue).Convert(defaultValueType).Interface()
return convertedValue
}
return keyValue
}
}
14 changes: 8 additions & 6 deletions dbot/slash_handler/txt2img.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Date: 2023-08-22 17:13:19
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-08-23 15:49:31
* @LastEditTime: 2023-08-27 00:31:13
* @Description: file content
*/
package slash_handler
Expand Down Expand Up @@ -109,17 +109,18 @@ func (shdl SlashHandler) Txt2imgOptions() *discordgo.ApplicationCommand {
}

func (shdl SlashHandler) Txt2imgSetOptions(dsOpt []*discordgo.ApplicationCommandInteractionDataOption, opt *intersvc.SdapiV1Txt2imgRequest) {
opt.NegativePrompt = ""
opt.Height = func() *int64 { v := int64(512); return &v }()
opt.Width = func() *int64 { v := int64(512); return &v }()
opt.NegativePrompt = shdl.GetSdDefaultSetting("negative_prompt", "").(string)
opt.Height = func() *int64 { v := shdl.GetSdDefaultSetting("height", int64(512)).(int64); return &v }()
opt.Width = func() *int64 { v := shdl.GetSdDefaultSetting("width", int64(512)).(int64); return &v }()
opt.SamplerIndex = func() *string { v := "Euler"; return &v }()
opt.Steps = func() *int64 { v := int64(20); return &v }()
opt.CfgScale = func() *float64 { v := 7.0; return &v }()
opt.CfgScale = func() *float64 { v := shdl.GetSdDefaultSetting("cfg_scale", 7.0).(float64); return &v }()
opt.Seed = func() *int64 { v := int64(-1); return &v }()
opt.NIter = func() *int64 { v := int64(1); return &v }()
opt.Styles = []string{}
opt.ScriptArgs = []interface{}{}
opt.AlwaysonScripts = map[string]interface{}{}
opt.OverrideSettings = map[string]interface{}{}

for _, v := range dsOpt {
switch v.Name {
Expand Down Expand Up @@ -167,6 +168,7 @@ func (shdl SlashHandler) Txt2imgAction(s *discordgo.Session, i *discordgo.Intera
})
} else {
files := make([]*discordgo.File, 0)
outinfo := txt2img.GetResponse().Info
for j, v := range txt2img.GetResponse().Images {
imageReader, err := utils.GetImageReaderByBase64(v)
if err != nil {
Expand All @@ -185,7 +187,7 @@ func (shdl SlashHandler) Txt2imgAction(s *discordgo.Session, i *discordgo.Intera
files = files[0:4]
}
s.FollowupMessageEdit(i.Interaction, msg.ID, &discordgo.WebhookEdit{
Content: func() *string { v := "Success"; return &v }(),
Content: func() *string { v := fmt.Sprintf("```\n%v```\n", *outinfo); return &v }(),
Files: files,
})
}
Expand Down
14 changes: 13 additions & 1 deletion utils/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* @Date: 2023-08-17 00:40:47
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-08-23 14:52:23
* @LastEditTime: 2023-08-26 23:58:29
* @Description: file content
*/
package utils

import (
"encoding/base64"
"encoding/json"
"reflect"
"strings"

"golang.org/x/text/cases"
Expand Down Expand Up @@ -42,3 +43,14 @@ func IsJsonString(str string) bool {
var js map[string]interface{}
return json.Unmarshal([]byte(str), &js) == nil
}

func IsZeroValue(value interface{}) bool {
if value == nil {
return true
}

reflectValue := reflect.ValueOf(value)
zeroValue := reflect.Zero(reflectValue.Type()).Interface()

return reflect.DeepEqual(value, zeroValue)
}

0 comments on commit f281b7e

Please sign in to comment.