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 7.0 kB

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