|
|
|
@@ -1,9 +1,14 @@ |
|
|
|
package repo |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.gitea.io/gitea/modules/git" |
|
|
|
"code.gitea.io/gitea/modules/modelarts" |
|
|
|
"code.gitea.io/gitea/modules/obs" |
|
|
|
"code.gitea.io/gitea/modules/storage" |
|
|
|
"errors" |
|
|
|
"github.com/unknwon/com" |
|
|
|
"io" |
|
|
|
"os" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
@@ -89,12 +94,11 @@ func NotebookNew(ctx *context.Context) { |
|
|
|
ctx.HTML(200, tplModelArtsNotebookNew) |
|
|
|
} |
|
|
|
|
|
|
|
func NotebookCreate(ctx *context.Context, form auth.CreateModelArtsForm) { |
|
|
|
func NotebookCreate(ctx *context.Context, form auth.CreateModelArtsNotebookForm) { |
|
|
|
ctx.Data["PageIsCloudBrain"] = true |
|
|
|
jobName := form.JobName |
|
|
|
uuid := form.Attachment |
|
|
|
description := form.Description |
|
|
|
//repo := ctx.Repo.Repository |
|
|
|
|
|
|
|
err := modelarts.GenerateTask(ctx, jobName, uuid, description) |
|
|
|
if err != nil { |
|
|
|
@@ -246,3 +250,152 @@ func NotebookDel(ctx *context.Context) { |
|
|
|
ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/notebook") |
|
|
|
} |
|
|
|
|
|
|
|
func TrainJobIndex(ctx *context.Context) { |
|
|
|
MustEnableModelArts(ctx) |
|
|
|
repo := ctx.Repo.Repository |
|
|
|
page := ctx.QueryInt("page") |
|
|
|
if page <= 0 { |
|
|
|
page = 1 |
|
|
|
} |
|
|
|
|
|
|
|
tasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{ |
|
|
|
ListOptions: models.ListOptions{ |
|
|
|
Page: page, |
|
|
|
PageSize: setting.UI.IssuePagingNum, |
|
|
|
}, |
|
|
|
RepoID: repo.ID, |
|
|
|
Type: models.TypeCloudBrainTrainJob, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
ctx.ServerError("Cloudbrain", err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5) |
|
|
|
pager.SetDefaultParams(ctx) |
|
|
|
ctx.Data["Page"] = pager |
|
|
|
|
|
|
|
ctx.Data["PageIsCloudBrain"] = true |
|
|
|
ctx.Data["Tasks"] = tasks |
|
|
|
ctx.HTML(200, tplModelArtsNotebookIndex) |
|
|
|
} |
|
|
|
|
|
|
|
func TrainJobNew(ctx *context.Context) { |
|
|
|
ctx.Data["PageIsCloudBrain"] = true |
|
|
|
|
|
|
|
t := time.Now() |
|
|
|
var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:] |
|
|
|
ctx.Data["job_name"] = jobName |
|
|
|
|
|
|
|
attachs, err := models.GetModelArtsUserAttachments(ctx.User.ID) |
|
|
|
if err != nil { |
|
|
|
ctx.ServerError("GetAllUserAttachments failed:", err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
ctx.Data["attachments"] = attachs |
|
|
|
ctx.Data["dataset_path"] = modelarts.DataSetMountPath |
|
|
|
ctx.Data["env"] = modelarts.NotebookEnv |
|
|
|
ctx.Data["notebook_type"] = modelarts.NotebookType |
|
|
|
ctx.Data["flavor"] = modelarts.FlavorInfo |
|
|
|
ctx.HTML(200, tplModelArtsNotebookNew) |
|
|
|
} |
|
|
|
|
|
|
|
func TrainJobCreate(ctx *context.Context, form auth.CreateModelArtsTrainJobForm) { |
|
|
|
ctx.Data["PageIsCloudBrain"] = true |
|
|
|
jobName := form.JobName |
|
|
|
/* |
|
|
|
uuid := form.Attachment |
|
|
|
description := form.Description |
|
|
|
|
|
|
|
*/ |
|
|
|
repo := ctx.Repo.Repository |
|
|
|
codePath := setting.JobPath + jobName + modelarts.CodeLocalPath |
|
|
|
|
|
|
|
if err := git.Clone(repo.RepoPath(), codePath, git.CloneRepoOptions{}); err != nil { |
|
|
|
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) |
|
|
|
ctx.RenderWithErr("Failed to clone repository", tplModelArtsNotebookNew, &form) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//todo: upload code (send to file_server todo this work?) |
|
|
|
if err := uploadCodeToObs(codePath, jobName, ""); err != nil { |
|
|
|
log.Error("Failed to uploadCodeToObs: %s (%v)", repo.FullName(), err) |
|
|
|
ctx.RenderWithErr("Failed to uploadCodeToObs", tplModelArtsNotebookNew, &form) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
err := modelarts.GenerateTask(ctx, jobName, uuid, description) |
|
|
|
if err != nil { |
|
|
|
ctx.RenderWithErr(err.Error(), tplModelArtsNotebookNew, &form) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/notebook") |
|
|
|
} |
|
|
|
|
|
|
|
// readDir reads the directory named by dirname and returns |
|
|
|
// a list of directory entries sorted by filename. |
|
|
|
func readDir(dirname string) ([]os.FileInfo, error) { |
|
|
|
f, err := os.Open(dirname) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
list, err := f.Readdir(100) |
|
|
|
f.Close() |
|
|
|
if err != nil { |
|
|
|
if err == io.EOF { |
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
//sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) |
|
|
|
return list, nil |
|
|
|
} |
|
|
|
|
|
|
|
func uploadCodeToObs(codePath, jobName, parentDir string) error { |
|
|
|
log.Info(codePath) |
|
|
|
files, err := readDir(codePath) |
|
|
|
if err != nil { |
|
|
|
log.Error("readDir(%s) failed: %s", codePath, err.Error()) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
for _, file := range files { |
|
|
|
if file.IsDir() { |
|
|
|
input := &obs.PutObjectInput{} |
|
|
|
input.Bucket = setting.Bucket |
|
|
|
input.Key = codePath + file.Name() + "/" |
|
|
|
log.Info(input.Key) |
|
|
|
_, err = storage.ObsCli.PutObject(input) |
|
|
|
if err != nil { |
|
|
|
log.Error("PutObject(%s) failed: %s", input.Key, err.Error()) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
if err = uploadCodeToObs(codePath + file.Name() + "/", jobName, parentDir + file.Name() + "/"); err != nil { |
|
|
|
log.Error("uploadCodeToObs(%s) failed: %s", file.Name(), err.Error()) |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
input := &obs.PutFileInput{} |
|
|
|
input.Bucket = setting.Bucket |
|
|
|
input.Key = setting.CodePathPrefix + jobName + "/" + parentDir + file.Name() |
|
|
|
log.Info(input.Key) |
|
|
|
input.SourceFile = codePath + file.Name() |
|
|
|
log.Info(input.SourceFile) |
|
|
|
_, err = storage.ObsCli.PutFile(input) |
|
|
|
if err != nil { |
|
|
|
log.Error("PutFile(%s) failed: %s", input.SourceFile, err.Error()) |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |