Astridkraft commited on
Commit
bdac8dc
·
verified ·
1 Parent(s): 40e899f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -42
app.py CHANGED
@@ -319,6 +319,8 @@ def auto_detect_face_area(image):
319
  pipe_txt2img = None
320
  current_pipe_model_id = None
321
  pipe_img2img = None
 
 
322
 
323
 
324
  #Das Laden des Modells bedeutet, die trainierten Gewichte (Parameter) von der Festplatte zu lesen und
@@ -463,49 +465,112 @@ def load_txt2img(model_id):
463
  print(f"❌ Auch Fallback fehlgeschlagen: {fallback_error}")
464
  raise
465
 
466
- def load_img2img():
467
- """Lädt Multi-ControlNet-Inpainting-Pipeline (Pose + Canny)"""
468
- global pipe_img2img
469
- if pipe_img2img is None:
470
- print("🔄 Lade Multi-ControlNet-Inpainting-Modell (OpenPose + Canny)...")
471
- try:
472
- # LADE BEIDE ControlNet-Modelle
473
- controlnet_openpose = ControlNetModel.from_pretrained(
474
- "lllyasviel/sd-controlnet-openpose",
475
- torch_dtype=torch_dtype
476
- )
477
- controlnet_canny = ControlNetModel.from_pretrained(
478
- "lllyasviel/sd-controlnet-canny",
479
- torch_dtype=torch_dtype
480
- )
481
-
482
- # ÜBERGEBE EINE LISTE an die Pipeline
483
- pipe_img2img = StableDiffusionControlNetInpaintPipeline.from_pretrained(
484
- "runwayml/stable-diffusion-v1-5",
485
- controlnet=[controlnet_openpose, controlnet_canny], # <-- Liste
486
- torch_dtype=torch_dtype,
487
- safety_checker=None,
488
- requires_safety_checker=False
489
- ).to(device)
490
-
491
- # Scheduler konfigurieren (Ihre bestehende Logik)
492
- pipe_img2img.scheduler = DPMSolverMultistepScheduler.from_config(
493
- pipe_img2img.scheduler.config,
494
- algorithm_type="sde-dpmsolver++",
495
- use_karras_sigmas=True,
496
- timestep_spacing="trailing"
497
- )
498
- print("✅ Multi-ControlNet-Inpainting-Pipeline geladen (OpenPose + Canny)")
499
-
500
- except Exception as e:
501
- print(f"❌ Fehler beim Laden der Multi-ControlNet-Inpainting-Pipeline: {e}")
502
- raise
503
-
504
- # Optimierungen
505
- pipe_img2img.enable_attention_slicing()
506
- print("✅ Multi-ControlNet-Inpainting-Pipeline optimiert")
507
 
508
- return pipe_img2img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509
 
510
 
511
 
 
319
  pipe_txt2img = None
320
  current_pipe_model_id = None
321
  pipe_img2img = None
322
+ pipe_img2img_pose = None
323
+ pipe_img2img_depth = None
324
 
325
 
326
  #Das Laden des Modells bedeutet, die trainierten Gewichte (Parameter) von der Festplatte zu lesen und
 
465
  print(f"❌ Auch Fallback fehlgeschlagen: {fallback_error}")
466
  raise
467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
 
