Skip to content

Commit

Permalink
wanx: download option
Browse files Browse the repository at this point in the history
  • Loading branch information
devinyf committed Feb 24, 2024
1 parent 1933ecf commit 0147850
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func main() {
Params: wanx.ImageSynthesisParams{
N: 1,
},
Download: true // 从 URL 下载图片
}
ctx := context.TODO()

Expand All @@ -94,6 +95,8 @@ func main() {
}

for _, blob := range imgBlobs {
// blob.Data 会在 request 中设置了 Download: true 时下载
// 否则使用 blob.ImgURL
saveImg2Desktop(blob.ImgType, blob.Data)
}
}
Expand Down
5 changes: 5 additions & 0 deletions dtypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dashscopego

const (
DashscopeTokenEnvName = "DASHSCOPE_API_KEY" //nolint:gosec
)
2 changes: 2 additions & 0 deletions tongyiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func TestImageGeneration(t *testing.T) {
Input: wanx.ImageSynthesisInput{
Prompt: "A beautiful sunset",
},
Download: true,
}

imgBlobs, err := cli.CreateImageGeneration(ctx, req)
Expand All @@ -201,6 +202,7 @@ func TestImageGeneration(t *testing.T) {

for _, blob := range imgBlobs {
assert.NotEmpty(t, blob.Data)
assert.NotEmpty(t, blob.ImgURL)
assert.Equal(t, "image/png", blob.ImgType)
}
}
Expand Down
8 changes: 5 additions & 3 deletions wanx/wanx_dtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ type ImageSynthesisInput struct {
}

type ImageSynthesisRequest struct {
Model string `json:"model"`
Input ImageSynthesisInput `json:"input"`
Params ImageSynthesisParams `json:"parameters"`
Model string `json:"model"`
Input ImageSynthesisInput `json:"input"`
Params ImageSynthesisParams `json:"parameters"`
Download bool `json:"-"`
}

type Output struct {
Expand Down Expand Up @@ -76,6 +77,7 @@ type ImageResponse struct {
type ImgBlob struct {
// types include: "image/png".
ImgType string
ImgURL string
// Raw bytes for media formats.
Data []byte
}
Expand Down
17 changes: 13 additions & 4 deletions wanx/wanxcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ func CreateImageGeneration(ctx context.Context, payload *ImageSynthesisRequest,

blobList := make([]*ImgBlob, 0, len(resp.Results))
for _, img := range resp.Results {
imgByte, err := httpcli.GetImage(ctx, img.URL, tokenOpt)
if err != nil {
return nil, err
tmpImgBlob := &ImgBlob{
ImgType: "image/png",
ImgURL: img.URL,
}

if payload.Download {
imgByte, err := httpcli.GetImage(ctx, img.URL, tokenOpt)
if err != nil {
return nil, err
}

tmpImgBlob.Data = imgByte
}

blobList = append(blobList, &ImgBlob{Data: imgByte, ImgType: "image/png"})
blobList = append(blobList, tmpImgBlob)
}

return blobList, nil
Expand Down

0 comments on commit 0147850

Please sign in to comment.