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 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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:"INDEX NOT NULL"`
  13. Version string `xorm:"NOT NULL"`
  14. VersionCount int `xorm:"NOT NULL DEFAULT 0"`
  15. New int `xorm:"NOT NULL"`
  16. Type int `xorm:"NOT NULL"`
  17. Size int64 `xorm:"NOT NULL"`
  18. Description string `xorm:"varchar(2000)"`
  19. Label string `xorm:"varchar(1000)"`
  20. Path string `xorm:"varchar(400) NOT NULL"`
  21. DownloadCount int `xorm:"NOT NULL DEFAULT 0"`
  22. Engine int64 `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:"INDEX NULL"`
  27. CodeBranch string `xorm:"varchar(400) NULL"`
  28. CodeCommitID string `xorm:"NULL"`
  29. UserId int64 `xorm:"NOT NULL"`
  30. UserName string
  31. UserRelAvatarLink string
  32. TrainTaskInfo string `xorm:"text NULL"`
  33. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  34. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  35. IsCanOper bool
  36. IsCanDelete bool
  37. }
  38. type AiModelManageConvert struct {
  39. ID string `xorm:"pk"`
  40. Name string `xorm:"INDEX NOT NULL"`
  41. Status int `xorm:"NOT NULL DEFAULT 0"`
  42. SrcEngine int `xorm:"NOT NULL DEFAULT 0"`
  43. RepoId int64 `xorm:"INDEX NULL"`
  44. ModelId string `xorm:"NOT NULL"`
  45. ModelVersion string `xorm:"NOT NULL"`
  46. DestFormat int `xorm:"NOT NULL DEFAULT 0"`
  47. NetOutputFormat int `xorm:"NULL"`
  48. UserId int64 `xorm:"NOT NULL"`
  49. RunTime int64 `xorm:"NULL"`
  50. InputShape string `xorm:"varchar(2000)"`
  51. InputDataFormat string `xorm:"NOT NULL"`
  52. Description string `xorm:"varchar(2000)"`
  53. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  54. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  55. UserName string
  56. UserRelAvatarLink string
  57. IsCanOper bool
  58. IsCanDelete bool
  59. }
  60. type AiModelQueryOptions struct {
  61. ListOptions
  62. RepoID int64 // include all repos if empty
  63. UserID int64
  64. ModelID string
  65. SortType string
  66. New int
  67. // JobStatus CloudbrainStatus
  68. Type int
  69. Status int
  70. }
  71. func SaveModelConvert(modelConvert *AiModelManageConvert) error {
  72. sess := x.NewSession()
  73. defer sess.Close()
  74. re, err := sess.Insert(modelConvert)
  75. if err != nil {
  76. log.Info("insert modelConvert error." + err.Error())
  77. return err
  78. }
  79. log.Info("success to save modelConvert db.re=" + fmt.Sprint((re)))
  80. return nil
  81. }
  82. func SaveModelToDb(model *AiModelManage) error {
  83. sess := x.NewSession()
  84. defer sess.Close()
  85. re, err := sess.Insert(model)
  86. if err != nil {
  87. log.Info("insert error." + err.Error())
  88. return err
  89. }
  90. log.Info("success to save db.re=" + fmt.Sprint((re)))
  91. return nil
  92. }
  93. func QueryModelConvertById(id string) (*AiModelManageConvert, error) {
  94. sess := x.NewSession()
  95. defer sess.Close()
  96. sess.Select("*").Table(new(AiModelManageConvert)).Where("id='" + id + "'")
  97. aiModelManageConvertList := make([]*AiModelManageConvert, 0)
  98. err := sess.Find(&aiModelManageConvertList)
  99. if err == nil {
  100. if len(aiModelManageConvertList) == 1 {
  101. return aiModelManageConvertList[0], nil
  102. }
  103. }
  104. return nil, err
  105. }
  106. func QueryModelById(id string) (*AiModelManage, error) {
  107. sess := x.NewSession()
  108. defer sess.Close()
  109. sess.Select("*").Table("ai_model_manage").
  110. Where("id='" + id + "'")
  111. aiModelManageList := make([]*AiModelManage, 0)
  112. err := sess.Find(&aiModelManageList)
  113. if err == nil {
  114. if len(aiModelManageList) == 1 {
  115. return aiModelManageList[0], nil
  116. }
  117. }
  118. return nil, err
  119. }
  120. func DeleteModelConvertById(id string) error {
  121. sess := x.NewSession()
  122. defer sess.Close()
  123. re, err := sess.Delete(&AiModelManageConvert{
  124. ID: id,
  125. })
  126. if err != nil {
  127. return err
  128. }
  129. log.Info("success to delete AiModelManageConvert from db.re=" + fmt.Sprint((re)))
  130. return nil
  131. }
  132. func DeleteModelById(id string) error {
  133. sess := x.NewSession()
  134. defer sess.Close()
  135. re, err := sess.Delete(&AiModelManage{
  136. ID: id,
  137. })
  138. if err != nil {
  139. return err
  140. }
  141. log.Info("success to delete from db.re=" + fmt.Sprint((re)))
  142. return nil
  143. }
  144. func ModifyModelDescription(id string, description string) error {
  145. var sess *xorm.Session
  146. sess = x.ID(id)
  147. defer sess.Close()
  148. re, err := sess.Cols("description").Update(&AiModelManage{
  149. Description: description,
  150. })
  151. if err != nil {
  152. return err
  153. }
  154. log.Info("success to update description from db.re=" + fmt.Sprint((re)))
  155. return nil
  156. }
  157. func ModifyModelNewProperty(id string, new int, versioncount int) error {
  158. var sess *xorm.Session
  159. sess = x.ID(id)
  160. defer sess.Close()
  161. re, err := sess.Cols("new", "version_count").Update(&AiModelManage{
  162. New: new,
  163. VersionCount: versioncount,
  164. })
  165. if err != nil {
  166. return err
  167. }
  168. log.Info("success to update new property from db.re=" + fmt.Sprint((re)))
  169. return nil
  170. }
  171. func ModifyModelDownloadCount(id string) error {
  172. sess := x.NewSession()
  173. defer sess.Close()
  174. if _, err := sess.Exec("UPDATE `ai_model_manage` SET download_count = download_count + 1 WHERE id = ?", id); err != nil {
  175. return err
  176. }
  177. return nil
  178. }
  179. func QueryModelByName(name string, repoId int64) []*AiModelManage {
  180. sess := x.NewSession()
  181. defer sess.Close()
  182. sess.Select("*").Table("ai_model_manage").
  183. Where("name='" + name + "' and repo_id=" + fmt.Sprint(repoId)).OrderBy("created_unix desc")
  184. aiModelManageList := make([]*AiModelManage, 0)
  185. sess.Find(&aiModelManageList)
  186. return aiModelManageList
  187. }
  188. func QueryModel(opts *AiModelQueryOptions) ([]*AiModelManage, int64, error) {
  189. sess := x.NewSession()
  190. defer sess.Close()
  191. var cond = builder.NewCond()
  192. if opts.RepoID > 0 {
  193. cond = cond.And(
  194. builder.Eq{"ai_model_manage.repo_id": opts.RepoID},
  195. )
  196. }
  197. if opts.UserID > 0 {
  198. cond = cond.And(
  199. builder.Eq{"ai_model_manage.user_id": opts.UserID},
  200. )
  201. }
  202. if opts.New >= 0 {
  203. cond = cond.And(
  204. builder.Eq{"ai_model_manage.new": opts.New},
  205. )
  206. }
  207. if len(opts.ModelID) > 0 {
  208. cond = cond.And(
  209. builder.Eq{"ai_model_manage.id": opts.ModelID},
  210. )
  211. }
  212. if (opts.Type) >= 0 {
  213. cond = cond.And(
  214. builder.Eq{"ai_model_manage.type": opts.Type},
  215. )
  216. }
  217. count, err := sess.Where(cond).Count(new(AiModelManage))
  218. if err != nil {
  219. return nil, 0, fmt.Errorf("Count: %v", err)
  220. }
  221. if opts.Page >= 0 && opts.PageSize > 0 {
  222. var start int
  223. if opts.Page == 0 {
  224. start = 0
  225. } else {
  226. start = (opts.Page - 1) * opts.PageSize
  227. }
  228. sess.Limit(opts.PageSize, start)
  229. }
  230. sess.OrderBy("ai_model_manage.created_unix DESC")
  231. aiModelManages := make([]*AiModelManage, 0, setting.UI.IssuePagingNum)
  232. if err := sess.Table("ai_model_manage").Where(cond).
  233. Find(&aiModelManages); err != nil {
  234. return nil, 0, fmt.Errorf("Find: %v", err)
  235. }
  236. return aiModelManages, count, nil
  237. }
  238. func QueryModelConvert(opts *AiModelQueryOptions) ([]*AiModelManageConvert, int64, error) {
  239. sess := x.NewSession()
  240. defer sess.Close()
  241. var cond = builder.NewCond()
  242. if opts.RepoID > 0 {
  243. cond = cond.And(
  244. builder.Eq{"ai_model_manage_convert.repo_id": opts.RepoID},
  245. )
  246. }
  247. if opts.UserID > 0 {
  248. cond = cond.And(
  249. builder.Eq{"ai_model_manage_convert.user_id": opts.UserID},
  250. )
  251. }
  252. if (opts.Status) >= 0 {
  253. cond = cond.And(
  254. builder.Eq{"ai_model_manage_convert.status": opts.Status},
  255. )
  256. }
  257. count, err := sess.Where(cond).Count(new(AiModelManageConvert))
  258. if err != nil {
  259. return nil, 0, fmt.Errorf("Count: %v", err)
  260. }
  261. if opts.Page >= 0 && opts.PageSize > 0 {
  262. var start int
  263. if opts.Page == 0 {
  264. start = 0
  265. } else {
  266. start = (opts.Page - 1) * opts.PageSize
  267. }
  268. sess.Limit(opts.PageSize, start)
  269. }
  270. sess.OrderBy("ai_model_manage_convert.created_unix DESC")
  271. aiModelManageConvert := make([]*AiModelManageConvert, 0, setting.UI.IssuePagingNum)
  272. if err := sess.Table(new(AiModelManageConvert)).Where(cond).
  273. Find(&aiModelManageConvert); err != nil {
  274. return nil, 0, fmt.Errorf("Find: %v", err)
  275. }
  276. return aiModelManageConvert, count, nil
  277. }