Skip to content

Commit

Permalink
Pipelining
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Mar 3, 2024
1 parent c6a640c commit f9d3496
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 190 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
main
last.txt
y
newprompts
gitcoin-core.txt
database.tar.gz
save.txt
Expand Down
23 changes: 3 additions & 20 deletions chopper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,15 @@ package main

import (
"fmt"
"strings"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)

var ErrInvalidAddress = fmt.Errorf("not a valid address")
var ErrInvalidSeed = fmt.Errorf("invalid seed")

func (a *App) Chopper(input string) (string, map[string]string, error) {
if !base.IsValidAddress(input) {
return "", map[string]string{}, ErrInvalidAddress
}

if strings.HasSuffix(input, ".eth") {
var ok bool
if input, ok = a.conn.GetEnsAddress(input); !ok {
return "", map[string]string{}, ErrInvalidAddress
}
}

hash := hexutil.Encode(crypto.Keccak256([]byte(input)))
seed := hash[2:] + input[2:]
if len(seed) < 104 {
return "", map[string]string{}, ErrInvalidSeed
_, seed, err := a.SeedBuilder(input)
if err != nil {
return "", map[string]string{}, err
}

keys := []string{"adverb", "adjective", "emotionshort", "emotion", "literary", "noun", "style", "style2", "color1", "color2", "color3", "variant1", "variant2", "variant3", "background", "orientation"}
Expand Down
241 changes: 121 additions & 120 deletions dalle.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (a *App) GetDalledress(ensOrAddr string) (Dalledress, error) {
return Dalledress{}, fmt.Errorf("adverbs not loaded")
}

addr, _ := a.conn.GetEnsAddress(ensOrAddr)
// addr, _ := a.conn.GetEnsAddress(ensOrAddr)
addr := ensOrAddr
// if base.HexToAddress(addr) == base.ZeroAddr || !base.IsValidAddress(addr) {
// return Dalledress{}, fmt.Errorf("ENS not registered: %s", ensOrAddr)
// }
Expand Down Expand Up @@ -208,8 +209,8 @@ func (a *App) GetDalledress(ensOrAddr string) (Dalledress, error) {
Variant1: Attribute{Seed: seed[56:68]},
Variant2: Attribute{Seed: seed[44:56]},
Variant3: Attribute{Seed: seed[32:44]},
Background: Attribute{Seed: hash[20:32]},
Orientation: Attribute{Seed: hash[8:20]},
Background: Attribute{Seed: seed[20:32]},
Orientation: Attribute{Seed: seed[8:20]},
}

lengths := []int{
Expand Down Expand Up @@ -249,8 +250,8 @@ func (a *App) GetDalledress(ensOrAddr string) (Dalledress, error) {
dd.Style.Val = a.styles[dd.Style.Num]
dd.ShortStyle = a.shortStyles[dd.Style.Num]
dd.Style2.Val = a.styles[dd.Style2.Num]
dd.Color1.Val = a.colors[dd.Color1.Num] // "#" + muteColor(dd.Color1.Seed[:8], dd.Color1.Seed[2:2])
dd.Color2.Val = a.colors[dd.Color1.Num] // "#" + muteColor(dd.Color2.Seed[:8], dd.Color2.Seed[3:3])
dd.Color1.Val = a.colors[dd.Color1.Num]
dd.Color2.Val = a.colors[dd.Color2.Num]
dd.Color3.Val = "#" + muteColor(dd.Color3.Seed[:8], dd.Color3.Seed[4:4])
dd.Variant1.Val = clip(a.styles[dd.Variant1.Num])
dd.Variant2.Val = clip(a.styles[dd.Variant2.Num])
Expand Down Expand Up @@ -358,143 +359,143 @@ var reserved = make(map[string]bool)

func (a *App) GetImage(ensOrAddr string) {
// logger.Info("Generating image for", ensOrAddr)
if addr, _ := a.conn.GetEnsAddress(ensOrAddr); len(addr) < 42 { // base.HexToAddress(addr) == base.ZeroAddr || !base.IsValidAddress(addr) {
logger.Error(fmt.Errorf("ENS not registered: %s", ensOrAddr))
return
} else {
folder := "./generated/"
file.EstablishFolder(folder)
file.EstablishFolder(strings.Replace(folder, "/generated", "/txt-generated", -1))
file.EstablishFolder(strings.Replace(folder, "/generated", "/annotated", -1))
file.EstablishFolder(strings.Replace(folder, "/generated", "/stitched", -1))
cnt := 0
fn := ""
for {
fn = filepath.Join(folder, fmt.Sprintf("%s-%05d.png", addr, cnt))
fM.Lock()
if !file.FileExists(fn) && !reserved[fn] {
reserved[fn] = true
fM.Unlock()
break
}
addr := ensOrAddr
// if addr, _ := a.conn.GetEnsAddress(ensOrAddr); len(addr) < 42 { // base.HexToAddress(addr) == base.ZeroAddr || !base.IsValidAddress(addr) {
// logger.Error(fmt.Errorf("ENS not registered: %s", ensOrAddr))
// return
// } else {
folder := "./generated/"
file.EstablishFolder(folder)
file.EstablishFolder(strings.Replace(folder, "/generated", "/txt-generated", -1))
file.EstablishFolder(strings.Replace(folder, "/generated", "/annotated", -1))
file.EstablishFolder(strings.Replace(folder, "/generated", "/stitched", -1))
cnt := 0
fn := ""
for {
fn = filepath.Join(folder, fmt.Sprintf("%s-%05d.png", addr, cnt))
fM.Lock()
if !file.FileExists(fn) && !reserved[fn] {
reserved[fn] = true
fM.Unlock()
cnt++
break
}
// msg := fmt.Sprintf("%s,%s,%s,image\n", utils.FormattedDate(time.Now().Unix()), addr, strings.ToLower(ensOrAddr))
// file.AppendToAsciiFile("dalledress.csv", msg)
if file.FileExists(fn) {
utils.System("open " + fn)
return
}

logger.Info(colors.Cyan, addr, colors.Yellow, "- improving the prompt...", colors.Off)
fM.Unlock()
cnt++
}
// msg := fmt.Sprintf("%s,%s,%s,image\n", utils.FormattedDate(time.Now().Unix()), addr, strings.ToLower(ensOrAddr))
// file.AppendToAsciiFile("dalledress.csv", msg)
if file.FileExists(fn) {
utils.System("open " + fn)
return
}

prompt := a.GetImprovedPrompt(ensOrAddr)
size := "1024x1024"
if strings.Contains(prompt, "horizontal") {
size = "1792x1024"
} else if strings.Contains(prompt, "vertical") {
size = "1024x1792"
}
logger.Info(colors.Cyan, addr, colors.Yellow, "- improving the prompt...", colors.Off)

quality := "standard"
if os.Getenv("DALLE_QUALITY") != "" {
quality = os.Getenv("DALLE_QUALITY")
}
prompt := a.GetImprovedPrompt(ensOrAddr)
size := "1024x1024"
if strings.Contains(prompt, "horizontal") {
size = "1792x1024"
} else if strings.Contains(prompt, "vertical") {
size = "1024x1792"
}

url := "https://api.openai.com/v1/images/generations"
payload := DalleRequest{
Prompt: prompt,
N: 1,
Quality: quality,
Style: "vivid",
Model: "dall-e-3",
Size: size,
}
quality := "standard"
if os.Getenv("DALLE_QUALITY") != "" {
quality = os.Getenv("DALLE_QUALITY")
}

payloadBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
url := "https://api.openai.com/v1/images/generations"
payload := DalleRequest{
Prompt: prompt,
N: 1,
Quality: quality,
Style: "vivid",
Model: "dall-e-3",
Size: size,
}

apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatal("No OPENAI_API_KEY key found")
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}

logger.Info(colors.Cyan, addr, colors.Yellow, "- generating the image...", colors.Off)
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatal("No OPENAI_API_KEY key found")
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
logger.Info(colors.Cyan, addr, colors.Yellow, "- generating the image...", colors.Off)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)

body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// logger.Info("DalleResponse: ", string(body))
var dalleResp DalleResponse
err = json.Unmarshal(body, &dalleResp)
if err != nil {
panic(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}

if resp.StatusCode != 200 {
fmt.Println("Error:", resp.Status, resp.StatusCode, string(body))
return
}
// logger.Info("DalleResponse: ", string(body))
var dalleResp DalleResponse
err = json.Unmarshal(body, &dalleResp)
if err != nil {
panic(err)
}

if len(dalleResp.Data) == 0 {
fmt.Println("No images returned")
return
}
if resp.StatusCode != 200 {
fmt.Println("Error:", resp.Status, resp.StatusCode, string(body))
return
}

imageURL := dalleResp.Data[0].Url
if len(dalleResp.Data) == 0 {
fmt.Println("No images returned")
return
}

// Download the image
imageResp, err := http.Get(imageURL)
if err != nil {
panic(err)
}
defer imageResp.Body.Close()
imageURL := dalleResp.Data[0].Url

txtFn := strings.Replace(strings.Replace(fn, ".png", ".txt", -1), "generated", "txt-generated", -1)
file.StringToAsciiFile(txtFn, prompt)
// utils.System("open " + txtFn)
// Download the image
imageResp, err := http.Get(imageURL)
if err != nil {
panic(err)
}
defer imageResp.Body.Close()

os.Remove(fn)
file, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
logger.Error("Failed to open output file: ", fn)
panic(err)
}
defer file.Close()
txtFn := strings.Replace(strings.Replace(fn, ".png", ".txt", -1), "generated", "txt-generated", -1)
file.StringToAsciiFile(txtFn, prompt)
// utils.System("open " + txtFn)

_, err = io.Copy(file, imageResp.Body)
if err != nil {
panic(err)
}
os.Remove(fn)
file, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
logger.Error("Failed to open output file: ", fn)
panic(err)
}
defer file.Close()

path, err := annotate(fn, "bottom", 0.2)
if err != nil {
fmt.Println("Error annotating image:", err)
return
}
logger.Info(colors.Cyan, addr, colors.Green, "- image saved as", colors.White+strings.Trim(path, " "), colors.Off)
utils.System("open " + path)
_, err = io.Copy(file, imageResp.Body)
if err != nil {
panic(err)
}

path, err := annotate(fn, "bottom", 0.2)
if err != nil {
fmt.Println("Error annotating image:", err)
return
}
logger.Info(colors.Cyan, addr, colors.Green, "- image saved as", colors.White+strings.Trim(path, " "), colors.Off)
utils.System("open " + path)
// }
}

func (a *App) GetModeration(ensOrAddr string) string {
Expand Down
30 changes: 15 additions & 15 deletions images.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ func loadImageAsBase64(path string) (string, error) {
}

func (a *App) GetImageData(ensOrAddr string) string {
if addr, _ := a.conn.GetEnsAddress(ensOrAddr); len(addr) < 42 { // base.HexToAddress(addr) == base.ZeroAddr || !base.IsValidAddress(addr) {
logger.Error(fmt.Errorf("ENS not registered: %s", ensOrAddr))
return ""
} else {
folder := "./generated/"
fn := filepath.Join(folder, fmt.Sprintf("%s.png", addr))
if file.FileExists(fn) {

base64Image, err := loadImageAsBase64("path/to/your/image.png")
if err != nil {
return ""
}
return "data:image/png;base64," + base64Image
} else {
logger.Fatal(fn + " not found")
addr := ensOrAddr
// if addr, _ := a.conn.GetEnsAddress(ensOrAddr); len(addr) < 42 { // base.HexToAddress(addr) == base.ZeroAddr || !base.IsValidAddress(addr) {
// logger.Error(fmt.Errorf("ENS not registered: %s", ensOrAddr))
// return ""
// } else {
folder := "./generated/"
fn := filepath.Join(folder, fmt.Sprintf("%s.png", addr))
if file.FileExists(fn) {
base64Image, err := loadImageAsBase64("path/to/your/image.png")
if err != nil {
return ""
}
return "data:image/png;base64," + base64Image
} else {
logger.Fatal(fn + " not found")
}
// }
return ""
}
Loading

0 comments on commit f9d3496

Please sign in to comment.