Leteint commited on
Commit
0385335
·
verified ·
1 Parent(s): 5c17ba6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -54
app.py CHANGED
@@ -7,48 +7,40 @@ from diffusers import AutoPipelineForText2Image
7
  from PIL import PngImagePlugin
8
 
9
  BASE_MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
10
-
11
- # SEULEMENT LoRA face/detail compatible
12
  LORA_FACE_REPO = "akash-guptag/Detailers_By_Stable_Yogi"
13
  LORA_FACE_ADAPTER = "face_detail"
14
 
15
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
16
  DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
17
 
18
- print("Chargement pipeline SDXL...")
19
  pipe = AutoPipelineForText2Image.from_pretrained(
20
  BASE_MODEL_ID,
21
  torch_dtype=DTYPE,
22
- safety_checker=None,
23
- requires_safety_checker=False,
24
  variant="fp16" if DTYPE == torch.float16 else None,
 
 
25
  )
26
  pipe.to(DEVICE)
 
 
 
 
 
 
27
  pipe.set_progress_bar_config(disable=True)
28
 
29
- print("Chargement LoRA face/detail (Stable Yogi)...")
30
  pipe.load_lora_weights(LORA_FACE_REPO, adapter_name=LORA_FACE_ADAPTER)
31
 
32
  os.makedirs("outputs", exist_ok=True)
33
 
34
  @spaces.GPU()
35
- def generate(
36
- prompt: str,
37
- negative_prompt: str,
38
- seed: float,
39
- steps: float,
40
- guidance: float,
41
- width: float,
42
- height: float,
43
- face_enabled: bool,
44
- face_weight: float,
45
- script_name: str,
46
- ):
47
  if not prompt:
48
  return None, "Prompt vide.", ""
49
 
50
- seed_int = int(seed) if seed is not None else 0
51
- generator = torch.Generator(device=DEVICE).manual_seed(seed_int) if seed_int >= 0 else torch.Generator(device=DEVICE)
52
 
53
  adapters = [LORA_FACE_ADAPTER]
54
  weights = [float(face_weight) if face_enabled else 0.0]
@@ -65,60 +57,58 @@ def generate(
65
  generator=generator,
66
  )
67
  except Exception as e:
68
- return None, f"Erreur SDXL : {repr(e)}", ""
69
 
70
  image = result.images[0]
71
 
72
  metadata = {
73
- "base_model": BASE_MODEL_ID,
74
- "face_lora_repo": LORA_FACE_REPO,
75
- "face_enabled": bool(face_enabled),
76
- "face_weight": float(face_weight) if face_enabled else 0.0,
77
  "prompt": prompt,
78
- "seed": seed_int,
79
  "steps": int(steps),
80
- "guidance_scale": float(guidance),
81
- "width": int(width),
82
- "height": int(height),
83
  }
84
 
85
- base_name = script_name.strip().replace(" ", "_") or "sdxl_detailer"
86
- img_path = os.path.join("outputs", f"{base_name}.png")
87
- json_path = os.path.join("outputs", f"{base_name}.json")
88
 
89
  pnginfo = PngImagePlugin.PngInfo()
90
- pnginfo.add_text("generation_params", json.dumps(metadata, ensure_ascii=False))
91
  image.save(img_path, pnginfo=pnginfo)
92
 
93
  with open(json_path, "w", encoding="utf-8") as f:
94
- json.dump(metadata, f, ensure_ascii=False, indent=2)
95
 
96
- return image, json.dumps(metadata, ensure_ascii=False, indent=2), json_path
97
 
98
- # UI simplifiée
99
- with gr.Blocks(title="SDXL + Face Detail LoRA") as demo:
100
- gr.Markdown("## SDXL 1.0 + LoRA Detailer (Stable Yogi)")
101
 
102
  with gr.Row():
103
  with gr.Column():
104
- prompt = gr.Textbox(label="Prompt", lines=4, value="masterpiece, 1girl, detailed face, sharp eyes, realistic skin")
105
- negative = gr.Textbox(label="Negative", value="blurry, deformed, low quality")
106
-
107
- seed = gr.Number(label="Seed (-1=random)", value=-1)
108
- steps = gr.Slider(10, 60, 30, step=1, label="Steps")
109
- guidance = gr.Slider(1.0, 20.0, 7.0, step=0.5, label="Guidance")
110
-
111
- width = gr.Slider(512, 1536, 1024, step=64, label="Width")
112
- height = gr.Slider(512, 1536, 1024, step=64, label="Height")
 
 
 
 
113
 
114
- face_enabled = gr.Checkbox("Activer LoRA face/detail", value=True)
115
- face_weight = gr.Slider(0.0, 1.5, 0.8, step=0.05, label="Poids LoRA")
116
 
