DreamingOracle commited on
Commit
c582ce1
·
verified ·
1 Parent(s): 2f8202d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -27
app.py CHANGED
@@ -9,10 +9,7 @@ import torch
9
  import os
10
  from PIL import Image, ImageFilter, ImageOps
11
  from huggingface_hub import login, hf_hub_download
12
- import os
13
-
14
- # Add this import for theming
15
- from gradio import themes
16
 
17
 
18
  if "HF_TOKEN" in os.environ:
@@ -49,7 +46,10 @@ if not os.path.exists(adetailer_model_path):
49
  adetailer_model = YOLO(adetailer_model_path)
50
 
51
  MAX_SEED = np.iinfo(np.int32).max
52
- MAX_IMAGE_SIZE = 1024 # @spaces.GPU #[uncomment to use ZeroGPU]
 
 
 
53
 
54
  def infer(
55
  prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, scheduler_name="PNDM", save_format="png", progress=gr.Progress(track_tqdm=True),):
@@ -70,16 +70,13 @@ def infer(
70
  # ---------------------------
71
  try:
72
  results = adetailer_model(image)
73
- # ultralytics YOLO results typically have results[0].boxes
74
  if results and len(results) and getattr(results[0], "boxes", None):
75
  for box in results[0].boxes:
76
- # extract bbox (xyxy)
77
  x1, y1, x2, y2 = map(int, box.xyxy[0])
78
 
79
- # 1) Pad bbox so inpaint has surrounding context
80
  w = max(1, x2 - x1)
81
  h = max(1, y2 - y1)
82
- pad = int(max(10, 0.18 * max(w, h))) # ~18% padding, tweak if desired to lower value for scenic panorama preservation
83
  x1p = max(0, x1 - pad)
84
  y1p = max(0, y1 - pad)
85
  x2p = min(image.width, x2 + pad)
@@ -87,45 +84,34 @@ def infer(
87
 
88
  face = image.crop((x1p, y1p, x2p, y2p))
89
 
90
- # 2) Make dimensions divisible by 8 (many pipelines prefer multiples of 8)
91
  fw, fh = face.size
92
  fw8 = max(8, (fw // 8) * 8)
93
  fh8 = max(8, (fh // 8) * 8)
94
  if (fw8, fh8) != (fw, fh):
95
  face = face.resize((fw8, fh8), Image.LANCZOS)
96
 
97
- # 3) Create a full white mask for inpainting (255 = area to replace)
98
  mask = Image.new("L", face.size, 255)
99
 
100
- # 4) Create a blurred mask for soft blending when pasting back
101
- blur_radius = max(4, int(min(face.size) / 10)) # heuristic; adjust if needed
102
  paste_mask = mask.filter(ImageFilter.GaussianBlur(radius=blur_radius))
103
 
104
- # 5) Inpaint on the crop — use gentler strength so output doesn't look pasted
105
- # (this re-uses your existing `pipe` call which previously worked for you)
106
  inpaint_result = pipe(
107
  prompt=prompt + ", high detail face",
108
  image=face,
109
  mask_image=mask,
110
- strength=0.45, # reduced from 0.75 to lower mismatch
111
  num_inference_steps=20,
112
  guidance_scale=7.5,
113
  generator=generator
114
  ).images[0]
115
 
116
- # 6) Optionally attempt a simple color-match (blend) if lighting differs.
117
- # Here we keep it simple and rely on the blurred mask to smooth edges.
118
- # Ensure paste_mask is 'L'
119
  if paste_mask.mode != "L":
120
  paste_mask = paste_mask.convert("L")
121
 
122
- # 7) Paste the inpainted crop back using the soft mask for feathered edges
123
  image.paste(inpaint_result, (x1p, y1p), paste_mask)
124
  except Exception as e:
125
- # Log exception to console for debugging but don't crash the whole run
126
  print("ADetailer post-process failed:", e)
127
 
128
- # Save in selected format for download consistency
129
  output_path = f"generated_image.{save_format}"
130
  image.save(output_path, format=save_format.upper())
131
  return image, seed
@@ -135,14 +121,15 @@ examples = [
135
  "head helmet portrait of a futuristic armored soldier, worn brushed metal armor with neon blue accents, realistic cloth under-armor, weathering and scratches, volumetric rim light, cinematic pose, high detail, photoreal",
136
  "close up view of silver coins on a table",]
137
 
138
- css = """#col-container { margin: 0 auto; max-width: 640px;}"""
139
-
140
- # Create the custom purple theme
141
- custom_theme = themes.Default(primary_hue="purple")
 
142
 
143
  with gr.Blocks(css=css, theme=custom_theme) as demo:
144
  with gr.Column(elem_id="col-container"):
145
- gr.Markdown(" # DPS-Quagmaform AI txt2img")
146
  with gr.Row():
147
  prompt = gr.Text(
148
  label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False,
@@ -181,6 +168,14 @@ with gr.Blocks(css=css, theme=custom_theme) as demo:
181
  choices=["png", "jpg"], value="png", label="Select Output Format"
182
  )
183
  gr.Examples(examples=examples, inputs=[prompt])
 
 
 
 
 
 
 
 
184
  gr.on(
185
  triggers=[run_button.click, prompt.submit],
186
  fn=infer,
 
9
  import os
10
  from PIL import Image, ImageFilter, ImageOps
11
  from huggingface_hub import login, hf_hub_download
12
+ from gradio.themes import Default # For theming
 
 
 
13
 
14
 
15
  if "HF_TOKEN" in os.environ:
 
46
  adetailer_model = YOLO(adetailer_model_path)
47
 
48
  MAX_SEED = np.iinfo(np.int32).max
49
+ MAX_IMAGE_SIZE = 1024
50
+
51
+ # Purple theme
52
+ custom_theme = Default(primary_hue="purple")
53
 
54
  def infer(
55
  prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, scheduler_name="PNDM", save_format="png", progress=gr.Progress(track_tqdm=True),):
 
70
  # ---------------------------
71
  try:
72
  results = adetailer_model(image)
 
73
  if results and len(results) and getattr(results[0], "boxes", None):
74
  for box in results[0].boxes:
 
75
  x1, y1, x2, y2 = map(int, box.xyxy[0])
76
 
 
77
  w = max(1, x2 - x1)
78
  h = max(1, y2 - y1)
79
+ pad = int(max(10, 0.18 * max(w, h)))
80
  x1p = max(0, x1 - pad)
81
  y1p = max(0, y1 - pad)
82
  x2p = min(image.width, x2 + pad)
 
84
 
85
  face = image.crop((x1p, y1p, x2p, y2p))
86
 
 
87
  fw, fh = face.size
88
  fw8 = max(8, (fw // 8) * 8)
89
  fh8 = max(8, (fh // 8) * 8)
90
  if (fw8, fh8) != (fw, fh):
91
  face = face.resize((fw8, fh8), Image.LANCZOS)
92
 
 
93
  mask = Image.new("L", face.size, 255)
94
 
95
+ blur_radius = max(4, int(min(face.size) / 10))
 
96
  paste_mask = mask.filter(ImageFilter.GaussianBlur(radius=blur_radius))
97
 
 
 
98
  inpaint_result = pipe(
99
  prompt=prompt + ", high detail face",
100
  image=face,
101
  mask_image=mask,
102
+ strength=0.45,
103
  num_inference_steps=20,
104
  guidance_scale=7.5,
105
  generator=generator
106
  ).images[0]
107
 
 
 
 
108
  if paste_mask.mode != "L":
109
  paste_mask = paste_mask.convert("L")
110
 
 
111
  image.paste(inpaint_result, (x1p, y1p), paste_mask)
112
  except Exception as e:
 
113
  print("ADetailer post-process failed:", e)
114
 
 
115
  output_path = f"generated_image.{save_format}"
116
  image.save(output_path, format=save_format.upper())
117
  return image, seed
 
121
  "head helmet portrait of a futuristic armored soldier, worn brushed metal armor with neon blue accents, realistic cloth under-armor, weathering and scratches, volumetric rim light, cinematic pose, high detail, photoreal",
122
  "close up view of silver coins on a table",]
123
 
124
+ # Updated CSS 12826
125
+ css = """
126
+ #col-container { margin: 0 auto; max-width: 640px;}
127
+ #community-row {justify-content: center; gap: 30px;}
128
+ """
129
 
130
  with gr.Blocks(css=css, theme=custom_theme) as demo:
131
  with gr.Column(elem_id="col-container"):
132
+ gr.Markdown("# DPS-Quagmaform AI txt2img")
133
  with gr.Row():
134
  prompt = gr.Text(
135
  label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False,
 
168
  choices=["png", "jpg"], value="png", label="Select Output Format"
169
  )
170
  gr.Examples(examples=examples, inputs=[prompt])
171
+
172
+ # Community
173
+ gr.Markdown("### Community")
174
+ with gr.Row(elem_id="community-row"):
175
+
176
+ gr.Button("Join Discord 💬", link="https://discord.gg/deepspace", variant="primary")
177
+ gr.Button("Join Telegram 📱", link="https://t.me/DeepSpaceHispano", variant="primary")
178
+
179
  gr.on(
180
  triggers=[run_button.click, prompt.submit],
181
  fn=infer,