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.

aisafety.go 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package repo
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/aisafety"
  11. "code.gitea.io/gitea/modules/cloudbrain"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/storage"
  16. "code.gitea.io/gitea/modules/util"
  17. "code.gitea.io/gitea/services/cloudbrain/resource"
  18. uuid "github.com/satori/go.uuid"
  19. )
  20. func CloudBrainAiSafetyCreateTest(ctx *context.Context) {
  21. log.Info("start to create CloudBrainAiSafetyCreate")
  22. uuid := uuid.NewV4()
  23. id := uuid.String()
  24. //jobType := ctx.Query("jobType")
  25. //if jobType == string(models.JobTypeBenchmark) {
  26. jsonMap, err := getJsonContent("http://192.168.207.34:8065/Test_zap1234/openi_aisafety/raw/branch/master/result/0301.json")
  27. if err == nil {
  28. aisafety.GetAlgorithmList()
  29. aisafety.CreateSafetyTask(id, "test", jsonMap)
  30. time.Sleep(time.Duration(2) * time.Second)
  31. aisafety.GetTaskStatus(id)
  32. }
  33. //}
  34. }
  35. func CloudBrainAiSafetyCreate(ctx *context.Context) {
  36. ctx.Data["PageIsCloudBrain"] = true
  37. displayJobName := ctx.Query("DisplayJobName")
  38. jobName := util.ConvertDisplayJobNameToJobName(displayJobName)
  39. image := strings.TrimSpace(ctx.Query("Image"))
  40. command := "python /code/inferench.py > " + cloudbrain.ModelMountPath + "/" + displayJobName + "-" + cloudbrain.LogFile
  41. codePath := setting.JobPath + jobName + cloudbrain.CodeMountPath
  42. description := ctx.Query("Description")
  43. specId := ctx.QueryInt64("SpecId")
  44. ctx.Data["description"] = description
  45. repo := ctx.Repo.Repository
  46. tasks, err := models.GetCloudbrainsByDisplayJobName(repo.ID, string(models.JobTypeBenchmark), displayJobName)
  47. if err == nil {
  48. if len(tasks) != 0 {
  49. log.Error("the job name did already exist", ctx.Data["MsgID"])
  50. cloudBrainNewDataPrepare(ctx)
  51. ctx.RenderWithErr("the job name did already exist", tplCloudBrainBenchmarkNew, nil)
  52. return
  53. }
  54. } else {
  55. if !models.IsErrJobNotExist(err) {
  56. log.Error("system error, %v", err, ctx.Data["MsgID"])
  57. cloudBrainNewDataPrepare(ctx)
  58. ctx.RenderWithErr("system error", tplCloudBrainBenchmarkNew, nil)
  59. return
  60. }
  61. }
  62. if !jobNamePattern.MatchString(jobName) {
  63. cloudBrainNewDataPrepare(ctx)
  64. ctx.RenderWithErr(ctx.Tr("repo.cloudbrain_jobname_err"), tplCloudBrainBenchmarkNew, nil)
  65. return
  66. }
  67. spec, err := resource.GetAndCheckSpec(ctx.User.ID, specId, models.FindSpecsOptions{
  68. JobType: models.JobTypeBenchmark,
  69. ComputeResource: models.GPU,
  70. Cluster: models.OpenICluster,
  71. AiCenterCode: models.AICenterOfCloudBrainOne})
  72. if err != nil || spec == nil {
  73. cloudBrainNewDataPrepare(ctx)
  74. ctx.RenderWithErr("Resource specification not available", tplCloudBrainBenchmarkNew, nil)
  75. return
  76. }
  77. count, err := models.GetBenchmarkCountByUserID(ctx.User.ID)
  78. if err != nil {
  79. log.Error("GetCloudbrainCountByUserID failed:%v", err, ctx.Data["MsgID"])
  80. cloudBrainNewDataPrepare(ctx)
  81. ctx.RenderWithErr("system error", tplCloudBrainBenchmarkNew, nil)
  82. return
  83. } else {
  84. if count >= 1 {
  85. log.Error("the user already has running or waiting task", ctx.Data["MsgID"])
  86. cloudBrainNewDataPrepare(ctx)
  87. ctx.RenderWithErr(ctx.Tr("repo.cloudbrain.morethanonejob"), tplCloudBrainBenchmarkNew, nil)
  88. return
  89. }
  90. }
  91. os.RemoveAll(codePath)
  92. if err := downloadCode(repo, codePath, cloudbrain.DefaultBranchName); err != nil {
  93. log.Error("downloadCode failed, %v", err, ctx.Data["MsgID"])
  94. cloudBrainNewDataPrepare(ctx)
  95. ctx.RenderWithErr("system error", tplCloudBrainBenchmarkNew, nil)
  96. return
  97. }
  98. if _, err := os.Stat(codePath + "/inference.py"); err != nil {
  99. if os.IsNotExist(err) {
  100. // file does not exist
  101. log.Error("inference.py does not exist, %v", err, ctx.Data["MsgID"])
  102. cloudBrainNewDataPrepare(ctx)
  103. ctx.RenderWithErr("inference.py does not exist", tplCloudBrainBenchmarkNew, nil)
  104. } else {
  105. log.Error("Stat failed, %v", err, ctx.Data["MsgID"])
  106. cloudBrainNewDataPrepare(ctx)
  107. ctx.RenderWithErr("system error", tplCloudBrainBenchmarkNew, nil)
  108. }
  109. return
  110. }
  111. if err := uploadCodeToMinio(codePath+"/", jobName, cloudbrain.CodeMountPath+"/"); err != nil {
  112. log.Error("uploadCodeToMinio failed, %v", err, ctx.Data["MsgID"])
  113. cloudBrainNewDataPrepare(ctx)
  114. ctx.RenderWithErr("system error", tplCloudBrainBenchmarkNew, nil)
  115. return
  116. }
  117. uuid := "dee79f68-19f1-42dd-b004-bc9ce08415ca"
  118. datasetInfos, datasetNames, err := models.GetDatasetInfo(uuid)
  119. if err != nil {
  120. log.Error("GetDatasetInfo failed: %v", err, ctx.Data["MsgID"])
  121. cloudBrainNewDataPrepare(ctx)
  122. ctx.RenderWithErr(ctx.Tr("cloudbrain.error.dataset_select"), tplCloudBrainBenchmarkNew, nil)
  123. return
  124. }
  125. log.Info("Command=" + command)
  126. log.Info("ModelPath=" + storage.GetMinioPath(jobName, cloudbrain.ModelMountPath+"/"))
  127. req := cloudbrain.GenerateCloudBrainTaskReq{
  128. Ctx: ctx,
  129. DisplayJobName: displayJobName,
  130. JobName: jobName,
  131. Image: image,
  132. Command: command,
  133. Uuids: uuid,
  134. DatasetNames: datasetNames,
  135. DatasetInfos: datasetInfos,
  136. CodePath: storage.GetMinioPath(jobName, cloudbrain.CodeMountPath+"/"),
  137. ModelPath: storage.GetMinioPath(jobName, cloudbrain.ModelMountPath+"/"),
  138. BenchmarkPath: storage.GetMinioPath(jobName, cloudbrain.BenchMarkMountPath+"/"),
  139. Snn4ImageNetPath: storage.GetMinioPath(jobName, cloudbrain.Snn4imagenetMountPath+"/"),
  140. BrainScorePath: storage.GetMinioPath(jobName, cloudbrain.BrainScoreMountPath+"/"),
  141. JobType: string(models.JobTypeModelSafety),
  142. Description: description,
  143. BranchName: cloudbrain.DefaultBranchName,
  144. BootFile: "",
  145. Params: "",
  146. CommitID: "",
  147. ResultPath: storage.GetMinioPath(jobName, cloudbrain.ResultPath+"/"),
  148. Spec: spec,
  149. }
  150. err = cloudbrain.GenerateTask(req)
  151. if err != nil {
  152. cloudBrainNewDataPrepare(ctx)
  153. ctx.RenderWithErr(err.Error(), tplCloudBrainBenchmarkNew, nil)
  154. return
  155. }
  156. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain/benchmark")
  157. }
  158. func getJsonContent(url string) (map[string]interface{}, error) {
  159. resp, err := http.Get(url)
  160. if err != nil || resp.StatusCode != 200 {
  161. log.Info("Get organizations url error=" + err.Error())
  162. return nil, err
  163. }
  164. bytes, err := ioutil.ReadAll(resp.Body)
  165. resp.Body.Close()
  166. if err != nil {
  167. log.Info("Get organizations url error=" + err.Error())
  168. return nil, err
  169. }
  170. log.Info("json str =" + string(bytes))
  171. jsonMap := make(map[string]interface{})
  172. err = json.Unmarshal(bytes, &jsonMap)
  173. if err != nil {
  174. log.Info("json err=" + err.Error())
  175. }
  176. return jsonMap, nil
  177. }