diff --git a/wmyun-module-system/wmyun-module-system-biz/pom.xml b/wmyun-module-system/wmyun-module-system-biz/pom.xml index 8510843..c867be6 100644 --- a/wmyun-module-system/wmyun-module-system-biz/pom.xml +++ b/wmyun-module-system/wmyun-module-system-biz/pom.xml @@ -41,6 +41,23 @@ pdfbox 2.0.29 + + + + + + + + com.volcengine + volcengine-java-sdk-ark-runtime + LATEST + + + io.swagger.core.v3 + swagger-annotations + + + com.wmyun @@ -103,10 +120,10 @@ - - - - + + + + diff --git a/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/controller/admin/forward/OCRController.java b/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/controller/admin/forward/OCRController.java new file mode 100644 index 0000000..6559c9c --- /dev/null +++ b/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/controller/admin/forward/OCRController.java @@ -0,0 +1,106 @@ +package com.wmyun.module.system.controller.admin.forward; + +import com.volcengine.ark.runtime.exception.ArkHttpException; +import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart; +import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest; +import com.volcengine.ark.runtime.model.completion.chat.ChatMessage; +import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole; +import com.volcengine.ark.runtime.service.ArkService; +import com.wmyun.framework.common.pojo.CommonResult; +import com.wmyun.module.system.controller.admin.forward.vo.OCRReqVO; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import jodd.util.StringUtil; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import static com.wmyun.framework.common.pojo.CommonResult.error; +import static com.wmyun.framework.common.pojo.CommonResult.success; + +/** + * @author zhouc + * @date 2025/3/3 21:01 + * @className OCRController + * @description + */ +@Tag(name = "管理后台 -OCR识别") +@RestController +@RequestMapping("/system/ocr") +@Validated +@Transactional +public class OCRController { + private static final String API_KEY = "83e8d382-1f6f-47f3-aa8e-1ea1f135ca2b"; + private static final String DEEPSEEK_OCR_MODEL = "ep-20250224214646-xxb4g"; + private static final String baseUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"; + + @PostMapping(value = "/OCRinterface", consumes = "multipart/form-data") + @Operation(summary = "OCR识别") + @PreAuthorize("@ss.hasPermission('system:forward:OCRinterface')") + public CommonResult OCRinterface(@RequestParam("file") MultipartFile file, + @RequestParam("mag") String mag) { + try { + String imageurl = ""; + // 校验文件非空 + if (file.isEmpty()) { + throw new IllegalArgumentException("文件为空"); + } + String contentType = file.getContentType(); + // 读取字节流并编码 + try (InputStream inputStream = file.getInputStream()) { + byte[] bytes = inputStream.readAllBytes(); + imageurl = Base64.getEncoder().encodeToString(bytes); + } + imageurl = "data:" + contentType + ";base64," + imageurl; + Object getmsg = getmsg(mag, imageurl); + return success(getmsg.toString()); + } catch (Exception exception) { + String message = exception.getMessage(); + return error(411, message); + } + } + + public Object getmsg(String mag, String imageurl) { + AtomicReference msg = new AtomicReference<>(""); + try { + ArkService service = ArkService.builder().apiKey(API_KEY).baseUrl(baseUrl).build(); +// ArkService service = new ArkService(ARK_API_KEY); + System.out.println("----- image input -----"); + final List messages = new ArrayList<>(); + final List multiParts = new ArrayList<>(); + multiParts.add(ChatCompletionContentPart.builder().type("text").text(mag).build()); + multiParts.add(ChatCompletionContentPart.builder().type("image_url").imageUrl( + new ChatCompletionContentPart.ChatCompletionContentPartImageURL(imageurl) + ).build()); + final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER) + .multiContent(multiParts).build(); + messages.add(userMessage); + + ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() + .model(DEEPSEEK_OCR_MODEL) + .messages(messages) + .build(); + + service.createChatCompletion(chatCompletionRequest).getChoices().forEach( + choice -> + msg.set(choice.getMessage().getContent())); + + service.shutdownExecutor(); + } catch (ArkHttpException e) { + System.out.print(e.toString()); + } + return msg; + } + + +} diff --git a/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/controller/admin/forward/vo/OCRReqVO.java b/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/controller/admin/forward/vo/OCRReqVO.java new file mode 100644 index 0000000..3cb1223 --- /dev/null +++ b/wmyun-module-system/wmyun-module-system-biz/src/main/java/com/wmyun/module/system/controller/admin/forward/vo/OCRReqVO.java @@ -0,0 +1,24 @@ +package com.wmyun.module.system.controller.admin.forward.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.web.multipart.MultipartFile; + +/** + * @author zhouc + * @date 2025/3/4 11:41 + * @className OCRReqVO + * @description + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Schema(description = "管理后台 - OCR Request VO") +public class OCRReqVO { + @Schema(description = "文件", example = "https://XXXXXX/login") + private MultipartFile file; + @Schema(description = "用户消息", example = "今天天气怎么样!") + private String mag; +}