469
+ def load_img2img(keep_environment=False):
470
+ """
471
+ Lädt die Multi-ControlNet-Inpainting-Pipeline dynamisch basierend auf dem Modus.
472
+
473
+ Args:
474
+ keep_environment (bool):
475
+ - False: Lädt OpenPose + Canny (für 'Focus verändern')
476
+ - True: Lädt Depth + Canny (für 'Umgebung ändern' oder 'Ausschließlich Gesicht')
477
+
478
+ Returns:
479
+ Die korrekt konfigurierte Pipeline
480
+ """
481
+ global pipe_img2img_pose, pipe_img2img_depth
482
+
483
+ # Initialisiere globale Variablen, falls noch nicht geschehen
484
+ if 'pipe_img2img_pose' not in globals():
485
+ pipe_img2img_pose = None
486
+ if 'pipe_img2img_depth' not in globals():
487
+ pipe_img2img_depth = None
488
+
489
+ if keep_environment:
490
+ # ===== MODUS: Depth + Canny =====
491
+ if pipe_img2img_depth is None:
492
+ print("🔄 Lade Multi-ControlNet-Inpainting-Modell (Depth + Canny)...")
493
+ try:
494
+ # LADE BEIDE ControlNet-Modelle für Depth-Modus
495
+ controlnet_depth = ControlNetModel.from_pretrained(
496
+ "lllyasviel/sd-controlnet-depth",
497
+ torch_dtype=torch_dtype
498
+ )
499
+ controlnet_canny = ControlNetModel.from_pretrained(
500
+ "lllyasviel/sd-controlnet-canny",
501
+ torch_dtype=torch_dtype
502
+ )
503
+
504
+ # WICHTIG: Reihenfolge muss mit prepare_controlnet_maps übereinstimmen!
505
+ # [Depth, Canny]
506
+ pipe_img2img_depth = StableDiffusionControlNetInpaintPipeline.from_pretrained(
507
+ "runwayml/stable-diffusion-v1-5",
508
+ controlnet=[controlnet_depth, controlnet_canny], # Depth zuerst!
509
+ torch_dtype=torch_dtype,
510
+ safety_checker=None,
511
+ requires_safety_checker=False
512
+ ).to(device)
513
+
514
+ # Scheduler konfigurieren
515
+ pipe_img2img_depth.scheduler = DPMSolverMultistepScheduler.from_config(
516
+ pipe_img2img_depth.scheduler.config,
517
+ algorithm_type="sde-dpmsolver++",
518
+ use_karras_sigmas=True,
519
+ timestep_spacing="trailing"
520
+ )
521
+
522
+ # Optimierungen
523
+ pipe_img2img_depth.enable_attention_slicing()
524
+ print("✅ Multi-ControlNet-Inpainting-Pipeline geladen (Depth + Canny)")
525
+
526
+ except Exception as e:
527
+ print(f"❌ Fehler beim Laden der Depth+Canny Pipeline: {e}")
528
+ raise
529
+
530
+ return pipe_img2img_depth
531
+
532
+ else:
533
+ # ===== MODUS: OpenPose + Canny =====
534
+ if pipe_img2img_pose is None:
535
+ print("���� Lade Multi-ControlNet-Inpainting-Modell (OpenPose + Canny)...")
536
+ try:
537
+ # LADE BEIDE ControlNet-Modelle für Pose-Modus
538
+ controlnet_openpose = ControlNetModel.from_pretrained(
539
+ "lllyasviel/sd-controlnet-openpose",
540
+ torch_dtype=torch_dtype
541
+ )
542
+ controlnet_canny = ControlNetModel.from_pretrained(
543
+ "lllyasviel/sd-controlnet-canny",
544
+ torch_dtype=torch_dtype
545
+ )
546
+
547
+ # WICHTIG: Reihenfolge muss mit prepare_controlnet_maps übereinstimmen!
548
+ # [OpenPose, Canny]
549
+ pipe_img2img_pose = StableDiffusionControlNetInpaintPipeline.from_pretrained(
550
+ "runwayml/stable-diffusion-v1-5",
551
+ controlnet=[controlnet_openpose, controlnet_canny], # OpenPose zuerst!
552
+ torch_dtype=torch_dtype,
553
+ safety_checker=None,
554
+ requires_safety_checker=False
555
+ ).to(device)
556
+
557
+ # Scheduler konfigurieren
558
+ pipe_img2img_pose.scheduler = DPMSolverMultistepScheduler.from_config(
559
+ pipe_img2img_pose.scheduler.config,
560
+ algorithm_type="sde-dpmsolver++",
561
+ use_karras_sigmas=True,
562
+ timestep_spacing="trailing"
563
+ )
564
+
565
+ # Optimierungen
566
+ pipe_img2img_pose.enable_attention_slicing()
567
+ print("✅ Multi-ControlNet-Inpainting-Pipeline geladen (OpenPose + Canny)")
568
+
569
+ except Exception as e:
570
+ print(f"❌ Fehler beim Laden der OpenPose+Canny Pipeline: {e}")
571
+ raise
572
+
573
+ return pipe_img2img_pose
574
 
575
 
576