Revrse commited on
Commit
60602f0
·
verified ·
1 Parent(s): f532fe6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -24
app.py CHANGED
@@ -311,8 +311,13 @@ def remove_objects(image, object_name, confidence_threshold, mask_expansion, inp
311
  # Step 2: Create mask with adaptive expansion
312
  mask = create_mask_from_detections(image, detections, mask_expansion)
313
 
314
- # Step 3: Use SDXL for inpainting
315
- inpaint_api_url = "https://api-inference.huggingface.co/models/diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
 
 
 
 
 
316
 
317
  headers = {"Authorization": f"Bearer {token}"}
318
 
@@ -342,26 +347,46 @@ def remove_objects(image, object_name, confidence_threshold, mask_expansion, inp
342
  'strength': 0.99
343
  }
344
 
345
- try:
346
- response = requests.post(inpaint_api_url, headers=headers, files=files, data=data, timeout=90)
347
-
348
- if response.status_code == 200:
349
- result_image = Image.open(io.BytesIO(response.content))
350
- detected_labels = [d.get('label', 'unknown') for d in detections]
351
- status_msg = f" Successfully removed {len(detections)} '{object_name}' object(s)\n"
352
- status_msg += f"🎯 Detected as: {', '.join(detected_labels)}\n"
353
- status_msg += f"🔧 Used: Advanced YOLO + SDXL Inpainting"
354
- else:
355
- # Fallback: return original with mask overlay for debugging
356
- result_image = create_mask_overlay(image, mask)
357
- status_msg = f"⚠️ SDXL inpainting failed (HTTP {response.status_code}). Showing detected areas in red.\n"
358
- status_msg += f"🎯 Found {len(detections)} '{object_name}' object(s) - detection was successful"
359
 
360
- except Exception as e:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  # Fallback: return original with mask overlay for debugging
362
  result_image = create_mask_overlay(image, mask)
363
- status_msg = f"⚠️ SDXL inpainting failed: {str(e)}. Showing detected areas in red.\n"
364
- status_msg += f"🎯 Found {len(detections)} '{object_name}' object(s) - detection was successful"
 
365
 
366
  return result_image, mask, status_msg
367
 
@@ -534,16 +559,16 @@ with gr.Blocks(
534
  - **And literally thousands more!**
535
 
536
  ### ⚠️ System Info:
537
- - **🔍 Models**: YOLOS-Small + DETR + OWL-ViT (auto-fallback)
538
- - **🎨 Inpainting**: SDXL for photorealistic results
539
- - **⏱️ Processing**: 30-90 seconds (first request may be slower)
540
  - **🔧 Auto-retry**: Tries multiple models if one is busy
541
  - **Token Required**: HF token needed for API access
542
 
543
  **If you get "models unavailable" error:**
544
  - Wait 2-3 minutes and try again (models loading)
545
- - Check your HF token is valid
546
- - Try during off-peak hours
547
  """)
548
 
549
  if __name__ == "__main__":
 
311
  # Step 2: Create mask with adaptive expansion
312
  mask = create_mask_from_detections(image, detections, mask_expansion)
313
 
314
+ # Step 3: Use reliable inpainting model
315
+ # Try multiple inpainting models in order of preference
316
+ inpaint_models = [
317
+ "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-inpainting",
318
+ "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-inpainting",
319
+ "https://api-inference.huggingface.co/models/diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
320
+ ]
321
 
322
  headers = {"Authorization": f"Bearer {token}"}
323
 
 
347
  'strength': 0.99
348
  }
349
 
350
+ # Try multiple inpainting models
351
+ inpainting_success = False
352
+ last_error = ""
353
+
354
+ for i, inpaint_api_url in enumerate(inpaint_models):
355
+ try:
356
+ print(f"Trying inpainting model {i+1}/{len(inpaint_models)}: {inpaint_api_url.split('/')[-1]}")
 
 
 
 
 
 
 
357
 
358
+ response = requests.post(inpaint_api_url, headers=headers, files=files, data=data, timeout=120)
359
+
360
+ if response.status_code == 503:
361
+ # Model is loading, wait and retry once
362
+ import time
363
+ time.sleep(10)
364
+ response = requests.post(inpaint_api_url, headers=headers, files=files, data=data, timeout=120)
365
+
366
+ if response.status_code == 200:
367
+ result_image = Image.open(io.BytesIO(response.content))
368
+ detected_labels = [d.get('label', 'unknown') for d in detections]
369
+ status_msg = f"✅ Successfully removed {len(detections)} '{object_name}' object(s)\n"
370
+ status_msg += f"🎯 Detected as: {', '.join(detected_labels)}\n"
371
+ status_msg += f"🔧 Used: {inpaint_api_url.split('/')[-1]} for inpainting"
372
+ inpainting_success = True
373
+ break
374
+ else:
375
+ last_error = f"HTTP {response.status_code}: {response.text[:200]}"
376
+ print(f"Model {i+1} failed: {last_error}")
377
+ continue
378
+
379
+ except Exception as e:
380
+ last_error = str(e)
381
+ print(f"Model {i+1} error: {last_error}")
382
+ continue
383
+
384
+ if not inpainting_success:
385
  # Fallback: return original with mask overlay for debugging
386
  result_image = create_mask_overlay(image, mask)
387
+ status_msg = f"⚠️ All inpainting models failed. Last error: {last_error}\n"
388
+ status_msg += f"🎯 Found {len(detections)} '{object_name}' object(s) - detection was successful\n"
389
+ status_msg += f"📍 Showing detected areas in red overlay"
390
 
391
  return result_image, mask, status_msg
392
 
 
559
  - **And literally thousands more!**
560
 
561
  ### ⚠️ System Info:
562
+ - **🔍 Detection**: YOLOS-Small + DETR + OWL-ViT (auto-fallback)
563
+ - **🎨 Inpainting**: Multiple SD models with auto-fallback
564
+ - **⏱️ Processing**: 30-120 seconds (first request may be slower)
565
  - **🔧 Auto-retry**: Tries multiple models if one is busy
566
  - **Token Required**: HF token needed for API access
567
 
568
  **If you get "models unavailable" error:**
569
  - Wait 2-3 minutes and try again (models loading)
570
+ - Check your HF token is valid and has API quota
571
+ - Try during off-peak hours for better performance
572
  """)
573
 
574
  if __name__ == "__main__":