4tyzap commited on
Commit
10c34d9
·
verified ·
1 Parent(s): 37e79ed

Upload adetailer_patch.py

Browse files
RuinedFooocus/modules/adetailer_patch.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Monkey patch for integrating ADetailer into RuinedFooocus. This module patches the process function from modules.sdxl_pipeline so that after the original image generation, ADetailer can optionally refine the results if the `use_adetailer` flag is enabled in settings or in the gen_data dictionary. This patch is loaded by importing modules.adetailer_patch in settings.py.
3
+ """
4
+ from modules import settings
5
+ from modules import sdxl_pipeline
6
+ import numpy as np
7
+
8
+ try:
9
+ import adetailer
10
+ except Exception:
11
+ adetailer = None
12
+
13
+ # Save reference to original process
14
+ _original_process = sdxl_pipeline.process
15
+
16
+
17
+ def patched_process(*args, **kwargs):
18
+ """Wrapper for sdxl_pipeline.process that applies ADetailer if enabled."""
19
+ # Call original sdxl pipeline process to generate images (list of numpy arrays)
20
+ images = _original_process(*args, **kwargs)
21
+ # Determine whether to use ADetailer
22
+ use_flag = False
23
+ gen_data = None
24
+ if args:
25
+ gen_data = args[0]
26
+ # Check if gen_data is a dict or object with use_adetailer flag
27
+ try:
28
+ if isinstance(gen_data, dict) and gen_data.get("use_adetailer") is not None:
29
+ use_flag = bool(gen_data.get("use_adetailer"))
30
+ elif hasattr(gen_data, "use_adetailer"):
31
+ use_flag = bool(getattr(gen_data, "use_adetailer"))
32
+ except Exception:
33
+ pass
34
+ # Fall back to global settings
35
+ if not use_flag:
36
+ try:
37
+ use_flag = settings.get_setting("use_adetailer")
38
+ except Exception:
39
+ use_flag = False
40
+ # If enabled and adetailer available, process each image
41
+ if use_flag and adetailer is not None:
42
+ processed = []
43
+ for img in images:
44
+ try:
45
+ from PIL import Image
46
+ pil_img = Image.fromarray(img)
47
+ result = pil_img
48
+ # Attempt various functions to run adetailer
49
+ try:
50
+ if hasattr(adetailer, "run"):
51
+ result = adetailer.run(pil_img)
52
+ elif hasattr(adetailer, "process"):
53
+ result = adetailer.process(pil_img)
54
+ else:
55
+ from adetailer import scripts # type: ignore
56
+ if hasattr(scripts, "ladetailer"):
57
+ result = scripts.ladetailer.process(pil_img) # type: ignore
58
+ except Exception:
59
+ result = pil_img
60
+ processed.append(np.array(result))
61
+ except Exception as e:
62
+ print("ADetailer processing error:", e)
63
+ processed.append(img)
64
+ images = processed
65
+ return images
66
+
67
+ # Apply patch
68
+ sdxl_pipeline.process = patched_process
69
+ print("ADetailer patch loaded: sdxl_pipeline.process has been patched.")