Update app.py
Browse files
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
|
| 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
|
| 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 |
-
|
| 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
|
| 69 |
|
| 70 |
image = result.images[0]
|
| 71 |
|
| 72 |
metadata = {
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"face_enabled": bool(face_enabled),
|
| 76 |
-
"face_weight": float(face_weight) if face_enabled else 0.0,
|
| 77 |
"prompt": prompt,
|
| 78 |
-
"seed":
|
| 79 |
"steps": int(steps),
|
| 80 |
-
"
|
| 81 |
-
"width": int(width),
|
| 82 |
-
"height": int(height),
|
| 83 |
}
|
| 84 |
|
| 85 |
-
base_name = script_name.strip().replace(" ", "_") or "
|
| 86 |
-
img_path =
|
| 87 |
-
json_path =
|
| 88 |
|
| 89 |
pnginfo = PngImagePlugin.PngInfo()
|
| 90 |
-
pnginfo.add_text("
|
| 91 |
image.save(img_path, pnginfo=pnginfo)
|
| 92 |
|
| 93 |
with open(json_path, "w", encoding="utf-8") as f:
|
| 94 |
-
json.dump(metadata, f,
|
| 95 |
|
| 96 |
-
return image, json.dumps(metadata,
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
gr.Markdown("## SDXL 1.0 + LoRA Detailer (Stable Yogi)")
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
with gr.Column():
|
| 104 |
-
prompt = gr.Textbox(
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
face_weight = gr.Slider(0.0, 1.5, 0.8, step=0.05, label="Poids LoRA")
|
| 116 |
|
| 117 |
-
|
| 118 |
-
gr.Button("Générer", variant="primary").click(
|
| 119 |
-
generate,
|
| 120 |
-
|
| 121 |
-
|
| 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__":
|