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("π₯ SDXL NSFW
|
| 17 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 18 |
BASE_MODEL_ID,
|
| 19 |
torch_dtype=DTYPE,
|
|
@@ -23,22 +23,21 @@ pipe = AutoPipelineForText2Image.from_pretrained(
|
|
| 23 |
)
|
| 24 |
pipe.to(DEVICE)
|
| 25 |
|
| 26 |
-
# BYPASS NSFW total
|
| 27 |
def dummy_checker(images, **kwargs):
|
| 28 |
return images, [False] * len(images)
|
| 29 |
pipe.safety_checker = dummy_checker
|
| 30 |
pipe.set_progress_bar_config(disable=True)
|
| 31 |
|
| 32 |
-
print("LoRA
|
| 33 |
pipe.load_lora_weights(LORA_FACE_REPO, adapter_name=LORA_FACE_ADAPTER)
|
| 34 |
|
| 35 |
os.makedirs("outputs", exist_ok=True)
|
| 36 |
|
| 37 |
@spaces.GPU()
|
| 38 |
def generate(prompt, negative, seed_str, steps, guidance, width, height, face_weight, filename):
|
| 39 |
-
#
|
| 40 |
try:
|
| 41 |
-
seed = int(float(seed_str)) if seed_str
|
| 42 |
except:
|
| 43 |
seed = -1
|
| 44 |
|
|
@@ -47,8 +46,8 @@ def generate(prompt, negative, seed_str, steps, guidance, width, height, face_we
|
|
| 47 |
pipe.set_adapters([LORA_FACE_ADAPTER], adapter_weights=[float(face_weight)])
|
| 48 |
|
| 49 |
result = pipe(
|
| 50 |
-
prompt=prompt,
|
| 51 |
-
negative_prompt=negative
|
| 52 |
num_inference_steps=int(steps),
|
| 53 |
guidance_scale=float(guidance),
|
| 54 |
width=int(width),
|
|
@@ -56,53 +55,45 @@ def generate(prompt, negative, seed_str, steps, guidance, width, height, face_we
|
|
| 56 |
generator=generator,
|
| 57 |
).images[0]
|
| 58 |
|
| 59 |
-
metadata = {
|
| 60 |
-
"model": BASE_MODEL_ID,
|
| 61 |
-
"lora": LORA_FACE_REPO,
|
| 62 |
-
"prompt": prompt,
|
| 63 |
-
"seed": seed,
|
| 64 |
-
"steps": int(steps),
|
| 65 |
-
"guidance": float(guidance),
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
safe_name = filename.strip().replace(" ", "_").replace("/", "_") or "sdxl_nsfw"
|
| 69 |
img_path = f"outputs/{safe_name}.png"
|
| 70 |
json_path = f"outputs/{safe_name}.json"
|
| 71 |
|
|
|
|
|
|
|
| 72 |
pnginfo = PngImagePlugin.PngInfo()
|
| 73 |
pnginfo.add_text("params", json.dumps(metadata))
|
| 74 |
result.save(img_path, pnginfo=pnginfo)
|
| 75 |
|
| 76 |
-
with open(json_path, "w"
|
| 77 |
json.dump(metadata, f, indent=2)
|
| 78 |
|
| 79 |
return result, json.dumps(metadata, indent=2), json_path
|
| 80 |
|
| 81 |
-
with gr.Blocks(title="π₯ SDXL NSFW
|
| 82 |
-
gr.Markdown("# SDXL 1.0 **NSFW
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
with gr.Column():
|
| 86 |
-
prompt = gr.Textbox("Prompt",
|
| 87 |
-
negative = gr.Textbox("Negative", value="blurry, deformed, ugly
|
| 88 |
-
seed = gr.Number("Seed
|
| 89 |
-
steps = gr.Slider(20, 50, 35, step=1
|
| 90 |
-
guidance = gr.Slider(5
|
| 91 |
-
width = gr.Slider(512, 1536, 1024, step=64
|
| 92 |
-
height = gr.Slider(512, 1536, 1024, step=64
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
with gr.Column():
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
gr.Button("π Generate
|
| 102 |
generate,
|
| 103 |
-
[prompt, negative, seed, steps, guidance, width, height,
|
| 104 |
-
[
|
| 105 |
)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
demo.launch()
|
|
|
|
| 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...")
|
| 17 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 18 |
BASE_MODEL_ID,
|
| 19 |
torch_dtype=DTYPE,
|
|
|
|
| 23 |
)
|
| 24 |
pipe.to(DEVICE)
|
| 25 |
|
|
|
|
| 26 |
def dummy_checker(images, **kwargs):
|
| 27 |
return images, [False] * len(images)
|
| 28 |
pipe.safety_checker = dummy_checker
|
| 29 |
pipe.set_progress_bar_config(disable=True)
|
| 30 |
|
| 31 |
+
print("LoRA...")
|
| 32 |
pipe.load_lora_weights(LORA_FACE_REPO, adapter_name=LORA_FACE_ADAPTER)
|
| 33 |
|
| 34 |
os.makedirs("outputs", exist_ok=True)
|
| 35 |
|
| 36 |
@spaces.GPU()
|
| 37 |
def generate(prompt, negative, seed_str, steps, guidance, width, height, face_weight, filename):
|
| 38 |
+
# SEED SAFE
|
| 39 |
try:
|
| 40 |
+
seed = int(float(seed_str)) if seed_str.strip() else -1
|
| 41 |
except:
|
| 42 |
seed = -1
|
| 43 |
|
|
|
|
| 46 |
pipe.set_adapters([LORA_FACE_ADAPTER], adapter_weights=[float(face_weight)])
|
| 47 |
|
| 48 |
result = pipe(
|
| 49 |
+
prompt=prompt or "masterpiece",
|
| 50 |
+
negative_prompt=negative,
|
| 51 |
num_inference_steps=int(steps),
|
| 52 |
guidance_scale=float(guidance),
|
| 53 |
width=int(width),
|
|
|
|
| 55 |
generator=generator,
|
| 56 |
).images[0]
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
safe_name = filename.strip().replace(" ", "_").replace("/", "_") or "sdxl_nsfw"
|
| 59 |
img_path = f"outputs/{safe_name}.png"
|
| 60 |
json_path = f"outputs/{safe_name}.json"
|
| 61 |
|
| 62 |
+
metadata = {"prompt": prompt, "seed": seed, "steps": int(steps), "guidance": float(guidance)}
|
| 63 |
+
|
| 64 |
pnginfo = PngImagePlugin.PngInfo()
|
| 65 |
pnginfo.add_text("params", json.dumps(metadata))
|
| 66 |
result.save(img_path, pnginfo=pnginfo)
|
| 67 |
|
| 68 |
+
with open(json_path, "w") as f:
|
| 69 |
json.dump(metadata, f, indent=2)
|
| 70 |
|
| 71 |
return result, json.dumps(metadata, indent=2), json_path
|
| 72 |
|
| 73 |
+
with gr.Blocks(title="π₯ SDXL NSFW") as demo:
|
| 74 |
+
gr.Markdown("# SDXL 1.0 **NSFW** + Face LoRA\nβ
No Errors")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column():
|
| 78 |
+
prompt = gr.Textbox("Prompt", value="1girl nude, masterpiece, detailed skin, sharp face")
|
| 79 |
+
negative = gr.Textbox("Negative", value="blurry, deformed, ugly")
|
| 80 |
+
seed = gr.Number("Seed", value=-1)
|
| 81 |
+
steps = gr.Slider(20, 50, 35, step=1)
|
| 82 |
+
guidance = gr.Slider(5, 12, 7.5, step=0.1)
|
| 83 |
+
width = gr.Slider(512, 1536, 1024, step=64)
|
| 84 |
+
height = gr.Slider(512, 1536, 1024, step=64)
|
| 85 |
+
lora_w = gr.Slider(0, 1.2, 0.7, step=0.05, label="LoRA Weight")
|
| 86 |
+
name = gr.Textbox("Filename", value="test_nsfw")
|
| 87 |
|
| 88 |
with gr.Column():
|
| 89 |
+
img_out = gr.Image(label="Image")
|
| 90 |
+
json_out = gr.Textbox(label="Metadata", lines=8)
|
| 91 |
+
file_out = gr.File(label="Download JSON") # β FIX: label= au lieu de positional
|
| 92 |
|
| 93 |
+
gr.Button("π NSFW Generate", variant="primary").click(
|
| 94 |
generate,
|
| 95 |
+
[prompt, negative, seed, steps, guidance, width, height, lora_w, name],
|
| 96 |
+
[img_out, json_out, file_out]
|
| 97 |
)
|
| 98 |
|
| 99 |
+
demo.launch()
|
|
|