Skip to content

Commit

Permalink
feat: testing on main
Browse files Browse the repository at this point in the history
  • Loading branch information
djpiper28 committed Mar 15, 2024
1 parent e7399a9 commit e21dbb3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions kube_cache/aiStuff/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func (ai *KubeAi) GenerateKubeRecipe(kubeName1, kubeName2 string) (string, error
}

ai.Lock.Unlock()
sleepTime := apiRestTime - time.Since(ai.LastAccess)
sleepTime := apiRestTime - time.Since(ai.LastAccess)

log.Printf("Rate limited, going to bed for %dms...", sleepTime / time.Millisecond)
log.Printf("Rate limited, going to bed for %dms...", sleepTime/time.Millisecond)
time.Sleep(sleepTime)
}

Expand Down
15 changes: 1 addition & 14 deletions kube_cache/aiStuff/dalle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"log"
"net/http"
"os"
"time"

imgresize "github.com/CosmicKube/kube_cache/imgResize"
Expand Down Expand Up @@ -117,19 +116,7 @@ func (ai *KubeAi) GenerateDalleForKube(kubeName string) ([]byte, error) {
img, err := ai.generateDalleForKube(kubeName)
if err != nil {
ai.Metrics.IncrementDalleErrors()
log.Println("Cannot generate image falling back to default image")
defaultFile, err := os.Open("default.png")
if err != nil {
log.Printf("Error creating default file: %s", err)
return nil, err
}

img, err := io.ReadAll(defaultFile)
if err != nil {
log.Printf("Error reading default image: %s", err)
return nil, err
}

log.Printf("Error generating Dalle for kube: %s", err)
return img, nil
}
ai.Metrics.IncrementDalleRequests()
Expand Down
31 changes: 29 additions & 2 deletions kube_cache/server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package server

import (
"errors"
"io"
"log"
"os"

"github.com/CosmicKube/kube_cache/aiStuff"
"github.com/CosmicKube/kube_cache/metrics"
"github.com/CosmicKube/kube_cache/model"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)

type Server struct {
Expand Down Expand Up @@ -143,20 +146,44 @@ func (s *Server) craft(c *gin.Context, id1, id2 string) (model.KubeRecipe, error
return model.KubeRecipe{}, errors.New("Cannot generate kube recipe")
}

log.Printf("Using default image for kube %s until DALLE finishes", newKubeId)
defaultFile, err := os.Open("default.png")
if err != nil {
log.Printf("Error creating default file: %s", err)
return model.KubeRecipe{}, err
}

img, err := io.ReadAll(defaultFile)
if err != nil {
log.Printf("Error reading default image: %s", err)
return model.KubeRecipe{}, err
}

err = s.Database.SetKubeRecipe(kube1, kube2, newKubeId, img)
if err != nil {
log.Printf("Cannot save kube recipe: %s", err)
}

recipe, err = s.Database.GetKubeRecipe(id1, id2)
if err != nil {
log.Printf("Cannot get kube recipe: %s", err)
}

log.Printf("Generated new kube: %s, generating image in new thread", newKubeId)
go func() {
image, err := s.Ai.GenerateDalleForKube(newKubeId)
if err != nil {
log.Printf("Error generating Dalle for kube: %s", err)
}

err = s.Database.SetKubeRecipe(kube1, kube2, newKubeId, image)
id, err := uuid.Parse(newKubeId)
if err != nil {
log.Printf("Error parsing new kube id: %s", err)
}

err = s.Database.SetKubeImage(model.Kube{Id: id}, image)
if err != nil {
log.Printf("Cannot save kube recipe: %s", err)
log.Printf("Cannot save kube image: %s", err)
}
}()

Expand Down

0 comments on commit e21dbb3

Please sign in to comment.