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.

ai_model_manage.go 18 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package repo
  2. import (
  3. "archive/zip"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  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. uuid "github.com/satori/go.uuid"
  16. )
  17. const (
  18. Model_prefix = "aimodels/"
  19. tplModelManageIndex = "repo/modelmanage/index"
  20. tplModelManageDownload = "repo/modelmanage/download"
  21. tplModelInfo = "repo/modelmanage/showinfo"
  22. MODEL_LATEST = 1
  23. MODEL_NOT_LATEST = 0
  24. )
  25. func saveModelByParameters(jobId string, versionName string, name string, version string, label string, description string, ctx *context.Context) error {
  26. aiTask, err := models.GetCloudbrainByJobIDAndVersionName(jobId, versionName)
  27. if err != nil {
  28. log.Info("query task error." + err.Error())
  29. return err
  30. }
  31. uuid := uuid.NewV4()
  32. id := uuid.String()
  33. modelPath := id
  34. var lastNewModelId string
  35. var modelSize int64
  36. cloudType := models.TypeCloudBrainTwo
  37. log.Info("find task name:" + aiTask.JobName)
  38. aimodels := models.QueryModelByName(name, aiTask.RepoID)
  39. if len(aimodels) > 0 {
  40. for _, model := range aimodels {
  41. if model.Version == version {
  42. return errors.New(ctx.Tr("repo.model.manage.create_error"))
  43. }
  44. if model.New == MODEL_LATEST {
  45. lastNewModelId = model.ID
  46. }
  47. }
  48. }
  49. cloudType = aiTask.Type
  50. //download model zip //train type
  51. if cloudType == models.TypeCloudBrainTwo {
  52. modelPath, modelSize, err = downloadModelFromCloudBrainTwo(id, aiTask.JobName, "", aiTask.TrainUrl)
  53. if err != nil {
  54. log.Info("download model from CloudBrainTwo faild." + err.Error())
  55. return err
  56. }
  57. }
  58. accuracy := make(map[string]string)
  59. accuracy["F1"] = ""
  60. accuracy["Recall"] = ""
  61. accuracy["Accuracy"] = ""
  62. accuracy["Precision"] = ""
  63. accuracyJson, _ := json.Marshal(accuracy)
  64. log.Info("accuracyJson=" + string(accuracyJson))
  65. aiTaskJson, _ := json.Marshal(aiTask)
  66. model := &models.AiModelManage{
  67. ID: id,
  68. Version: version,
  69. VersionCount: len(aimodels) + 1,
  70. Label: label,
  71. Name: name,
  72. Description: description,
  73. New: MODEL_LATEST,
  74. Type: cloudType,
  75. Path: modelPath,
  76. Size: modelSize,
  77. AttachmentId: aiTask.Uuid,
  78. RepoId: aiTask.RepoID,
  79. UserId: ctx.User.ID,
  80. CodeBranch: aiTask.BranchName,
  81. CodeCommitID: aiTask.CommitID,
  82. Engine: aiTask.EngineID,
  83. TrainTaskInfo: string(aiTaskJson),
  84. Accuracy: string(accuracyJson),
  85. }
  86. err = models.SaveModelToDb(model)
  87. if err != nil {
  88. return err
  89. }
  90. if len(lastNewModelId) > 0 {
  91. //udpate status and version count
  92. models.ModifyModelNewProperty(lastNewModelId, MODEL_NOT_LATEST, 0)
  93. }
  94. //
  95. var units []models.RepoUnit
  96. var deleteUnitTypes []models.UnitType
  97. units = append(units, models.RepoUnit{
  98. RepoID: ctx.Repo.Repository.ID,
  99. Type: models.UnitTypeModelManage,
  100. Config: &models.ModelManageConfig{
  101. EnableModelManage: true,
  102. },
  103. })
  104. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeModelManage)
  105. models.UpdateRepositoryUnits(ctx.Repo.Repository, units, deleteUnitTypes)
  106. log.Info("save model end.")
  107. return nil
  108. }
  109. func SaveNewNameModel(ctx *context.Context) {
  110. name := ctx.Query("Name")
  111. if name == "" {
  112. ctx.Error(500, fmt.Sprintf("name or version is null."))
  113. return
  114. }
  115. aimodels := models.QueryModelByName(name, ctx.Repo.Repository.ID)
  116. if len(aimodels) > 0 {
  117. ctx.Error(500, ctx.Tr("repo.model_rename"))
  118. return
  119. }
  120. SaveModel(ctx)
  121. log.Info("save model end.")
  122. }
  123. func SaveModel(ctx *context.Context) {
  124. log.Info("save model start.")
  125. JobId := ctx.Query("JobId")
  126. VersionName := ctx.Query("VersionName")
  127. name := ctx.Query("Name")
  128. version := ctx.Query("Version")
  129. label := ctx.Query("Label")
  130. description := ctx.Query("Description")
  131. if !ctx.Repo.CanWrite(models.UnitTypeModelManage) {
  132. ctx.ServerError("No right.", errors.New(ctx.Tr("repo.model_noright")))
  133. return
  134. }
  135. if JobId == "" || VersionName == "" {
  136. ctx.Error(500, fmt.Sprintf("JobId or VersionName is null."))
  137. return
  138. }
  139. if name == "" || version == "" {
  140. ctx.Error(500, fmt.Sprintf("name or version is null."))
  141. return
  142. }
  143. err := saveModelByParameters(JobId, VersionName, name, version, label, description, ctx)
  144. if err != nil {
  145. log.Info("save model error." + err.Error())
  146. ctx.Error(500, fmt.Sprintf("save model error. %v", err))
  147. return
  148. }
  149. log.Info("save model end.")
  150. }
  151. func downloadModelFromCloudBrainTwo(modelUUID string, jobName string, parentDir string, trainUrl string) (string, int64, error) {
  152. objectkey := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, jobName, setting.OutPutPath, parentDir), "/")
  153. if trainUrl != "" {
  154. objectkey = strings.Trim(trainUrl[len(setting.Bucket)+1:], "/")
  155. }
  156. modelDbResult, err := storage.GetOneLevelAllObjectUnderDir(setting.Bucket, objectkey, "")
  157. log.Info("bucket=" + setting.Bucket + " objectkey=" + objectkey)
  158. if err != nil {
  159. log.Info("get TrainJobListModel failed:", err)
  160. return "", 0, err
  161. }
  162. if len(modelDbResult) == 0 {
  163. return "", 0, errors.New("cannot create model, as model is empty.")
  164. }
  165. prefix := objectkey + "/"
  166. destKeyNamePrefix := Model_prefix + models.AttachmentRelativePath(modelUUID) + "/"
  167. size, err := storage.ObsCopyManyFile(setting.Bucket, prefix, setting.Bucket, destKeyNamePrefix)
  168. dataActualPath := setting.Bucket + "/" + destKeyNamePrefix
  169. return dataActualPath, size, nil
  170. }
  171. func DeleteModel(ctx *context.Context) {
  172. log.Info("delete model start.")
  173. id := ctx.Query("ID")
  174. err := deleteModelByID(ctx, id)
  175. if err != nil {
  176. ctx.JSON(500, err.Error())
  177. } else {
  178. ctx.JSON(200, map[string]string{
  179. "result_code": "0",
  180. })
  181. }
  182. }
  183. func isCanDeleteOrDownload(ctx *context.Context, model *models.AiModelManage) bool {
  184. if ctx.User.IsAdmin || ctx.User.ID == model.UserId {
  185. return true
  186. }
  187. if ctx.Repo.IsOwner() {
  188. return true
  189. }
  190. return false
  191. }
  192. func deleteModelByID(ctx *context.Context, id string) error {
  193. log.Info("delete model start. id=" + id)
  194. model, err := models.QueryModelById(id)
  195. if !isCanDeleteOrDownload(ctx, model) {
  196. return errors.New(ctx.Tr("repo.model_noright"))
  197. }
  198. if err == nil {
  199. log.Info("bucket=" + setting.Bucket + " path=" + model.Path)
  200. if strings.HasPrefix(model.Path, setting.Bucket+"/"+Model_prefix) {
  201. err := storage.ObsRemoveObject(setting.Bucket, model.Path[len(setting.Bucket)+1:])
  202. if err != nil {
  203. log.Info("Failed to delete model. id=" + id)
  204. return err
  205. }
  206. }
  207. err = models.DeleteModelById(id)
  208. if err == nil { //find a model to change new
  209. aimodels := models.QueryModelByName(model.Name, model.RepoId)
  210. if model.New == MODEL_LATEST {
  211. if len(aimodels) > 0 {
  212. //udpate status and version count
  213. models.ModifyModelNewProperty(aimodels[0].ID, MODEL_LATEST, len(aimodels))
  214. }
  215. } else {
  216. for _, tmpModel := range aimodels {
  217. if tmpModel.New == MODEL_LATEST {
  218. models.ModifyModelNewProperty(tmpModel.ID, MODEL_LATEST, len(aimodels))
  219. break
  220. }
  221. }
  222. }
  223. }
  224. }
  225. return err
  226. }
  227. func QueryModelByParameters(repoId int64, page int) ([]*models.AiModelManage, int64, error) {
  228. return models.QueryModel(&models.AiModelQueryOptions{
  229. ListOptions: models.ListOptions{
  230. Page: page,
  231. PageSize: setting.UI.IssuePagingNum,
  232. },
  233. RepoID: repoId,
  234. Type: -1,
  235. New: MODEL_LATEST,
  236. })
  237. }
  238. func DownloadMultiModelFile(ctx *context.Context) {
  239. log.Info("DownloadMultiModelFile start.")
  240. id := ctx.Query("ID")
  241. log.Info("id=" + id)
  242. task, err := models.QueryModelById(id)
  243. if err != nil {
  244. log.Error("no such model!", err.Error())
  245. ctx.ServerError("no such model:", err)
  246. return
  247. }
  248. if !isCanDeleteOrDownload(ctx, task) {
  249. ctx.ServerError("no right.", errors.New(ctx.Tr("repo.model_noright")))
  250. return
  251. }
  252. path := Model_prefix + models.AttachmentRelativePath(id) + "/"
  253. allFile, err := storage.GetAllObjectByBucketAndPrefix(setting.Bucket, path)
  254. if err == nil {
  255. //count++
  256. models.ModifyModelDownloadCount(id)
  257. returnFileName := task.Name + "_" + task.Version + ".zip"
  258. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+returnFileName)
  259. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  260. w := zip.NewWriter(ctx.Resp)
  261. defer w.Close()
  262. for _, oneFile := range allFile {
  263. if oneFile.IsDir {
  264. log.Info("zip dir name:" + oneFile.FileName)
  265. } else {
  266. log.Info("zip file name:" + oneFile.FileName)
  267. fDest, err := w.Create(oneFile.FileName)
  268. if err != nil {
  269. log.Info("create zip entry error, download file failed: %s\n", err.Error())
  270. ctx.ServerError("download file failed:", err)
  271. return
  272. }
  273. body, err := storage.ObsDownloadAFile(setting.Bucket, path+oneFile.FileName)
  274. if err != nil {
  275. log.Info("download file failed: %s\n", err.Error())
  276. ctx.ServerError("download file failed:", err)
  277. return
  278. } else {
  279. defer body.Close()
  280. p := make([]byte, 1024)
  281. var readErr error
  282. var readCount int
  283. // 读取对象内容
  284. for {
  285. readCount, readErr = body.Read(p)
  286. if readCount > 0 {
  287. fDest.Write(p[:readCount])
  288. }
  289. if readErr != nil {
  290. break
  291. }
  292. }
  293. }
  294. }
  295. }
  296. } else {
  297. log.Info("error,msg=" + err.Error())
  298. ctx.ServerError("no file to download.", err)
  299. }
  300. }
  301. func QueryTrainJobVersionList(ctx *context.Context) {
  302. log.Info("query train job version list. start.")
  303. JobID := ctx.Query("JobID")
  304. VersionListTasks, count, err := models.QueryModelTrainJobVersionList(JobID)
  305. log.Info("query return count=" + fmt.Sprint(count))
  306. if err != nil {
  307. ctx.ServerError("QueryTrainJobList:", err)
  308. } else {
  309. ctx.JSON(200, VersionListTasks)
  310. }
  311. }
  312. func QueryTrainJobList(ctx *context.Context) {
  313. log.Info("query train job list. start.")
  314. repoId := ctx.QueryInt64("repoId")
  315. VersionListTasks, count, err := models.QueryModelTrainJobList(repoId)
  316. log.Info("query return count=" + fmt.Sprint(count))
  317. if err != nil {
  318. ctx.ServerError("QueryTrainJobList:", err)
  319. } else {
  320. ctx.JSON(200, VersionListTasks)
  321. }
  322. }
  323. func DownloadSingleModelFile(ctx *context.Context) {
  324. log.Info("DownloadSingleModelFile start.")
  325. id := ctx.Params(":ID")
  326. parentDir := ctx.Query("parentDir")
  327. fileName := ctx.Query("fileName")
  328. path := Model_prefix + models.AttachmentRelativePath(id) + "/" + parentDir + fileName
  329. if setting.PROXYURL != "" {
  330. body, err := storage.ObsDownloadAFile(setting.Bucket, path)
  331. if err != nil {
  332. log.Info("download error.")
  333. } else {
  334. //count++
  335. models.ModifyModelDownloadCount(id)
  336. defer body.Close()
  337. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+fileName)
  338. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  339. p := make([]byte, 1024)
  340. var readErr error
  341. var readCount int
  342. // 读取对象内容
  343. for {
  344. readCount, readErr = body.Read(p)
  345. if readCount > 0 {
  346. ctx.Resp.Write(p[:readCount])
  347. //fmt.Printf("%s", p[:readCount])
  348. }
  349. if readErr != nil {
  350. break
  351. }
  352. }
  353. }
  354. } else {
  355. url, err := storage.GetObsCreateSignedUrlByBucketAndKey(setting.Bucket, path)
  356. if err != nil {
  357. log.Error("GetObsCreateSignedUrl failed: %v", err.Error(), ctx.Data["msgID"])
  358. ctx.ServerError("GetObsCreateSignedUrl", err)
  359. return
  360. }
  361. //count++
  362. models.ModifyModelDownloadCount(id)
  363. http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently)
  364. }
  365. }
  366. func ShowModelInfo(ctx *context.Context) {
  367. ctx.Data["ID"] = ctx.Query("ID")
  368. ctx.Data["name"] = ctx.Query("name")
  369. ctx.Data["isModelManage"] = true
  370. ctx.HTML(200, tplModelInfo)
  371. }
  372. func ShowSingleModel(ctx *context.Context) {
  373. name := ctx.Query("name")
  374. log.Info("Show single ModelInfo start.name=" + name)
  375. models := models.QueryModelByName(name, ctx.Repo.Repository.ID)
  376. userIds := make([]int64, len(models))
  377. for i, model := range models {
  378. model.IsCanOper = isOper(ctx, model.UserId)
  379. userIds[i] = model.UserId
  380. }
  381. userNameMap := queryUserName(userIds)
  382. for _, model := range models {
  383. value := userNameMap[model.UserId]
  384. if value != nil {
  385. model.UserName = value.Name
  386. model.UserRelAvatarLink = value.RelAvatarLink()
  387. }
  388. }
  389. ctx.JSON(http.StatusOK, models)
  390. }
  391. func queryUserName(intSlice []int64) map[int64]*models.User {
  392. keys := make(map[int64]string)
  393. uniqueElements := []int64{}
  394. for _, entry := range intSlice {
  395. if _, value := keys[entry]; !value {
  396. keys[entry] = ""
  397. uniqueElements = append(uniqueElements, entry)
  398. }
  399. }
  400. result := make(map[int64]*models.User)
  401. userLists, err := models.GetUsersByIDs(uniqueElements)
  402. if err == nil {
  403. for _, user := range userLists {
  404. result[user.ID] = user
  405. }
  406. }
  407. return result
  408. }
  409. func ShowOneVersionOtherModel(ctx *context.Context) {
  410. repoId := ctx.Repo.Repository.ID
  411. name := ctx.Query("name")
  412. aimodels := models.QueryModelByName(name, repoId)
  413. userIds := make([]int64, len(aimodels))
  414. for i, model := range aimodels {
  415. model.IsCanOper = isOper(ctx, model.UserId)
  416. userIds[i] = model.UserId
  417. }
  418. userNameMap := queryUserName(userIds)
  419. for _, model := range aimodels {
  420. value := userNameMap[model.UserId]
  421. if value != nil {
  422. model.UserName = value.Name
  423. model.UserRelAvatarLink = value.RelAvatarLink()
  424. }
  425. }
  426. if len(aimodels) > 0 {
  427. ctx.JSON(200, aimodels[1:])
  428. } else {
  429. ctx.JSON(200, aimodels)
  430. }
  431. }
  432. func ShowModelTemplate(ctx *context.Context) {
  433. ctx.Data["isModelManage"] = true
  434. repoId := ctx.Repo.Repository.ID
  435. Type := -1
  436. _, count, _ := models.QueryModel(&models.AiModelQueryOptions{
  437. ListOptions: models.ListOptions{
  438. Page: 1,
  439. PageSize: 2,
  440. },
  441. RepoID: repoId,
  442. Type: Type,
  443. New: MODEL_LATEST,
  444. })
  445. ctx.Data["MODEL_COUNT"] = count
  446. _, trainCount, _ := models.QueryModelTrainJobList(repoId)
  447. log.Info("query train count=" + fmt.Sprint(trainCount))
  448. ctx.Data["TRAIN_COUNT"] = trainCount
  449. ctx.HTML(200, tplModelManageIndex)
  450. }
  451. func isQueryRight(ctx *context.Context) bool {
  452. if ctx.Repo.Repository.IsPrivate {
  453. if ctx.Repo.CanRead(models.UnitTypeModelManage) || ctx.User.IsAdmin || ctx.Repo.IsAdmin() || ctx.Repo.IsOwner() {
  454. return true
  455. }
  456. return false
  457. } else {
  458. return true
  459. }
  460. }
  461. func isOper(ctx *context.Context, modelUserId int64) bool {
  462. if ctx.User == nil {
  463. return false
  464. }
  465. if ctx.User.IsAdmin || ctx.Repo.IsOwner() || ctx.User.ID == modelUserId {
  466. return true
  467. }
  468. return false
  469. }
  470. func ShowModelPageInfo(ctx *context.Context) {
  471. log.Info("ShowModelInfo start.")
  472. if !isQueryRight(ctx) {
  473. ctx.ServerError("no right.", errors.New(ctx.Tr("repo.model_noright")))
  474. return
  475. }
  476. page := ctx.QueryInt("page")
  477. if page <= 0 {
  478. page = 1
  479. }
  480. pageSize := ctx.QueryInt("pageSize")
  481. if pageSize <= 0 {
  482. pageSize = setting.UI.IssuePagingNum
  483. }
  484. repoId := ctx.Repo.Repository.ID
  485. Type := -1
  486. modelResult, count, err := models.QueryModel(&models.AiModelQueryOptions{
  487. ListOptions: models.ListOptions{
  488. Page: page,
  489. PageSize: pageSize,
  490. },
  491. RepoID: repoId,
  492. Type: Type,
  493. New: MODEL_LATEST,
  494. })
  495. if err != nil {
  496. ctx.ServerError("Cloudbrain", err)
  497. return
  498. }
  499. userIds := make([]int64, len(modelResult))
  500. for i, model := range modelResult {
  501. model.IsCanOper = isOper(ctx, model.UserId)
  502. userIds[i] = model.UserId
  503. }
  504. userNameMap := queryUserName(userIds)
  505. for _, model := range modelResult {
  506. value := userNameMap[model.UserId]
  507. if value != nil {
  508. model.UserName = value.Name
  509. model.UserRelAvatarLink = value.RelAvatarLink()
  510. }
  511. }
  512. mapInterface := make(map[string]interface{})
  513. mapInterface["data"] = modelResult
  514. mapInterface["count"] = count
  515. ctx.JSON(http.StatusOK, mapInterface)
  516. }
  517. func ModifyModel(id string, description string) error {
  518. err := models.ModifyModelDescription(id, description)
  519. if err == nil {
  520. log.Info("modify success.")
  521. } else {
  522. log.Info("Failed to modify.id=" + id + " desc=" + description + " error:" + err.Error())
  523. }
  524. return err
  525. }
  526. func ModifyModelInfo(ctx *context.Context) {
  527. log.Info("modify model start.")
  528. id := ctx.Query("ID")
  529. description := ctx.Query("Description")
  530. task, err := models.QueryModelById(id)
  531. if err != nil {
  532. log.Error("no such model!", err.Error())
  533. ctx.ServerError("no such model:", err)
  534. return
  535. }
  536. if !isCanDeleteOrDownload(ctx, task) {
  537. ctx.ServerError("no right.", errors.New(ctx.Tr("repo.model_noright")))
  538. return
  539. }
  540. err = ModifyModel(id, description)
  541. if err != nil {
  542. log.Info("modify error," + err.Error())
  543. ctx.ServerError("error.", err)
  544. } else {
  545. ctx.JSON(200, "success")
  546. }
  547. }
  548. func QueryModelListForPredict(ctx *context.Context) {
  549. repoId := ctx.Repo.Repository.ID
  550. modelResult, count, err := models.QueryModel(&models.AiModelQueryOptions{
  551. ListOptions: models.ListOptions{
  552. Page: -1,
  553. PageSize: -1,
  554. },
  555. RepoID: repoId,
  556. Type: -1,
  557. New: -1,
  558. })
  559. if err != nil {
  560. ctx.ServerError("Cloudbrain", err)
  561. return
  562. }
  563. log.Info("query return count=" + fmt.Sprint(count))
  564. nameList := make([]string, 0)
  565. nameMap := make(map[string][]*models.AiModelManage)
  566. for _, model := range modelResult {
  567. if _, value := nameMap[model.Name]; !value {
  568. models := make([]*models.AiModelManage, 0)
  569. models = append(models, model)
  570. nameMap[model.Name] = models
  571. nameList = append(nameList, model.Name)
  572. } else {
  573. nameMap[model.Name] = append(nameMap[model.Name], model)
  574. }
  575. }
  576. mapInterface := make(map[string]interface{})
  577. mapInterface["nameList"] = nameList
  578. mapInterface["nameMap"] = nameMap
  579. ctx.JSON(http.StatusOK, mapInterface)
  580. }
  581. func QueryModelFileForPredict(ctx *context.Context) {
  582. id := ctx.Query("ID")
  583. model, err := models.QueryModelById(id)
  584. if err != nil {
  585. log.Error("no such model!", err.Error())
  586. ctx.ServerError("no such model:", err)
  587. return
  588. }
  589. prefix := model.Path[len(setting.Bucket)+1:]
  590. fileinfos, err := storage.GetAllObjectByBucketAndPrefix(setting.Bucket, prefix)
  591. ctx.JSON(http.StatusOK, fileinfos)
  592. }
  593. func QueryOneLevelModelFile(ctx *context.Context) {
  594. id := ctx.Query("ID")
  595. parentDir := ctx.Query("parentDir")
  596. model, err := models.QueryModelById(id)
  597. if err != nil {
  598. log.Error("no such model!", err.Error())
  599. ctx.ServerError("no such model:", err)
  600. return
  601. }
  602. prefix := model.Path[len(setting.Bucket)+1:]
  603. fileinfos, err := storage.GetOneLevelAllObjectUnderDir(setting.Bucket, prefix, parentDir)
  604. ctx.JSON(http.StatusOK, fileinfos)
  605. }