Secking commited on
Commit
97106f1
·
1 Parent(s): aae537f

Implement magic code validation with Modal volume integration

Browse files

- Add Base64 decoding of magic codes to retrieve epoch folder names
- Mount MODERATION_MODAL_VOLUME and verify folder existence
- Filter sub-directories by UUID pattern (RFC 4122 format)
- Return segment list to populate Segment IDs dropdown
- Handle errors: invalid codes, missing folders, volume access failures
- Display error messages directly in dropdown label on failure

Addresses all acceptance criteria for magic code backend validation.

Files changed (1) hide show
  1. app.py +125 -21
app.py CHANGED
@@ -6,8 +6,10 @@
6
 
7
 
8
  import os
 
9
  import cv2
10
  import time
 
11
  import modal
12
  import logging
13
  import gradio as gr
@@ -91,34 +93,136 @@ def process_video(video_path, notes, email, company_name) -> str:
91
  return "Unexpected error during video processing."
92
 
93
 
94
- # Placeholder functions for Segmentation Editing tab (future backend integration)
 
 
 
95
  def submit_magic_code(magic_code):
96
  """
97
- Placeholder function to validate magic code and retrieve segment list.
98
- TODO: Connect to backend API endpoint for magic code validation.
 
 
 
 
 
 
99
  """
100
  if not magic_code:
101
  return (
102
- gr.update(choices=[]), # dropdown
103
- "Please enter a magic code", # status message
104
- gr.update(visible=False), # row2
105
- gr.update(visible=False), # row3
106
- gr.update(visible=False), # row4
107
- gr.update(visible=False) # row5
108
  )
109
 
