JacobLinCool commited on
Commit
92a946e
Β·
verified Β·
1 Parent(s): 7c73b28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -11,7 +11,7 @@ import cv2
11
  from PIL import Image
12
  import zipfile
13
  import io
14
- import tempfile
15
  import os
16
 
17
  DEVICE = Accelerator().device
@@ -85,13 +85,18 @@ def generate_image_embeddings(zip_file):
85
 
86
  embeddings = np.vstack(embeddings)
87
 
88
- # Save embeddings to a temporary file
89
- with tempfile.NamedTemporaryFile(delete=False, suffix=".npy") as tmp:
90
- np.save(tmp.name, embeddings)
91
- output_path = tmp.name
 
 
 
 
 
92
 
93
  message = f"βœ… Successfully generated embeddings for {len(images)} images\nShape: {embeddings.shape}"
94
- return output_path, message
95
 
96
  except Exception as e:
97
  return None, f"❌ Error: {str(e)}"
@@ -170,13 +175,19 @@ def generate_video_embeddings(video_file, fps):
170
 
171
  embeddings = np.vstack(embeddings)
172
 
173
- # Save embeddings to a temporary file
174
- with tempfile.NamedTemporaryFile(delete=False, suffix=".npy") as tmp:
175
- np.save(tmp.name, embeddings)
176
- output_path = tmp.name
 
 
 
 
 
 
177
 
178
  message = f"βœ… Successfully generated embeddings for {len(frames)} frames (extracted at {fps} fps)\nShape: {embeddings.shape}"
179
- return output_path, message
180
 
181
  except Exception as e:
182
  return None, f"❌ Error: {str(e)}"
@@ -194,7 +205,7 @@ with gr.Blocks(title="Video & Image Embedding Generator") as demo:
194
  zip_input = gr.File(label="Upload ZIP file", file_types=[".zip"])
195
  img_submit_btn = gr.Button("Generate Embeddings", variant="primary")
196
  with gr.Column():
197
- img_output = gr.File(label="Download Embeddings (.npy)")
198
  img_status = gr.Textbox(label="Status", lines=3)
199
 
200
  img_submit_btn.click(
@@ -219,7 +230,7 @@ with gr.Blocks(title="Video & Image Embedding Generator") as demo:
219
  )
220
  vid_submit_btn = gr.Button("Generate Embeddings", variant="primary")
221
  with gr.Column():
222
- vid_output = gr.File(label="Download Embeddings (.npy)")
223
  vid_status = gr.Textbox(label="Status", lines=3)
224
 
225
  vid_submit_btn.click(
@@ -233,8 +244,8 @@ with gr.Blocks(title="Video & Image Embedding Generator") as demo:
233
  ### πŸ“ Notes:
234
  - Images in ZIP: Supports PNG, JPG, JPEG, BMP, WEBP formats
235
  - Video: Supports common video formats (MP4, AVI, MOV, etc.)
236
- - Output: NumPy array file (.npy) containing normalized embeddings
237
- - Load embeddings: `embeddings = np.load('embeddings.npy')`
238
  """
239
  )
240
 
 
11
  from PIL import Image
12
  import zipfile
13
  import io
14
+ import json
15
  import os
16
 
17
  DEVICE = Accelerator().device
 
85
 
86
  embeddings = np.vstack(embeddings)
87
 
88
+ # Create JSON output
89
+ result = json.dumps(
90
+ {
91
+ "embeddings": embeddings.tolist(),
92
+ "shape": list(embeddings.shape),
93
+ "count": len(images),
94
+ },
95
+ indent=2,
96
+ )
97
 
98
  message = f"βœ… Successfully generated embeddings for {len(images)} images\nShape: {embeddings.shape}"
99
+ return result, message
100
 
101
  except Exception as e:
102
  return None, f"❌ Error: {str(e)}"
 
175
 
176
  embeddings = np.vstack(embeddings)
177
 
178
+ # Create JSON output
179
+ result = json.dumps(
180
+ {
181
+ "embeddings": embeddings.tolist(),
182
+ "shape": list(embeddings.shape),
183
+ "count": len(frames),
184
+ "fps": fps,
185
+ },
186
+ indent=2,
187
+ )
188
 
189
  message = f"βœ… Successfully generated embeddings for {len(frames)} frames (extracted at {fps} fps)\nShape: {embeddings.shape}"
190
+ return result, message
191
 
192
  except Exception as e:
193
  return None, f"❌ Error: {str(e)}"
 
205
  zip_input = gr.File(label="Upload ZIP file", file_types=[".zip"])
206
  img_submit_btn = gr.Button("Generate Embeddings", variant="primary")
207
  with gr.Column():
208
+ img_output = gr.JSON(label="Embeddings (JSON)")
209
  img_status = gr.Textbox(label="Status", lines=3)
210
 
211
  img_submit_btn.click(
 
230
  )
231
  vid_submit_btn = gr.Button("Generate Embeddings", variant="primary")
232
  with gr.Column():
233
+ vid_output = gr.JSON(label="Embeddings (JSON)")
234
  vid_status = gr.Textbox(label="Status", lines=3)
235
 
236
  vid_submit_btn.click(
 
244
  ### πŸ“ Notes:
245
  - Images in ZIP: Supports PNG, JPG, JPEG, BMP, WEBP formats
246
  - Video: Supports common video formats (MP4, AVI, MOV, etc.)
247
+ - Output: JSON object containing normalized embeddings with metadata
248
+ - Structure: `{"embeddings": [...], "shape": [n, dim], "count": n, "fps": f}`
249
  """
250
  )
251