Spaces:
Running
on
Zero
Running
on
Zero
π Fix: ensure DeepFashion2 model is downloaded before loading
Browse files
app.py
CHANGED
|
@@ -1,19 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
MODEL_URL = "https://huggingface.co/Bingsu/adetailer/resolve/main/deepfashion2_yolov8s-seg.pt"
|
| 4 |
-
MODEL_PATH = "deepfashion2_yolov8s-seg.pt"
|
| 5 |
-
|
| 6 |
-
# βββ Download DeepFashion2 model if not already present βββ
|
| 7 |
-
if not os.path.exists(MODEL_PATH):
|
| 8 |
-
import urllib.request
|
| 9 |
-
print("[INFO] Downloading DeepFashion2 YOLOv8 model...")
|
| 10 |
-
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 11 |
-
print("[INFO] Model downloaded.")
|
| 12 |
-
else:
|
| 13 |
-
print("[INFO] DeepFashion2 model already exists.")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
import traceback
|
| 18 |
from datetime import datetime
|
| 19 |
import torch, gc
|
|
@@ -21,16 +6,18 @@ from PIL import Image
|
|
| 21 |
import gradio as gr
|
| 22 |
|
| 23 |
from inference import generate_with_lora
|
| 24 |
-
from background_edit import run_background_removal_and_inpaint
|
| 25 |
-
from background_edit import run_clothing_inpaint
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
def _print_trace():
|
| 35 |
traceback.print_exc()
|
| 36 |
|
|
@@ -38,10 +25,11 @@ def unload_models():
|
|
| 38 |
torch.cuda.empty_cache()
|
| 39 |
gc.collect()
|
| 40 |
|
| 41 |
-
def
|
| 42 |
image,
|
| 43 |
prompt_1, neg_1, strength_1, guidance_1,
|
| 44 |
-
prompt_2, neg_2, guidance_2
|
|
|
|
| 45 |
):
|
| 46 |
try:
|
| 47 |
if image is None:
|
|
@@ -66,100 +54,78 @@ def safe_generate_and_inpaint(
|
|
| 66 |
# Step 2: Background Inpainting
|
| 67 |
print("[INFO] Step 2: Inpainting background...", flush=True)
|
| 68 |
unload_models()
|
| 69 |
-
|
| 70 |
image_path=path,
|
| 71 |
prompt=prompt_2,
|
| 72 |
negative_prompt=neg_2,
|
| 73 |
guidance_scale=guidance_2
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
except gr.Error as e:
|
| 79 |
-
return None, None, f"π {str(e)}"
|
| 80 |
except Exception as e:
|
| 81 |
_print_trace()
|
| 82 |
-
return None, None, f"β Unexpected Error: {type(e).__name__}: {str(e)}"
|
| 83 |
-
|
| 84 |
-
def guarded_clothing(image, prompt, neg, guidance):
|
| 85 |
-
try:
|
| 86 |
-
result, err = run_clothing_inpaint(image, prompt, neg, guidance)
|
| 87 |
-
return result, err
|
| 88 |
-
except Exception as e:
|
| 89 |
-
import traceback
|
| 90 |
-
traceback.print_exc()
|
| 91 |
-
return None, f"β Unexpected Error: {type(e).__name__}: {str(e)}"
|
| 92 |
|
| 93 |
-
#
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
-
gr.Markdown("## π§ Headshot + Background Generator (
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
input_image = gr.Image(type="pil", label="Upload Headshot")
|
| 99 |
|
| 100 |
gr.Markdown("### Step 1: Headshot Refinement (LoRA)")
|
| 101 |
-
|
| 102 |
with gr.Row():
|
| 103 |
prompt_1 = gr.Textbox(label="Headshot Prompt", value="a professional headshot of a confident woman in her 30s with blonde hair")
|
| 104 |
neg_1 = gr.Textbox(label="Headshot Negative Prompt", value="deformed, cartoon, anime, sketch, blurry, low quality")
|
| 105 |
-
|
| 106 |
with gr.Row():
|
| 107 |
strength_1 = gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Refinement Strength")
|
| 108 |
guidance_1 = gr.Slider(1, 20, value=17, step=0.5, label="Guidance Scale (Headshot)")
|
| 109 |
|
| 110 |
gr.Markdown("### Step 2: Background Inpainting (SDXL)")
|
| 111 |
-
|
| 112 |
with gr.Row():
|
| 113 |
prompt_2 = gr.Textbox(label="Background Prompt", value="modern hospital background, clean, soft lighting")
|
| 114 |
neg_2 = gr.Textbox(label="Background Negative Prompt", value="fantasy, cartoon, cluttered, sketch")
|
| 115 |
-
|
| 116 |
with gr.Row():
|
| 117 |
guidance_2 = gr.Slider(1, 20, value=10, step=0.5, label="Guidance Scale (Background)")
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
output_refined = gr.Image(type="pil", label="Step 1: Refined Headshot")
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
error_box = gr.Markdown(label="Error", value="", visible=True)
|
| 126 |
|
| 127 |
go_btn.click(
|
| 128 |
-
fn=
|
| 129 |
inputs=[
|
| 130 |
-
input_image,
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
],
|
| 133 |
-
outputs=[output_refined, output_final, error_box]
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Markdown("### π Step 3: Clothing Replacement")
|
| 137 |
-
|
| 138 |
-
with gr.Row():
|
| 139 |
-
clothing_prompt = gr.Textbox(
|
| 140 |
-
label="Clothing Prompt",
|
| 141 |
-
value="white female CEO professional blazer, clean look"
|
| 142 |
-
)
|
| 143 |
-
clothing_negative = gr.Textbox(
|
| 144 |
-
label="Clothing Negative Prompt",
|
| 145 |
-
value="hoodie, casual wear, fantasy, cartoon, jeans, distorted, blurry"
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
with gr.Row():
|
| 149 |
-
clothing_guidance = gr.Slider(1, 20, value=17.0, step=0.5, label="Clothing Guidance Scale")
|
| 150 |
-
|
| 151 |
-
with gr.Row():
|
| 152 |
-
clothing_btn = gr.Button("π§΅ Inpaint Clothing")
|
| 153 |
-
clothing_output = gr.Image(type="pil", label="Step 3: Final Image with New Clothing")
|
| 154 |
-
|
| 155 |
-
clothing_error = gr.Markdown(label="Clothing Error", value="", visible=True)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
clothing_btn.click(
|
| 159 |
-
fn=guarded_clothing,
|
| 160 |
-
inputs=[output_final, clothing_prompt, clothing_negative, clothing_guidance],
|
| 161 |
-
outputs=[clothing_output, clothing_error],
|
| 162 |
-
preprocess=False
|
| 163 |
)
|
| 164 |
|
| 165 |
demo.launch(debug=True)
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import traceback
|
| 3 |
from datetime import datetime
|
| 4 |
import torch, gc
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
from inference import generate_with_lora
|
| 9 |
+
from background_edit import run_background_removal_and_inpaint, run_clothing_inpaint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Ensure DeepFashion2 model is downloaded early
|
| 12 |
+
MODEL_URL = "https://huggingface.co/Bingsu/adetailer/resolve/main/deepfashion2_yolov8s-seg.pt"
|
| 13 |
+
MODEL_PATH = "deepfashion2_yolov8s-seg.pt"
|
| 14 |
+
if not os.path.exists(MODEL_PATH):
|
| 15 |
+
import urllib.request
|
| 16 |
+
print("[INFO] Downloading DeepFashion2 YOLOv8 model...")
|
| 17 |
+
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 18 |
+
print("[INFO] Model downloaded.")
|
| 19 |
|
| 20 |
+
# βββββββββββββββ Helpers βββββββββββββββ
|
| 21 |
def _print_trace():
|
| 22 |
traceback.print_exc()
|
| 23 |
|
|
|
|
| 25 |
torch.cuda.empty_cache()
|
| 26 |
gc.collect()
|
| 27 |
|
| 28 |
+
def safe_generate_all_steps(
|
| 29 |
image,
|
| 30 |
prompt_1, neg_1, strength_1, guidance_1,
|
| 31 |
+
prompt_2, neg_2, guidance_2,
|
| 32 |
+
prompt_3, neg_3, guidance_3
|
| 33 |
):
|
| 34 |
try:
|
| 35 |
if image is None:
|
|
|
|
| 54 |
# Step 2: Background Inpainting
|
| 55 |
print("[INFO] Step 2: Inpainting background...", flush=True)
|
| 56 |
unload_models()
|
| 57 |
+
with_bg = run_background_removal_and_inpaint(
|
| 58 |
image_path=path,
|
| 59 |
prompt=prompt_2,
|
| 60 |
negative_prompt=neg_2,
|
| 61 |
guidance_scale=guidance_2
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Step 3: Clothing Inpainting
|
| 65 |
+
print("[INFO] Step 3: Inpainting clothing...", flush=True)
|
| 66 |
+
final, err = run_clothing_inpaint(
|
| 67 |
+
with_bg,
|
| 68 |
+
prompt_3,
|
| 69 |
+
neg_3,
|
| 70 |
+
guidance_3
|
| 71 |
+
)
|
| 72 |
+
if err:
|
| 73 |
+
return refined, with_bg, None, err
|
| 74 |
+
return refined, with_bg, final, ""
|
| 75 |
|
| 76 |
except gr.Error as e:
|
| 77 |
+
return None, None, None, f"π {str(e)}"
|
| 78 |
except Exception as e:
|
| 79 |
_print_trace()
|
| 80 |
+
return None, None, None, f"β Unexpected Error: {type(e).__name__}: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# βββββββββββββββ Gradio UI βββββββββββββββ
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown("## π§ Full Headshot + Background + Clothing Generator (One Click)")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
input_image = gr.Image(type="pil", label="Upload Headshot")
|
| 88 |
|
| 89 |
gr.Markdown("### Step 1: Headshot Refinement (LoRA)")
|
|
|
|
| 90 |
with gr.Row():
|
| 91 |
prompt_1 = gr.Textbox(label="Headshot Prompt", value="a professional headshot of a confident woman in her 30s with blonde hair")
|
| 92 |
neg_1 = gr.Textbox(label="Headshot Negative Prompt", value="deformed, cartoon, anime, sketch, blurry, low quality")
|
|
|
|
| 93 |
with gr.Row():
|
| 94 |
strength_1 = gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Refinement Strength")
|
| 95 |
guidance_1 = gr.Slider(1, 20, value=17, step=0.5, label="Guidance Scale (Headshot)")
|
| 96 |
|
| 97 |
gr.Markdown("### Step 2: Background Inpainting (SDXL)")
|
|
|
|
| 98 |
with gr.Row():
|
| 99 |
prompt_2 = gr.Textbox(label="Background Prompt", value="modern hospital background, clean, soft lighting")
|
| 100 |
neg_2 = gr.Textbox(label="Background Negative Prompt", value="fantasy, cartoon, cluttered, sketch")
|
|
|
|
| 101 |
with gr.Row():
|
| 102 |
guidance_2 = gr.Slider(1, 20, value=10, step=0.5, label="Guidance Scale (Background)")
|
| 103 |
|
| 104 |
+
gr.Markdown("### Step 3: Clothing Replacement")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
prompt_3 = gr.Textbox(label="Clothing Prompt", value="white female CEO professional blazer, clean look")
|
| 107 |
+
neg_3 = gr.Textbox(label="Clothing Negative Prompt", value="hoodie, casual wear, fantasy, cartoon, jeans, distorted, blurry")
|
| 108 |
+
with gr.Row():
|
| 109 |
+
guidance_3 = gr.Slider(1, 20, value=17.0, step=0.5, label="Clothing Guidance Scale")
|
| 110 |
+
|
| 111 |
+
go_btn = gr.Button("β¨ Run Full Pipeline (All 3 Steps)")
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
output_refined = gr.Image(type="pil", label="Step 1: Refined Headshot")
|
| 115 |
+
output_bg = gr.Image(type="pil", label="Step 2: With New Background")
|
| 116 |
+
output_final = gr.Image(type="pil", label="Step 3: Final with New Clothing")
|
| 117 |
|
| 118 |
error_box = gr.Markdown(label="Error", value="", visible=True)
|
| 119 |
|
| 120 |
go_btn.click(
|
| 121 |
+
fn=safe_generate_all_steps,
|
| 122 |
inputs=[
|
| 123 |
+
input_image,
|
| 124 |
+
prompt_1, neg_1, strength_1, guidance_1,
|
| 125 |
+
prompt_2, neg_2, guidance_2,
|
| 126 |
+
prompt_3, neg_3, guidance_3
|
| 127 |
],
|
| 128 |
+
outputs=[output_refined, output_bg, output_final, error_box]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
demo.launch(debug=True)
|