Instructions to use mlworks90/fashion-inpainting-system with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use mlworks90/fashion-inpainting-system with Diffusers:
pip install -U diffusers transformers accelerate
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline controlnet = ControlNetModel.from_pretrained("mlworks90/fashion-inpainting-system") pipe = StableDiffusionControlNetPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", controlnet=controlnet ) - Notebooks
- Google Colab
- Kaggle
File size: 18,042 Bytes
5d3e95d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | """
FASHION SAFETY CHECKER - CLEAN PRODUCTION VERSION
================================================
Production-ready fashion safety validation with:
- Silent blocking fix applied
- User-friendly "generating synthetic face" messaging
- Minimal logging with essential blocking reports only
- Full parameter control (face_scale, safety_mode)
"""
import cv2
import numpy as np
from PIL import Image
import torch
from typing import Dict, List, Tuple, Optional, Union
import os
import warnings
from dataclasses import dataclass
from enum import Enum
from contextlib import redirect_stdout
from io import StringIO
# Suppress warnings for production
warnings.filterwarnings('ignore')
class SafetyLevel(Enum):
SAFE = "safe"
WARNING = "warning"
UNSAFE = "unsafe"
BLOCKED = "blocked"
@dataclass
class SafetyResult:
is_safe: bool
safety_level: SafetyLevel
confidence: float
issues: List[str]
warnings: List[str]
detailed_analysis: Dict
user_message: str
class FashionOptimizedSafetyChecker:
"""Production fashion safety checker"""
def __init__(self, strictness_level: str = "fashion_moderate", verbose: bool = False):
self.strictness_level = strictness_level
self.verbose = verbose
self._configure_thresholds()
self._init_fashion_context()
self._init_detection_systems()
def _configure_thresholds(self):
"""Configure safety thresholds"""
configs = {
"fashion_permissive": {"content_safety_threshold": 0.3, "fashion_context_bonus": 0.3},
"fashion_moderate": {"content_safety_threshold": 0.5, "fashion_context_bonus": 0.2},
"fashion_strict": {"content_safety_threshold": 0.7, "fashion_context_bonus": 0.1},
"legacy_strict": {"content_safety_threshold": 0.9, "fashion_context_bonus": 0.0}
}
self.thresholds = configs.get(self.strictness_level, configs["fashion_moderate"])
def _init_fashion_context(self):
"""Initialize fashion keywords"""
self.fashion_keywords = {
'evening_wear': ['evening', 'formal', 'gown', 'cocktail'],
'activewear': ['workout', 'sports', 'athletic', 'swimwear', 'bikini'],
'professional': ['business', 'office', 'suit', 'blazer'],
'casual': ['casual', 'everyday', 'street']
}
def _init_detection_systems(self):
"""Initialize detection systems silently"""
try:
self.face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
except Exception:
self.face_cascade = None
def validate_target_image(self,
target_image: Union[str, Image.Image, np.ndarray],
prompt_hint: str = "",
debug_output_path: Optional[str] = None) -> SafetyResult:
"""Production validation with proper bikini + strict mode detection"""
try:
# Load image
if isinstance(target_image, str):
image_pil = Image.open(target_image).convert('RGB')
image_np = np.array(image_pil)
elif isinstance(target_image, Image.Image):
image_np = np.array(target_image.convert('RGB'))
else:
image_np = target_image
# Fashion context analysis
fashion_context = self._analyze_fashion_context(prompt_hint)
# CRITICAL: Detect bikini + strict mode combination
is_bikini_request = 'bikini' in prompt_hint.lower() or 'swimwear' in prompt_hint.lower()
is_strict_mode = self.strictness_level == "fashion_strict"
# Safety scoring
base_score = 0.8
if fashion_context['is_fashion_image']:
base_score += fashion_context['score'] * self.thresholds['fashion_context_bonus']
# STRICT MODE: Block bikini requests
if is_strict_mode and is_bikini_request:
return SafetyResult(
is_safe=False,
safety_level=SafetyLevel.BLOCKED,
confidence=0.2, # Low confidence due to strict blocking
issues=["Bikini content blocked in strict mode"],
warnings=[],
detailed_analysis={'strict_mode_block': True, 'bikini_detected': True},
user_message="Content blocked due to strict safety settings."
)
# Normal safety evaluation
if base_score >= 0.8:
safety_level = SafetyLevel.SAFE
is_safe = True
user_message = "Fashion validation passed."
elif base_score >= 0.6:
safety_level = SafetyLevel.WARNING
is_safe = True
user_message = "Fashion validation passed with minor concerns."
else:
safety_level = SafetyLevel.BLOCKED
is_safe = False
user_message = "Content blocked due to safety concerns."
return SafetyResult(
is_safe=is_safe,
safety_level=safety_level,
confidence=base_score,
issues=[],
warnings=[],
detailed_analysis={'bikini_detected': is_bikini_request, 'strict_mode': is_strict_mode},
user_message=user_message
)
except Exception as e:
return SafetyResult(
is_safe=False,
safety_level=SafetyLevel.BLOCKED,
confidence=0.0,
issues=["Validation error"],
warnings=[],
detailed_analysis={},
user_message="Safety validation failed."
)
def _analyze_fashion_context(self, prompt_hint: str) -> Dict:
"""Analyze fashion context from prompt"""
context = {'is_fashion_image': False, 'score': 0.0}
if prompt_hint:
prompt_lower = prompt_hint.lower()
for keywords in self.fashion_keywords.values():
if any(keyword in prompt_lower for keyword in keywords):
context['is_fashion_image'] = True
context['score'] = 0.3
break
return context
class FashionAwarePipeline:
"""Production fashion pipeline"""
def __init__(self, safety_mode: str = "fashion_moderate", verbose: bool = False):
self.safety_checker = FashionOptimizedSafetyChecker(strictness_level=safety_mode, verbose=verbose)
self.safety_mode = safety_mode
self.verbose = verbose
def safe_fashion_transformation(self,
source_image_path: str,
checkpoint_path: str,
outfit_prompt: str,
output_path: str = "fashion_result.jpg",
face_scale: float = 0.95,
safety_override: bool = False) -> Dict:
"""Production fashion transformation with clear blocking reports"""
result = {
'success': False,
'face_swap_applied': False,
'final_output': None,
'user_message': None,
'safety_level': None,
'blocking_reason': None,
'safety_approved': False
}
try:
# Generate outfit
from fixed_realistic_vision_pipeline import FixedRealisticVisionPipeline
# Suppress initialization prints only
if not self.verbose:
f = StringIO()
with redirect_stdout(f):
outfit_pipeline = FixedRealisticVisionPipeline(checkpoint_path, device='cuda')
else:
outfit_pipeline = FixedRealisticVisionPipeline(checkpoint_path, device='cuda')
# Generate outfit (suppress technical details in non-verbose mode)
outfit_path = output_path.replace('.jpg', '_outfit.jpg')
if not self.verbose:
with redirect_stdout(f):
generated_image, generation_metadata = outfit_pipeline.generate_outfit(
source_image_path=source_image_path,
outfit_prompt=outfit_prompt,
output_path=outfit_path
)
else:
generated_image, generation_metadata = outfit_pipeline.generate_outfit(
source_image_path=source_image_path,
outfit_prompt=outfit_prompt,
output_path=outfit_path
)
# FIRST: Do safety validation to get the real blocking reason
safety_result = self.safety_checker.validate_target_image(
target_image=outfit_path,
prompt_hint=outfit_prompt,
debug_output_path=None
)
result['safety_level'] = safety_result.safety_level.value
# Check generation metadata
single_person_ok = generation_metadata.get('validation', {}).get('single_person', False)
# DETERMINE THE REAL BLOCKING REASON
# If safety failed AND single_person is False, it's likely a safety block causing synthetic face
if not safety_result.is_safe and not single_person_ok:
# This is likely a safety block manifesting as "synthetic face" (single_person=False)
print(f"🚫 Content generation blocked - generating synthetic face")
print(f" Issue: Content safety restrictions triggered")
print(f" Action: Please try a more conservative outfit style")
print(f" Safety level: {safety_result.safety_level.value}")
result['blocking_reason'] = f"Safety restrictions: {safety_result.safety_level.value}"
result['user_message'] = "Content generation blocked - generating synthetic face. Content safety restrictions apply."
result['final_output'] = outfit_path
result['safety_approved'] = False
return result
elif not single_person_ok and safety_result.is_safe:
# This is a genuine multiple people detection issue
print(f"🚫 Content generation blocked - generating synthetic face")
print(f" Issue: Multiple people detected in generated content")
print(f" Action: Please try a different outfit description")
result['blocking_reason'] = "Multiple people detected"
result['user_message'] = "Content generation blocked - generating synthetic face. Multiple people detected in image."
result['final_output'] = outfit_path
return result
# Face swap decision
proceed = (safety_result.is_safe or
(safety_override and safety_result.safety_level != SafetyLevel.BLOCKED))
if proceed:
result['safety_approved'] = True
try:
# Apply face swap (suppress ALL prints during face swap execution)
if not self.verbose:
with redirect_stdout(f):
from integrated_fashion_pipelinbe_with_adjustable_face_scaling import IntegratedFashionPipeline
integrated_pipeline = IntegratedFashionPipeline()
# Face swap execution - suppress technical details
final_image = integrated_pipeline.face_swapper.swap_faces_with_target_scaling(
source_image=source_image_path,
target_image=outfit_path,
face_scale=face_scale,
output_path=output_path,
quality_mode="balanced",
crop_to_original=False
)
else:
from integrated_fashion_pipelinbe_with_adjustable_face_scaling import IntegratedFashionPipeline
integrated_pipeline = IntegratedFashionPipeline()
final_image = integrated_pipeline.face_swapper.swap_faces_with_target_scaling(
source_image=source_image_path,
target_image=outfit_path,
face_scale=face_scale,
output_path=output_path,
quality_mode="balanced",
crop_to_original=False
)
# SUCCESS
result['success'] = True
result['face_swap_applied'] = True
result['final_output'] = output_path
result['user_message'] = "Fashion transformation completed successfully."
except Exception as e:
# TECHNICAL FAILURE
outfit_failure_path = output_path.replace('.jpg', '_outfit_only.jpg')
generated_image.save(outfit_failure_path)
print(f"🚫 Content generation blocked - generating synthetic face")
print(f" Issue: Technical error during face processing")
print(f" Action: Please try again")
result['success'] = False
result['face_swap_applied'] = False
result['final_output'] = outfit_failure_path
result['user_message'] = "Content generation blocked - generating synthetic face. Technical error occurred."
result['blocking_reason'] = f"Technical failure: {str(e)}"
result['safety_approved'] = True
else:
# SAFETY BLOCK
outfit_blocked_path = output_path.replace('.jpg', '_outfit_only.jpg')
generated_image.save(outfit_blocked_path)
print(f"🚫 Content generation blocked - generating synthetic face")
print(f" Issue: Content safety restrictions")
print(f" Action: Please try a more conservative outfit style")
result['success'] = False
result['face_swap_applied'] = False
result['final_output'] = outfit_blocked_path
result['user_message'] = "Content generation blocked - generating synthetic face. Please try a more conservative outfit style."
result['blocking_reason'] = f"Safety restrictions: {safety_result.safety_level.value}"
result['safety_approved'] = False
return result
except Exception as e:
print(f"🚫 Content generation blocked - generating synthetic face")
print(f" Issue: System error occurred")
print(f" Action: Please try again")
result['blocking_reason'] = f"System error: {str(e)}"
result['user_message'] = "Content generation blocked - generating synthetic face. System error occurred."
return result
def fashion_safe_generate(source_image_path: str,
checkpoint_path: str,
outfit_prompt: str,
output_path: str = "fashion_result.jpg",
face_scale: float = 0.95,
safety_mode: str = "fashion_moderate",
safety_override: bool = False,
verbose: bool = False) -> Dict:
"""
PRODUCTION VERSION: Fashion generation with user-friendly messaging
"""
pipeline = FashionAwarePipeline(safety_mode=safety_mode, verbose=verbose)
return pipeline.safe_fashion_transformation(
source_image_path=source_image_path,
checkpoint_path=checkpoint_path,
outfit_prompt=outfit_prompt,
output_path=output_path,
face_scale=face_scale,
safety_override=safety_override
)
def create_fashion_safety_pipeline(preset: str = "production",
safety_mode: Optional[str] = None,
verbose: bool = False) -> FashionAwarePipeline:
"""
Create fashion pipeline with production configuration
Args:
preset: "production", "demo", "strict_production", "permissive"
safety_mode: Override safety mode ("fashion_moderate" default)
verbose: Enable detailed logging (False for production)
"""
presets = {
'production': 'fashion_moderate',
'demo': 'fashion_moderate',
'strict_production': 'fashion_strict',
'permissive': 'fashion_permissive'
}
final_safety_mode = safety_mode if safety_mode is not None else presets.get(preset, "fashion_moderate")
return FashionAwarePipeline(safety_mode=final_safety_mode, verbose=verbose) |