-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"image" | ||
"image/jpeg" | ||
"image/png" | ||
"mime" | ||
"os" | ||
"time" | ||
|
||
"github.com/TannerKvarfordt/hfapigo" | ||
) | ||
|
||
const HuggingFaceTokenEnv = "HUGGING_FACE_TOKEN" | ||
|
||
func init() { | ||
key := os.Getenv(HuggingFaceTokenEnv) | ||
if key != "" { | ||
hfapigo.SetAPIKey(key) | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Print("Enter an image prompt: ") | ||
|
||
reader := bufio.NewReader(os.Stdin) | ||
input, err := reader.ReadString('\n') | ||
if err != nil { | ||
fmt.Print(err) | ||
return | ||
} | ||
|
||
type ChanRv struct { | ||
resp image.Image | ||
format string | ||
err error | ||
} | ||
ch := make(chan ChanRv) | ||
|
||
fmt.Print("Sending request") | ||
go func() { | ||
img, fmt, err := hfapigo.SendTextToImageRequest(hfapigo.RecommendedTextToImageModel, &hfapigo.TextToImageRequest{ | ||
Inputs: input, | ||
Options: *hfapigo.NewOptions().SetWaitForModel(true), | ||
}) | ||
ch <- ChanRv{img, fmt, err} | ||
}() | ||
|
||
for { | ||
select { | ||
default: | ||
fmt.Print(".") | ||
time.Sleep(time.Millisecond * 100) | ||
case chrv := <-ch: | ||
filename := fmt.Sprintf("output.%s", chrv.format) | ||
fout, err := os.Create(filename) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer fout.Close() | ||
|
||
mimetype := mime.TypeByExtension(fmt.Sprintf(".%s", chrv.format)) | ||
|
||
switch mimetype { | ||
case "image/jpeg": | ||
err = jpeg.Encode(fout, chrv.resp, nil) | ||
case "image/png": | ||
err = png.Encode(fout, chrv.resp) | ||
default: | ||
err = fmt.Errorf("unknown image format: %s", chrv.format) | ||
} | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
fmt.Printf("\nWrote image to %s\n", filename) | ||
} | ||
|
||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package hfapigo | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"image" | ||
|
||
_ "image/jpeg" | ||
_ "image/png" | ||
) | ||
|
||
const RecommendedTextToImageModel = "runwayml/stable-diffusion-v1-5" | ||
|
||
// Request structure for text-to-image model | ||
type TextToImageRequest struct { | ||
Inputs string `json:"inputs,omitempty"` | ||
Options Options `json:"options,omitempty"` | ||
} | ||
|
||
// Send a TextToImageRequest. If successful, returns the generated image object, format name, and nil. | ||
// If unsuccessful, returns nil, "", and an error. | ||
func SendTextToImageRequest(model string, request *TextToImageRequest) (image.Image, string, error) { | ||
if request == nil { | ||
return nil, "", errors.New("nil TextToImageRequest") | ||
} | ||
|
||
jsonBuf, err := json.Marshal(request) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
respBody, err := MakeHFAPIRequest(jsonBuf, model) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return image.Decode(bytes.NewReader(respBody)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package hfapigo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/TannerKvarfordt/hfapigo" | ||
) | ||
|
||
func TestTextToImage(t *testing.T) { | ||
{ // Test valid request | ||
img, fmt, err := hfapigo.SendTextToImageRequest(hfapigo.RecommendedTextToImageModel, &hfapigo.TextToImageRequest{ | ||
Inputs: "A dog and a cat sleeping adorably.", | ||
Options: *hfapigo.NewOptions().SetWaitForModel(true), | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if fmt == "" { | ||
t.Fatal("empty encoding returned") | ||
} | ||
if img == nil { | ||
t.Fatal("nil image returned") | ||
} | ||
} | ||
|
||
{ // Test invalid request | ||
img, fmt, err := hfapigo.SendTextToImageRequest("not-a-model", &hfapigo.TextToImageRequest{ | ||
Inputs: "A dog and a cat sleeping adorably.", | ||
Options: *hfapigo.NewOptions().SetWaitForModel(true), | ||
}) | ||
if err == nil { | ||
t.Fatal("expected an error") | ||
} | ||
if fmt != "" { | ||
t.Fatal("expected an empty encoding string") | ||
} | ||
if img != nil { | ||
t.Fatal("expected a nil image") | ||
} | ||
} | ||
} |