UnisKB/apps/system_manage/parsers/__init__.py

39 lines
1.7 KiB
Python

"""Stable application boundary for independently deployed parser engines."""
from pathlib import Path
MINERU_INPUTS = frozenset({'pdf', 'png', 'jpg', 'jpeg', 'webp', 'bmp', 'tiff', 'gif', 'jp2'})
NATIVE_INPUTS = frozenset({'docx', 'doc', 'xls', 'xlsx', 'csv', 'html', 'htm', 'md', 'txt'})
def extension(name):
return Path(name).suffix.lower().lstrip('.')
def parse(parser, name, content, options, headers, progress_callback=None):
from rest_framework.exceptions import ValidationError
kind = extension(name)
if kind in NATIVE_INPUTS:
from .native import parse as native_parse
if progress_callback:
progress_callback({'stage': '使用内置处理器解析文档', 'percent': None})
return native_parse(name, content)
protocol = options.get('protocol', 'legacy')
if protocol == 'legacy':
if parser.endpoint.rstrip('/').endswith('/v1'):
raise ValidationError('原版接口地址不能以 /v1 结尾,请修改解析服务配置')
if kind not in MINERU_INPUTS:
raise ValidationError('当前解析服务不支持该文件格式')
from .legacy import parse as engine_parse
elif protocol == 'v1':
from system_manage.ai_hub_parser_v1 import parse as engine_parse
else:
raise ValidationError('不支持的解析接口类型')
result = engine_parse(parser, name, content, options, headers, progress_callback=progress_callback)
result.setdefault('source', {})
result['source'] = {'engine': 'mineru', 'protocol': protocol,
'configured_version': options.get('engine_version', ''), **result['source']}
return result
__all__ = ['parse', 'extension', 'MINERU_INPUTS', 'NATIVE_INPUTS']