avinashHuggingface108 commited on
Commit
93bf10c
·
1 Parent(s): 5d1f54f

Fix 502 timeout: Upload returns immediately, processing starts on first status check

Browse files
Files changed (1) hide show
  1. highlights_api.py +45 -15
highlights_api.py CHANGED
@@ -155,23 +155,27 @@ async def upload_video(
155
  }
156
  }
157
 
158
- # Start processing in background
159
- background_tasks.add_task(
160
- process_video_highlights,
161
- job_id,
162
- temp_video_path,
163
- interval,
164
- min_score,
165
- max_highlights,
166
- whisper_model,
167
- timeout,
168
- enable_visual
169
- )
 
 
 
170
 
 
171
  return AnalysisResponse(
172
  job_id=job_id,
173
- status="processing",
174
- message="Video uploaded successfully. Processing started."
175
  )
176
 
177
  except Exception as e:
@@ -186,6 +190,29 @@ async def get_job_status(job_id: str):
186
  # Check active jobs
187
  if job_id in active_jobs:
188
  job = active_jobs[job_id]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  return JobStatus(
190
  job_id=job_id,
191
  status=job["status"],
@@ -222,7 +249,7 @@ async def download_file(filename: str):
222
  filename=filename
223
  )
224
 
225
- async def process_video_highlights(
226
  job_id: str,
227
  video_path: str,
228
  interval: float,
@@ -273,6 +300,9 @@ async def process_video_highlights(
273
  # Analyze segment
274
  analysis = analyzer.analyze_segment(video_path, segment, temp_frame_path)
275
  analyzed_segments.append(analysis)
 
 
 
276
 
277
  # Cleanup temp frame
278
  try:
 
155
  }
156
  }
157
 
158
+ # Store job parameters for immediate return
159
+ active_jobs[job_id] = {
160
+ "status": "queued",
161
+ "progress": 0,
162
+ "message": "Video uploaded. Processing will start shortly.",
163
+ "params": {
164
+ "video_path": temp_video_path,
165
+ "interval": interval,
166
+ "min_score": min_score,
167
+ "max_highlights": max_highlights,
168
+ "whisper_model": whisper_model,
169
+ "timeout": timeout,
170
+ "enable_visual": enable_visual
171
+ }
172
+ }
173
 
174
+ # Return immediately - processing will be triggered by first status check
175
  return AnalysisResponse(
176
  job_id=job_id,
177
+ status="queued",
178
+ message="Video uploaded successfully. Check status to begin processing."
179
  )
180
 
181
  except Exception as e:
 
190
  # Check active jobs
191
  if job_id in active_jobs:
192
  job = active_jobs[job_id]
193
+
194
+ # If job is queued, start processing
195
+ if job["status"] == "queued":
196
+ # Start processing in background
197
+ params = job["params"]
198
+ asyncio.create_task(
199
+ process_video_highlights_async(
200
+ job_id,
201
+ params["video_path"],
202
+ params["interval"],
203
+ params["min_score"],
204
+ params["max_highlights"],
205
+ params["whisper_model"],
206
+ params["timeout"],
207
+ params["enable_visual"]
208
+ )
209
+ )
210
+
211
+ # Update status to processing
212
+ active_jobs[job_id]["status"] = "processing"
213
+ active_jobs[job_id]["progress"] = 5
214
+ active_jobs[job_id]["message"] = "Processing started..."
215
+
216
  return JobStatus(
217
  job_id=job_id,
218
  status=job["status"],
 
249
  filename=filename
250
  )
251
 
252
+ async def process_video_highlights_async(
253
  job_id: str,
254
  video_path: str,
255
  interval: float,
 
300
  # Analyze segment
301
  analysis = analyzer.analyze_segment(video_path, segment, temp_frame_path)
302
  analyzed_segments.append(analysis)
303
+
304
+ # Yield control to allow other requests
305
+ await asyncio.sleep(0)
306
 
307
  # Cleanup temp frame
308
  try: