mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-03 11:19:10 +08:00
feat(ocr): 添加OCR功能支持及文件类型校验
添加OCR功能钩子useOcr,支持图片文件识别 添加不支持文件类型的错误提示国际化文案
This commit is contained in:
parent
523ce9b7dc
commit
37d9ff59cc
39
src/renderer/src/hooks/useOcr.ts
Normal file
39
src/renderer/src/hooks/useOcr.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { useAppSelector } from '@renderer/store'
|
||||||
|
import { ImageFileMetadata, isImageFile } from '@renderer/types'
|
||||||
|
import { SupportedOcrFile } from '@renderer/types/ocr'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
|
export const useImageOcr = () => {}
|
||||||
|
|
||||||
|
export const useOcr = () => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
const imageProvider = useAppSelector((state) => state.ocr.imageProvider)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对图片文件进行OCR识别
|
||||||
|
* @param image 图片文件元数据
|
||||||
|
* @returns OCR识别结果的Promise
|
||||||
|
*/
|
||||||
|
const ocrImage = async (image: ImageFileMetadata) => {
|
||||||
|
return window.api.ocr.ocr(image, imageProvider)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对支持的文件进行OCR识别
|
||||||
|
* @param file 支持OCR的文件
|
||||||
|
* @returns OCR识别结果的Promise
|
||||||
|
* @throws 当文件类型不支持时抛出错误
|
||||||
|
*/
|
||||||
|
const ocr = async (file: SupportedOcrFile) => {
|
||||||
|
if (isImageFile(file)) {
|
||||||
|
return ocrImage(file)
|
||||||
|
}
|
||||||
|
// @ts-expect-error all types should be covered
|
||||||
|
throw new Error(t('ocr.file.not_supported', { type: file.type }))
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
ocrImage,
|
||||||
|
ocr
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1558,6 +1558,11 @@
|
|||||||
},
|
},
|
||||||
"tip": "如果响应成功,则只针对超过30秒的消息进行提醒"
|
"tip": "如果响应成功,则只针对超过30秒的消息进行提醒"
|
||||||
},
|
},
|
||||||
|
"ocr": {
|
||||||
|
"file": {
|
||||||
|
"not_supported": "不支持的文件类型 {{type}}"
|
||||||
|
}
|
||||||
|
},
|
||||||
"ollama": {
|
"ollama": {
|
||||||
"keep_alive_time": {
|
"keep_alive_time": {
|
||||||
"description": "对话后模型在内存中保持的时间(默认:5 分钟)",
|
"description": "对话后模型在内存中保持的时间(默认:5 分钟)",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user