| File was renamed from aigc-director/aigc-claw/backend/tool/image_client.py |
| | |
| | | from config import Config |
| | | |
| | | try: |
| | | from tool.image_dashscope import DashScopeClient |
| | | from tool.image_jimeng import JiMengClient |
| | | from tool.image_seedream import SeedreamClient |
| | | from tool.image_gpt import ImageGPT |
| | | from tool.image_processor import ImageProcessor |
| | | from models.image_dashscope import DashScopeClient |
| | | from models.image_seedream import SeedreamClient |
| | | from models.image_gpt import ImageGPT |
| | | from models.image_processor import ImageProcessor |
| | | except ImportError: |
| | | from .image_dashscope import DashScopeClient |
| | | from .image_jimeng import JiMengClient |
| | | from .image_seedream import SeedreamClient |
| | | from .image_gpt import ImageGPT |
| | | from .image_processor import ImageProcessor |
| | | |
| | | logger = logging.getLogger(__name__) |
| | | |
| | | |
| | | class ImageClient: |
| | | def __init__(self, |
| | | dashscope_api_key: Optional[str] = None, |
| | | dashscope_base_url: Optional[str] = None, |
| | | jimeng_base_url: Optional[str] = None, |
| | | jimeng_access_key: Optional[str] = None, |
| | | jimeng_secret_key: Optional[str] = None, |
| | | gpt_api_key: Optional[str] = None, |
| | | gpt_base_url: Optional[str] = None, |
| | | local_proxy: Optional[str] = None, |
| | |
| | | ark_base_url: Optional[str] = None): |
| | | """ |
| | | Unified Image Generation Client |
| | | Routes requests to DashScope, JiMeng, Seedream, or GPT based on model name. |
| | | 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 |
| | | ) |
| | | |
| | | # Initialize JiMeng Client |
| | | self.jimeng_client = JiMengClient( |
| | | base_url=jimeng_base_url, |
| | | access_key=jimeng_access_key, |
| | | secret_key=jimeng_secret_key |
| | | ) |
| | | |
| | | # Initialize Seedream Client |
| | |
| | | image_paths: List of local file paths or URLs for reference images. |
| | | model: Model name to determine which provider to use. |
| | | save_dir: Custom directory to save downloaded images. |
| | | session_id: Session ID for organizing saved files (especially for JiMeng) |
| | | session_id: Session ID for organizing saved files. |
| | | video_ratio: Aspect ratio of the video, e.g., "16:9", "9:16", "4:3", "3:4", "1:1". |
| | | resolution: Resolution string, e.g., "720P", "1080P", "2K", "4K". |
| | | |
| | |
| | | model = "wan2.7-image" # Default model |
| | | |
| | | if Config.PRINT_MODEL_INPUT: |
| | | print("---- IMAGE GENERATION REQUEST ----") |
| | | print(f"Prompt: {prompt}") |
| | | lines = [ |
| | | "---- IMAGE GENERATION REQUEST ----", |
| | | f"Prompt: {prompt}", |
| | | ] |
| | | if image_paths: |
| | | print(f"Refs: {len(image_paths)}") |
| | | lines.append(f"Refs: {len(image_paths)}") |
| | | for p in image_paths: |
| | | if str(p).startswith("data:"): |
| | | print(f" - [Base64图片]") |
| | | else: |
| | | print(f" - {p}") |
| | | print(f"Model: {model}") |
| | | print(f"Video Ratio: {video_ratio}") |
| | | print(f"Resolution: {resolution}") |
| | | print(f"Final Size: {size}") |
| | | lines.append(" - [Base64图片]" if str(p).startswith("data:") else f" - {p}") |
| | | lines.extend([ |
| | | f"Model: {model}", |
| | | f"Video Ratio: {video_ratio}", |
| | | f"Resolution: {resolution}", |
| | | f"Final Size: {size}", |
| | | ]) |
| | | if session_id: |
| | | print(f"Session ID: {session_id}") |
| | | print("-" * 30) |
| | | lines.append(f"Session ID: {session_id}") |
| | | lines.append("-" * 30) |
| | | logger.info("\n%s", "\n".join(lines)) |
| | | |
| | | # Determine backend provider |
| | | is_jimeng = "jimeng" in model.lower() |
| | | is_seedream = "seedream" in model.lower() |
| | | is_sora = "sora" in model.lower() or "gpt" in model.lower() |
| | | |
| | |
| | | |
| | | generated_local_paths = [] |
| | | |
| | | if is_jimeng: |
| | | # --- JiMeng Logic --- |
| | | try: |
| | | # JiMengClient handles local paths and typically saves results to code/result internally |
| | | # or we rely on its return value. |
| | | # JiMengClient.generate_image returns list of local paths (saved from base64) |
| | | logging.info(f"ImageClient requesting JiMeng: {model}") |
| | | paths = self.jimeng_client.generate_image( |
| | | prompt=prompt, |
| | | image_paths=image_paths if image_paths else [], |
| | | model=model, |
| | | session_id=session_id, |
| | | size=size |
| | | ) |
| | | |
| | | # If JiMengClient saves to a default location, we might want to move them or just return them. |
| | | # The provided JiMengClient saves to 'backend/code/result'. |
| | | # We simply return those paths. |
| | | generated_local_paths.extend(paths) |
| | | |
| | | except Exception as e: |
| | | logging.error(f"JiMeng generation failed: {e}") |
| | | |
| | | elif is_seedream: |
| | | if is_seedream: |
| | | # --- Seedream Logic --- |
| | | try: |
| | | logging.info(f"ImageClient requesting Seedream: {model}") |
| | | logger.info("ImageClient routed to Seedream: model=%s", model) |
| | | |
| | | paths = self.seedream_client.generate_image( |
| | | prompt=prompt, |
| | |
| | | generated_local_paths.extend(paths) |
| | | |
| | | except Exception as e: |
| | | logging.error(f"Seedream generation failed: {e}") |
| | | logger.exception("Seedream generation failed: %s", e) |
| | | |
| | | elif is_sora: |
| | | # --- GPT/Sora Logic --- |
| | | try: |
| | | logging.info(f"ImageClient requesting GPT/Sora: {model}") |
| | | logger.info("ImageClient routed to GPT/Sora: model=%s", model) |
| | | if image_paths: |
| | | logging.warning("Sora/GPT model only supports Text-to-Image. Ignoring reference images.") |
| | | logger.warning("Sora/GPT model only supports Text-to-Image. Ignoring reference images.") |
| | | |
| | | # OpenAI uses 'x' separator, e.g. 1024x1024 |
| | | # Attempt to map size if needed or just replace '*' |
| | |
| | | if path and os.path.exists(path): |
| | | generated_local_paths.append(path) |
| | | else: |
| | | logging.error(f"GPT/Sora returned invalid path or download failed: {path}") |
| | | logger.error("GPT/Sora returned invalid path or download failed: %s", path) |
| | | |
| | | except Exception as e: |
| | | logging.error(f"GPT/Sora generation failed: {e}") |
| | | logger.exception("GPT/Sora generation failed: %s", e) |
| | | |
| | | else: |
| | | # --- DashScope Logic --- |
| | | try: |
| | | logging.info(f"ImageClient requesting DashScope: {model}") |
| | | logger.info("ImageClient routed to DashScope: model=%s", model) |
| | | |
| | | if image_paths and len(image_paths) > 0: |
| | | # Pre-process image paths for DashScope |
| | |
| | | generated_local_paths.extend(paths) |
| | | |
| | | except Exception as e: |
| | | logging.error(f"DashScope generation failed: {e}") |
| | | logger.exception("DashScope generation failed: %s", e) |
| | | |
| | | return generated_local_paths |