theoracle commited on
Commit
93be815
Β·
1 Parent(s): 5c4b209

Add separate prompt and guidance control for both steps

Browse files
Files changed (2) hide show
  1. app.py +41 -25
  2. background_edit.py +2 -2
app.py CHANGED
@@ -16,31 +16,40 @@ def unload_models():
16
  torch.cuda.empty_cache()
17
  gc.collect()
18
 
19
- def safe_generate_and_inpaint(image, prompt, negative_prompt, strength, guidance_scale):
 
 
 
 
20
  try:
21
  if image is None:
22
  raise gr.Error("Please upload an image first.")
23
 
24
- # Step 1: Refinement
25
- print("[INFO] Step 1: Generating refined image...", flush=True)
26
  refined = generate_with_lora(
27
  image=image,
28
- prompt=prompt,
29
- negative_prompt=negative_prompt,
30
- strength=strength,
31
- guidance_scale=guidance_scale,
32
  )
33
 
34
- # Save to disk
35
  os.makedirs("./outputs", exist_ok=True)
36
  ts = datetime.now().strftime("%Y%m%d_%H%M%S")
37
  path = f"./outputs/step1_result_{ts}.png"
38
  refined.save(path)
39
 
40
- # Step 2: Background removal and inpainting
41
  print("[INFO] Step 2: Inpainting background...", flush=True)
42
  unload_models()
43
- result = run_background_removal_and_inpaint(path, prompt, negative_prompt)
 
 
 
 
 
44
 
45
  return refined, result, ""
46
 
