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