Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -72,11 +72,14 @@ def preload_models():
|
|
72 |
get_model("segmentation", "deeplabv3_resnet50", device="cpu")
|
73 |
get_model("depth", "midas_v21_small_256", device="cpu")
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
"""
|
78 |
Master handler for resolving input and processing.
|
79 |
-
Returns
|
80 |
"""
|
81 |
session_id = generate_session_id()
|
82 |
logger.info(f"Session ID: {session_id} | Handler activated with mode: {mode}")
|
@@ -84,15 +87,25 @@ def handle(mode, media_upload, url, run_det, det_model, det_confidence, run_seg,
|
|
84 |
|
85 |
media = resolve_input(mode, media_upload, url)
|
86 |
if not media:
|
87 |
-
return
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
first_input = media[0]
|
90 |
-
|
91 |
# --- VIDEO PATH ---
|
92 |
if isinstance(first_input, str) and first_input.lower().endswith((".mp4", ".mov", ".avi")):
|
93 |
valid, err = validate_video(first_input)
|
94 |
if not valid:
|
95 |
-
return
|
|
|
|
|
|
|
|
|
|
|
96 |
try:
|
97 |
_, msg, output_video_path = process_video(
|
98 |
video_path=first_input,
|
@@ -105,16 +118,31 @@ def handle(mode, media_upload, url, run_det, det_model, det_confidence, run_seg,
|
|
105 |
depth_model=depth_model,
|
106 |
blend=blend
|
107 |
)
|
108 |
-
return
|
|
|
|
|
|
|
|
|
|
|
109 |
except Exception as e:
|
110 |
logger.error(f"Video processing failed: {e}")
|
111 |
-
return
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
# --- IMAGE
|
114 |
elif isinstance(first_input, Image.Image):
|
115 |
valid, err = validate_image(first_input)
|
116 |
if not valid:
|
117 |
-
return
|
|
|
|
|
|
|
|
|
|
|
118 |
try:
|
119 |
result_img, msg, output_zip = process_image(
|
120 |
image=first_input,
|
@@ -127,17 +155,38 @@ def handle(mode, media_upload, url, run_det, det_model, det_confidence, run_seg,
|
|
127 |
depth_model=depth_model,
|
128 |
blend=blend
|
129 |
)
|
130 |
-
return
|
|
|
|
|
|
|
|
|
|
|
131 |
except timeout_decorator.timeout_decorator.TimeoutError:
|
132 |
logger.error("Image processing timed out.")
|
133 |
-
return
|
|
|
|
|
|
|
|
|
|
|
134 |
except Exception as e:
|
135 |
logger.error(f"Image processing failed: {e}")
|
136 |
-
return
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
logger.warning("Unsupported media type resolved.")
|
139 |
log_runtime(start_time)
|
140 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
def show_preview_from_upload(files):
|
143 |
if not files:
|
@@ -289,7 +338,7 @@ with gr.Blocks() as demo:
|
|
289 |
|
290 |
# Button Click Event
|
291 |
run.click(
|
292 |
-
handle,
|
293 |
inputs=[
|
294 |
mode, media_upload, url,
|
295 |
run_det, det_model, det_confidence,
|
@@ -297,20 +346,14 @@ with gr.Blocks() as demo:
|
|
297 |
run_depth, depth_model,
|
298 |
blend
|
299 |
],
|
300 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
301 |
)
|
302 |
|
303 |
-
run.click(
|
304 |
-
handle,
|
305 |
-
inputs=[
|
306 |
-
mode, media_upload, url,
|
307 |
-
run_det, det_model, det_confidence,
|
308 |
-
run_seg, seg_model,
|
309 |
-
run_depth, depth_model,
|
310 |
-
blend
|
311 |
-
],
|
312 |
-
outputs=[vid_out, json_out, zip_out]
|
313 |
-
)
|
314 |
|
315 |
# Footer Section
|
316 |
gr.Markdown("---")
|
|
|
72 |
get_model("segmentation", "deeplabv3_resnet50", device="cpu")
|
73 |
get_model("depth", "midas_v21_small_256", device="cpu")
|
74 |
|
75 |
+
def handle(mode, media_upload, url,
|
76 |
+
run_det, det_model, det_confidence,
|
77 |
+
run_seg, seg_model,
|
78 |
+
run_depth, depth_model,
|
79 |
+
blend):
|
80 |
"""
|
81 |
Master handler for resolving input and processing.
|
82 |
+
Returns: (img_out, vid_out, json_out, zip_out)
|
83 |
"""
|
84 |
session_id = generate_session_id()
|
85 |
logger.info(f"Session ID: {session_id} | Handler activated with mode: {mode}")
|
|
|
87 |
|
88 |
media = resolve_input(mode, media_upload, url)
|
89 |
if not media:
|
90 |
+
return (
|
91 |
+
gr.update(visible=False),
|
92 |
+
gr.update(visible=False),
|
93 |
+
format_error("No valid input provided. Please check your upload or URL."),
|
94 |
+
None
|
95 |
+
)
|
96 |
|
97 |
first_input = media[0]
|
98 |
+
|
99 |
# --- VIDEO PATH ---
|
100 |
if isinstance(first_input, str) and first_input.lower().endswith((".mp4", ".mov", ".avi")):
|
101 |
valid, err = validate_video(first_input)
|
102 |
if not valid:
|
103 |
+
return (
|
104 |
+
gr.update(visible=False),
|
105 |
+
gr.update(visible=False),
|
106 |
+
format_error(err),
|
107 |
+
None
|
108 |
+
)
|
109 |
try:
|
110 |
_, msg, output_video_path = process_video(
|
111 |
video_path=first_input,
|
|
|
118 |
depth_model=depth_model,
|
119 |
blend=blend
|
120 |
)
|
121 |
+
return (
|
122 |
+
gr.update(visible=False), # hide image
|
123 |
+
gr.update(value=output_video_path, visible=True), # show video
|
124 |
+
msg,
|
125 |
+
output_video_path # for download
|
126 |
+
)
|
127 |
except Exception as e:
|
128 |
logger.error(f"Video processing failed: {e}")
|
129 |
+
return (
|
130 |
+
gr.update(visible=False),
|
131 |
+
gr.update(visible=False),
|
132 |
+
format_error(str(e)),
|
133 |
+
None
|
134 |
+
)
|
135 |
|
136 |
+
# --- IMAGE PATH ---
|
137 |
elif isinstance(first_input, Image.Image):
|
138 |
valid, err = validate_image(first_input)
|
139 |
if not valid:
|
140 |
+
return (
|
141 |
+
gr.update(visible=False),
|
142 |
+
gr.update(visible=False),
|
143 |
+
format_error(err),
|
144 |
+
None
|
145 |
+
)
|
146 |
try:
|
147 |
result_img, msg, output_zip = process_image(
|
148 |
image=first_input,
|
|
|
155 |
depth_model=depth_model,
|
156 |
blend=blend
|
157 |
)
|
158 |
+
return (
|
159 |
+
gr.update(value=result_img, visible=True), # show image
|
160 |
+
gr.update(visible=False), # hide video
|
161 |
+
msg,
|
162 |
+
output_zip
|
163 |
+
)
|
164 |
except timeout_decorator.timeout_decorator.TimeoutError:
|
165 |
logger.error("Image processing timed out.")
|
166 |
+
return (
|
167 |
+
gr.update(visible=False),
|
168 |
+
gr.update(visible=False),
|
169 |
+
format_error("Processing timed out. Try a smaller image or simpler model."),
|
170 |
+
None
|
171 |
+
)
|
172 |
except Exception as e:
|
173 |
logger.error(f"Image processing failed: {e}")
|
174 |
+
return (
|
175 |
+
gr.update(visible=False),
|
176 |
+
gr.update(visible=False),
|
177 |
+
format_error(str(e)),
|
178 |
+
None
|
179 |
+
)
|
180 |
|
181 |
logger.warning("Unsupported media type resolved.")
|
182 |
log_runtime(start_time)
|
183 |
+
return (
|
184 |
+
gr.update(visible=False),
|
185 |
+
gr.update(visible=False),
|
186 |
+
format_error("Unsupported input type."),
|
187 |
+
None
|
188 |
+
)
|
189 |
+
|
190 |
|
191 |
def show_preview_from_upload(files):
|
192 |
if not files:
|
|
|
338 |
|
339 |
# Button Click Event
|
340 |
run.click(
|
341 |
+
fn=handle,
|
342 |
inputs=[
|
343 |
mode, media_upload, url,
|
344 |
run_det, det_model, det_confidence,
|
|
|
346 |
run_depth, depth_model,
|
347 |
blend
|
348 |
],
|
349 |
+
outputs=[
|
350 |
+
img_out, # will be visible only if it's an image
|
351 |
+
vid_out, # will be visible only if it's a video
|
352 |
+
json_out,
|
353 |
+
zip_out
|
354 |
+
]
|
355 |
)
|
356 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
357 |
|
358 |
# Footer Section
|
359 |
gr.Markdown("---")
|