xiatian 3 ماه پیش
والد
کامیت
cbac59b35b

+ 27 - 17
src/main/java/cn/com/qmth/am/bean/OcrDto.java

@@ -1,35 +1,45 @@
 package cn.com.qmth.am.bean;
 
-import java.io.File;
-
-import com.qmth.boot.core.solar.model.OrgInfo;
+import cn.com.qmth.am.entity.QuestionEntity;
+import cn.com.qmth.am.entity.StudentScoreEntity;
 
 public class OcrDto {
 
-    private File file;
+    private StudentScoreEntity score;
+
+    private QuestionEntity quetion;
 
-    private OrgInfo org;
+    private OcrServer ocrServer;
 
-    public File getFile() {
-        return file;
+    public OcrDto(StudentScoreEntity score, QuestionEntity quetion, OcrServer ocrServer) {
+        super();
+        this.score = score;
+        this.quetion = quetion;
+        this.ocrServer = ocrServer;
     }
 
-    public void setFile(File file) {
-        this.file = file;
+    public StudentScoreEntity getScore() {
+        return score;
     }
 
-    public OrgInfo getOrg() {
-        return org;
+    public void setScore(StudentScoreEntity score) {
+        this.score = score;
     }
 
-    public void setOrg(OrgInfo org) {
-        this.org = org;
+    public QuestionEntity getQuetion() {
+        return quetion;
     }
 
-    public OcrDto(File file, OrgInfo org) {
-        super();
-        this.file = file;
-        this.org = org;
+    public void setQuetion(QuestionEntity quetion) {
+        this.quetion = quetion;
+    }
+
+    public OcrServer getOcrServer() {
+        return ocrServer;
+    }
+
+    public void setOcrServer(OcrServer ocrServer) {
+        this.ocrServer = ocrServer;
     }
 
 }

+ 35 - 0
src/main/java/cn/com/qmth/am/bean/OcrServer.java

@@ -0,0 +1,35 @@
+package cn.com.qmth.am.bean;
+
+public class OcrServer {
+
+    private String model;
+
+    private String server;
+
+    private String key;
+
+    public String getModel() {
+        return model;
+    }
+
+    public void setModel(String model) {
+        this.model = model;
+    }
+
+    public String getServer() {
+        return server;
+    }
+
+    public void setServer(String server) {
+        this.server = server;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+}

+ 10 - 0
src/main/java/cn/com/qmth/am/bean/StudentScoreImageDto.java

@@ -2,6 +2,8 @@ package cn.com.qmth.am.bean;
 
 public class StudentScoreImageDto {
 
+    private OcrServer ocrServer;
+
     private Long studentScoreId;
 
     private byte[] image;
@@ -42,4 +44,12 @@ public class StudentScoreImageDto {
         this.suff = suff;
     }
 
+    public OcrServer getOcrServer() {
+        return ocrServer;
+    }
+
+    public void setOcrServer(OcrServer ocrServer) {
+        this.ocrServer = ocrServer;
+    }
+
 }

+ 25 - 0
src/main/java/cn/com/qmth/am/config/OcrServerCfg.java

@@ -0,0 +1,25 @@
+package cn.com.qmth.am.config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+import cn.com.qmth.am.bean.OcrServer;
+
+@ConfigurationProperties(prefix = "am.ocr")
+@Component
+public class OcrServerCfg {
+
+    private List<OcrServer> ocrConfig = new ArrayList<>();
+
+    public List<OcrServer> getOcrConfig() {
+        return ocrConfig;
+    }
+
+    public void setOcrConfig(List<OcrServer> ocrConfig) {
+        this.ocrConfig = ocrConfig;
+    }
+
+}

+ 8 - 19
src/main/java/cn/com/qmth/am/consumer/OcrConsumer.java

@@ -6,8 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Service;
 
-import cn.com.qmth.am.entity.QuestionEntity;
-import cn.com.qmth.am.entity.StudentScoreEntity;
+import cn.com.qmth.am.bean.OcrDto;
 import cn.com.qmth.am.service.StudentService;
 
 @Scope("prototype")
@@ -16,17 +15,15 @@ public class OcrConsumer implements Runnable {
 
     private CountDownLatch endGate;
 
-    private StudentScoreEntity score;
-
-    private QuestionEntity quetion;
-
     @Autowired
     private StudentService studentService;
 
+    private OcrDto ocrDto;
+
     @Override
     public void run() {
         try {
-            studentService.buildImage(score, quetion);
+            studentService.buildImage(ocrDto);
         } finally {
             endGate.countDown();
         }
@@ -40,20 +37,12 @@ public class OcrConsumer implements Runnable {
         this.endGate = endGate;
     }
 
-    public StudentScoreEntity getScore() {
-        return score;
-    }
-
-    public void setScore(StudentScoreEntity score) {
-        this.score = score;
-    }
-
-    public QuestionEntity getQuetion() {
-        return quetion;
+    public OcrDto getOcrDto() {
+        return ocrDto;
     }
 
-    public void setQuetion(QuestionEntity quetion) {
-        this.quetion = quetion;
+    public void setOcrDto(OcrDto ocrDto) {
+        this.ocrDto = ocrDto;
     }
 
 }

+ 2 - 1
src/main/java/cn/com/qmth/am/service/DsMarkingService.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.am.service;
 
+import cn.com.qmth.am.bean.OcrServer;
 import cn.com.qmth.am.bean.ds.AutoScoreRequest;
 import cn.com.qmth.am.bean.ds.AutoScoreResult;
 import cn.com.qmth.am.entity.QuestionEntity;
@@ -11,6 +12,6 @@ public interface DsMarkingService {
 
     AutoScoreResult autoScore(AutoScoreRequest req, QuestionEntity q);
 
-    String ocr(String base64);
+    String ocr(OcrServer ocrServer, String base64);
 
 }

+ 2 - 1
src/main/java/cn/com/qmth/am/service/StudentScoreService.java

@@ -4,6 +4,7 @@ import java.util.List;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import cn.com.qmth.am.bean.OcrDto;
 import cn.com.qmth.am.bean.StudentScoreImageDto;
 import cn.com.qmth.am.bean.StudentScoreVo;
 import cn.com.qmth.am.entity.QuestionEntity;
@@ -17,7 +18,7 @@ public interface StudentScoreService extends IService<StudentScoreEntity> {
 
     void updateAnswerErr(Long id, String string);
 
-    void createSlice(StudentScoreEntity score, QuestionEntity q);
+    void createSlice(OcrDto ocrDto);
 
     void ocr(StudentScoreImageDto dto);
 

+ 3 - 3
src/main/java/cn/com/qmth/am/service/StudentService.java

@@ -1,8 +1,8 @@
 package cn.com.qmth.am.service;
 
+import cn.com.qmth.am.bean.OcrDto;
 import cn.com.qmth.am.bean.StudentScoreVo;
 import cn.com.qmth.am.entity.QuestionEntity;
-import cn.com.qmth.am.entity.StudentScoreEntity;
 
 /**
  * 类注释
@@ -11,9 +11,9 @@ public interface StudentService {
 
     void importStudent();
 
-    void buildImage(StudentScoreEntity score, QuestionEntity quetion);
+    void buildImage(OcrDto ocrDto);
 
-    void createSlice(StudentScoreEntity score, QuestionEntity quetion);
+    void createSlice(OcrDto ocrDto);
 
     void reset(Long examId, String subjectCode);
 

+ 7 - 6
src/main/java/cn/com/qmth/am/service/impl/DsMarkingServiceImpl.java

@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
 import com.alibaba.fastjson.JSONObject;
 import com.qmth.boot.core.retrofit.exception.RetrofitResponseError;
 
+import cn.com.qmth.am.bean.OcrServer;
 import cn.com.qmth.am.bean.ds.AutoScoreRequest;
 import cn.com.qmth.am.bean.ds.AutoScoreResult;
 import cn.com.qmth.am.bean.ds.ChatReq;
@@ -66,10 +67,10 @@ public class DsMarkingServiceImpl implements DsMarkingService {
     // }
     // }
     @Override
-    public String ocr(String base64) {
-        OcrReq dreq = new OcrReq(sysProperty.getOcrModel());
+    public String ocr(OcrServer ocrServer, String base64) {
+        OcrReq dreq = new OcrReq(ocrServer.getModel());
         dreq.addMsg(new OcrMessage(base64));
-        String res = ocr(dreq);
+        String res = ocr(ocrServer, dreq);
         ChatResult result = JSONObject.parseObject(res, ChatResult.class);
         String text = result.getChoices().stream().filter(choice -> choice.getMessage().getRole() == ChatRole.assistant)
                 .map(choice -> choice.getMessage().getContent()).findFirst().orElse("");
@@ -224,13 +225,13 @@ public class DsMarkingServiceImpl implements DsMarkingService {
     }
 
     @SuppressWarnings("deprecation")
-    private String ocr(ChatReq dreq) {
+    private String ocr(OcrServer ocrServer, ChatReq dreq) {
 
         Map<String, String> headers = new HashMap<>();
-        headers.put("Authorization", "Bearer " + sysProperty.getOcrKey());
+        headers.put("Authorization", "Bearer " + ocrServer.getKey());
         Response resp = null;
         try {
-            resp = OKHttpUtil.call(HttpMethod.POST, sysProperty.getOcrServer(), headers, JSONObject.toJSONString(dreq));
+            resp = OKHttpUtil.call(HttpMethod.POST, ocrServer.getServer(), headers, JSONObject.toJSONString(dreq));
             if (resp.code() != 200) {
                 throw new RetrofitResponseError(resp.code(), resp.body().string());
             } else {

+ 7 - 5
src/main/java/cn/com/qmth/am/service/impl/StudentScoreServiceImpl.java

@@ -36,6 +36,7 @@ import cn.com.qmth.am.bean.AiMarkingDto;
 import cn.com.qmth.am.bean.AnswerImageDto;
 import cn.com.qmth.am.bean.AutoScoreEnRequest;
 import cn.com.qmth.am.bean.ImageSlice;
+import cn.com.qmth.am.bean.OcrDto;
 import cn.com.qmth.am.bean.StudentScoreImageDto;
 import cn.com.qmth.am.bean.StudentScoreInfo;
 import cn.com.qmth.am.bean.StudentScoreVo;
@@ -131,12 +132,13 @@ public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, Studen
 
     @Transactional
     @Override
-    public void createSlice(StudentScoreEntity score, QuestionEntity q) {
+    public void createSlice(OcrDto ocrDto) {
         StudentScoreImageDto dto = new StudentScoreImageDto();
-        dto.setStudentScoreId(score.getId());
-        getSlice(score, q, dto);
+        dto.setStudentScoreId(ocrDto.getScore().getId());
+        dto.setOcrServer(ocrDto.getOcrServer());
+        getSlice(ocrDto.getScore(), ocrDto.getQuetion(), dto);
         ocr(dto);
-        saveSliceImage(q, score, dto.getImage());
+        saveSliceImage(ocrDto.getQuetion(), ocrDto.getScore(), dto.getImage());
     }
 
     private void getSlice(StudentScoreEntity score, QuestionEntity q, StudentScoreImageDto dto) {
@@ -339,7 +341,7 @@ public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, Studen
     private String ocrDispose(StudentScoreImageDto dto) {
         try {
             String base64 = FileUtil.byteToBase64(dto.getImage(), dto.getSuff());
-            String ret = dsMarkingService.ocr(base64);
+            String ret = dsMarkingService.ocr(dto.getOcrServer(), base64);
             return ret;
 
         } catch (Exception e) {

+ 11 - 10
src/main/java/cn/com/qmth/am/service/impl/StudentServiceImpl.java

@@ -14,9 +14,9 @@ import org.springframework.transaction.annotation.Transactional;
 
 import com.qmth.boot.core.exception.StatusException;
 
+import cn.com.qmth.am.bean.OcrDto;
 import cn.com.qmth.am.bean.StudentScoreVo;
 import cn.com.qmth.am.entity.QuestionEntity;
-import cn.com.qmth.am.entity.StudentScoreEntity;
 import cn.com.qmth.am.enums.DataStatus;
 import cn.com.qmth.am.service.QuestionService;
 import cn.com.qmth.am.service.StmmsService;
@@ -80,26 +80,27 @@ public class StudentServiceImpl implements StudentService {
     }
 
     @Override
-    public void buildImage(StudentScoreEntity score, QuestionEntity quetion) {
-        if (DataStatus.WAITING.equals(score.getAnswerStatus()) || DataStatus.FAILED.equals(score.getAnswerStatus())) {
-            studentService.createSlice(score, quetion);
+    public void buildImage(OcrDto ocrDto) {
+        if (DataStatus.WAITING.equals(ocrDto.getScore().getAnswerStatus())
+                || DataStatus.FAILED.equals(ocrDto.getScore().getAnswerStatus())) {
+            studentService.createSlice(ocrDto);
         }
     }
 
     @Override
-    public void createSlice(StudentScoreEntity score, QuestionEntity q) {
-        if (q == null) {
-            studentScoreService.updateAnswerErr(score.getId(), "未找到试题信息");
+    public void createSlice(OcrDto ocrDto) {
+        if (ocrDto.getQuetion() == null) {
+            studentScoreService.updateAnswerErr(ocrDto.getScore().getId(), "未找到试题信息");
             return;
         }
         try {
-            studentScoreService.createSlice(score, q);
+            studentScoreService.createSlice(ocrDto);
         } catch (Exception e) {
             if (e instanceof StatusException) {
-                studentScoreService.updateAnswerErr(score.getId(), e.getMessage());
+                studentScoreService.updateAnswerErr(ocrDto.getScore().getId(), e.getMessage());
             } else {
                 log.error("系统异常", e);
-                studentScoreService.updateAnswerErr(score.getId(), "系统异常");
+                studentScoreService.updateAnswerErr(ocrDto.getScore().getId(), "系统异常");
             }
         }
     }

+ 10 - 3
src/main/java/cn/com/qmth/am/task/OcrJob.java

@@ -18,6 +18,8 @@ import org.springframework.stereotype.Service;
 
 import com.qmth.boot.core.concurrent.service.ConcurrentService;
 
+import cn.com.qmth.am.bean.OcrDto;
+import cn.com.qmth.am.config.OcrServerCfg;
 import cn.com.qmth.am.config.SysProperty;
 import cn.com.qmth.am.consumer.OcrConsumer;
 import cn.com.qmth.am.entity.QuestionEntity;
@@ -42,6 +44,9 @@ public class OcrJob {
     @Autowired
     private SysProperty sysProperty;
 
+    @Autowired
+    private OcrServerCfg ocrServerCfg;
+
     private ExecutorService executor;
 
     @PostConstruct
@@ -64,7 +69,7 @@ public class OcrJob {
 
     @Scheduled(fixedDelay = 5 * 1000, initialDelay = 5 * 1000)
     public void doJob() {
-        if (!sysProperty.getOcrTaskEnable()) {
+        if (!sysProperty.getOcrTaskEnable() || CollectionUtils.isEmpty(ocrServerCfg.getOcrConfig())) {
             return;
         }
 
@@ -94,6 +99,7 @@ public class OcrJob {
         for (QuestionEntity q : qs) {
             qmap.put(q.getId(), q);
         }
+        int index = 0;
         CountDownLatch endGate = new CountDownLatch(scores.size());
         for (StudentScoreEntity score : scores) {
             if (!sysProperty.getOcrTaskEnable()) {
@@ -102,10 +108,11 @@ public class OcrJob {
             QuestionEntity q = qmap.get(score.getQuestionId());
             if (q != null) {
                 OcrConsumer com = SpringContextHolder.getBean(OcrConsumer.class);
-                com.setScore(score);
-                com.setQuetion(q);
+                OcrDto dto = new OcrDto(score, q, ocrServerCfg.getOcrConfig().get(index));
+                com.setOcrDto(dto);
                 com.setEndGate(endGate);
                 executor.execute(com);
+                index = (index + 1) % ocrServerCfg.getOcrConfig().size();
             }
         }
         try {