@@ -52,37 +61,44 @@ def safe_generate_and_inpaint(image, prompt, negative_prompt, strength, guidance
52
 
53
  # ───────────────────── Gradio UI ─────────────────────
54
  with gr.Blocks() as demo:
55
- gr.Markdown("## πŸ–ΌοΈ AI Headshot Enhancer + Background Replacer")
56
- gr.Markdown("Upload a headshot, adjust the prompt, and click one button. We'll refine the image and replace the background automatically.")
57
 
58
  with gr.Row():
59
  input_image = gr.Image(type="pil", label="Upload Headshot")
60
 
 
 
61
  with gr.Row():
62
- prompt = gr.Textbox(
63
- label="Prompt",
64
- value="a professional corporate headshot of a confident woman in her 30s with blonde hair"
65
- )
66
- negative_prompt = gr.Textbox(
67
- label="Negative Prompt",
68
- value="deformed, cartoon, anime, illustration, painting, drawing, sketch, low resolution, blurry, out of focus, pixelated"
69
- )
 
 
 
 
70
 
71
  with gr.Row():
72
- strength = gr.Slider(0.1, 1.0, value=0.20, step=0.05, label="Refinement Strength")
73
- guidance = gr.Slider(1, 20, value=17.0, step=0.5, label="Guidance Scale")
74
 
75
- go_btn = gr.Button("✨ Run Full Process (Refine + Inpaint)")
76
 
77
  with gr.Row():
78
  output_refined = gr.Image(type="pil", label="Step 1: Refined Headshot")
79
- output_final = gr.Image(type="pil", label="Step 2: Final Image with Background")
80
 
81
  error_box = gr.Markdown(label="Error", value="", visible=True)
82
 
83
  go_btn.click(
84
  fn=safe_generate_and_inpaint,
85
- inputs=[input_image, prompt, negative_prompt, strength, guidance],
 
 
 
86
  outputs=[output_refined, output_final, error_box]
87
  )
88
 
 
16
  torch.cuda.empty_cache()
17
  gc.collect()
18
 
19
+ def safe_generate_and_inpaint(
20
+ image,
21
+ prompt_1, neg_1, strength_1, guidance_1,
22
+ prompt_2, neg_2, guidance_2
23
+ ):
24
  try:
25
  if image is None:
26
  raise gr.Error("Please upload an image first.")
27
 
28
+ # Step 1: Headshot Refinement
29
+ print("[INFO] Step 1: Refining headshot...", flush=True)
30
  refined = generate_with_lora(
31
  image=image,
32
+ prompt=prompt_1,
33
+ negative_prompt=neg_1,
34
+ strength=strength_1,
35
+ guidance_scale=guidance_1,
36
  )
37
 
38
+ # Save intermediate result to disk
39
  os.makedirs("./outputs", exist_ok=True)
40
  ts = datetime.now().strftime("%Y%m%d_%H%M%S")
41
  path = f"./outputs/step1_result_{ts}.png"
42
  refined.save(path)
43
 
44
+ # Step 2: Background Inpainting
45
  print("[INFO] Step 2: Inpainting background...", flush=True)
46
  unload_models()
47
+ result = run_background_removal_and_inpaint(
48
+ image_path=path,
49
+ prompt=prompt_2,
50
+ negative_prompt=neg_2,
51
+ guidance_scale=guidance_2
52
+ )
53
 
54
  return refined, result, ""
55
 
 
61
 
62
  # ───────────────────── Gradio UI ─────────────────────
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("## 🧠 Headshot + Background Generator (Full Prompt Control)")
 
65
 
66
  with gr.Row():
67
  input_image = gr.Image(type="pil", label="Upload Headshot")
68
 
69
+ gr.Markdown("### Step 1: Headshot Refinement (LoRA)")
70
+
71
  with gr.Row():
72
+ prompt_1 = gr.Textbox(label="Headshot Prompt", value="a professional headshot of a confident woman in her 30s with blonde hair")
73
+ neg_1 = gr.Textbox(label="Headshot Negative Prompt", value="deformed, cartoon, anime, sketch, blurry, low quality")
74
+
75
+ with gr.Row():
76
+ strength_1 = gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Refinement Strength")
77
+ guidance_1 = gr.Slider(1, 20, value=17, step=0.5, label="Guidance Scale (Headshot)")
78
+
79
+ gr.Markdown("### Step 2: Background Inpainting (SDXL)")
80
+
81
+ with gr.Row():
82
+ prompt_2 = gr.Textbox(label="Background Prompt", value="modern hospital background, clean, soft lighting")
83
+ neg_2 = gr.Textbox(label="Background Negative Prompt", value="fantasy, cartoon, cluttered, sketch")
84
 
85
  with gr.Row():
86
+ guidance_2 = gr.Slider(1, 20, value=10, step=0.5, label="Guidance Scale (Background)")
 
87
 
88
+ go_btn = gr.Button("✨ Generate Refined Headshot + Background")
89
 
90
  with gr.Row():
91
  output_refined = gr.Image(type="pil", label="Step 1: Refined Headshot")
92
+ output_final = gr.Image(type="pil", label="Step 2: Final Image with Background")
93
 
94
  error_box = gr.Markdown(label="Error", value="", visible=True)
95
 
96
  go_btn.click(
97
  fn=safe_generate_and_inpaint,
98
+ inputs=[
99
+ input_image, prompt_1, neg_1, strength_1, guidance_1,
100
+ prompt_2, neg_2, guidance_2
101
+ ],
102
  outputs=[output_refined, output_final, error_box]
103
  )
104
 
background_edit.py CHANGED
@@ -33,7 +33,7 @@ inpaint_pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
33
  use_auth_token=os.getenv("HF_TOKEN")
34
  ).to("cuda")
35
 
36
- def run_background_removal_and_inpaint(image_path, prompt, negative_prompt):
37
  if not image_path or not os.path.isfile(image_path):
38
  raise gr.Error("No valid image found. Please run Step 1 first.")
39
 
@@ -59,7 +59,7 @@ def run_background_removal_and_inpaint(image_path, prompt, negative_prompt):
59
  negative_prompt=negative_prompt or "",
60
  image=img_pil,
61
  mask_image=mask_pil,
62
- guidance_scale=10,
63
  num_inference_steps=40
64
  ).images[0]
65
 
 
33
  use_auth_token=os.getenv("HF_TOKEN")
34
  ).to("cuda")
35
 
36
+ def run_background_removal_and_inpaint(image_path, prompt, negative_prompt, guidance_scale=10):
37
  if not image_path or not os.path.isfile(image_path):
38
  raise gr.Error("No valid image found. Please run Step 1 first.")
39
 
 
59
  negative_prompt=negative_prompt or "",
60
  image=img_pil,
61
  mask_image=mask_pil,
62
+ guidance_scale=guidance_scale,
63
  num_inference_steps=40
64
  ).images[0]
65