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.

modelarts.go 6.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package repo
  2. import (
  3. "code.gitea.io/gitea/modules/modelarts"
  4. "errors"
  5. "github.com/unknwon/com"
  6. "strconv"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. tplModelArtsIndex base.TplName = "repo/modelarts/index"
  17. tplModelArtsNew base.TplName = "repo/modelarts/new"
  18. tplModelArtsShow base.TplName = "repo/modelarts/show"
  19. )
  20. // MustEnableDataset check if repository enable internal cb
  21. func MustEnableModelArts(ctx *context.Context) {
  22. if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) {
  23. ctx.NotFound("MustEnableCloudbrain", nil)
  24. return
  25. }
  26. }
  27. func ModelArtsIndex(ctx *context.Context) {
  28. MustEnableModelArts(ctx)
  29. repo := ctx.Repo.Repository
  30. page := ctx.QueryInt("page")
  31. if page <= 0 {
  32. page = 1
  33. }
  34. ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  35. ListOptions: models.ListOptions{
  36. Page: page,
  37. PageSize: setting.UI.IssuePagingNum,
  38. },
  39. RepoID: repo.ID,
  40. Type: models.TypeCloudBrainTwo,
  41. })
  42. if err != nil {
  43. ctx.ServerError("Cloudbrain", err)
  44. return
  45. }
  46. for i, task := range ciTasks {
  47. if task.Status == string(models.JobRunning) {
  48. ciTasks[i].CanDebug = true
  49. } else {
  50. ciTasks[i].CanDebug = false
  51. }
  52. }
  53. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  54. pager.SetDefaultParams(ctx)
  55. ctx.Data["Page"] = pager
  56. ctx.Data["PageIsCloudBrain"] = true
  57. ctx.Data["Tasks"] = ciTasks
  58. ctx.HTML(200, tplModelArtsIndex)
  59. }
  60. func ModelArtsNew(ctx *context.Context) {
  61. ctx.Data["PageIsCloudBrain"] = true
  62. t := time.Now()
  63. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:]
  64. ctx.Data["job_name"] = jobName
  65. attachs, err := models.GetModelArtsUserAttachments(ctx.User.ID)
  66. if err != nil {
  67. ctx.ServerError("GetAllUserAttachments failed:", err)
  68. return
  69. }
  70. ctx.Data["attachments"] = attachs
  71. ctx.Data["dataset_path"] = modelarts.DataSetMountPath
  72. ctx.Data["env"] = modelarts.NotebookEnv
  73. ctx.Data["notebook_type"] = modelarts.NotebookType
  74. ctx.Data["flavor"] = modelarts.FlavorInfo
  75. ctx.HTML(200, tplModelArtsNew)
  76. }
  77. func ModelArtsCreate(ctx *context.Context, form auth.CreateModelArtsForm) {
  78. ctx.Data["PageIsCloudBrain"] = true
  79. jobName := form.JobName
  80. uuid := form.Attachment
  81. description := form.Description
  82. //repo := ctx.Repo.Repository
  83. err := modelarts.GenerateTask(ctx, jobName, uuid, description)
  84. if err != nil {
  85. ctx.RenderWithErr(err.Error(), tplModelArtsNew, &form)
  86. return
  87. }
  88. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  89. }
  90. func ModelArtsShow(ctx *context.Context) {
  91. ctx.Data["PageIsCloudBrain"] = true
  92. var jobID = ctx.Params(":jobid")
  93. task, err := models.GetCloudbrainByJobID(jobID)
  94. if err != nil {
  95. ctx.Data["error"] = err.Error()
  96. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  97. return
  98. }
  99. result, err := modelarts.GetJob(jobID)
  100. if err != nil {
  101. ctx.Data["error"] = err.Error()
  102. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  103. return
  104. }
  105. if result != nil {
  106. task.Status = result.Status
  107. err = models.UpdateJob(task)
  108. if err != nil {
  109. ctx.Data["error"] = err.Error()
  110. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  111. return
  112. }
  113. createTime, _ := com.StrTo(result.CreationTimestamp).Int64()
  114. result.CreateTime = time.Unix(int64(createTime/1000), 0).Format("2006-01-02 15:04:05")
  115. endTime, _ := com.StrTo(result.LatestUpdateTimestamp).Int64()
  116. result.LatestUpdateTime = time.Unix(int64(endTime/1000), 0).Format("2006-01-02 15:04:05")
  117. result.QueuingInfo.BeginTime = time.Unix(int64(result.QueuingInfo.BeginTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  118. result.QueuingInfo.EndTime = time.Unix(int64(result.QueuingInfo.EndTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  119. }
  120. ctx.Data["task"] = task
  121. ctx.Data["jobID"] = jobID
  122. ctx.Data["result"] = result
  123. ctx.HTML(200, tplModelArtsShow)
  124. }
  125. func ModelArtsDebug(ctx *context.Context) {
  126. var jobID = ctx.Params(":jobid")
  127. task, err := models.GetCloudbrainByJobID(jobID)
  128. if err != nil {
  129. ctx.ServerError("GetCloudbrainByJobID failed", err)
  130. return
  131. }
  132. //https://console.ai.pcl.cn/modelarts/internal/hub/notebook/user/DE-afcdf674-6489-11eb-bfe7-0255ac100057/lab
  133. //debugUrl := setting.DebugServerHost + "jpylab_" + task.JobID + "_" + task.SubTaskName
  134. //debugUrl := setting.ModelArtsHost + "/modelarts/internal/hub/notebook/user/" + task.JobID + "/lab"
  135. debugUrl := "https://console.ai.pcl.cn/modelarts/internal/hub/notebook/user/" + task.JobID + "/lab"
  136. ctx.Redirect(debugUrl)
  137. }
  138. func ModelArtsStop(ctx *context.Context) {
  139. var jobID = ctx.Params(":jobid")
  140. log.Info(jobID)
  141. task, err := models.GetCloudbrainByJobID(jobID)
  142. if err != nil {
  143. ctx.ServerError("GetCloudbrainByJobID failed", err)
  144. return
  145. }
  146. if task.Status != string(models.JobRunning) {
  147. log.Error("the job(%s) is not running", task.JobName)
  148. ctx.ServerError("the job is not running", errors.New("the job is not running"))
  149. return
  150. }
  151. param := models.NotebookAction{
  152. Action: models.ActionStop,
  153. }
  154. res, err := modelarts.StopJob(jobID, param)
  155. if err != nil {
  156. log.Error("StopJob(%s) failed:%v", task.JobName, err.Error())
  157. ctx.ServerError("StopJob failed", err)
  158. return
  159. }
  160. task.Status = res.CurrentStatus
  161. err = models.UpdateJob(task)
  162. if err != nil {
  163. ctx.ServerError("UpdateJob failed", err)
  164. return
  165. }
  166. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  167. }
  168. func ModelArtsDel(ctx *context.Context) {
  169. var jobID = ctx.Params(":jobid")
  170. task, err := models.GetCloudbrainByJobID(jobID)
  171. if err != nil {
  172. ctx.ServerError("GetCloudbrainByJobID failed", err)
  173. return
  174. }
  175. if task.Status != string(models.JobStopped) {
  176. log.Error("the job(%s) has not been stopped", task.JobName)
  177. ctx.ServerError("the job has not been stopped", errors.New("the job has not been stopped"))
  178. return
  179. }
  180. _, err = modelarts.DelJob(jobID)
  181. if err != nil {
  182. log.Error("DelJob(%s) failed:%v", task.JobName, err.Error())
  183. ctx.ServerError("DelJob failed", err)
  184. return
  185. }
  186. err = models.DeleteJob(task)
  187. if err != nil {
  188. ctx.ServerError("DeleteJob failed", err)
  189. return
  190. }
  191. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  192. }