You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

images.go 3.3 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package repo
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/grampus"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/modelarts"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. type NPUImageINFO struct {
  14. ID string `json:"id"`
  15. Value string `json:"value"`
  16. }
  17. func GetPublicImages(ctx *context.APIContext) {
  18. uid := getUID(ctx)
  19. opts := models.SearchImageOptions{
  20. IncludePublicOnly: true,
  21. UID: uid,
  22. Keyword: ctx.Query("q"),
  23. Topics: ctx.Query("topic"),
  24. IncludeOfficialOnly: ctx.QueryBool("recommend"),
  25. SearchOrderBy: "type desc, num_stars desc,id desc",
  26. Status: models.IMAGE_STATUS_SUCCESS,
  27. CloudbrainType: ctx.QueryInt("cloudbrainType"),
  28. }
  29. getImages(ctx, &opts)
  30. }
  31. func GetCustomImages(ctx *context.APIContext) {
  32. uid := getUID(ctx)
  33. opts := models.SearchImageOptions{
  34. UID: uid,
  35. IncludeOwnerOnly: true,
  36. Keyword: ctx.Query("q"),
  37. Topics: ctx.Query("topic"),
  38. Status: -1,
  39. SearchOrderBy: "id desc",
  40. }
  41. getImages(ctx, &opts)
  42. }
  43. func GetStarImages(ctx *context.APIContext) {
  44. uid := getUID(ctx)
  45. opts := models.SearchImageOptions{
  46. UID: uid,
  47. IncludeStarByMe: true,
  48. Keyword: ctx.Query("q"),
  49. Topics: ctx.Query("topic"),
  50. Status: models.IMAGE_STATUS_SUCCESS,
  51. SearchOrderBy: "id desc",
  52. }
  53. getImages(ctx, &opts)
  54. }
  55. func GetNpuImages(ctx *context.APIContext) {
  56. cloudbrainType := ctx.QueryInt("type")
  57. if cloudbrainType == 0 { //modelarts
  58. getModelArtsImages(ctx)
  59. } else { //c2net
  60. getC2netNpuImages(ctx)
  61. }
  62. }
  63. func getModelArtsImages(ctx *context.APIContext) {
  64. var versionInfos modelarts.VersionInfo
  65. _ = json.Unmarshal([]byte(setting.EngineVersions), &versionInfos)
  66. var npuImageInfos []NPUImageINFO
  67. for _, info := range versionInfos.Version {
  68. npuImageInfos = append(npuImageInfos, NPUImageINFO{
  69. ID: strconv.Itoa(info.ID),
  70. Value: info.Value,
  71. })
  72. }
  73. ctx.JSON(http.StatusOK, npuImageInfos)
  74. }
  75. func getC2netNpuImages(ctx *context.APIContext) {
  76. images, err := grampus.GetImages(grampus.ProcessorTypeNPU)
  77. var npuImageInfos []NPUImageINFO
  78. if err != nil {
  79. log.Error("GetImages failed:", err.Error())
  80. ctx.JSON(http.StatusOK, []NPUImageINFO{})
  81. } else {
  82. for _, info := range images.Infos {
  83. npuImageInfos = append(npuImageInfos, NPUImageINFO{
  84. ID: info.ID,
  85. Value: info.Name,
  86. })
  87. }
  88. ctx.JSON(http.StatusOK, npuImageInfos)
  89. }
  90. }
  91. func getImages(ctx *context.APIContext, opts *models.SearchImageOptions) {
  92. page := ctx.QueryInt("page")
  93. if page <= 0 {
  94. page = 1
  95. }
  96. pageSize := ctx.QueryInt("pageSize")
  97. if pageSize <= 0 {
  98. pageSize = 15
  99. }
  100. opts.ListOptions = models.ListOptions{
  101. Page: page,
  102. PageSize: pageSize,
  103. }
  104. imageList, total, err := models.SearchImage(opts)
  105. if err != nil {
  106. log.Error("Can not get images:%v", err)
  107. ctx.JSON(http.StatusOK, models.ImagesPageResult{
  108. Count: 0,
  109. Images: []*models.Image{},
  110. })
  111. } else {
  112. ctx.JSON(http.StatusOK, models.ImagesPageResult{
  113. Count: total,
  114. Images: imageList,
  115. })
  116. }
  117. }
  118. func getUID(ctx *context.APIContext) int64 {
  119. var uid int64 = -1
  120. if ctx.IsSigned {
  121. uid = ctx.User.ID
  122. }
  123. return uid
  124. }