Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ import spaces
|
|
19 |
from registry import get_model
|
20 |
from core.describe_scene import describe_scene
|
21 |
from core.process import process_image
|
22 |
-
from core.input_handler import resolve_input, validate_video, validate_image
|
23 |
from utils.helpers import format_error, generate_session_id
|
24 |
from huggingface_hub import hf_hub_download
|
25 |
|
@@ -84,32 +84,80 @@ def handle(mode, media_upload, url, run_det, det_model, det_confidence, run_seg,
|
|
84 |
if not media:
|
85 |
return None, format_error("No valid input provided. Please check your upload or URL."), None
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
logger.warning("Unsupported media type resolved.")
|
111 |
log_runtime(start_time)
|
112 |
-
return None, format_error("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
# Gradio Interface
|
115 |
with gr.Blocks() as demo:
|
@@ -131,10 +179,6 @@ with gr.Blocks() as demo:
|
|
131 |
# URL input
|
132 |
url = gr.Textbox(label="URL (Image/Video)", visible=False)
|
133 |
|
134 |
-
# Preview uploaded media
|
135 |
-
preview_image = gr.Image(label="Preview Image", visible=False)
|
136 |
-
preview_video = gr.Video(label="Preview Video", visible=False)
|
137 |
-
|
138 |
# Toggle visibility
|
139 |
def toggle_inputs(selected_mode):
|
140 |
return [
|
@@ -146,8 +190,8 @@ with gr.Blocks() as demo:
|
|
146 |
]
|
147 |
|
148 |
mode.change(toggle_inputs, inputs=mode, outputs=[media_upload, url])
|
149 |
-
media_upload.change(show_preview_from_upload, inputs=media_upload, outputs=[
|
150 |
-
url.submit(show_preview_from_url, inputs=url, outputs=[
|
151 |
|
152 |
# Visibility logic function
|
153 |
def toggle_visibility(checked):
|
@@ -203,9 +247,12 @@ with gr.Blocks() as demo:
|
|
203 |
# single_img_preview = gr.Image(label="Preview (Image)", visible=False)
|
204 |
# gallery_preview = gr.Gallery(label="Preview (Gallery)", columns=3, height="auto", visible=False)
|
205 |
# video_preview = gr.Video(label="Preview (Video)", visible=False)
|
206 |
-
|
|
|
|
|
207 |
json_out = gr.JSON(label="Scene JSON")
|
208 |
zip_out = gr.File(label="Download Results")
|
|
|
209 |
|
210 |
|
211 |
|
@@ -219,10 +266,28 @@ with gr.Blocks() as demo:
|
|
219 |
|
220 |
|
221 |
# Button Click Event
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
run.click(
|
223 |
handle,
|
224 |
-
inputs=[
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
)
|
227 |
|
228 |
# Footer Section
|
|
|
19 |
from registry import get_model
|
20 |
from core.describe_scene import describe_scene
|
21 |
from core.process import process_image
|
22 |
+
from core.input_handler import resolve_input, validate_video, validate_image
|
23 |
from utils.helpers import format_error, generate_session_id
|
24 |
from huggingface_hub import hf_hub_download
|
25 |
|
|
|
84 |
if not media:
|
85 |
return None, format_error("No valid input provided. Please check your upload or URL."), None
|
86 |
|
87 |
+
first_input = media[0]
|
88 |
+
|
89 |
+
# --- VIDEO PATH ---
|
90 |
+
if isinstance(first_input, str) and first_input.lower().endswith((".mp4", ".mov", ".avi")):
|
91 |
+
valid, err = validate_video(first_input)
|
92 |
+
if not valid:
|
93 |
+
return None, format_error(err), None
|
94 |
+
try:
|
95 |
+
_, msg, output_video_path = process_video(
|
96 |
+
video_path=first_input,
|
97 |
+
run_det=run_det,
|
98 |
+
det_model=det_model,
|
99 |
+
det_confidence=det_confidence,
|
100 |
+
run_seg=run_seg,
|
101 |
+
seg_model=seg_model,
|
102 |
+
run_depth=run_depth,
|
103 |
+
depth_model=depth_model,
|
104 |
+
blend=blend
|
105 |
+
)
|
106 |
+
return gr.update(visible=False), msg, output_video_path # Hide img_out, show vid_out via click handler
|
107 |
+
except Exception as e:
|
108 |
+
logger.error(f"Video processing failed: {e}")
|
109 |
+
return None, format_error(str(e)), None
|
110 |
+
|
111 |
+
# --- IMAGE INPUT ---
|
112 |
+
elif isinstance(first_input, Image.Image):
|
113 |
+
valid, err = validate_image(first_input)
|
114 |
+
if not valid:
|
115 |
+
return None, format_error(err), None
|
116 |
+
try:
|
117 |
+
result_img, msg, output_zip = process_image(
|
118 |
+
image=first_input,
|
119 |
+
run_det=run_det,
|
120 |
+
det_model=det_model,
|
121 |
+
det_confidence=det_confidence,
|
122 |
+
run_seg=run_seg,
|
123 |
+
seg_model=seg_model,
|
124 |
+
run_depth=run_depth,
|
125 |
+
depth_model=depth_model,
|
126 |
+
blend=blend
|
127 |
+
)
|
128 |
+
return result_img, msg, output_zip # Show img_out only
|
129 |
+
except timeout_decorator.timeout_decorator.TimeoutError:
|
130 |
+
logger.error("Image processing timed out.")
|
131 |
+
return None, format_error("Processing timed out. Try a smaller image or simpler model."), None
|
132 |
+
except Exception as e:
|
133 |
+
logger.error(f"Image processing failed: {e}")
|
134 |
+
return None, format_error(str(e)), None
|
135 |
|
136 |
logger.warning("Unsupported media type resolved.")
|
137 |
log_runtime(start_time)
|
138 |
+
return None, format_error("Unsupported input type."), None
|
139 |
+
|
140 |
+
def show_preview_from_upload(files):
|
141 |
+
if not files:
|
142 |
+
return gr.update(visible=False), gr.update(visible=False)
|
143 |
+
file = files[0]
|
144 |
+
path = file.name.lower()
|
145 |
+
if path.endswith((".png", ".jpg", ".jpeg", ".webp")):
|
146 |
+
img = Image.open(file).convert("RGB")
|
147 |
+
return gr.update(value=img, visible=True), gr.update(visible=False)
|
148 |
+
elif path.endswith((".mp4", ".mov", ".avi")):
|
149 |
+
return gr.update(visible=False), gr.update(value=path, visible=True)
|
150 |
+
return gr.update(visible=False), gr.update(visible=False)
|
151 |
+
|
152 |
+
def show_preview_from_url(url_input):
|
153 |
+
if not url_input:
|
154 |
+
return gr.update(visible=False), gr.update(visible=False)
|
155 |
+
path = url_input.strip().lower()
|
156 |
+
if path.endswith((".png", ".jpg", ".jpeg", ".webp")):
|
157 |
+
return gr.update(value=url_input, visible=True), gr.update(visible=False)
|
158 |
+
elif path.endswith((".mp4", ".mov", ".avi")):
|
159 |
+
return gr.update(visible=False), gr.update(value=url_input, visible=True)
|
160 |
+
return gr.update(visible=False), gr.update(visible=False)
|
161 |
|
162 |
# Gradio Interface
|
163 |
with gr.Blocks() as demo:
|
|
|
179 |
# URL input
|
180 |
url = gr.Textbox(label="URL (Image/Video)", visible=False)
|
181 |
|
|
|
|
|
|
|
|
|
182 |
# Toggle visibility
|
183 |
def toggle_inputs(selected_mode):
|
184 |
return [
|
|
|
190 |
]
|
191 |
|
192 |
mode.change(toggle_inputs, inputs=mode, outputs=[media_upload, url])
|
193 |
+
media_upload.change(show_preview_from_upload, inputs=media_upload, outputs=[img_out, vid_out])
|
194 |
+
url.submit(show_preview_from_url, inputs=url, outputs=[img_out, vid_out])
|
195 |
|
196 |
# Visibility logic function
|
197 |
def toggle_visibility(checked):
|
|
|
247 |
# single_img_preview = gr.Image(label="Preview (Image)", visible=False)
|
248 |
# gallery_preview = gr.Gallery(label="Preview (Gallery)", columns=3, height="auto", visible=False)
|
249 |
# video_preview = gr.Video(label="Preview (Video)", visible=False)
|
250 |
+
# Only one is shown at a time — image or video
|
251 |
+
img_out = gr.Image(label="Preview / Processed Output", visible=False)
|
252 |
+
vid_out = gr.Video(label="Preview / Processed Output", visible=False)
|
253 |
json_out = gr.JSON(label="Scene JSON")
|
254 |
zip_out = gr.File(label="Download Results")
|
255 |
+
|
256 |
|
257 |
|
258 |
|
|
|
266 |
|
267 |
|
268 |
# Button Click Event
|
269 |
+
run.click(
|
270 |
+
handle,
|
271 |
+
inputs=[
|
272 |
+
mode, media_upload, url,
|
273 |
+
run_det, det_model, det_confidence,
|
274 |
+
run_seg, seg_model,
|
275 |
+
run_depth, depth_model,
|
276 |
+
blend
|
277 |
+
],
|
278 |
+
outputs=[img_out, json_out, zip_out]
|
279 |
+
)
|
280 |
+
|
281 |
run.click(
|
282 |
handle,
|
283 |
+
inputs=[
|
284 |
+
mode, media_upload, url,
|
285 |
+
run_det, det_model, det_confidence,
|
286 |
+
run_seg, seg_model,
|
287 |
+
run_depth, depth_model,
|
288 |
+
blend
|
289 |
+
],
|
290 |
+
outputs=[vid_out, json_out, zip_out]
|
291 |
)
|
292 |
|
293 |
# Footer Section
|