Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,17 @@ import spaces
|
|
| 8 |
import json
|
| 9 |
import re
|
| 10 |
|
| 11 |
-
# Initialize object detection using
|
| 12 |
class AdvancedYOLODetector:
|
| 13 |
def __init__(self):
|
| 14 |
-
# Using
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Fallback models in order of preference:
|
| 18 |
self.fallback_models = [
|
| 19 |
-
"https://api-inference.huggingface.co/models/
|
| 20 |
-
"https://api-inference.huggingface.co/models/
|
| 21 |
-
"https://api-inference.huggingface.co/models/
|
| 22 |
-
"https://api-inference.huggingface.co/models/
|
| 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
|
|
|
|
|
|
|
|
|
|
| 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
|
| 385 |
|
| 386 |
Upload an image and specify **ANY object** you want to remove - no limitations!
|
| 387 |
|
| 388 |
**How it works:**
|
| 389 |
-
1. 🔍 **
|
| 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 |
-
- **
|
| 532 |
- **🎨 Inpainting**: SDXL for photorealistic results
|
| 533 |
-
- **⏱️ Processing**: 30-90 seconds
|
| 534 |
-
- **🔧
|
| 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__":
|