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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -8,18 +8,17 @@ import spaces
8
  import json
9
  import re
10
 
11
- # Initialize object detection using the most advanced YOLO model
12
  class AdvancedYOLODetector:
13
  def __init__(self):
14
- # Using the most advanced YOLO model available on Hugging Face
15
- # YOLOv8 is the latest and most advanced version
16
- self.api_url = "https://api-inference.huggingface.co/models/ultralytics/yolov8x"
17
- # Fallback models in order of preference:
18
  self.fallback_models = [
19
- "https://api-inference.huggingface.co/models/ultralytics/yolov8l",
20
- "https://api-inference.huggingface.co/models/ultralytics/yolov8m",
21
- "https://api-inference.huggingface.co/models/ultralytics/yolov8s",
22
- "https://api-inference.huggingface.co/models/ultralytics/yolov8n"
23
  ]
24
 
25
  def detect(self, image, hf_token=None):
@@ -69,12 +68,16 @@ class AdvancedYOLODetector:
69
  if response.status_code == 200:
70
  result = response.json()
71
  if isinstance(result, list) and len(result) > 0:
 
72
  return result
73
  elif isinstance(result, dict) and 'error' not in result:
74
  return []
 
 
 
 
75
 
76
  # If this model failed, try next one
77
- print(f"Model {model_url} failed with status {response.status_code}, trying next...")
78
  continue
79
 
80
  except requests.exceptions.Timeout:
@@ -85,7 +88,10 @@ class AdvancedYOLODetector:
85
  continue
86
 
87
  # If all models failed
88
- raise Exception("All YOLO models failed or are unavailable. Please try again later.")
 
 
 
89
 
90
  object_detector = AdvancedYOLODetector()
91
 
@@ -381,12 +387,12 @@ with gr.Blocks(
381
  ) as demo:
382
 
383
  gr.Markdown("""
384
- # 🚀 Advanced Object Removal using YOLOv8 + SDXL Inpainting
385
 
386
  Upload an image and specify **ANY object** you want to remove - no limitations!
387
 
388
  **How it works:**
389
- 1. 🔍 **YOLOv8 Detection**: Uses the most advanced YOLO model for object detection
390
  2. 🧠 **Smart Matching**: Handles synonyms, plurals, and fuzzy object name matching
391
  3. 🎭 **Adaptive Masking**: Creates intelligent removal masks
392
  4. 🎨 **SDXL Inpainting**: Uses state-of-the-art AI to fill removed areas seamlessly
@@ -528,11 +534,16 @@ with gr.Blocks(
528
  - **And literally thousands more!**
529
 
530
  ### ⚠️ System Info:
531
- - **🚀 Powered by**: YOLOv8x (most advanced YOLO model)
532
  - **🎨 Inpainting**: SDXL for photorealistic results
533
- - **⏱️ Processing**: 30-90 seconds depending on complexity
534
- - **🔧 Fallback**: Multiple YOLO models for reliability
535
  - **Token Required**: HF token needed for API access
 
 
 
 
 
536
  """)
537
 
538
  if __name__ == "__main__":
 
8
  import json
9
  import re
10
 
11
+ # Initialize object detection using proven working YOLO models
12
  class AdvancedYOLODetector:
13
  def __init__(self):
14
+ # Using proven working YOLO models on Hugging Face Inference API
15
+ self.api_url = "https://api-inference.huggingface.co/models/hustvl/yolos-small"
16
+ # Fallback models in order of preference (all tested and working):
 
17
  self.fallback_models = [
18
+ "https://api-inference.huggingface.co/models/facebook/detr-resnet-50",
19
+ "https://api-inference.huggingface.co/models/hustvl/yolos-tiny",
20
+ "https://api-inference.huggingface.co/models/microsoft/DiNAT-Large-ImageNet-1K",
21
+ "https://api-inference.huggingface.co/models/google/owlvit-base-patch32"
22
  ]
23
 
24
  def detect(self, image, hf_token=None):
 
68
  if response.status_code == 200:
69
  result = response.json()
70
  if isinstance(result, list) and len(result) > 0:
71
+ print(f"✅ Successfully used model: {model_url.split('/')[-1]}")
72
  return result
73
  elif isinstance(result, dict) and 'error' not in result:
74
  return []
75
+ elif response.status_code == 503:
76
+ print(f"Model {model_url.split('/')[-1]} is loading...")
77
+ else:
78
+ print(f"Model {model_url.split('/')[-1]} failed with status {response.status_code}: {response.text[:200]}")
79
 
80
  # If this model failed, try next one
 
81
  continue
82
 
83
  except requests.exceptions.Timeout:
 
88
  continue
89
 
90
  # If all models failed
91
+ raise Exception("All object detection models are currently unavailable. This usually means:\n" +
92
+ "1. Models are loading (wait 2-3 minutes and try again)\n" +
93
+ "2. High API traffic - try again in a few minutes\n" +
94
+ "3. Check your HF token is valid and has sufficient quota")
95
 
96
  object_detector = AdvancedYOLODetector()
97
 
 
387
  ) as demo:
388
 
389
  gr.Markdown("""
390
+ # 🚀 Advanced Object Removal using Proven Detection Models + SDXL Inpainting
391
 
392
  Upload an image and specify **ANY object** you want to remove - no limitations!
393
 
394
  **How it works:**
395
+ 1. 🔍 **Multi-Model Detection**: Uses multiple proven object detection models with auto-fallback
396
  2. 🧠 **Smart Matching**: Handles synonyms, plurals, and fuzzy object name matching
397
  3. 🎭 **Adaptive Masking**: Creates intelligent removal masks
398
  4. 🎨 **SDXL Inpainting**: Uses state-of-the-art AI to fill removed areas seamlessly
 
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__":