Revrse commited on
Commit
691026e
·
verified ·
1 Parent(s): 9f6d4f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -11,13 +11,14 @@ class ObjectDetector:
11
  def __init__(self):
12
  self.api_url = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50"
13
 
14
- def detect(self, image):
15
- # Use HF_TOKEN environment variable if available, otherwise require user token
16
- hf_token = os.getenv("HF_TOKEN")
17
- if not hf_token:
18
- raise Exception("HF_TOKEN not found in environment variables")
 
19
 
20
- headers = {"Authorization": f"Bearer {hf_token}"}
21
 
22
  # Convert PIL image to bytes
23
  img_buffer = io.BytesIO()
@@ -48,13 +49,13 @@ COCO_CLASSES = [
48
  'toothbrush'
49
  ]
50
 
51
- def detect_objects(image, target_class, confidence_threshold):
52
  """
53
  Detect objects in the image and return bounding boxes for the target class
54
  """
55
  try:
56
  # Use Hugging Face Inference API for object detection
57
- results = object_detector.detect(image)
58
 
59
  # Filter results for target class
60
  target_detections = []
@@ -89,7 +90,7 @@ def create_mask_from_detections(image, detections, mask_expansion=10):
89
  return mask
90
 
91
  @spaces.GPU
92
- def remove_objects(image, object_class, confidence_threshold, mask_expansion, inpaint_prompt):
93
  """
94
  Main function to remove objects from image using SDXL inpainting
95
  """
@@ -97,8 +98,13 @@ def remove_objects(image, object_class, confidence_threshold, mask_expansion, in
97
  if image is None:
98
  raise gr.Error("Please upload an image")
99
 
 
 
 
 
 
100
  # Step 1: Detect objects
101
- detections = detect_objects(image, object_class, confidence_threshold)
102
 
103
  if not detections:
104
  return image, None, f"No {object_class} objects detected with confidence > {confidence_threshold}"
@@ -109,12 +115,7 @@ def remove_objects(image, object_class, confidence_threshold, mask_expansion, in
109
  # Step 3: Use SDXL for inpainting via Hugging Face Inference API
110
  inpaint_api_url = "https://api-inference.huggingface.co/models/diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
111
 
112
- # Get HF token from environment
113
- hf_token = os.getenv("HF_TOKEN")
114
- if not hf_token:
115
- raise gr.Error("HF_TOKEN not found in environment variables")
116
-
117
- headers = {"Authorization": f"Bearer {hf_token}"}
118
 
119
  # Convert images to bytes
120
  img_buffer = io.BytesIO()
@@ -234,6 +235,13 @@ with gr.Blocks(
234
  info="Be specific about the desired background/replacement"
235
  )
236
 
 
 
 
 
 
 
 
237
  remove_btn = gr.Button("🚀 Remove Objects", variant="primary", size="lg")
238
 
239
  with gr.Column(scale=2):
@@ -266,7 +274,8 @@ with gr.Blocks(
266
  object_class,
267
  confidence_threshold,
268
  mask_expansion,
269
- inpaint_prompt
 
270
  ],
271
  outputs=[output_image, mask_image, status_text]
272
  )
@@ -307,7 +316,8 @@ with gr.Blocks(
307
  **And 60+ more COCO classes!**
308
 
309
  ### ⚠️ Important Notes:
310
- - This Space requires a Hugging Face token to be set in the environment
 
311
  - Processing may take 30-60 seconds depending on image size
312
  - Results depend on object detection accuracy and image complexity
313
  - Red overlay indicates detected areas when inpainting fails
 
11
  def __init__(self):
12
  self.api_url = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50"
13
 
14
+ def detect(self, image, hf_token=None):
15
+ # Try multiple ways to get HF token
16
+ token = hf_token or os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
17
+
18
+ if not token:
19
+ raise Exception("HF Token required. Please set HF_TOKEN in Space secrets or environment variables")
20
 
21
+ headers = {"Authorization": f"Bearer {token}"}
22
 
23
  # Convert PIL image to bytes
24
  img_buffer = io.BytesIO()
 
49
  'toothbrush'
50
  ]
51
 
52
+ def detect_objects(image, target_class, confidence_threshold, hf_token=None):
53
  """
54
  Detect objects in the image and return bounding boxes for the target class
55
  """
56
  try:
57
  # Use Hugging Face Inference API for object detection
58
+ results = object_detector.detect(image, hf_token)
59
 
60
  # Filter results for target class
61
  target_detections = []
 
90
  return mask
91
 
92
  @spaces.GPU
93
+ def remove_objects(image, object_class, confidence_threshold, mask_expansion, inpaint_prompt, hf_token):
94
  """
95
  Main function to remove objects from image using SDXL inpainting
96
  """
 
98
  if image is None:
99
  raise gr.Error("Please upload an image")
100
 
101
+ # Try to get token from multiple sources
102
+ token = hf_token or os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
103
+ if not token:
104
+ raise gr.Error("Please provide your Hugging Face token or set HF_TOKEN in Space secrets")
105
+
106
  # Step 1: Detect objects
107
+ detections = detect_objects(image, object_class, confidence_threshold, token)
108
 
109
  if not detections:
110
  return image, None, f"No {object_class} objects detected with confidence > {confidence_threshold}"
 
115
  # Step 3: Use SDXL for inpainting via Hugging Face Inference API
116
  inpaint_api_url = "https://api-inference.huggingface.co/models/diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
117
 
118
+ headers = {"Authorization": f"Bearer {token}"}
 
 
 
 
 
119
 
120
  # Convert images to bytes
121
  img_buffer = io.BytesIO()
 
235
  info="Be specific about the desired background/replacement"
236
  )
237
 
238
+ hf_token = gr.Textbox(
239
+ label="🔑 Hugging Face Token (Optional)",
240
+ type="password",
241
+ placeholder="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
242
+ info="Get token from https://huggingface.co/settings/tokens (or set HF_TOKEN in Space secrets)"
243
+ )
244
+
245
  remove_btn = gr.Button("🚀 Remove Objects", variant="primary", size="lg")
246
 
247
  with gr.Column(scale=2):
 
274
  object_class,
275
  confidence_threshold,
276
  mask_expansion,
277
+ inpaint_prompt,
278
+ hf_token
279
  ],
280
  outputs=[output_image, mask_image, status_text]
281
  )
 
316
  **And 60+ more COCO classes!**
317
 
318
  ### ⚠️ Important Notes:
319
+ - **Token Required**: Either enter your HF token above OR set `HF_TOKEN` in Space secrets
320
+ - **Get Token**: Visit https://huggingface.co/settings/tokens to create one
321
  - Processing may take 30-60 seconds depending on image size
322
  - Results depend on object detection accuracy and image complexity
323
  - Red overlay indicates detected areas when inpainting fails