117
- script_name = gr.Textbox("Nom sortie", value="test_detailer")
118
- gr.Button("Générer", variant="primary").click(
119
- generate,
120
- inputs=[prompt, negative, seed, steps, guidance, width, height, face_enabled, face_weight, script_name],
121
- outputs=[gr.Image(label="Image"), gr.Textbox(label="JSON", lines=15), gr.File(label="Télécharger JSON")]
122
  )
123
 
124
  if __name__ == "__main__":
 
7
  from PIL import PngImagePlugin
8
 
9
  BASE_MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
 
 
10
  LORA_FACE_REPO = "akash-guptag/Detailers_By_Stable_Yogi"
11
  LORA_FACE_ADAPTER = "face_detail"
12
 
13
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
14
  DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
15
 
16
+ print("Chargement SDXL...")
17
  pipe = AutoPipelineForText2Image.from_pretrained(
18
  BASE_MODEL_ID,
19
  torch_dtype=DTYPE,
 
 
20
  variant="fp16" if DTYPE == torch.float16 else None,
21
+ safety_checker=None,
22
+ requires_safety_checker=False,
23
  )
24
  pipe.to(DEVICE)
25
+
26
+ # MONKEY PATCH NSFW : Force "toujours safe"
27
+ def dummy_safety_checker(images, **kwargs):
28
+ return images, [False] * len(images)
29
+
30
+ pipe.safety_checker = dummy_safety_checker
31
  pipe.set_progress_bar_config(disable=True)
32
 
33
+ print("Chargement LoRA face...")
34
  pipe.load_lora_weights(LORA_FACE_REPO, adapter_name=LORA_FACE_ADAPTER)
35
 
36
  os.makedirs("outputs", exist_ok=True)
37
 
38
  @spaces.GPU()
39
+ def generate(prompt, negative_prompt, seed, steps, guidance, width, height, face_enabled, face_weight, script_name):
 
 
 
 
 
 
 
 
 
 
 
40
  if not prompt:
41
  return None, "Prompt vide.", ""
42
 
43
+ generator = torch.Generator(DEVICE).manual_seed(int(seed)) if seed >= 0 else torch.Generator(DEVICE)
 
44
 
45
  adapters = [LORA_FACE_ADAPTER]
46
  weights = [float(face_weight) if face_enabled else 0.0]
 
57
  generator=generator,
58
  )
59
  except Exception as e:
60
+ return None, f"Erreur: {e}", ""
61
 
62
  image = result.images[0]
63
 
64
  metadata = {
65
+ "model": BASE_MODEL_ID,
66
+ "lora_face": LORA_FACE_REPO,
 
 
67
  "prompt": prompt,
68
+ "seed": int(seed),
69
  "steps": int(steps),
70
+ "guidance": float(guidance),
 
 
71
  }
72
 
73
+ base_name = script_name.strip().replace(" ", "_") or "nsfw_sdxl"
74
+ img_path = f"outputs/{base_name}.png"
75
+ json_path = f"outputs/{base_name}.json"
76
 
77
  pnginfo = PngImagePlugin.PngInfo()
78
+ pnginfo.add_text("params", json.dumps(metadata))
79
  image.save(img_path, pnginfo=pnginfo)
80
 
81
  with open(json_path, "w", encoding="utf-8") as f:
82
+ json.dump(metadata, f, indent=2)
83
 
84
+ return image, json.dumps(metadata, indent=2), json_path
85
 
86
+ with gr.Blocks(title="SDXL NSFW Unlocked") as demo:
87
+ gr.Markdown("## SDXL 1.0 NSFW ✅ + Face Detail LoRA")
 
88
 
89
  with gr.Row():
90
  with gr.Column():
91
+ prompt = gr.Textbox(
92
+ "Prompt NSFW", lines=4,
93
+ value="masterpiece, best quality, 1girl, nude, detailed anatomy, realistic skin, sharp face, hourglass figure"
94
+ )
95
+ negative = gr.Textbox(
96
+ "Negative", value="blurry, deformed, ugly, lowres, extra limbs"
97
+ )
98
+ seed = gr.Number("Seed (-1=random)", value=-1)
99
+ steps = gr.Slider(20, 50, 35, step=1, label="Steps")
100
+ guidance = gr.Slider(5.0, 12.0, 7.5, step=0.1, label="Guidance")
101
+ w, h = 1024, 1024
102
+ width = gr.Slider(512, 1536, w, step=64, label="Width")
103
+ height = gr.Slider(512, 1536, h, step=64, label="Height")
104
 
105
+ face_weight = gr.Slider(0.0, 1.2, 0.7, step=0.05, label="LoRA Face Weight")
 
106
 
107
+ name = gr.Textbox("Nom fichier", value="nsfw_test")
108
+ gr.Button("🚀 Générer NSFW", variant="primary").click(
109
+ generate,
110
+ [prompt, negative, seed, steps, guidance, width, height, True, face_weight, name],
111
+ [gr.Image("Image"), gr.Textbox("Metadata", lines=10), gr.File("JSON")]
112
  )
113
 
114
  if __name__ == "__main__":