Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ LORA_FACE_ADAPTER = "face_detail"
|
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 15 |
|
| 16 |
-
print("
|
| 17 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 18 |
BASE_MODEL_ID,
|
| 19 |
torch_dtype=DTYPE,
|
|
@@ -23,92 +23,76 @@ pipe = AutoPipelineForText2Image.from_pretrained(
|
|
| 23 |
)
|
| 24 |
pipe.to(DEVICE)
|
| 25 |
|
| 26 |
-
# MONKEY PATCH NSFW
|
| 27 |
-
def
|
| 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("
|
| 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,
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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 |
-
"
|
| 67 |
"prompt": prompt,
|
| 68 |
"seed": int(seed),
|
| 69 |
"steps": int(steps),
|
| 70 |
"guidance": float(guidance),
|
| 71 |
}
|
| 72 |
|
| 73 |
-
|
| 74 |
-
img_path = f"outputs/{
|
| 75 |
-
json_path = f"outputs/{
|
| 76 |
|
| 77 |
pnginfo = PngImagePlugin.PngInfo()
|
| 78 |
pnginfo.add_text("params", json.dumps(metadata))
|
| 79 |
-
|
| 80 |
|
| 81 |
with open(json_path, "w", encoding="utf-8") as f:
|
| 82 |
json.dump(metadata, f, indent=2)
|
| 83 |
|
| 84 |
-
return
|
| 85 |
|
| 86 |
-
with gr.Blocks(title="SDXL NSFW Unlocked") as demo:
|
| 87 |
-
gr.Markdown("#
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
-
with gr.Column():
|
| 91 |
-
prompt = gr.Textbox(
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
|
| 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 |
-
|
| 108 |
-
gr.Button("🚀
|
| 109 |
-
generate,
|
| 110 |
-
[prompt, negative, seed, steps, guidance, width, height,
|
| 111 |
-
[gr.Image("Image"), gr.Textbox("Metadata
|
| 112 |
)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
|
|
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 15 |
|
| 16 |
+
print("SDXL NSFW loading...")
|
| 17 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 18 |
BASE_MODEL_ID,
|
| 19 |
torch_dtype=DTYPE,
|
|
|
|
| 23 |
)
|
| 24 |
pipe.to(DEVICE)
|
| 25 |
|
| 26 |
+
# MONKEY PATCH NSFW
|
| 27 |
+
def dummy_checker(images, **kwargs):
|
| 28 |
return images, [False] * len(images)
|
| 29 |
+
pipe.safety_checker = dummy_checker
|
| 30 |
|
|
|
|
| 31 |
pipe.set_progress_bar_config(disable=True)
|
| 32 |
|
| 33 |
+
print("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, seed, steps, guidance, width, height, face_weight, name):
|
|
|
|
|
|
|
|
|
|
| 40 |
generator = torch.Generator(DEVICE).manual_seed(int(seed)) if seed >= 0 else torch.Generator(DEVICE)
|
| 41 |
|
| 42 |
+
pipe.set_adapters([LORA_FACE_ADAPTER], adapter_weights=[face_weight])
|
| 43 |
+
|
| 44 |
+
result = pipe(
|
| 45 |
+
prompt=prompt,
|
| 46 |
+
negative_prompt=negative or None,
|
| 47 |
+
num_inference_steps=int(steps),
|
| 48 |
+
guidance_scale=float(guidance),
|
| 49 |
+
width=int(width),
|
| 50 |
+
height=int(height),
|
| 51 |
+
generator=generator,
|
| 52 |
+
).images[0]
|
| 53 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
metadata = {
|
| 55 |
"model": BASE_MODEL_ID,
|
| 56 |
+
"lora": LORA_FACE_REPO,
|
| 57 |
"prompt": prompt,
|
| 58 |
"seed": int(seed),
|
| 59 |
"steps": int(steps),
|
| 60 |
"guidance": float(guidance),
|
| 61 |
}
|
| 62 |
|
| 63 |
+
safe_name = name.strip().replace(" ", "_") or "sdxl_nsfw"
|
| 64 |
+
img_path = f"outputs/{safe_name}.png"
|
| 65 |
+
json_path = f"outputs/{safe_name}.json"
|
| 66 |
|
| 67 |
pnginfo = PngImagePlugin.PngInfo()
|
| 68 |
pnginfo.add_text("params", json.dumps(metadata))
|
| 69 |
+
result.save(img_path, pnginfo=pnginfo)
|
| 70 |
|
| 71 |
with open(json_path, "w", encoding="utf-8") as f:
|
| 72 |
json.dump(metadata, f, indent=2)
|
| 73 |
|
| 74 |
+
return result, json.dumps(metadata, indent=2), json_path # ← chemin relatif OK pour gr.File
|
| 75 |
|
| 76 |
+
with gr.Blocks(title="🔥 SDXL NSFW Unlocked") as demo:
|
| 77 |
+
gr.Markdown("# SDXL 1.0 NSFW + Face Detail LoRA\n✅ Safety checker BYPASSED")
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
+
with gr.Column(scale=1):
|
| 81 |
+
prompt = gr.Textbox("Prompt", lines=4, value="1girl nude, masterpiece, detailed anatomy, realistic skin, sharp face")
|
| 82 |
+
negative = gr.Textbox("Negative", value="blurry, deformed, ugly, low quality")
|
| 83 |
+
seed = gr.Number("Seed", value=-1)
|
| 84 |
+
steps = gr.Slider(20, 50, 35, step=1)
|
| 85 |
+
guidance = gr.Slider(5, 12, 7.5, step=0.1)
|
| 86 |
+
width = gr.Slider(512, 1536, 1024, step=64)
|
| 87 |
+
height = gr.Slider(512, 1536, 1024, step=64)
|
| 88 |
+
face_weight = gr.Slider(0, 1.2, 0.7, step=0.05, label="LoRA Weight")
|
| 89 |
+
filename = gr.Textbox("Filename", value="nsfw_test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
with gr.Column(scale=1):
|
| 92 |
+
gr.Button("🚀 Generate NSFW", variant="primary", size="lg").click(
|
| 93 |
+
generate,
|
| 94 |
+
[prompt, negative, seed, steps, guidance, width, height, face_weight, filename],
|
| 95 |
+
[gr.Image(label="Generated Image"), gr.Textbox(label="Metadata JSON"), gr.File(label="Download JSON")] # ← FIX ICI
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|