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.7 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. err := modelarts.GenerateTask(ctx, jobName, uuid, description)
  90. if err != nil {
  91. ctx.RenderWithErr(err.Error(), tplModelArtsNew, &form)
  92. return
  93. }
  94. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  95. }
  96. func ModelArtsShow(ctx *context.Context) {
  97. ctx.Data["PageIsCloudBrain"] = true
  98. var jobID = ctx.Params(":jobid")
  99. task, err := models.GetCloudbrainByJobID(jobID)
  100. if err != nil {
  101. ctx.Data["error"] = err.Error()
  102. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  103. return
  104. }
  105. result, err := modelarts.GetJob(jobID)
  106. if err != nil {
  107. ctx.Data["error"] = err.Error()
  108. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  109. return
  110. }
  111. if result != nil {
  112. task.Status = result.Status
  113. err = models.UpdateJob(task)
  114. if err != nil {
  115. ctx.Data["error"] = err.Error()
  116. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  117. return
  118. }
  119. createTime, _ := com.StrTo(result.CreationTimestamp).Int64()
  120. result.CreateTime = time.Unix(int64(createTime/1000), 0).Format("2006-01-02 15:04:05")
  121. endTime, _ := com.StrTo(result.LatestUpdateTimestamp).Int64()
  122. result.LatestUpdateTime = time.Unix(int64(endTime/1000), 0).Format("2006-01-02 15:04:05")
  123. result.QueuingInfo.BeginTime = time.Unix(int64(result.QueuingInfo.BeginTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  124. result.QueuingInfo.EndTime = time.Unix(int64(result.QueuingInfo.EndTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  125. }
  126. ctx.Data["task"] = task
  127. ctx.Data["jobID"] = jobID
  128. ctx.Data["result"] = result
  129. ctx.HTML(200, tplModelArtsShow)
  130. }
  131. func ModelArtsDebug(ctx *context.Context) {
  132. var jobID = ctx.Params(":jobid")
  133. _, err := models.GetCloudbrainByJobID(jobID)
  134. if err != nil {
  135. ctx.ServerError("GetCloudbrainByJobID failed", err)
  136. return
  137. }
  138. result, err := modelarts.GetJob(jobID)
  139. if err != nil {
  140. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  141. return
  142. }
  143. res, err := modelarts.GetJobToken(jobID)
  144. if err != nil {
  145. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  146. return
  147. }
  148. urls := strings.Split(result.Spec.Annotations.Url, "/")
  149. urlPrefix := result.Spec.Annotations.TargetDomain
  150. for i, url := range urls {
  151. if i > 2 {
  152. urlPrefix += "/" + url
  153. }
  154. }
  155. //urlPrefix := result.Spec.Annotations.TargetDomain + "/modelarts/internal/hub/notebook/user/" + task.JobID
  156. log.Info(urlPrefix)
  157. debugUrl := urlPrefix + "?token=" + res.Token
  158. ctx.Redirect(debugUrl)
  159. }
  160. func ModelArtsStop(ctx *context.Context) {
  161. var jobID = ctx.Params(":jobid")
  162. log.Info(jobID)
  163. task, err := models.GetCloudbrainByJobID(jobID)
  164. if err != nil {
  165. ctx.ServerError("GetCloudbrainByJobID failed", err)
  166. return
  167. }
  168. if task.Status != string(models.JobRunning) {
  169. log.Error("the job(%s) is not running", task.JobName)
  170. ctx.ServerError("the job is not running", errors.New("the job is not running"))
  171. return
  172. }
  173. param := models.NotebookAction{
  174. Action: models.ActionStop,
  175. }
  176. res, err := modelarts.StopJob(jobID, param)
  177. if err != nil {
  178. log.Error("StopJob(%s) failed:%v", task.JobName, err.Error())
  179. ctx.ServerError("StopJob failed", err)
  180. return
  181. }
  182. task.Status = res.CurrentStatus
  183. err = models.UpdateJob(task)
  184. if err != nil {
  185. ctx.ServerError("UpdateJob failed", err)
  186. return
  187. }
  188. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  189. }
  190. func ModelArtsDel(ctx *context.Context) {
  191. var jobID = ctx.Params(":jobid")
  192. task, err := models.GetCloudbrainByJobID(jobID)
  193. if err != nil {
  194. ctx.ServerError("GetCloudbrainByJobID failed", err)
  195. return
  196. }
  197. if task.Status != string(models.JobStopped) {
  198. log.Error("the job(%s) has not been stopped", task.JobName)
  199. ctx.ServerError("the job has not been stopped", errors.New("the job has not been stopped"))
  200. return
  201. }
  202. _, err = modelarts.DelJob(jobID)
  203. if err != nil {
  204. log.Error("DelJob(%s) failed:%v", task.JobName, err.Error())
  205. ctx.ServerError("DelJob failed", err)
  206. return
  207. }
  208. err = models.DeleteJob(task)
  209. if err != nil {
  210. ctx.ServerError("DeleteJob failed", err)
  211. return
  212. }
  213. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  214. }