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.

ai_model_manage.go 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package models
  2. import (
  3. "fmt"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "code.gitea.io/gitea/modules/timeutil"
  7. "xorm.io/builder"
  8. "xorm.io/xorm"
  9. )
  10. type AiModelManage struct {
  11. ID string `xorm:"pk"`
  12. Name string `xorm:"NOT NULL"`
  13. Version string `xorm:"NOT NULL"`
  14. Parent string `xorm:"NOT NULL"`
  15. Type int `xorm:"NOT NULL"`
  16. Size int64 `xorm:"NOT NULL"`
  17. Description string `xorm:"varchar(2000)"`
  18. Label string `xorm:"varchar(1000)"`
  19. Path string `xorm:"varchar(400) NOT NULL"`
  20. ConfigJson string `xorm:"text"`
  21. DownloadCount int `xorm:"NOT NULL DEFAULT 0"`
  22. Engine int `xorm:"NOT NULL DEFAULT 0"`
  23. Status int `xorm:"NOT NULL DEFAULT 0"`
  24. Accuracy string `xorm:"varchar(1000)"`
  25. AttachmentId string `xorm:"NULL"`
  26. RepoId int64 `xorm:"NULL"`
  27. CodeBranch string `xorm:"varchar(400) NULL"`
  28. CodeCommitID string `xorm:"NULL"`
  29. UserId int64 `xorm:"NOT NULL"`
  30. TrainTaskInfo string `xorm:"text NULL"`
  31. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  32. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  33. }
  34. type AiModelQueryOptions struct {
  35. ListOptions
  36. RepoID int64 // include all repos if empty
  37. UserID int64
  38. ModelID string
  39. SortType string
  40. // JobStatus CloudbrainStatus
  41. Type int
  42. }
  43. func SaveModelToDb(model *AiModelManage) error {
  44. sess := x.NewSession()
  45. defer sess.Close()
  46. re, err := sess.Insert(model)
  47. if err != nil {
  48. return err
  49. }
  50. log.Info("success to save db.re=" + fmt.Sprint((re)))
  51. return nil
  52. }
  53. func QueryModelById(id string) (*AiModelManage, error) {
  54. sess := x.NewSession()
  55. defer sess.Close()
  56. sess.Select("*").Table("ai_model_manage").
  57. Where("id='" + id + "'")
  58. aiModelManageList := make([]*AiModelManage, 0)
  59. err := sess.Find(&aiModelManageList)
  60. if err == nil {
  61. if len(aiModelManageList) == 1 {
  62. return aiModelManageList[0], nil
  63. }
  64. }
  65. return nil, err
  66. }
  67. func DeleteModelById(id string) error {
  68. sess := x.NewSession()
  69. defer sess.Close()
  70. re, err := sess.Delete(&AiModelManage{
  71. ID: id,
  72. })
  73. if err != nil {
  74. return err
  75. }
  76. log.Info("success to delete from db.re=" + fmt.Sprint((re)))
  77. return nil
  78. }
  79. func ModifyModelDescription(id string, description string) error {
  80. var sess *xorm.Session
  81. sess = x.ID(id)
  82. defer sess.Close()
  83. re, err := sess.Cols("description").Update(&AiModelManage{
  84. Description: description,
  85. })
  86. if err != nil {
  87. return err
  88. }
  89. log.Info("success to update description from db.re=" + fmt.Sprint((re)))
  90. return nil
  91. }
  92. func QueryModelByName(name string, uid int64) []*AiModelManage {
  93. sess := x.NewSession()
  94. defer sess.Close()
  95. sess.Select("*").Table("ai_model_manage").
  96. Where("name='" + name + "' and user_id=" + fmt.Sprint(uid))
  97. aiModelManageList := make([]*AiModelManage, 0)
  98. sess.Find(&aiModelManageList)
  99. return aiModelManageList
  100. }
  101. func QueryModel(opts *AiModelQueryOptions) ([]*AiModelManage, int64, error) {
  102. sess := x.NewSession()
  103. defer sess.Close()
  104. var cond = builder.NewCond()
  105. if opts.RepoID > 0 {
  106. cond = cond.And(
  107. builder.Eq{"ai_model_manage.repo_id": opts.RepoID},
  108. )
  109. }
  110. if opts.UserID > 0 {
  111. cond = cond.And(
  112. builder.Eq{"ai_model_manage.user_id": opts.UserID},
  113. )
  114. }
  115. if len(opts.ModelID) > 0 {
  116. cond = cond.And(
  117. builder.Eq{"ai_model_manage.id": opts.ModelID},
  118. )
  119. }
  120. if (opts.Type) >= 0 {
  121. cond = cond.And(
  122. builder.Eq{"ai_model_manage.type": opts.Type},
  123. )
  124. }
  125. count, err := sess.Where(cond).Count(new(AiModelManage))
  126. if err != nil {
  127. return nil, 0, fmt.Errorf("Count: %v", err)
  128. }
  129. if opts.Page >= 0 && opts.PageSize > 0 {
  130. var start int
  131. if opts.Page == 0 {
  132. start = 0
  133. } else {
  134. start = (opts.Page - 1) * opts.PageSize
  135. }
  136. sess.Limit(opts.PageSize, start)
  137. }
  138. sess.OrderBy("ai_model_manage.created_unix DESC")
  139. aiModelManages := make([]*AiModelManage, 0, setting.UI.IssuePagingNum)
  140. if err := sess.Table("ai_model_manage").Where(cond).
  141. Find(&aiModelManages); err != nil {
  142. return nil, 0, fmt.Errorf("Find: %v", err)
  143. }
  144. sess.Close()
  145. return aiModelManages, count, nil
  146. }