From 470f2f4715e83ba37af4adedcf4bffac9c6159e4 Mon Sep 17 00:00:00 2001
From: hit-cxf <2024111082@stu.hit.edu.cn>
Date: Thu, 18 Jun 2026 19:58:01 +0800
Subject: [PATCH] Add lazy load to LLM VLM Image Video client
---
video-claw/video-claw/backend/models/video_client.py | 53 +++++++---
video-claw/video-claw/backend/models/image_client.py | 55 +++++++---
video-claw/video-claw/backend/models/llm_client.py | 82 +++++++++++----
video-claw/video-claw/backend/models/vlm_client.py | 55 +++++++---
4 files changed, 173 insertions(+), 72 deletions(-)
diff --git a/video-claw/video-claw/backend/models/image_client.py b/video-claw/video-claw/backend/models/image_client.py
index fc5f86b..66bc913 100644
--- a/video-claw/video-claw/backend/models/image_client.py
+++ b/video-claw/video-claw/backend/models/image_client.py
@@ -40,24 +40,17 @@
Unified Image Generation Client
Routes requests to DashScope, Seedream, or GPT based on model name.
"""
- # Initialize DashScope Client
- self.dashscope_client = DashScopeClient(
- api_key=dashscope_api_key,
- base_url=dashscope_base_url
- )
+ self._dashscope_api_key = dashscope_api_key
+ self._dashscope_base_url = dashscope_base_url
+ self._ark_api_key = ark_api_key
+ self._ark_base_url = ark_base_url
+ self._gpt_api_key = gpt_api_key
+ self._gpt_base_url = gpt_base_url
+ self._proxy = Config.provider_proxy("openai") if proxy is None else proxy
- # Initialize Seedream Client
- self.seedream_client = SeedreamClient(
- api_key=ark_api_key,
- base_url=ark_base_url
- )
-
- # Initialize GPT Image Client
- self.gpt_client = ImageGPT(
- api_key=gpt_api_key,
- base_url=gpt_base_url,
- proxy=Config.provider_proxy("openai") if proxy is None else proxy
- )
+ self._dashscope_client = None
+ self._seedream_client = None
+ self._gpt_client = None
# Initialize Image Processor for downloads
self.image_processor = ImageProcessor()
@@ -65,6 +58,34 @@
# Default save directory
self.base_save_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "code", "result", "image_client")
+ @property
+ def dashscope_client(self):
+ if self._dashscope_client is None:
+ self._dashscope_client = DashScopeClient(
+ api_key=self._dashscope_api_key,
+ base_url=self._dashscope_base_url,
+ )
+ return self._dashscope_client
+
+ @property
+ def seedream_client(self):
+ if self._seedream_client is None:
+ self._seedream_client = SeedreamClient(
+ api_key=self._ark_api_key,
+ base_url=self._ark_base_url,
+ )
+ return self._seedream_client
+
+ @property
+ def gpt_client(self):
+ if self._gpt_client is None:
+ self._gpt_client = ImageGPT(
+ api_key=self._gpt_api_key,
+ base_url=self._gpt_base_url,
+ proxy=self._proxy,
+ )
+ return self._gpt_client
+
def generate_image(self,
prompt: str,
image_paths: Optional[List[str]] = None,
diff --git a/video-claw/video-claw/backend/models/llm_client.py b/video-claw/video-claw/backend/models/llm_client.py
index bca9266..1926fde 100644
--- a/video-claw/video-claw/backend/models/llm_client.py
+++ b/video-claw/video-claw/backend/models/llm_client.py
@@ -27,13 +27,59 @@
class LLM:
def __init__(self, gemini_base_url="", gemini_api_key="", gpt_base_url="", gpt_api_key="", deepseek_base_url="", deepseek_api_key="", dashscope_api_key=""):
- self.gemini_base_url = gemini_base_url or Config.GOOGLE_GEMINI_BASE_URL
- self.gemini_api_key = gemini_api_key or Config.GEMINI_API_KEY
- self.gpt_base_url = gpt_base_url or Config.OPENAI_BASE_URL
- self.gpt_api_key = gpt_api_key or Config.OPENAI_API_KEY
- self.deepseek_base_url = deepseek_base_url or Config.DEEPSEEK_BASE_URL
- self.deepseek_api_key = deepseek_api_key or Config.DEEPSEEK_API_KEY
- self.dashscope_api_key = dashscope_api_key or Config.DASHSCOPE_API_KEY
+ self._gemini_base_url = gemini_base_url or Config.GOOGLE_GEMINI_BASE_URL
+ self._gemini_api_key = gemini_api_key or Config.GEMINI_API_KEY
+ self._gpt_base_url = gpt_base_url or Config.OPENAI_BASE_URL
+ self._gpt_api_key = gpt_api_key or Config.OPENAI_API_KEY
+ self._deepseek_base_url = deepseek_base_url or Config.DEEPSEEK_BASE_URL
+ self._deepseek_api_key = deepseek_api_key or Config.DEEPSEEK_API_KEY
+ self._dashscope_api_key = dashscope_api_key or Config.DASHSCOPE_API_KEY
+
+ self._gemini_client = None
+ self._gpt_client = None
+ self._deepseek_client = None
+ self._dashscope_client = None
+ self._dashscope_vl_client = None
+
+ @property
+ def gemini_client(self):
+ if self._gemini_client is None:
+ self._gemini_client = Gemini(
+ base_url=self._gemini_base_url,
+ api_key=self._gemini_api_key,
+ )
+ return self._gemini_client
+
+ @property
+ def gpt_client(self):
+ if self._gpt_client is None:
+ self._gpt_client = GPT(
+ base_url=self._gpt_base_url,
+ api_key=self._gpt_api_key,
+ proxy=Config.provider_proxy("openai"),
+ )
+ return self._gpt_client
+
+ @property
+ def deepseek_client(self):
+ if self._deepseek_client is None:
+ self._deepseek_client = DeepSeek(
+ base_url=self._deepseek_base_url,
+ api_key=self._deepseek_api_key,
+ )
+ return self._deepseek_client
+
+ @property
+ def dashscope_client(self):
+ if self._dashscope_client is None:
+ self._dashscope_client = QwenLLM(api_key=self._dashscope_api_key)
+ return self._dashscope_client
+
+ @property
+ def dashscope_vl_client(self):
+ if self._dashscope_vl_client is None:
+ self._dashscope_vl_client = QwenVLClient(api_key=self._dashscope_api_key)
+ return self._dashscope_vl_client
def full_to_half(self, text):
if not isinstance(text, str):
@@ -78,32 +124,22 @@
result = ""
model_lower = model.lower()
if model_lower.startswith("gemini"):
- client = Gemini(base_url=self.gemini_base_url, api_key=self.gemini_api_key)
- result = client.query(prompt, image_urls=image_urls, model=model)
+ result = self.gemini_client.query(prompt, image_urls=image_urls, model=model)
elif "gpt" in model_lower:
# OpenAI series models
- client = GPT(
- base_url=self.gpt_base_url,
- api_key=self.gpt_api_key,
- proxy=Config.provider_proxy("openai"),
- )
- result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
+ result = self.gpt_client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
elif "kimi" in model_lower or "qwen3.6-plus" in model_lower or "qwen3.6-flash" in model_lower or "vl" in model_lower:
# DashScope VLM models (using MultiModalConversation API)
- dashscope_vl_client = QwenVLClient(api_key=self.dashscope_api_key)
- result = dashscope_vl_client.chat(text=prompt, images=image_urls, model=model, stream=False)
+ result = self.dashscope_vl_client.chat(text=prompt, images=image_urls, model=model, stream=False)
elif "deepseek-v3.2" in model_lower:
# DeepSeek v3.2 (通过 DashScope Generation API)
- client = QwenLLM(api_key=self.dashscope_api_key)
- result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
+ result = self.dashscope_client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
elif model_lower.startswith("deepseek") and "v3.2" not in model_lower:
# Original DeepSeek provider
- client = DeepSeek(base_url=self.deepseek_base_url, api_key=self.deepseek_api_key)
- result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
+ result = self.deepseek_client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
else:
# Default to Qwen models / deepseek-v3.2 via DashScope Generation API
- client = QwenLLM(api_key=self.dashscope_api_key)
- result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
+ result = self.dashscope_client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
if safe_content:
result = self.full_to_half(result)
diff --git a/video-claw/video-claw/backend/models/video_client.py b/video-claw/video-claw/backend/models/video_client.py
index c29790d..89d8012 100644
--- a/video-claw/video-claw/backend/models/video_client.py
+++ b/video-claw/video-claw/backend/models/video_client.py
@@ -45,24 +45,45 @@
ark_api_key: Optional[str] = None,
ark_base_url: Optional[str] = None,
):
- # 万象客户端
- self.Dashscope_client = DashscopeVideoClient(
- api_key=dashscope_api_key or Config.DASHSCOPE_API_KEY,
- base_url=dashscope_base_url or Config.DASHSCOPE_BASE_URL,
- )
+ self._dashscope_api_key = dashscope_api_key or Config.DASHSCOPE_API_KEY
+ self._dashscope_base_url = dashscope_base_url or Config.DASHSCOPE_BASE_URL
+ self._kling_access_key = kling_access_key or Config.KLING_ACCESS_KEY
+ self._kling_secret_key = kling_secret_key or Config.KLING_SECRET_KEY
+ self._kling_base_url = kling_base_url or Config.KLING_BASE_URL
+ self._ark_api_key = ark_api_key or Config.ARK_API_KEY
+ self._ark_base_url = ark_base_url or Config.ARK_BASE_URL
- # 可灵客户端
- self.kling_client = KlingVideoClient(
- access_key=kling_access_key or Config.KLING_ACCESS_KEY,
- secret_key=kling_secret_key or Config.KLING_SECRET_KEY,
- base_url=kling_base_url or Config.KLING_BASE_URL,
- )
+ self._dashscope_client = None
+ self._kling_client = None
+ self._seedance_client = None
- # Seedance 客户端
- self.seedance_client = SeedanceVideoClient(
- api_key=ark_api_key or Config.ARK_API_KEY,
- base_url=ark_base_url or Config.ARK_BASE_URL,
- )
+ @property
+ def Dashscope_client(self):
+ if self._dashscope_client is None:
+ self._dashscope_client = DashscopeVideoClient(
+ api_key=self._dashscope_api_key,
+ base_url=self._dashscope_base_url,
+ )
+ return self._dashscope_client
+
+ @property
+ def kling_client(self):
+ if self._kling_client is None:
+ self._kling_client = KlingVideoClient(
+ access_key=self._kling_access_key,
+ secret_key=self._kling_secret_key,
+ base_url=self._kling_base_url,
+ )
+ return self._kling_client
+
+ @property
+ def seedance_client(self):
+ if self._seedance_client is None:
+ self._seedance_client = SeedanceVideoClient(
+ api_key=self._ark_api_key,
+ base_url=self._ark_base_url,
+ )
+ return self._seedance_client
def generate_video(
self,
diff --git a/video-claw/video-claw/backend/models/vlm_client.py b/video-claw/video-claw/backend/models/vlm_client.py
index 798eed1..131f9ef 100644
--- a/video-claw/video-claw/backend/models/vlm_client.py
+++ b/video-claw/video-claw/backend/models/vlm_client.py
@@ -36,22 +36,45 @@
Unified VLM (Vision Language Model) Client
Routes requests to DashScope (QwenVL) or Gemini based on model name.
"""
- # Initialize DashScope Client
- self.dashscope_client = QwenVLClient(
- api_key=dashscope_api_key,
- base_url=dashscope_base_url
- )
- # Initialize Gemini Client
- self.gemini_client = GeminiVLClient(
- api_key=gemini_api_key,
- base_url=gemini_base_url
- )
- # Initialize GPT Client
- self.gpt_client = GPTVLClient(
- api_key=gpt_api_key,
- base_url=gpt_base_url,
- proxy=Config.provider_proxy("openai") if proxy is None else proxy
- )
+ self._dashscope_api_key = dashscope_api_key
+ self._dashscope_base_url = dashscope_base_url
+ self._gemini_api_key = gemini_api_key
+ self._gemini_base_url = gemini_base_url
+ self._gpt_api_key = gpt_api_key
+ self._gpt_base_url = gpt_base_url
+ self._proxy = Config.provider_proxy("openai") if proxy is None else proxy
+
+ self._dashscope_client = None
+ self._gemini_client = None
+ self._gpt_client = None
+
+ @property
+ def dashscope_client(self):
+ if self._dashscope_client is None:
+ self._dashscope_client = QwenVLClient(
+ api_key=self._dashscope_api_key,
+ base_url=self._dashscope_base_url,
+ )
+ return self._dashscope_client
+
+ @property
+ def gemini_client(self):
+ if self._gemini_client is None:
+ self._gemini_client = GeminiVLClient(
+ api_key=self._gemini_api_key,
+ base_url=self._gemini_base_url,
+ )
+ return self._gemini_client
+
+ @property
+ def gpt_client(self):
+ if self._gpt_client is None:
+ self._gpt_client = GPTVLClient(
+ api_key=self._gpt_api_key,
+ base_url=self._gpt_base_url,
+ proxy=self._proxy,
+ )
+ return self._gpt_client
def query(self,
prompt: str,
--
Gitblit v1.8.0