mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Move base64_image related functions to common directory (#10957)
### What problem does this PR solve? As title ### Type of change - [x] Refactoring --------- Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@ -24,7 +24,7 @@ from api.utils.api_utils import server_error_response, get_data_error_result, va
|
|||||||
from api.db import StatusEnum, LLMType
|
from api.db import StatusEnum, LLMType
|
||||||
from api.db.db_models import TenantLLM
|
from api.db.db_models import TenantLLM
|
||||||
from api.utils.api_utils import get_json_result
|
from api.utils.api_utils import get_json_result
|
||||||
from api.utils.base64_image import test_image
|
from common.base64_image import test_image
|
||||||
from rag.llm import EmbeddingModel, ChatModel, RerankModel, CvModel, TTSModel
|
from rag.llm import EmbeddingModel, ChatModel, RerankModel, CvModel, TTSModel
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,56 +1,15 @@
|
|||||||
import base64
|
#
|
||||||
import logging
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||||
from functools import partial
|
#
|
||||||
from io import BytesIO
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
from PIL import Image
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
test_image_base64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAA6ElEQVR4nO3QwQ3AIBDAsIP9d25XIC+EZE8QZc18w5l9O+AlZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBT+IYAHHLHkdEgAAAABJRU5ErkJggg=="
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
test_image = base64.b64decode(test_image_base64)
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
async def image2id(d: dict, storage_put_func: partial, objname:str, bucket:str="imagetemps"):
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
import logging
|
# See the License for the specific language governing permissions and
|
||||||
from io import BytesIO
|
# limitations under the License.
|
||||||
import trio
|
#
|
||||||
from rag.svr.task_executor import minio_limiter
|
|
||||||
if not d.get("image"):
|
|
||||||
return
|
|
||||||
|
|
||||||
with BytesIO() as output_buffer:
|
|
||||||
if isinstance(d["image"], bytes):
|
|
||||||
output_buffer.write(d["image"])
|
|
||||||
output_buffer.seek(0)
|
|
||||||
else:
|
|
||||||
# If the image is in RGBA mode, convert it to RGB mode before saving it in JPEG format.
|
|
||||||
if d["image"].mode in ("RGBA", "P"):
|
|
||||||
converted_image = d["image"].convert("RGB")
|
|
||||||
d["image"] = converted_image
|
|
||||||
try:
|
|
||||||
d["image"].save(output_buffer, format='JPEG')
|
|
||||||
except OSError as e:
|
|
||||||
logging.warning(
|
|
||||||
"Saving image exception, ignore: {}".format(str(e)))
|
|
||||||
|
|
||||||
async with minio_limiter:
|
|
||||||
await trio.to_thread.run_sync(lambda: storage_put_func(bucket=bucket, fnm=objname, binary=output_buffer.getvalue()))
|
|
||||||
d["img_id"] = f"{bucket}-{objname}"
|
|
||||||
if not isinstance(d["image"], bytes):
|
|
||||||
d["image"].close()
|
|
||||||
del d["image"] # Remove image reference
|
|
||||||
|
|
||||||
|
|
||||||
def id2image(image_id:str|None, storage_get_func: partial):
|
|
||||||
if not image_id:
|
|
||||||
return
|
|
||||||
arr = image_id.split("-")
|
|
||||||
if len(arr) != 2:
|
|
||||||
return
|
|
||||||
bkt, nm = image_id.split("-")
|
|
||||||
try:
|
|
||||||
blob = storage_get_func(bucket=bkt, filename=nm)
|
|
||||||
if not blob:
|
|
||||||
return
|
|
||||||
return Image.open(BytesIO(blob))
|
|
||||||
except Exception as e:
|
|
||||||
logging.exception(e)
|
|
||||||
|
|||||||
@ -49,8 +49,6 @@ from api.constants import IMG_BASE64_PREFIX
|
|||||||
from api.db import FileType
|
from api.db import FileType
|
||||||
from common.file_utils import get_project_base_directory
|
from common.file_utils import get_project_base_directory
|
||||||
|
|
||||||
PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
|
|
||||||
|
|
||||||
LOCK_KEY_pdfplumber = "global_shared_lock_pdfplumber"
|
LOCK_KEY_pdfplumber = "global_shared_lock_pdfplumber"
|
||||||
if LOCK_KEY_pdfplumber not in sys.modules:
|
if LOCK_KEY_pdfplumber not in sys.modules:
|
||||||
sys.modules[LOCK_KEY_pdfplumber] = threading.Lock()
|
sys.modules[LOCK_KEY_pdfplumber] = threading.Lock()
|
||||||
|
|||||||
72
common/base64_image.py
Normal file
72
common/base64_image.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#
|
||||||
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import logging
|
||||||
|
from functools import partial
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
test_image_base64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAA6ElEQVR4nO3QwQ3AIBDAsIP9d25XIC+EZE8QZc18w5l9O+AlZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBWYFZgVmBT+IYAHHLHkdEgAAAABJRU5ErkJggg=="
|
||||||
|
test_image = base64.b64decode(test_image_base64)
|
||||||
|
|
||||||
|
|
||||||
|
async def image2id(d: dict, storage_put_func: partial, objname:str, bucket:str="imagetemps"):
|
||||||
|
import logging
|
||||||
|
from io import BytesIO
|
||||||
|
import trio
|
||||||
|
from rag.svr.task_executor import minio_limiter
|
||||||
|
if not d.get("image"):
|
||||||
|
return
|
||||||
|
|
||||||
|
with BytesIO() as output_buffer:
|
||||||
|
if isinstance(d["image"], bytes):
|
||||||
|
output_buffer.write(d["image"])
|
||||||
|
output_buffer.seek(0)
|
||||||
|
else:
|
||||||
|
# If the image is in RGBA mode, convert it to RGB mode before saving it in JPEG format.
|
||||||
|
if d["image"].mode in ("RGBA", "P"):
|
||||||
|
converted_image = d["image"].convert("RGB")
|
||||||
|
d["image"] = converted_image
|
||||||
|
try:
|
||||||
|
d["image"].save(output_buffer, format='JPEG')
|
||||||
|
except OSError as e:
|
||||||
|
logging.warning(
|
||||||
|
"Saving image exception, ignore: {}".format(str(e)))
|
||||||
|
|
||||||
|
async with minio_limiter:
|
||||||
|
await trio.to_thread.run_sync(lambda: storage_put_func(bucket=bucket, fnm=objname, binary=output_buffer.getvalue()))
|
||||||
|
d["img_id"] = f"{bucket}-{objname}"
|
||||||
|
if not isinstance(d["image"], bytes):
|
||||||
|
d["image"].close()
|
||||||
|
del d["image"] # Remove image reference
|
||||||
|
|
||||||
|
|
||||||
|
def id2image(image_id:str|None, storage_get_func: partial):
|
||||||
|
if not image_id:
|
||||||
|
return
|
||||||
|
arr = image_id.split("-")
|
||||||
|
if len(arr) != 2:
|
||||||
|
return
|
||||||
|
bkt, nm = image_id.split("-")
|
||||||
|
try:
|
||||||
|
blob = storage_get_func(bucket=bkt, filename=nm)
|
||||||
|
if not blob:
|
||||||
|
return
|
||||||
|
return Image.open(BytesIO(blob))
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception(e)
|
||||||
@ -21,7 +21,7 @@ from functools import partial
|
|||||||
import trio
|
import trio
|
||||||
|
|
||||||
from common.misc_utils import get_uuid
|
from common.misc_utils import get_uuid
|
||||||
from api.utils.base64_image import id2image, image2id
|
from common.base64_image import id2image, image2id
|
||||||
from deepdoc.parser.pdf_parser import RAGFlowPdfParser
|
from deepdoc.parser.pdf_parser import RAGFlowPdfParser
|
||||||
from rag.flow.base import ProcessBase, ProcessParamBase
|
from rag.flow.base import ProcessBase, ProcessParamBase
|
||||||
from rag.flow.hierarchical_merger.schema import HierarchicalMergerFromUpstream
|
from rag.flow.hierarchical_merger.schema import HierarchicalMergerFromUpstream
|
||||||
|
|||||||
@ -27,7 +27,7 @@ from api.db.services.file2document_service import File2DocumentService
|
|||||||
from api.db.services.file_service import FileService
|
from api.db.services.file_service import FileService
|
||||||
from api.db.services.llm_service import LLMBundle
|
from api.db.services.llm_service import LLMBundle
|
||||||
from common.misc_utils import get_uuid
|
from common.misc_utils import get_uuid
|
||||||
from api.utils.base64_image import image2id
|
from common.base64_image import image2id
|
||||||
from deepdoc.parser import ExcelParser
|
from deepdoc.parser import ExcelParser
|
||||||
from deepdoc.parser.mineru_parser import MinerUParser
|
from deepdoc.parser.mineru_parser import MinerUParser
|
||||||
from deepdoc.parser.pdf_parser import PlainParser, RAGFlowPdfParser, VisionParser
|
from deepdoc.parser.pdf_parser import PlainParser, RAGFlowPdfParser, VisionParser
|
||||||
|
|||||||
@ -18,7 +18,7 @@ from functools import partial
|
|||||||
import trio
|
import trio
|
||||||
|
|
||||||
from common.misc_utils import get_uuid
|
from common.misc_utils import get_uuid
|
||||||
from api.utils.base64_image import id2image, image2id
|
from common.base64_image import id2image, image2id
|
||||||
from deepdoc.parser.pdf_parser import RAGFlowPdfParser
|
from deepdoc.parser.pdf_parser import RAGFlowPdfParser
|
||||||
from rag.flow.base import ProcessBase, ProcessParamBase
|
from rag.flow.base import ProcessBase, ProcessParamBase
|
||||||
from rag.flow.splitter.schema import SplitterFromUpstream
|
from rag.flow.splitter.schema import SplitterFromUpstream
|
||||||
|
|||||||
@ -27,7 +27,7 @@ from api.db.services.canvas_service import UserCanvasService
|
|||||||
from api.db.services.knowledgebase_service import KnowledgebaseService
|
from api.db.services.knowledgebase_service import KnowledgebaseService
|
||||||
from api.db.services.pipeline_operation_log_service import PipelineOperationLogService
|
from api.db.services.pipeline_operation_log_service import PipelineOperationLogService
|
||||||
from api.utils.api_utils import timeout
|
from api.utils.api_utils import timeout
|
||||||
from api.utils.base64_image import image2id
|
from common.base64_image import image2id
|
||||||
from api.utils.log_utils import init_root_logger
|
from api.utils.log_utils import init_root_logger
|
||||||
from common.file_utils import get_project_base_directory
|
from common.file_utils import get_project_base_directory
|
||||||
from api.utils.configs import show_configs
|
from api.utils.configs import show_configs
|
||||||
|
|||||||
Reference in New Issue
Block a user