Skip to content

Commit

Permalink
Merge pull request #41 from SmilyOrg/split-ai-hosts
Browse files Browse the repository at this point in the history
Split AI hosts support
  • Loading branch information
SmilyOrg authored Oct 11, 2022
2 parents 65e071c + e30dadd commit 747e9b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
10 changes: 10 additions & 0 deletions defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ ai:
# Host of an AI server providing machine learning features. Defining this
# will enable search functionality on collection pages.
# See https://github.com/SmilyOrg/photofield-ai on how to set it up
#
# host: http://localhost:8081
#
# You can also define the visual (image scanning) host and the
# textual search host separately, e.g. to scan images with a PC w/ GPU,
# but keep the search part on a NAS.
#
# visual:
# host: http://localhost:8081
# textual:
# host: http://localhost:8081

media:
# Extract metadata from this many files concurrently
Expand Down
28 changes: 24 additions & 4 deletions internal/clip/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,32 @@ func FromRaw(bytes []byte, invnorm uint16) Embedding {
}
}

type AI struct {
type Model struct {
Host string `json:"host"`
}

type AI struct {
Host string `json:"host"`
Visual Model `json:"visual"`
Textual Model `json:"textual"`
}

func (a AI) Available() bool {
return a.Host != ""
return a.TextualHost() != ""
}

func (a AI) VisualHost() string {
if a.Visual.Host != "" {
return a.Visual.Host
}
return a.Host
}

func (a AI) TextualHost() string {
if a.Textual.Host != "" {
return a.Textual.Host
}
return a.Host
}

func (a AI) EmbedImagePath(path string) (Embedding, error) {
Expand Down Expand Up @@ -113,7 +133,7 @@ func (a AI) EmbedImageReader(r io.Reader) (Embedding, error) {

w.Close()

url := fmt.Sprintf("%s/image-embeddings", a.Host)
url := fmt.Sprintf("%s/image-embeddings", a.VisualHost())
res, err := http.Post(url, w.FormDataContentType(), &b)
if err != nil {
return nil, err
Expand Down Expand Up @@ -166,7 +186,7 @@ func (a AI) EmbedText(text string) (Embedding, error) {
return nil, err
}

url := fmt.Sprintf("%s/text-embeddings", a.Host)
url := fmt.Sprintf("%s/text-embeddings", a.TextualHost())
res, err := http.Post(url, "application/json", bytes.NewBuffer(b))
if err != nil {
return nil, err
Expand Down

0 comments on commit 747e9b9

Please sign in to comment.