http.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package gen
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "os"
  10. "time"
  11. "tryon/model"
  12. )
  13. const (
  14. API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/virtualmodel/generation/"
  15. DASHSCOPE_API_KEY = "sk-36a7725c51be4ffe8d3374ea534014b6"
  16. )
  17. type OutPutRes struct {
  18. OutPut OutPut `json:"output"`
  19. RequestId string `json:"request_id"`
  20. }
  21. type OutPut struct {
  22. TaskStatus string `json:"task_status"`
  23. TaskId string `json:"task_id"`
  24. }
  25. func Generate(model string, shoes []string, scale float64) (string, error) {
  26. fmt.Println(scale)
  27. // 设置请求体
  28. data := map[string]interface{}{
  29. "model": "shoemodel-v1",
  30. "input": map[string]interface{}{
  31. "template_image_url": model,
  32. "shoe_image_url": shoes,
  33. "scale": scale,
  34. },
  35. "parameters": map[string]interface{}{
  36. "n": 1,
  37. },
  38. }
  39. // 将请求体编码为 JSON
  40. jsonData, err := json.Marshal(data)
  41. if err != nil {
  42. return "", nil
  43. }
  44. req, err := http.NewRequest("POST", API_URL, bytes.NewBuffer(jsonData))
  45. if err != nil {
  46. return "", nil
  47. }
  48. // 设置请求头
  49. req.Header.Set("X-DashScope-Async", "enable")
  50. req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", DASHSCOPE_API_KEY))
  51. req.Header.Set("Content-Type", "application/json")
  52. // 发起 POST 请求
  53. client := &http.Client{}
  54. resp, err := client.Do(req)
  55. if err != nil {
  56. return "", err
  57. }
  58. defer resp.Body.Close()
  59. // 读取响应
  60. body, err := io.ReadAll(resp.Body)
  61. if err != nil {
  62. return "", err
  63. }
  64. bodyObj := OutPutRes{}
  65. err = json.Unmarshal(body, &bodyObj)
  66. if err != nil {
  67. return "", err
  68. }
  69. fmt.Println(bodyObj)
  70. return bodyObj.OutPut.TaskId, nil
  71. }
  72. func GetReslut(taskId string, outPut string) (string, error) {
  73. if len(taskId) == 0 {
  74. return "", errors.New("taskId is empty")
  75. }
  76. // 获取任务 ID
  77. // taskID := "6488c66d-2a0c-4134-90bb-39ab957d6cdf"
  78. returl := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/tasks/%s", taskId)
  79. // 创建 GET 请求
  80. getReq, err := http.NewRequest("GET", returl, nil)
  81. if err != nil {
  82. return "", err
  83. }
  84. // 设置 GET 请求头
  85. getReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", DASHSCOPE_API_KEY))
  86. // 发起 GET 请求
  87. client := &http.Client{}
  88. getResp, err := client.Do(getReq)
  89. if err != nil {
  90. return "", err
  91. }
  92. defer getResp.Body.Close()
  93. // 读取响应
  94. getBody, err := io.ReadAll(getResp.Body)
  95. if err != nil {
  96. return "", err
  97. }
  98. bodyObj := model.Response{}
  99. err = json.Unmarshal(getBody, &bodyObj)
  100. if err != nil {
  101. return "", err
  102. }
  103. fmt.Printf("%#v\n", bodyObj)
  104. if bodyObj.Output.TaskStatus == "SUCCEEDED" {
  105. // 下载到本地
  106. return bodyObj.Output.Results[0].URL, nil
  107. }
  108. return bodyObj.Output.TaskStatus, errors.New("后台处理中。。。")
  109. // {
  110. // "request_id": "a052b714-29d5-92c0-b981-43dc027523db",
  111. // "output": {
  112. // "task_id": "5f6316e3-ef6c-4143-abb5-81ffefdbfcd0",
  113. // "task_status": "SUCCEEDED",
  114. // "submit_time": "2024-11-04 16:27:38.824",
  115. // "scheduled_time": "2024-11-04 16:27:38.852",
  116. // "end_time": "2024-11-04 16:27:54.092",
  117. // "results": [
  118. // {
  119. // "url": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/7d/08/20241104/18d9de26/2024-11-04/42b28ff3-8e67-44fa-805e-72cab9e204fc-1/res_img.png?Expires=1730795274&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=KlU2NsBfFKDcl9NwklzH1i2X8bk%3D"
  120. // }
  121. // ],
  122. // "task_metrics": {
  123. // "TOTAL": 1,
  124. // "SUCCEEDED": 1,
  125. // "FAILED": 0
  126. // }
  127. // },
  128. // "usage": {
  129. // "image_count": 1
  130. // }
  131. // }
  132. }
  133. func Download(url string, outPut string) (string, error) {
  134. // 创建 GET 请求
  135. resp, err := http.Get(url)
  136. if err != nil {
  137. return "", err
  138. }
  139. defer resp.Body.Close() // 确保在函数返回时关闭响应体
  140. key := time.Now().Format("20060102_150405")
  141. outPutFile := fmt.Sprintf("%s/%s.png", outPut, key)
  142. out, err := os.Create(outPutFile)
  143. if err != nil {
  144. return "", err
  145. }
  146. defer out.Close()
  147. _, err = io.Copy(out, resp.Body)
  148. if err != nil {
  149. return "", err
  150. }
  151. return outPutFile, nil
  152. }