AiJob.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\admin\controller\ps;
  3. use app\common\controller\Backend;
  4. use app\common\library\ps\AiRecognizer;
  5. use app\common\library\ps\LlmClient;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use Exception;
  10. /**
  11. * 流程单-AI识版任务
  12. *
  13. * @icon fa fa-magic
  14. */
  15. class AiJob extends Backend
  16. {
  17. /**
  18. * Ai_job模型对象
  19. * @var \app\admin\model\ps\Ai_job
  20. */
  21. protected $model = null;
  22. protected $relationSearch = true;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\ps\Ai_job;
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. $this->view->assign("fileTypeList", $this->model->getFileTypeList());
  29. $client = new LlmClient();
  30. $this->view->assign('modelOptions', $client->formOptions());
  31. $this->view->assign('defaultVisionModel', LlmClient::forRecognize()->defaultModel('image'));
  32. $this->view->assign('defaultTextModel', LlmClient::local()->defaultModel('pdf'));
  33. $this->view->assign('defaultModel', LlmClient::forRecognize()->defaultModel('image'));
  34. }
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['factory'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 添加
  58. */
  59. public function add()
  60. {
  61. if (false === $this->request->isPost()) {
  62. return $this->view->fetch();
  63. }
  64. $params = $this->request->post('row/a');
  65. if (empty($params)) {
  66. $this->error(__('Parameter %s can not be empty', ''));
  67. }
  68. $params = $this->fillAiJobDefaults($this->preExcludeFields($params));
  69. $result = false;
  70. Db::startTrans();
  71. try {
  72. $result = $this->model->allowField(true)->save($params);
  73. Db::commit();
  74. } catch (ValidateException|PDOException|Exception $e) {
  75. Db::rollback();
  76. $this->error($e->getMessage());
  77. }
  78. if ($result === false) {
  79. $this->error(__('No rows were inserted'));
  80. }
  81. AiRecognizer::dispatch((int)$this->model->id);
  82. $this->success(__('Job_submitted'));
  83. }
  84. /**
  85. * 开始识别
  86. * @param string|null $ids
  87. */
  88. public function run($ids = null)
  89. {
  90. $ids = $ids ?: $this->request->param('ids');
  91. $idList = array_values(array_filter(array_map('intval', explode(',', (string)$ids))));
  92. if (!$idList) {
  93. $this->error(__('Parameter %s can not be empty', 'ids'));
  94. }
  95. if (count($idList) === 1) {
  96. ignore_user_abort(true);
  97. set_time_limit(360);
  98. try {
  99. $job = AiRecognizer::run($idList[0]);
  100. } catch (Exception $e) {
  101. $this->error($e->getMessage());
  102. }
  103. if (($job['status'] ?? '') === 'fail') {
  104. $this->error($job['error_msg'] ?: __('Recognize_failed'));
  105. }
  106. $msg = __('Recognize_success');
  107. if (!empty($job['template_id'])) {
  108. $msg .= ',' . __('Template_id') . ' #' . $job['template_id'];
  109. }
  110. $this->success($msg);
  111. }
  112. foreach ($idList as $id) {
  113. AiRecognizer::dispatch($id);
  114. }
  115. $this->success(__('Job_queued', [count($idList)]));
  116. }
  117. /**
  118. * @param array $row
  119. * @return array
  120. */
  121. protected function fillAiJobDefaults(array $row)
  122. {
  123. $fileUrl = isset($row['file_url']) ? $row['file_url'] : '';
  124. $fileType = isset($row['file_type']) ? $row['file_type'] : '';
  125. if ($fileType === '' && $fileUrl !== '') {
  126. $fileType = preg_match('/\.pdf$/i', $fileUrl) ? 'pdf' : 'image';
  127. }
  128. if ($fileType === '') {
  129. $fileType = 'image';
  130. }
  131. $row['file_type'] = $fileType;
  132. if (empty($row['model'])) {
  133. $row['model'] = LlmClient::forRecognize()->defaultModel($fileType);
  134. }
  135. if (empty($row['status'])) {
  136. $row['status'] = 'pending';
  137. }
  138. if (!isset($row['user_id']) || $row['user_id'] === '') {
  139. $row['user_id'] = 0;
  140. }
  141. if (!isset($row['template_id']) || $row['template_id'] === '') {
  142. $row['template_id'] = 0;
  143. }
  144. if (!isset($row['error_msg'])) {
  145. $row['error_msg'] = '';
  146. }
  147. return $row;
  148. }
  149. }