diff --git a/lazyllm/components/embedding/embed.py b/lazyllm/components/embedding/embed.py index d968cc51..bf7600a7 100644 --- a/lazyllm/components/embedding/embed.py +++ b/lazyllm/components/embedding/embed.py @@ -2,10 +2,7 @@ import json import lazyllm from lazyllm import LOG -from lazyllm.thirdparty import transformers as tf -from lazyllm.thirdparty import torch -from lazyllm.thirdparty import sentence_transformers -import numpy as np +from lazyllm.thirdparty import transformers as tf, torch, sentence_transformers, numpy as np class LazyHuggingFaceEmbedding(object): diff --git a/lazyllm/components/stable_diffusion/stable_diffusion3.py b/lazyllm/components/stable_diffusion/stable_diffusion3.py index ffbbe5d1..a360adb3 100644 --- a/lazyllm/components/stable_diffusion/stable_diffusion3.py +++ b/lazyllm/components/stable_diffusion/stable_diffusion3.py @@ -1,8 +1,8 @@ import os import base64 import uuid -from PIL import Image -import numpy as np +from lazyllm.thirdparty import PIL +from lazyllm.thirdparty import numpy as np from io import BytesIO import lazyllm @@ -31,12 +31,12 @@ def load_sd(self): @staticmethod def image_to_base64(image): - if isinstance(image, Image.Image): + if isinstance(image, PIL.Image.Image): buffered = BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") elif isinstance(image, np.ndarray): - image = Image.fromarray(image) + image = PIL.Image.fromarray(image) buffered = BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") @@ -50,10 +50,10 @@ def images_to_base64(images): @staticmethod def image_to_file(image, file_path): - if isinstance(image, Image.Image): + if isinstance(image, PIL.Image.Image): image.save(file_path, format="PNG") elif isinstance(image, np.ndarray): - image = Image.fromarray(image) + image = PIL.Image.fromarray(image) image.save(file_path, format="PNG") else: raise ValueError("Unsupported image type") diff --git a/lazyllm/components/text_to_speech/utils.py b/lazyllm/components/text_to_speech/utils.py index f08f6d00..83509c6c 100644 --- a/lazyllm/components/text_to_speech/utils.py +++ b/lazyllm/components/text_to_speech/utils.py @@ -4,7 +4,7 @@ from ..utils.file_operate import delete_old_files -def sound_to_file(sound: np.array, file_path: str, sample_rate: int = 24000) -> str: +def sound_to_file(sound: 'np.array', file_path: str, sample_rate: int = 24000) -> str: scaled_audio = np.int16(sound / np.max(np.abs(sound)) * 32767) scipy.io.wavfile.write(file_path, sample_rate, scaled_audio) return [file_path] diff --git a/lazyllm/thirdparty/__init__.py b/lazyllm/thirdparty/__init__.py index 61c33ef7..dac0f62e 100644 --- a/lazyllm/thirdparty/__init__.py +++ b/lazyllm/thirdparty/__init__.py @@ -80,6 +80,6 @@ def __getattribute__(self, __name): modules = ['redis', 'huggingface_hub', 'jieba', 'modelscope', 'pandas', 'jwt', 'rank_bm25', 'redisvl', 'datasets', 'deepspeed', 'fire', 'numpy', 'peft', 'torch', 'transformers', 'collie', 'faiss', 'flash_attn', 'google', 'lightllm', 'vllm', 'ChatTTS', 'wandb', 'funasr', 'sklearn', 'torchvision', 'scipy', 'pymilvus', - 'sentence_transformers', 'gradio', 'chromadb'] + 'sentence_transformers', 'gradio', 'chromadb', 'nltk', 'PIL', 'httpx', 'bm25s'] for m in modules: vars()[m] = PackageWrapper(m) diff --git a/lazyllm/tools/rag/component/bm25.py b/lazyllm/tools/rag/component/bm25.py index 56881869..1485f4e0 100644 --- a/lazyllm/tools/rag/component/bm25.py +++ b/lazyllm/tools/rag/component/bm25.py @@ -1,8 +1,7 @@ from typing import List, Tuple from ..doc_node import DocNode -import bm25s import Stemmer -from lazyllm.thirdparty import jieba +from lazyllm.thirdparty import jieba, bm25s from .stopwords import STOPWORDS_CHINESE diff --git a/lazyllm/tools/rag/readers/imageReader.py b/lazyllm/tools/rag/readers/imageReader.py index fe05f57f..021e5c10 100644 --- a/lazyllm/tools/rag/readers/imageReader.py +++ b/lazyllm/tools/rag/readers/imageReader.py @@ -4,19 +4,19 @@ from pathlib import Path from typing import Dict, List, Optional, cast from fsspec import AbstractFileSystem -from PIL import Image +from lazyllm.thirdparty import PIL from .readerBase import LazyLLMReaderBase, infer_torch_device from ..doc_node import DocNode -def img_2_b64(image: Image, format: str = "JPEG") -> str: +def img_2_b64(image: 'PIL.Image', format: str = "JPEG") -> str: buff = BytesIO() image.save(buff, format=format) return cast(str, base64.b64encode(buff.getvalue())) -def b64_2_img(data: str) -> Image: +def b64_2_img(data: str) -> 'PIL.Image': buff = BytesIO(base64.b64decode(data)) - return Image.open(buff) + return 'PIL.Image'.open(buff) class ImageReader(LazyLLMReaderBase): def __init__(self, parser_config: Optional[Dict] = None, keep_image: bool = False, parse_text: bool = False, @@ -59,9 +59,9 @@ def _load_data(self, file: Path, extra_info: Optional[Dict] = None, if fs: with fs.open(path=file) as f: - image = Image.open(f.read()) + image = PIL.Image.open(f.read()) else: - image = Image.open(file) + image = PIL.Image.open(file) if image.mode != "RGB": image = image.convert("RGB") diff --git a/lazyllm/tools/rag/readers/pandasReader.py b/lazyllm/tools/rag/readers/pandasReader.py index e3ad327a..6082b6b4 100644 --- a/lazyllm/tools/rag/readers/pandasReader.py +++ b/lazyllm/tools/rag/readers/pandasReader.py @@ -2,7 +2,7 @@ from typing import Dict, List, Optional from fsspec import AbstractFileSystem import importlib -import pandas as pd +from lazyllm.thirdparty import pandas as pd from .readerBase import LazyLLMReaderBase from ..doc_node import DocNode diff --git a/lazyllm/tools/rag/similarity.py b/lazyllm/tools/rag/similarity.py index 89b9be0e..a0489c9d 100644 --- a/lazyllm/tools/rag/similarity.py +++ b/lazyllm/tools/rag/similarity.py @@ -1,6 +1,6 @@ from typing import Optional, Callable, Literal, List from .component.bm25 import BM25 -import numpy as np +from lazyllm.thirdparty import numpy as np from .doc_node import DocNode registered_similarities = dict() diff --git a/lazyllm/tools/rag/transform.py b/lazyllm/tools/rag/transform.py index 62b516a8..439e9615 100644 --- a/lazyllm/tools/rag/transform.py +++ b/lazyllm/tools/rag/transform.py @@ -9,7 +9,7 @@ import re from typing import Any, Callable, Dict, List, Tuple, Union, Optional from lazyllm.components import AlpacaPrompter -import nltk +from lazyllm.thirdparty import nltk import tiktoken from .doc_node import DocNode, MetadataMode diff --git a/lazyllm/tools/webpages/webmodule.py b/lazyllm/tools/webpages/webmodule.py index 62f7757a..82fe2aa4 100644 --- a/lazyllm/tools/webpages/webmodule.py +++ b/lazyllm/tools/webpages/webmodule.py @@ -6,9 +6,8 @@ import sys import requests import traceback -from lazyllm.thirdparty import gradio as gr +from lazyllm.thirdparty import gradio as gr, PIL import time -from PIL import Image import re import lazyllm @@ -322,7 +321,7 @@ def get_log_and_message(s): for i, file_path in enumerate(file_paths): suffix = os.path.splitext(file_path)[-1].lower() file = None - if suffix in Image.registered_extensions().keys(): + if suffix in PIL.Image.registered_extensions().keys(): file = gr.Image(file_path) elif suffix in ('.mp3', '.wav'): file = gr.Audio(file_path) diff --git a/tests/advanced_tests/full_test/test_example.py b/tests/advanced_tests/full_test/test_example.py index 7403e299..760157c4 100644 --- a/tests/advanced_tests/full_test/test_example.py +++ b/tests/advanced_tests/full_test/test_example.py @@ -7,7 +7,7 @@ import pytest import random from gradio_client import Client -from PIL import Image +from lazyllm.thirdparty import PIL import lazyllm from lazyllm.launcher import cleanup @@ -130,7 +130,7 @@ def test_painting(self): assert type(res) is dict assert "files" in res assert len(res['files']) == 1 - image = Image.open(res['files'][0]) + image = PIL.Image.open(res['files'][0]) assert image.size == (1024, 1024) # test painting warpped in web diff --git a/tests/basic_tests/test_bm25.py b/tests/basic_tests/test_bm25.py index 0172e73a..a59a01b9 100644 --- a/tests/basic_tests/test_bm25.py +++ b/tests/basic_tests/test_bm25.py @@ -1,7 +1,7 @@ import unittest from lazyllm.tools.rag.component.bm25 import BM25 from lazyllm.tools.rag.doc_node import DocNode -import numpy as np +from lazyllm.thirdparty import numpy as np class TestBM25(unittest.TestCase):