Fix str/Path related typing

This commit is contained in:
Aarni Koskela 2025-04-25 13:59:42 +03:00
parent effff3273a
commit dc175f3d14
6 changed files with 28 additions and 20 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
from pathlib import Path
import mlx.core as mx import mlx.core as mx
import numpy as np import numpy as np
@ -48,7 +49,7 @@ class RuntimeConfig:
return self.model_config.num_train_steps return self.model_config.num_train_steps
@property @property
def image_path(self) -> str: def image_path(self) -> Path | None:
return self.config.image_path return self.config.image_path
@property @property
@ -56,15 +57,15 @@ class RuntimeConfig:
return self.config.image_strength return self.config.image_strength
@property @property
def depth_image_path(self) -> str | None: def depth_image_path(self) -> Path | None:
return self.config.depth_image_path return self.config.depth_image_path
@property @property
def redux_image_paths(self) -> str | None: def redux_image_paths(self) -> list[Path] | None:
return self.config.redux_image_paths return self.config.redux_image_paths
@property @property
def masked_image_path(self) -> str | None: def masked_image_path(self) -> Path | None:
return self.config.masked_image_path return self.config.masked_image_path
@property @property

View File

@ -1,5 +1,6 @@
import logging import logging
import os import os
from pathlib import Path
import mlx.core as mx import mlx.core as mx
import PIL.Image import PIL.Image
@ -19,8 +20,8 @@ class DepthUtil:
vae: VAE, vae: VAE,
depth_pro: DepthPro, depth_pro: DepthPro,
config: RuntimeConfig, config: RuntimeConfig,
image_path: str | None = None, image_path: str | Path | None = None,
depth_image_path: str | None = None, depth_image_path: str | Path | None = None,
) -> (mx.array, PIL.Image.Image): ) -> (mx.array, PIL.Image.Image):
# 1. Create the depth map or use existing one # 1. Create the depth map or use existing one
depth_image_path, depth_image = DepthUtil.get_or_create_depth_map( depth_image_path, depth_image = DepthUtil.get_or_create_depth_map(
@ -44,9 +45,9 @@ class DepthUtil:
@staticmethod @staticmethod
def get_or_create_depth_map( def get_or_create_depth_map(
depth_pro: DepthPro, depth_pro: DepthPro,
image_path: str | None = None, image_path: str | Path | None = None,
depth_map_path: str | None = None, depth_map_path: str | Path | None = None,
) -> tuple[str, PIL.Image.Image | None]: ) -> tuple[str | Path, PIL.Image.Image | None]:
# 1. If a depth map path is provided, use it directly # 1. If a depth map path is provided, use it directly
if depth_map_path: if depth_map_path:
if not os.path.exists(depth_map_path): if not os.path.exists(depth_map_path):

View File

@ -1,3 +1,5 @@
from pathlib import Path
import mlx.core as mx import mlx.core as mx
from mflux.config.runtime_config import RuntimeConfig from mflux.config.runtime_config import RuntimeConfig
@ -12,8 +14,8 @@ class MaskUtil:
vae: VAE, vae: VAE,
config: RuntimeConfig, config: RuntimeConfig,
latents: mx.array, latents: mx.array,
img_path: str, img_path: str | Path,
mask_path: str | None, mask_path: str | Path | None,
) -> mx.array: ) -> mx.array:
if not img_path or not mask_path: if not img_path or not mask_path:
# Return empty latents if no image or mask is provided # Return empty latents if no image or mask is provided

View File

@ -1,3 +1,5 @@
from pathlib import Path
import mlx.core as mx import mlx.core as mx
from mlx import nn from mlx import nn
from tqdm import tqdm from tqdm import tqdm
@ -158,7 +160,7 @@ class Flux1Redux(nn.Module):
clip_tokenizer: TokenizerCLIP, clip_tokenizer: TokenizerCLIP,
t5_text_encoder: T5Encoder, t5_text_encoder: T5Encoder,
clip_text_encoder: CLIPEncoder, clip_text_encoder: CLIPEncoder,
image_paths: list[str], image_paths: list[str] | list[Path],
image_encoder: SiglipVisionTransformer, image_encoder: SiglipVisionTransformer,
image_embedder: ReduxEncoder, image_embedder: ReduxEncoder,
) -> (mx.array, mx.array): ) -> (mx.array, mx.array):

View File

@ -1,3 +1,5 @@
from pathlib import Path
import mlx.core as mx import mlx.core as mx
from mflux import ImageUtil from mflux import ImageUtil
@ -8,7 +10,7 @@ from mflux.models.siglip_vision_transformer.siglip_vision_transformer import Sig
class ReduxUtil: class ReduxUtil:
@staticmethod @staticmethod
def embed_images( def embed_images(
image_paths: list[str], image_paths: list[str] | list[Path],
image_encoder: SiglipVisionTransformer, image_encoder: SiglipVisionTransformer,
image_embedder: ReduxEncoder, image_embedder: ReduxEncoder,
) -> list[mx.array]: # fmt:off ) -> list[mx.array]: # fmt:off
@ -24,7 +26,7 @@ class ReduxUtil:
@staticmethod @staticmethod
def _embed_single_image( def _embed_single_image(
image_path: str, image_path: str | Path,
image_encoder: SiglipVisionTransformer, image_encoder: SiglipVisionTransformer,
image_embedder: ReduxEncoder, image_embedder: ReduxEncoder,
) -> mx.array: # fmt:off ) -> mx.array: # fmt:off

View File

@ -27,12 +27,12 @@ class ImageUtil:
generation_time: float, generation_time: float,
lora_paths: list[str], lora_paths: list[str],
lora_scales: list[float], lora_scales: list[float],
controlnet_image_path: str | None = None, controlnet_image_path: str | Path | None = None,
image_path: str | None = None, image_path: str | Path | None = None,
redux_image_paths: list[str] | None = None, redux_image_paths: list[str] | list[Path] | None = None,
image_strength: float | None = None, image_strength: float | None = None,
masked_image_path: str | None = None, masked_image_path: str | Path | None = None,
depth_image_path: str | None = None, depth_image_path: str | Path | None = None,
) -> GeneratedImage: ) -> GeneratedImage:
normalized = ImageUtil._denormalize(decoded_latents) normalized = ImageUtil._denormalize(decoded_latents)
normalized_numpy = ImageUtil._to_numpy(normalized) normalized_numpy = ImageUtil._to_numpy(normalized)
@ -240,7 +240,7 @@ class ImageUtil:
log.error(f"Error saving image: {e}") log.error(f"Error saving image: {e}")
@staticmethod @staticmethod
def _embed_metadata(metadata: dict, path: str) -> None: def _embed_metadata(metadata: dict, path: str | Path) -> None:
try: try:
# Convert metadata dictionary to a string # Convert metadata dictionary to a string
metadata_str = json.dumps(metadata) metadata_str = json.dumps(metadata)