123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package gen
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "os"
- "time"
- "tryon/model"
- )
- const (
- API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/virtualmodel/generation/"
- DASHSCOPE_API_KEY = "sk-36a7725c51be4ffe8d3374ea534014b6"
- )
- type OutPutRes struct {
- OutPut OutPut `json:"output"`
- RequestId string `json:"request_id"`
- }
- type OutPut struct {
- TaskStatus string `json:"task_status"`
- TaskId string `json:"task_id"`
- }
- func Generate(model string, shoes []string, scale float64) (string, error) {
- fmt.Println(scale)
-
- data := map[string]interface{}{
- "model": "shoemodel-v1",
- "input": map[string]interface{}{
- "template_image_url": model,
- "shoe_image_url": shoes,
- "scale": scale,
- },
- "parameters": map[string]interface{}{
- "n": 1,
- },
- }
-
- jsonData, err := json.Marshal(data)
- if err != nil {
- return "", nil
- }
- req, err := http.NewRequest("POST", API_URL, bytes.NewBuffer(jsonData))
- if err != nil {
- return "", nil
- }
-
- req.Header.Set("X-DashScope-Async", "enable")
- req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", DASHSCOPE_API_KEY))
- req.Header.Set("Content-Type", "application/json")
-
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return "", err
- }
- bodyObj := OutPutRes{}
- err = json.Unmarshal(body, &bodyObj)
- if err != nil {
- return "", err
- }
- fmt.Println(bodyObj)
- return bodyObj.OutPut.TaskId, nil
- }
- func GetReslut(taskId string, outPut string) (string, error) {
- if len(taskId) == 0 {
- return "", errors.New("taskId is empty")
- }
-
-
- returl := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/tasks/%s", taskId)
-
- getReq, err := http.NewRequest("GET", returl, nil)
- if err != nil {
- return "", err
- }
-
- getReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", DASHSCOPE_API_KEY))
-
- client := &http.Client{}
- getResp, err := client.Do(getReq)
- if err != nil {
- return "", err
- }
- defer getResp.Body.Close()
-
- getBody, err := io.ReadAll(getResp.Body)
- if err != nil {
- return "", err
- }
- bodyObj := model.Response{}
- err = json.Unmarshal(getBody, &bodyObj)
- if err != nil {
- return "", err
- }
- fmt.Println(bodyObj)
- if bodyObj.Output.TaskStatus == "SUCCEEDED" {
-
- return bodyObj.Output.Results[0].URL, nil
- }
- return bodyObj.Output.TaskStatus, errors.New("后台处理中。。。")
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func Download(url string, outPut string) (string, error) {
-
- resp, err := http.Get(url)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- key := time.Now().Format("20060102_150405")
- outPutFile := fmt.Sprintf("%s/%s.png", outPut, key)
- out, err := os.Create(outPutFile)
- if err != nil {
- return "", err
- }
- defer out.Close()
- _, err = io.Copy(out, resp.Body)
- if err != nil {
- return "", err
- }
- return outPutFile, nil
- }
|