| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\admin\controller\ps;
- use app\common\controller\Backend;
- use app\common\library\ps\AiRecognizer;
- use app\common\library\ps\LlmClient;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use Exception;
- /**
- * 流程单-AI识版任务
- *
- * @icon fa fa-magic
- */
- class AiJob extends Backend
- {
- /**
- * Ai_job模型对象
- * @var \app\admin\model\ps\Ai_job
- */
- protected $model = null;
- protected $relationSearch = true;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\ps\Ai_job;
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("fileTypeList", $this->model->getFileTypeList());
- $client = new LlmClient();
- $this->view->assign('modelOptions', $client->formOptions());
- $this->view->assign('defaultVisionModel', LlmClient::forRecognize()->defaultModel('image'));
- $this->view->assign('defaultTextModel', LlmClient::local()->defaultModel('pdf'));
- $this->view->assign('defaultModel', LlmClient::forRecognize()->defaultModel('image'));
- }
- /**
- * 查看
- */
- public function index()
- {
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['factory'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if (false === $this->request->isPost()) {
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->fillAiJobDefaults($this->preExcludeFields($params));
- $result = false;
- Db::startTrans();
- try {
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were inserted'));
- }
- AiRecognizer::dispatch((int)$this->model->id);
- $this->success(__('Job_submitted'));
- }
- /**
- * 开始识别
- * @param string|null $ids
- */
- public function run($ids = null)
- {
- $ids = $ids ?: $this->request->param('ids');
- $idList = array_values(array_filter(array_map('intval', explode(',', (string)$ids))));
- if (!$idList) {
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- if (count($idList) === 1) {
- ignore_user_abort(true);
- set_time_limit(360);
- try {
- $job = AiRecognizer::run($idList[0]);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- if (($job['status'] ?? '') === 'fail') {
- $this->error($job['error_msg'] ?: __('Recognize_failed'));
- }
- $msg = __('Recognize_success');
- if (!empty($job['template_id'])) {
- $msg .= ',' . __('Template_id') . ' #' . $job['template_id'];
- }
- $this->success($msg);
- }
- foreach ($idList as $id) {
- AiRecognizer::dispatch($id);
- }
- $this->success(__('Job_queued', [count($idList)]));
- }
- /**
- * @param array $row
- * @return array
- */
- protected function fillAiJobDefaults(array $row)
- {
- $fileUrl = isset($row['file_url']) ? $row['file_url'] : '';
- $fileType = isset($row['file_type']) ? $row['file_type'] : '';
- if ($fileType === '' && $fileUrl !== '') {
- $fileType = preg_match('/\.pdf$/i', $fileUrl) ? 'pdf' : 'image';
- }
- if ($fileType === '') {
- $fileType = 'image';
- }
- $row['file_type'] = $fileType;
- if (empty($row['model'])) {
- $row['model'] = LlmClient::forRecognize()->defaultModel($fileType);
- }
- if (empty($row['status'])) {
- $row['status'] = 'pending';
- }
- if (!isset($row['user_id']) || $row['user_id'] === '') {
- $row['user_id'] = 0;
- }
- if (!isset($row['template_id']) || $row['template_id'] === '') {
- $row['template_id'] = 0;
- }
- if (!isset($row['error_msg'])) {
- $row['error_msg'] = '';
- }
- return $row;
- }
- }
|