110
- # Placeholder: Simulate retrieving segment IDs
111
- logger.info(f"Magic code submitted: {magic_code}")
112
- sample_segments = ["Segment_001", "Segment_002", "Segment_003"]
113
-
114
- return (
115
- gr.update(choices=sample_segments, value=sample_segments[0]), # dropdown
116
- f"Loaded {len(sample_segments)} segments", # status message
117
- gr.update(visible=True), # row2
118
- gr.update(visible=True), # row3
119
- gr.update(visible=True), # row4
120
- gr.update(visible=True) # row5
121
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
 
124
  def load_segment_frame(segment_id, frame_number):
 
6
 
7
 
8
  import os
9
+ import re
10
  import cv2
11
  import time
12
+ import base64
13
  import modal
14
  import logging
15
  import gradio as gr
 
93
  return "Unexpected error during video processing."
94
 
95
 
96
+ # UUID regex pattern for filtering segment sub-directories
97
+ UUID_PATTERN = re.compile(r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$')
98
+
99
+
100
  def submit_magic_code(magic_code):
101
  """
102
+ Validate magic code and retrieve segment list from Modal volume.
103
+
104
+ Decodes the Base64 magic code to obtain the epoch-based folder name,
105
+ mounts the moderation_volume, checks folder existence, and lists
106
+ UUID-pattern sub-directories.
107
+
108
+ Returns:
109
+ Tuple of (dropdown_update, status_message, row2_visibility, row3_visibility, row4_visibility, row5_visibility)
110
  """
111
  if not magic_code:
112
  return (
113
+ gr.update(choices=[], label="Segment IDs"),
114
+ "Please enter a magic code",
115
+ gr.update(visible=False),
116
+ gr.update(visible=False),
117
+ gr.update(visible=False),
118
+ gr.update(visible=False)
119
  )
120
 
121
+ try:
122
+ # Decode Base64 magic code to get folder name
123
+ try:
124
+ decoded_bytes = base64.b64decode(magic_code, validate=True)
125
+ folder_name = decoded_bytes.decode('utf-8').strip()
126
+ logger.info(f"Magic code decoded to folder: {folder_name}")
127
+ except Exception as e:
128
+ logger.error(f"Failed to decode magic code: {e}")
129
+ return (
130
+ gr.update(choices=[], label="Invalid magic code"),
131
+ "Invalid magic code format – please verify your code",
132
+ gr.update(visible=False),
133
+ gr.update(visible=False),
134
+ gr.update(visible=False),
135
+ gr.update(visible=False)
136
+ )
137
+
138
+ # Get volume name from environment
139
+ try:
140
+ modal_volume_name = os.environ['MODERATION_MODAL_VOLUME']
141
+ except KeyError:
142
+ logger.error("MODERATION_MODAL_VOLUME environment variable not set")
143
+ return (
144
+ gr.update(choices=[], label="Configuration error"),
145
+ "Server configuration error – contact support",
146
+ gr.update(visible=False),
147
+ gr.update(visible=False),
148
+ gr.update(visible=False),
149
+ gr.update(visible=False)
150
+ )
151
+
152
+ # Mount volume and check folder existence
153
+ try:
154
+ volume = modal.Volume.from_name(modal_volume_name)
155
+
156
+ # List contents at the folder path
157
+ folder_path = f"/{folder_name}"
158
+ try:
159
+ # Use listdir to check if folder exists and list contents
160
+ entries = volume.listdir(folder_path)
161
+ except Exception as list_error:
162
+ logger.error(f"Folder not found or inaccessible: {folder_path} - {list_error}")
163
+ return (
164
+ gr.update(choices=[], label="Folder not found"),
165
+ "Folder not found – please verify your magic code",
166
+ gr.update(visible=False),
167
+ gr.update(visible=False),
168
+ gr.update(visible=False),
169
+ gr.update(visible=False)
170
+ )
171
+
172
+ # Filter for UUID-pattern sub-directories
173
+ uuid_segments = []
174
+ for entry in entries:
175
+ # Extract just the name (last component of path)
176
+ entry_name = entry.path.rstrip('/').split('/')[-1]
177
+
178
+ # Check if it's a directory and matches UUID pattern
179
+ if entry.type == modal.volume.FileEntryType.DIRECTORY and UUID_PATTERN.match(entry_name):
180
+ uuid_segments.append(entry_name)
181
+
182
+ uuid_segments.sort() # Sort alphabetically for consistent display
183
+
184
+ if not uuid_segments:
185
+ logger.warning(f"No UUID segments found in folder: {folder_name}")
186
+ return (
187
+ gr.update(choices=[], label="No segments found"),
188
+ "No segment IDs found in this folder",
189
+ gr.update(visible=False),
190
+ gr.update(visible=False),
191
+ gr.update(visible=False),
192
+ gr.update(visible=False)
193
+ )
194
+
195
+ logger.info(f"Found {len(uuid_segments)} UUID segments in folder {folder_name}")
196
+ return (
197
+ gr.update(choices=uuid_segments, value=uuid_segments[0], label="Segment IDs"),
198
+ f"Loaded {len(uuid_segments)} segment(s)",
199
+ gr.update(visible=True),
200
+ gr.update(visible=True),
201
+ gr.update(visible=True),
202
+ gr.update(visible=True)
203
+ )
204
+
205
+ except Exception as e:
206
+ logger.error(f"Error accessing Modal volume: {e}")
207
+ return (
208
+ gr.update(choices=[], label="Volume access error"),
209
+ f"Error accessing storage: {str(e)}",
210
+ gr.update(visible=False),
211
+ gr.update(visible=False),
212
+ gr.update(visible=False),
213
+ gr.update(visible=False)
214
+ )
215
+
216
+ except Exception as e:
217
+ logger.error(f"Unexpected error in submit_magic_code: {e}")
218
+ return (
219
+ gr.update(choices=[], label="Error"),
220
+ "Unexpected error – please try again",
221
+ gr.update(visible=False),
222
+ gr.update(visible=False),
223
+ gr.update(visible=False),
224
+ gr.update(visible=False)
225
+ )
226
 
227
 
228
  def load_segment_frame(segment_id, frame_number):