DurgaDeepak commited on
Commit
75a9604
·
verified ·
1 Parent(s): 205a6cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -16,6 +16,7 @@ import cv2
16
  import timeout_decorator
17
  import spaces
18
  import tempfile
 
19
 
20
  from registry import get_model
21
  from core.describe_scene import describe_scene
@@ -141,25 +142,23 @@ def handle(mode, media_upload, url, run_det, det_model, det_confidence, run_seg,
141
  def show_preview_from_upload(files):
142
  if not files:
143
  return gr.update(visible=False), gr.update(visible=False)
144
-
145
  file = files[0]
146
  filename = file.name.lower()
147
 
148
- # Handle image
149
  if filename.endswith((".png", ".jpg", ".jpeg", ".webp")):
150
  img = Image.open(file).convert("RGB")
151
  return gr.update(value=img, visible=True), gr.update(visible=False)
152
 
153
- # Handle video
154
  elif filename.endswith((".mp4", ".mov", ".avi")):
155
- tmp_dir = tempfile.mkdtemp()
 
156
  ext = os.path.splitext(filename)[-1]
157
- save_path = os.path.join(tmp_dir, f"preview_video{ext}")
158
- with open(save_path, "wb") as f:
159
  f.write(file.read())
160
 
161
- # Gradio needs a proper path for video preview
162
- return gr.update(visible=False), gr.update(value=save_path, visible=True)
163
 
164
  return gr.update(visible=False), gr.update(visible=False)
165
 
@@ -261,7 +260,7 @@ with gr.Blocks() as demo:
261
  # video_preview = gr.Video(label="Preview (Video)", visible=False)
262
  # Only one is shown at a time — image or video
263
  img_out = gr.Image(label="Preview / Processed Output", visible=False)
264
- vid_out = gr.Video(label="Preview / Processed Output", visible=False)
265
  json_out = gr.JSON(label="Scene JSON")
266
  zip_out = gr.File(label="Download Results")
267
 
 
16
  import timeout_decorator
17
  import spaces
18
  import tempfile
19
+ import shutil
20
 
21
  from registry import get_model
22
  from core.describe_scene import describe_scene
 
142
  def show_preview_from_upload(files):
143
  if not files:
144
  return gr.update(visible=False), gr.update(visible=False)
145
+
146
  file = files[0]
147
  filename = file.name.lower()
148
 
 
149
  if filename.endswith((".png", ".jpg", ".jpeg", ".webp")):
150
  img = Image.open(file).convert("RGB")
151
  return gr.update(value=img, visible=True), gr.update(visible=False)
152
 
 
153
  elif filename.endswith((".mp4", ".mov", ".avi")):
154
+ # Copy uploaded video to a known temp location
155
+ temp_dir = tempfile.mkdtemp()
156
  ext = os.path.splitext(filename)[-1]
157
+ safe_path = os.path.join(temp_dir, f"uploaded_video{ext}")
158
+ with open(safe_path, "wb") as f:
159
  f.write(file.read())
160
 
161
+ return gr.update(visible=False), gr.update(value=safe_path, visible=True)
 
162
 
163
  return gr.update(visible=False), gr.update(visible=False)
164
 
 
260
  # video_preview = gr.Video(label="Preview (Video)", visible=False)
261
  # Only one is shown at a time — image or video
262
  img_out = gr.Image(label="Preview / Processed Output", visible=False)
263
+ vid_out = gr.Video(label="Preview / Processed Video", visible=False, streaming=True, autoplay=True)
264
  json_out = gr.JSON(label="Scene JSON")
265
  zip_out = gr.File(label="Download Results")
266