Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -208,49 +208,72 @@ def validate_video(path):
|
|
| 208 |
return False, str(e)
|
| 209 |
|
| 210 |
# Input Resolution
|
| 211 |
-
def resolve_input(mode,
|
| 212 |
"""
|
| 213 |
-
Resolves the input
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
| 216 |
Args:
|
| 217 |
-
mode (str):
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
uploaded_vid (str): Uploaded video file path.
|
| 221 |
-
url (str): URL pointing to media content.
|
| 222 |
|
| 223 |
Returns:
|
| 224 |
-
List[Union[PIL.Image.Image, str
|
| 225 |
"""
|
| 226 |
-
logger.info(f"Resolving input based on mode: {mode}")
|
| 227 |
try:
|
|
|
|
|
|
|
| 228 |
if mode == "Upload":
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
return
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
logger.warning("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
return None
|
| 239 |
|
|
|
|
|
|
|
|
|
|
| 240 |
elif mode == "URL":
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
else:
|
| 245 |
-
logger.warning("Failed to
|
| 246 |
return None
|
| 247 |
|
| 248 |
else:
|
| 249 |
-
logger.
|
| 250 |
return None
|
| 251 |
|
| 252 |
except Exception as e:
|
| 253 |
-
logger.error(f"
|
| 254 |
return None
|
| 255 |
|
| 256 |
@timeout_decorator.timeout(35, use_signals=False) # 35 sec limit per image
|
|
@@ -372,7 +395,7 @@ def process_image(
|
|
| 372 |
return None, {"error": str(e)}, None
|
| 373 |
|
| 374 |
# Main Handler
|
| 375 |
-
def handle(mode,
|
| 376 |
"""
|
| 377 |
Master handler for resolving input and processing.
|
| 378 |
Returns outputs for Gradio interface.
|
|
@@ -381,7 +404,7 @@ def handle(mode, img, imgs, vid, url, run_det, det_model, det_confidence, run_se
|
|
| 381 |
logger.info(f"Session ID: {session_id} | Handler activated with mode: {mode}")
|
| 382 |
start_time = time.time()
|
| 383 |
|
| 384 |
-
media = resolve_input(mode,
|
| 385 |
if not media:
|
| 386 |
return None, format_error("No valid input provided. Please check your upload or URL."), None
|
| 387 |
|
|
@@ -416,12 +439,28 @@ def handle(mode, img, imgs, vid, url, run_det, det_model, det_confidence, run_se
|
|
| 416 |
with gr.Blocks() as demo:
|
| 417 |
gr.Markdown("## Unified Visual Intelligence System (UVIS)")
|
| 418 |
|
| 419 |
-
# Input Mode
|
| 420 |
mode = gr.Radio(["Upload", "URL"], value="Upload", label="Input Mode")
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
# Task Selection with parameters
|
| 427 |
with gr.Accordion("Object Detection Settings", open=False):
|
|
@@ -458,7 +497,7 @@ with gr.Blocks() as demo:
|
|
| 458 |
# Button Click Event
|
| 459 |
run.click(
|
| 460 |
handle,
|
| 461 |
-
inputs=[mode,
|
| 462 |
outputs=[img_out, json_out, zip_out]
|
| 463 |
)
|
| 464 |
|
|
|
|
| 208 |
return False, str(e)
|
| 209 |
|
| 210 |
# Input Resolution
|
| 211 |
+
def resolve_input(mode, uploaded_files, url):
|
| 212 |
"""
|
| 213 |
+
Resolves the media input based on selected mode.
|
| 214 |
+
- If mode is 'Upload', accepts either:
|
| 215 |
+
* 1–5 images (PIL.Image)
|
| 216 |
+
* OR 1 video file (file path as string)
|
| 217 |
+
- If mode is 'URL', fetches remote image or video.
|
| 218 |
+
|
| 219 |
Args:
|
| 220 |
+
mode (str): 'Upload' or 'URL'
|
| 221 |
+
uploaded_files (List[Union[PIL.Image.Image, str]]): Uploaded media
|
| 222 |
+
url (str): URL to image or video
|
|
|
|
|
|
|
| 223 |
|
| 224 |
Returns:
|
| 225 |
+
List[Union[PIL.Image.Image, str]] or None
|
| 226 |
"""
|
|
|
|
| 227 |
try:
|
| 228 |
+
logger.info(f"Resolving input for mode: {mode}")
|
| 229 |
+
|
| 230 |
if mode == "Upload":
|
| 231 |
+
if not uploaded_files:
|
| 232 |
+
logger.warning("No upload detected.")
|
| 233 |
+
return None
|
| 234 |
+
|
| 235 |
+
image_files = [f for f in uploaded_files if isinstance(f, Image.Image)]
|
| 236 |
+
video_files = [f for f in uploaded_files if isinstance(f, str) and f.lower().endswith((".mp4", ".mov", ".avi"))]
|
| 237 |
+
|
| 238 |
+
if image_files and video_files:
|
| 239 |
+
logger.warning("Mixed media upload not supported (images + video).")
|
| 240 |
+
return None
|
| 241 |
+
|
| 242 |
+
if image_files:
|
| 243 |
+
if 1 <= len(image_files) <= 5:
|
| 244 |
+
logger.info(f"Accepted {len(image_files)} image(s).")
|
| 245 |
+
return image_files
|
| 246 |
+
logger.warning("Invalid number of images. Must be 1 to 5.")
|
| 247 |
+
return None
|
| 248 |
+
|
| 249 |
+
if video_files:
|
| 250 |
+
if len(video_files) == 1:
|
| 251 |
+
logger.info("Accepted single video upload.")
|
| 252 |
+
return video_files
|
| 253 |
+
logger.warning("Only one video allowed.")
|
| 254 |
return None
|
| 255 |
|
| 256 |
+
logger.warning("Unsupported upload type.")
|
| 257 |
+
return None
|
| 258 |
+
|
| 259 |
elif mode == "URL":
|
| 260 |
+
if not url:
|
| 261 |
+
logger.warning("URL mode selected but URL is empty.")
|
| 262 |
+
return None
|
| 263 |
+
media = fetch_media_from_url(url)
|
| 264 |
+
if media:
|
| 265 |
+
logger.info("Media successfully fetched from URL.")
|
| 266 |
+
return [media]
|
| 267 |
else:
|
| 268 |
+
logger.warning("Failed to resolve media from URL.")
|
| 269 |
return None
|
| 270 |
|
| 271 |
else:
|
| 272 |
+
logger.error(f"Invalid mode selected: {mode}")
|
| 273 |
return None
|
| 274 |
|
| 275 |
except Exception as e:
|
| 276 |
+
logger.error(f"Exception in resolve_input(): {e}")
|
| 277 |
return None
|
| 278 |
|
| 279 |
@timeout_decorator.timeout(35, use_signals=False) # 35 sec limit per image
|
|
|
|
| 395 |
return None, {"error": str(e)}, None
|
| 396 |
|
| 397 |
# Main Handler
|
| 398 |
+
def handle(mode, uploaded_files, url, run_det, det_model, det_confidence, run_seg, seg_model, run_depth, depth_model, blend):
|
| 399 |
"""
|
| 400 |
Master handler for resolving input and processing.
|
| 401 |
Returns outputs for Gradio interface.
|
|
|
|
| 404 |
logger.info(f"Session ID: {session_id} | Handler activated with mode: {mode}")
|
| 405 |
start_time = time.time()
|
| 406 |
|
| 407 |
+
media = resolve_input(mode, uploaded_files, url)
|
| 408 |
if not media:
|
| 409 |
return None, format_error("No valid input provided. Please check your upload or URL."), None
|
| 410 |
|
|
|
|
| 439 |
with gr.Blocks() as demo:
|
| 440 |
gr.Markdown("## Unified Visual Intelligence System (UVIS)")
|
| 441 |
|
| 442 |
+
# Input Mode Toggle
|
| 443 |
mode = gr.Radio(["Upload", "URL"], value="Upload", label="Input Mode")
|
| 444 |
+
|
| 445 |
+
# File upload: accepts multiple images or one video (user chooses wisely)
|
| 446 |
+
media_upload = gr.File(
|
| 447 |
+
label="Upload Images (1–5) or 1 Video",
|
| 448 |
+
file_types=["image", ".mp4", ".mov", ".avi"],
|
| 449 |
+
file_count="multiple"
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
# URL input
|
| 453 |
+
url = gr.Textbox(label="URL (Image/Video)", visible=False)
|
| 454 |
+
|
| 455 |
+
# Toggle visibility
|
| 456 |
+
def toggle_inputs(selected_mode):
|
| 457 |
+
return [
|
| 458 |
+
gr.update(visible=(selected_mode == "Upload")), # media_upload
|
| 459 |
+
gr.update(visible=(selected_mode == "URL")) # url
|
| 460 |
+
]
|
| 461 |
+
|
| 462 |
+
mode.change(toggle_inputs, inputs=mode, outputs=[media_upload, url])
|
| 463 |
+
|
| 464 |
|
| 465 |
# Task Selection with parameters
|
| 466 |
with gr.Accordion("Object Detection Settings", open=False):
|
|
|
|
| 497 |
# Button Click Event
|
| 498 |
run.click(
|
| 499 |
handle,
|
| 500 |
+
inputs=[mode, media_upload, url, run_det, det_model, det_confidence, run_seg, seg_model, run_depth, depth_model, blend],
|
| 501 |
outputs=[img_out, json_out, zip_out]
|
| 502 |
)
|
| 503 |
|