panelforge commited on
Commit
f13995d
·
verified ·
1 Parent(s): ce6ba71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -4,46 +4,43 @@ import random
4
  import spaces #[uncomment to use ZeroGPU]
5
  from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
6
  import torch
7
- from transformers import AutoModelForObjectDetection, AutoImageProcessor
 
 
 
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" # Your diffusion model
11
 
12
  # Load your main diffusion pipeline
13
- if torch.cuda.is_available():
14
- torch_dtype = torch.float16
15
- else:
16
- torch_dtype = torch.float32
17
-
18
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
19
  pipe.scheduler = DPMSolverSDEScheduler.from_config(pipe.scheduler.config, algorithm_type="dpmsolver++", solver_order=2, use_karras_sigmas=True)
20
  pipe = pipe.to(device)
21
 
22
  MAX_SEED = np.iinfo(np.int32).max
23
  MAX_IMAGE_SIZE = 1024
24
 
25
- # Load ADetailer model (from Hugging Face)
26
- adetailer_model_id = "Bingsu/adetailer"
27
- adetailer_model = AutoModelForObjectDetection.from_pretrained(adetailer_model_id)
28
- adetailer_processor = AutoImageProcessor.from_pretrained(adetailer_model_id)
29
 
30
  def fix_eyes_with_adetailer(image):
31
- # Convert image to format for ADetailer
32
- pixel_values = adetailer_processor(images=image, return_tensors="pt").pixel_values
33
- pixel_values = pixel_values.to(device)
34
 
35
- # Run ADetailer on the image
36
- with torch.no_grad():
37
- outputs = adetailer_model(pixel_values=pixel_values)
38
 
39
- # Post-process the outputs and apply the fixes (if any)
40
- corrected_image = image # Placeholder for the actual post-processing
41
- # Apply fixes based on the detection and correction model outputs
42
- # This step requires actual ADetailer implementation details for correcting eyes.
43
-
44
- return corrected_image # Return the corrected image
 
45
 
46
- @spaces.GPU #[uncomment to use ZeroGPU]
47
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
48
 
49
  if randomize_seed:
@@ -51,6 +48,7 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
51
 
52
  generator = torch.Generator().manual_seed(seed)
53
 
 
54
  image = pipe(
55
  prompt=prompt,
56
  negative_prompt=negative_prompt,
@@ -61,7 +59,7 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
61
  generator=generator
62
  ).images[0]
63
 
64
- # Apply ADetailer to fix eyes after generating the image
65
  corrected_image = fix_eyes_with_adetailer(image)
66
 
67
  return corrected_image, seed
 
4
  import spaces #[uncomment to use ZeroGPU]
5
  from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
6
  import torch
7
+ from huggingface_hub import hf_hub_download
8
+ from ultralytics import YOLO
9
+ import cv2
10
+ from PIL import Image
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" # Your diffusion model
14
 
15
  # Load your main diffusion pipeline
16
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch.float16)
 
 
 
 
 
17
  pipe.scheduler = DPMSolverSDEScheduler.from_config(pipe.scheduler.config, algorithm_type="dpmsolver++", solver_order=2, use_karras_sigmas=True)
18
  pipe = pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
+ # Download the ADetailer YOLOv8 face detection model
24
+ yolo_model_path = hf_hub_download(repo_id="Bingsu/adetailer", filename="face_yolov8n.pt")
25
+ yolo_model = YOLO(yolo_model_path)
 
26
 
27
  def fix_eyes_with_adetailer(image):
28
+ # Convert PIL image to OpenCV format for YOLO
29
+ img = np.array(image)
30
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
31
 
32
+ # Run the YOLO model on the image
33
+ results = yolo_model(img)
 
34
 
35
+ # Visualize and process the output
36
+ pred = results[0].plot() # Draw bounding boxes and other detections
37
+ pred = cv2.cvtColor(pred, cv2.COLOR_BGR2RGB)
38
+
39
+ # Convert the processed image back to PIL format
40
+ corrected_image = Image.fromarray(pred)
41
+ return corrected_image
42
 
43
+ @spaces.GPU #[uncomment to use ZeroGPU]
44
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
45
 
46
  if randomize_seed:
 
48
 
49
  generator = torch.Generator().manual_seed(seed)
50
 
51
+ # Generate the initial image with the diffusion model
52
  image = pipe(
53
  prompt=prompt,
54
  negative_prompt=negative_prompt,
 
59
  generator=generator
60
  ).images[0]
61
 
62
+ # Apply ADetailer to fix the eyes after generating the image
63
  corrected_image = fix_eyes_with_adetailer(image)
64
 
65
  return corrected_image, seed