beertoshi commited on
Commit
1c687f2
·
verified ·
1 Parent(s): 573b6e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -172
app.py CHANGED
@@ -1,115 +1,167 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import StableDiffusionInpaintPipeline
4
  from PIL import Image, ImageDraw, ImageFilter
5
  import numpy as np
6
  import spaces
7
 
8
- # BETTER MODEL - This one is trained on people and fashion!
9
- # Options (all work great for people):
10
- # model_id = "Lykon/DreamShaper" # Excellent for people
11
- # model_id = "prompthero/openjourney" # Good quality
12
- # model_id = "wavymulder/Analog-Diffusion" # Great for photos
13
- model_id = "Lykon/DreamShaper" # Best overall for people
14
 
15
- print(f"Loading {model_id} - this model is MUCH better for people!")
 
16
 
17
- pipe = StableDiffusionInpaintPipeline.from_pretrained(
18
- model_id,
19
- torch_dtype=torch.float16,
20
- safety_checker=None,
21
- requires_safety_checker=False
22
- )
23
- pipe.enable_attention_slicing()
24
- pipe.enable_vae_slicing()
25
 
26
- print("✅ Better model loaded!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Professional clothing prompts
29
  CLOTHING_PROMPTS = {
30
- "Indian Sari": (
31
- "beautiful woman wearing traditional red silk sari with golden embroidery, "
32
- "professional photography, natural pose, perfect hands, detailed fabric, "
33
- "elegant draping, studio lighting, high quality, sharp focus"
34
- ),
35
- "Japanese Kimono": (
36
- "person wearing authentic silk kimono with floral patterns, obi belt, "
37
- "traditional Japanese clothing, professional portrait, natural pose, "
38
- "perfect anatomy, detailed fabric texture, studio photography"
39
- ),
40
- "African Dashiki": (
41
- "person wearing colorful African dashiki with geometric patterns, "
42
- "traditional clothing, professional photo, natural pose, perfect hands, "
43
- "vibrant colors, detailed embroidery, high quality"
44
- ),
45
- "Chinese Qipao": (
46
- "elegant woman wearing traditional Chinese qipao dress, silk fabric, "
47
- "professional fashion photography, natural pose, perfect proportions, "
48
- "detailed patterns, studio lighting"
49
- ),
50
- "Scottish Kilt": (
51
- "man wearing traditional Scottish kilt with tartan pattern, "
52
- "highland dress, professional photo, natural stance, proper proportions, "
53
- "detailed fabric, high quality"
54
- ),
55
- "Middle Eastern Thobe": (
56
- "person wearing traditional white thobe robe, Middle Eastern clothing, "
57
- "professional portrait, natural pose, flowing fabric, elegant, "
58
- "studio photography, high quality"
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  }
61
 
62
- # Critical for good results - tells AI what to avoid
63
- NEGATIVE_PROMPT = (
64
- "deformed, bad anatomy, disfigured, poorly drawn face, mutation, mutated, "
65
- "extra limb, ugly, disgusting, poorly drawn hands, missing limb, floating limbs, "
66
- "disconnected limbs, malformed hands, blurry, mutated hands and fingers, "
67
- "watermark, oversaturated, distorted hands, amputation, missing hands, "
68
- "obese, doubled face, double hands, bad hands, bad anatomy, bad proportions, "
69
- "extra fingers, fused fingers, too many fingers, long neck, low quality"
70
- )
71
-
72
- def create_safe_mask(image, mask_strength="medium"):
73
- """Create mask that preserves face and hands"""
74
  width, height = image.size
75
  mask = Image.new('L', (width, height), 0)
76
  draw = ImageDraw.Draw(mask)
77
 
78
- if mask_strength == "light":
79
- # Very conservative - only center torso
80
  left = width * 0.35
81
  right = width * 0.65
82
  top = height * 0.45
83
  bottom = height * 0.65
84
  draw.ellipse([left, top, right, bottom], fill=255)
85
 
86
- elif mask_strength == "medium":
87
- # Moderate - torso and upper legs, avoid arms
88
  left = width * 0.25
89
  right = width * 0.75
90
- top = height * 0.4
91
  bottom = height * 0.75
 
 
92
  draw.ellipse([left, top, right, bottom], fill=255)
93
 
94
- # Cut out arm areas to preserve hands
95
- arm_width = width * 0.15
96
- draw.ellipse([left - arm_width, height * 0.45, left, height * 0.7], fill=0)
97
- draw.ellipse([right, height * 0.45, right + arm_width, height * 0.7], fill=0)
98
 
99
- else: # heavy
100
- # Most of body but still protect face and extremities
101
- left = width * 0.15
102
- right = width * 0.85
103
  top = height * 0.35
104
- bottom = height * 0.9
105
  draw.ellipse([left, top, right, bottom], fill=255)
106
 
107
- # Protect hand areas
108
- draw.ellipse([0, height * 0.5, width * 0.1, height * 0.8], fill=0)
109
- draw.ellipse([width * 0.9, height * 0.5, width, height * 0.8], fill=0)
110
 
111
- # Smooth blend
112
- mask = mask.filter(ImageFilter.GaussianBlur(radius=30))
 
113
 
114
  return mask
115
 
@@ -117,11 +169,11 @@ def create_safe_mask(image, mask_strength="medium"):
117
  def generate_clothing(
118
  input_image,
119
  clothing_type,
120
- mask_strength="medium",
121
- quality_steps=40,
122
- guidance_scale=7.5
123
  ):
124
- """Generate clothing with better model"""
125
 
126
  if input_image is None:
127
  return None, "Please upload an image"
@@ -139,43 +191,52 @@ def generate_clothing(
139
  # Store original
140
  original_size = image.size
141
 
142
- # Optimal size for quality (768 works well)
143
  target_size = 768
144
  if max(image.size) > target_size:
145
  scale = target_size / max(image.size)
146
  new_w = int(image.width * scale)
147
  new_h = int(image.height * scale)
148
- # Make divisible by 8
149
  new_w = new_w - (new_w % 8)
150
  new_h = new_h - (new_h % 8)
151
  image = image.resize((new_w, new_h), Image.Resampling.LANCZOS)
152
  else:
153
- # Still fix dimensions
154
  new_w = image.width - (image.width % 8)
155
  new_h = image.height - (image.height % 8)
156
  if (new_w, new_h) != image.size:
157
  image = image.resize((new_w, new_h), Image.Resampling.LANCZOS)
158
 
159
- print(f"Processing at: {image.size}")
 
160
 
161
- # Create safe mask
162
- mask = create_safe_mask(image, mask_strength)
163
-
164
- # Get prompt
165
- prompt = CLOTHING_PROMPTS[clothing_type]
166
-
167
- # Generate with better settings
168
- with torch.autocast("cuda"):
169
- result = pipe(
170
- prompt=prompt,
171
- negative_prompt=NEGATIVE_PROMPT,
172
- image=image,
173
- mask_image=mask,
174
- num_inference_steps=quality_steps,
175
- guidance_scale=guidance_scale,
176
- strength=0.85, # Good balance
177
- generator=torch.Generator("cuda").manual_seed(42) # Consistent results
178
- ).images[0]
 
 
 
 
 
 
 
 
 
179
 
180
  # Resize back
181
  if result.size != original_size:
@@ -192,14 +253,16 @@ def generate_clothing(
192
  return None, f"Error: {str(e)}"
193
 
194
  # Create UI
195
- with gr.Blocks(title="Traditional Clothing AI - Better Model", theme=gr.themes.Soft()) as app:
196
- gr.Markdown("""
197
- # 👘 Traditional Clothing AI - Premium Quality
198
 
199
- ### Using DreamShaper model - MUCH better for people!
200
- - ✅ Preserves anatomy correctly
201
- - No weird poses or hands
202
- - Professional quality results
 
 
203
  """)
204
 
205
  with gr.Row():
@@ -215,30 +278,28 @@ with gr.Blocks(title="Traditional Clothing AI - Better Model", theme=gr.themes.S
215
  label="Select Traditional Clothing"
216
  )
217
 
218
- with gr.Accordion("Advanced Settings", open=True):
219
- mask_strength = gr.Radio(
220
- choices=["light", "medium", "heavy"],
221
  value="medium",
222
- label="Mask Coverage",
223
- info="Light = safest (only torso), Heavy = more coverage"
224
  )
225
 
226
- quality_steps = gr.Slider(
227
  minimum=20,
228
  maximum=60,
229
  value=40,
230
  step=5,
231
- label="Quality Steps",
232
- info="Higher = better quality but slower"
233
  )
234
 
235
- guidance_scale = gr.Slider(
236
- minimum=5,
237
- maximum=12,
238
- value=7.5,
239
- step=0.5,
240
- label="Guidance Scale",
241
- info="How closely to follow the prompt"
242
  )
243
 
244
  generate_btn = gr.Button(
@@ -248,59 +309,26 @@ with gr.Blocks(title="Traditional Clothing AI - Better Model", theme=gr.themes.S
248
  )
249
 
250
  with gr.Column():
251
- output_image = gr.Image(
252
- label="Result"
253
- )
254
-
255
- status_text = gr.Textbox(
256
- label="Status",
257
- placeholder="Upload an image and click generate..."
258
- )
259
-
260
- # Examples
261
- gr.Examples(
262
- examples=[
263
- ["person1.jpg", "Indian Sari", "medium"],
264
- ["person2.jpg", "Japanese Kimono", "medium"],
265
- ["person3.jpg", "African Dashiki", "light"],
266
- ],
267
- inputs=[input_image, clothing_type, mask_strength],
268
- outputs=[output_image, status_text],
269
- fn=generate_clothing,
270
- cache_examples=False
271
- )
272
 
273
  gr.Markdown("""
274
- ---
275
- ### 🎯 Tips for Best Results:
276
-
277
- 1. **Photo Tips:**
278
- - Use clear, front-facing photos
279
- - Good lighting helps
280
- - Full body or 3/4 shots work best
281
-
282
- 2. **If hands/feet look weird:**
283
- - Use "Light" mask coverage
284
- - This only changes the torso area
285
-
286
- 3. **For better quality:**
287
- - Increase quality steps to 50-60
288
- - Takes longer but worth it
289
 
290
- 4. **Model Advantages:**
291
- - DreamShaper is trained on people
292
- - Much better anatomy understanding
293
- - Preserves natural poses
294
 
295
- ### 🌍 Note:
296
- This AI creates artistic interpretations of traditional clothing.
297
- Results may not be culturally perfect - please use respectfully.
 
298
  """)
299
 
300
- # Connect button
301
  generate_btn.click(
302
  fn=generate_clothing,
303
- inputs=[input_image, clothing_type, mask_strength, quality_steps, guidance_scale],
304
  outputs=[output_image, status_text]
305
  )
306
 
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import StableDiffusionInpaintPipeline, StableDiffusionImg2ImgPipeline
4
  from PIL import Image, ImageDraw, ImageFilter
5
  import numpy as np
6
  import spaces
7
 
8
+ # Use models that ACTUALLY WORK on HuggingFace
9
+ # Option 1: Better inpainting model
10
+ model_id = "runwayml/stable-diffusion-inpainting"
 
 
 
11
 
12
+ # Option 2: If you want to try regular SD models, use Img2Img pipeline instead
13
+ # model_id = "runwayml/stable-diffusion-v1-5"
14
 
15
+ print(f"Loading {model_id}...")
 
 
 
 
 
 
 
16
 
17
+ try:
18
+ # For inpainting models
19
+ pipe = StableDiffusionInpaintPipeline.from_pretrained(
20
+ model_id,
21
+ torch_dtype=torch.float16,
22
+ safety_checker=None,
23
+ requires_safety_checker=False,
24
+ use_safetensors=True
25
+ )
26
+ pipe.enable_attention_slicing()
27
+ pipe.enable_vae_slicing()
28
+ pipeline_type = "inpaint"
29
+ print("✅ Inpainting model loaded!")
30
+
31
+ except Exception as e:
32
+ print(f"Inpainting failed, trying img2img: {e}")
33
+ # Fallback to img2img pipeline
34
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
35
+ "runwayml/stable-diffusion-v1-5",
36
+ torch_dtype=torch.float16,
37
+ safety_checker=None,
38
+ requires_safety_checker=False
39
+ )
40
+ pipe.enable_attention_slicing()
41
+ pipeline_type = "img2img"
42
+ print("✅ Img2Img model loaded!")
43
 
44
+ # MUCH BETTER PROMPTS - This is the secret!
45
  CLOTHING_PROMPTS = {
46
+ "Indian Sari": {
47
+ "prompt": (
48
+ "beautiful indian woman wearing elegant red silk sari with golden embroidery, "
49
+ "professional fashion photography, perfect anatomy, natural pose, "
50
+ "detailed fabric texture, studio lighting, high quality, 8k, sharp focus, "
51
+ "intricate patterns, traditional jewelry"
52
+ ),
53
+ "negative": (
54
+ "deformed, bad anatomy, disfigured, poorly drawn face, mutation, mutated, "
55
+ "extra limb, ugly, disgusting, poorly drawn hands, missing limb, floating limbs, "
56
+ "disconnected limbs, malformed hands, blurry, mutated hands and fingers, "
57
+ "watermark, oversaturated, distorted hands, amputation, missing hands, "
58
+ "doubled face, double hands, weird pose, unnatural pose"
59
+ )
60
+ },
61
+ "Japanese Kimono": {
62
+ "prompt": (
63
+ "elegant person wearing traditional japanese silk kimono with cherry blossom patterns, "
64
+ "obi belt, professional portrait photography, perfect proportions, natural pose, "
65
+ "detailed fabric texture, studio lighting, high quality, traditional hairstyle"
66
+ ),
67
+ "negative": (
68
+ "bad anatomy, wrong proportions, extra limbs, missing limbs, deformed, "
69
+ "ugly, duplicate, mutilated, out of frame, extra fingers, mutated hands, "
70
+ "poorly drawn hands, mutation, blurry, bad proportions"
71
+ )
72
+ },
73
+ "African Dashiki": {
74
+ "prompt": (
75
+ "person wearing colorful traditional african dashiki with geometric kente patterns, "
76
+ "vibrant colors, professional photography, perfect anatomy, natural pose, "
77
+ "detailed embroidery, cultural authenticity, high quality, sharp details"
78
+ ),
79
+ "negative": (
80
+ "bad anatomy, deformed, ugly, disfigured, poorly drawn, mutation, "
81
+ "extra limbs, missing limbs, floating limbs, disconnected limbs, "
82
+ "long neck, bad proportions, unnatural pose"
83
+ )
84
+ },
85
+ "Chinese Qipao": {
86
+ "prompt": (
87
+ "elegant woman wearing traditional chinese qipao cheongsam dress, silk fabric, "
88
+ "intricate patterns, professional fashion photography, perfect proportions, "
89
+ "natural pose, studio lighting, high quality, detailed embroidery"
90
+ ),
91
+ "negative": (
92
+ "deformed, bad anatomy, disfigured, mutation, extra limbs, ugly, "
93
+ "poorly drawn hands, missing limbs, blurry, bad art, bad proportions, "
94
+ "gross proportions, malformed limbs"
95
+ )
96
+ },
97
+ "Scottish Kilt": {
98
+ "prompt": (
99
+ "man wearing traditional scottish kilt with authentic tartan pattern, "
100
+ "highland dress, professional photography, perfect anatomy, natural stance, "
101
+ "detailed fabric, formal sporran, high quality"
102
+ ),
103
+ "negative": (
104
+ "bad anatomy, deformed, poorly drawn, extra limbs, close up, weird pose, "
105
+ "duplicate, mutilated, mutated hands, bad proportions"
106
+ )
107
+ },
108
+ "Middle Eastern Thobe": {
109
+ "prompt": (
110
+ "person wearing traditional white thobe robe, middle eastern clothing, "
111
+ "flowing fabric, professional portrait, perfect proportions, elegant, "
112
+ "studio photography, high quality, natural pose"
113
+ ),
114
+ "negative": (
115
+ "deformed, bad anatomy, ugly, poorly drawn, mutation, extra limbs, "
116
+ "bad hands, poorly drawn hands, missing limbs, blurry"
117
+ )
118
+ }
119
  }
120
 
121
+ def create_smart_mask(image, coverage="medium"):
122
+ """Create intelligent mask that preserves anatomy"""
 
 
 
 
 
 
 
 
 
 
123
  width, height = image.size
124
  mask = Image.new('L', (width, height), 0)
125
  draw = ImageDraw.Draw(mask)
126
 
127
+ if coverage == "light":
128
+ # Very safe - only central torso
129
  left = width * 0.35
130
  right = width * 0.65
131
  top = height * 0.45
132
  bottom = height * 0.65
133
  draw.ellipse([left, top, right, bottom], fill=255)
134
 
135
+ elif coverage == "medium":
136
+ # Torso and upper body, preserve head and hands
137
  left = width * 0.25
138
  right = width * 0.75
139
+ top = height * 0.38 # Start below neck
140
  bottom = height * 0.75
141
+
142
+ # Main body
143
  draw.ellipse([left, top, right, bottom], fill=255)
144
 
145
+ # Exclude arm areas to preserve hands
146
+ arm_exclude = width * 0.12
147
+ draw.ellipse([left - arm_exclude, height * 0.45, left, height * 0.7], fill=0)
148
+ draw.ellipse([right, height * 0.45, right + arm_exclude, height * 0.7], fill=0)
149
 
150
+ else: # full
151
+ # More coverage but still protect extremities
152
+ left = width * 0.2
153
+ right = width * 0.8
154
  top = height * 0.35
155
+ bottom = height * 0.85
156
  draw.ellipse([left, top, right, bottom], fill=255)
157
 
158
+ # Protect hands
159
+ draw.rectangle([0, height * 0.6, width * 0.15, height], fill=0)
160
+ draw.rectangle([width * 0.85, height * 0.6, width, height], fill=0)
161
 
162
+ # Smooth edges
163
+ for _ in range(3):
164
+ mask = mask.filter(ImageFilter.GaussianBlur(radius=10))
165
 
166
  return mask
167
 
 
169
  def generate_clothing(
170
  input_image,
171
  clothing_type,
172
+ mask_coverage="medium",
173
+ num_steps=40,
174
+ strength=0.85
175
  ):
176
+ """Generate clothing with proper technique"""
177
 
178
  if input_image is None:
179
  return None, "Please upload an image"
 
191
  # Store original
192
  original_size = image.size
193
 
194
+ # Optimal size
195
  target_size = 768
196
  if max(image.size) > target_size:
197
  scale = target_size / max(image.size)
198
  new_w = int(image.width * scale)
199
  new_h = int(image.height * scale)
 
200
  new_w = new_w - (new_w % 8)
201
  new_h = new_h - (new_h % 8)
202
  image = image.resize((new_w, new_h), Image.Resampling.LANCZOS)
203
  else:
204
+ # Fix dimensions
205
  new_w = image.width - (image.width % 8)
206
  new_h = image.height - (image.height % 8)
207
  if (new_w, new_h) != image.size:
208
  image = image.resize((new_w, new_h), Image.Resampling.LANCZOS)
209
 
210
+ # Get prompts
211
+ prompt_data = CLOTHING_PROMPTS[clothing_type]
212
 
213
+ # Generate based on pipeline type
214
+ if pipeline_type == "inpaint":
215
+ # Create mask
216
+ mask = create_smart_mask(image, mask_coverage)
217
+
218
+ # Inpainting
219
+ with torch.autocast("cuda"):
220
+ result = pipe(
221
+ prompt=prompt_data["prompt"],
222
+ negative_prompt=prompt_data["negative"],
223
+ image=image,
224
+ mask_image=mask,
225
+ num_inference_steps=num_steps,
226
+ guidance_scale=7.5,
227
+ strength=strength
228
+ ).images[0]
229
+ else:
230
+ # For img2img, use lower strength
231
+ with torch.autocast("cuda"):
232
+ result = pipe(
233
+ prompt=prompt_data["prompt"],
234
+ negative_prompt=prompt_data["negative"],
235
+ image=image,
236
+ num_inference_steps=num_steps,
237
+ guidance_scale=7.5,
238
+ strength=0.6 # Lower for img2img
239
+ ).images[0]
240
 
241
  # Resize back
242
  if result.size != original_size:
 
253
  return None, f"Error: {str(e)}"
254
 
255
  # Create UI
256
+ with gr.Blocks(title="Traditional Clothing AI", theme=gr.themes.Soft()) as app:
257
+ gr.Markdown(f"""
258
+ # 👘 Traditional Clothing AI - Working Version
259
 
260
+ ### Model: {model_id} ({pipeline_type} mode)
261
+
262
+ **Tips for best results:**
263
+ - The SECRET is in the detailed prompts!
264
+ - Use "light" coverage if you see weird poses
265
+ - Higher steps = better quality
266
  """)
267
 
268
  with gr.Row():
 
278
  label="Select Traditional Clothing"
279
  )
280
 
281
+ with gr.Accordion("Settings", open=True):
282
+ mask_coverage = gr.Radio(
283
+ choices=["light", "medium", "full"],
284
  value="medium",
285
+ label="Clothing Coverage",
286
+ info="Light = safest, Full = most coverage"
287
  )
288
 
289
+ num_steps = gr.Slider(
290
  minimum=20,
291
  maximum=60,
292
  value=40,
293
  step=5,
294
+ label="Quality Steps"
 
295
  )
296
 
297
+ strength = gr.Slider(
298
+ minimum=0.5,
299
+ maximum=0.95,
300
+ value=0.85,
301
+ step=0.05,
302
+ label="Change Strength"
 
303
  )
304
 
305
  generate_btn = gr.Button(
 
309
  )
310
 
311
  with gr.Column():
312
+ output_image = gr.Image(label="Result")
313
+ status_text = gr.Textbox(label="Status")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
  gr.Markdown("""
316
+ ### 🎯 The Secret: Better Prompts!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
+ The model is less important than the prompts. This version uses:
319
+ - Detailed clothing descriptions
320
+ - Anatomy preservation keywords
321
+ - Strong negative prompts
322
 
323
+ ### If you see weird poses:
324
+ 1. Use "light" coverage
325
+ 2. Lower the strength to 0.7
326
+ 3. Increase steps to 50+
327
  """)
328
 
 
329
  generate_btn.click(
330
  fn=generate_clothing,
331
+ inputs=[input_image, clothing_type, mask_coverage, num_steps, strength],
332
  outputs=[output_image, status_text]
333
  )
334