refact: added request id
Browse files- handler.py +13 -5
handler.py
CHANGED
|
@@ -7,6 +7,7 @@ import tempfile
|
|
| 7 |
import gc
|
| 8 |
import time
|
| 9 |
import threading
|
|
|
|
| 10 |
from transformers import VideoLlavaProcessor, VideoLlavaForConditionalGeneration
|
| 11 |
|
| 12 |
class EndpointHandler:
|
|
@@ -93,7 +94,7 @@ class EndpointHandler:
|
|
| 93 |
# We print the error but do NOT raise it, ensuring the user still gets their result
|
| 94 |
print(f"Webhook failed: {str(e)}")
|
| 95 |
|
| 96 |
-
def _process_video(self, inputs, video_url, parameters, callback_url=None):
|
| 97 |
"""
|
| 98 |
Core video processing logic. Used by both sync and async paths.
|
| 99 |
If callback_url is provided, sends result via webhook.
|
|
@@ -188,6 +189,7 @@ class EndpointHandler:
|
|
| 188 |
# 9. SEND WEBHOOK (if callback_url provided)
|
| 189 |
if callback_url:
|
| 190 |
webhook_data = {
|
|
|
|
| 191 |
"input_prompt": inputs,
|
| 192 |
"video_url": video_url,
|
| 193 |
"result": response_payload
|
|
@@ -206,6 +208,7 @@ class EndpointHandler:
|
|
| 206 |
# Send error via webhook if callback_url provided
|
| 207 |
if callback_url:
|
| 208 |
webhook_data = {
|
|
|
|
| 209 |
"input_prompt": inputs,
|
| 210 |
"video_url": video_url,
|
| 211 |
"result": error_payload
|
|
@@ -229,24 +232,28 @@ class EndpointHandler:
|
|
| 229 |
video_url = data.get("video", None)
|
| 230 |
parameters = data.get("parameters", {})
|
| 231 |
|
|
|
|
|
|
|
|
|
|
| 232 |
# Validation
|
| 233 |
if not video_url:
|
| 234 |
-
return {"error": "Missing 'video' URL.", "status": "failed"}
|
| 235 |
|
| 236 |
# --- ASYNC MODE: Return early, process in background ---
|
| 237 |
if callback_url:
|
| 238 |
-
print(f"Async mode: will send result to {callback_url}")
|
| 239 |
|
| 240 |
# Spawn background thread for processing
|
| 241 |
thread = threading.Thread(
|
| 242 |
target=self._process_video,
|
| 243 |
-
args=(inputs, video_url, parameters, callback_url),
|
| 244 |
daemon=True # Daemon thread won't block process exit
|
| 245 |
)
|
| 246 |
thread.start()
|
| 247 |
|
| 248 |
# Return immediately with acknowledgment
|
| 249 |
return [{
|
|
|
|
| 250 |
"status": "accepted",
|
| 251 |
"message": "Processing started. Result will be sent to callback_url.",
|
| 252 |
"callback_url": callback_url
|
|
@@ -254,5 +261,6 @@ class EndpointHandler:
|
|
| 254 |
|
| 255 |
# --- SYNC MODE: Process and return result ---
|
| 256 |
else:
|
| 257 |
-
result = self._process_video(inputs, video_url, parameters)
|
|
|
|
| 258 |
return [result]
|
|
|
|
| 7 |
import gc
|
| 8 |
import time
|
| 9 |
import threading
|
| 10 |
+
import uuid
|
| 11 |
from transformers import VideoLlavaProcessor, VideoLlavaForConditionalGeneration
|
| 12 |
|
| 13 |
class EndpointHandler:
|
|
|
|
| 94 |
# We print the error but do NOT raise it, ensuring the user still gets their result
|
| 95 |
print(f"Webhook failed: {str(e)}")
|
| 96 |
|
| 97 |
+
def _process_video(self, inputs, video_url, parameters, callback_url=None, request_id=None):
|
| 98 |
"""
|
| 99 |
Core video processing logic. Used by both sync and async paths.
|
| 100 |
If callback_url is provided, sends result via webhook.
|
|
|
|
| 189 |
# 9. SEND WEBHOOK (if callback_url provided)
|
| 190 |
if callback_url:
|
| 191 |
webhook_data = {
|
| 192 |
+
"request_id": request_id,
|
| 193 |
"input_prompt": inputs,
|
| 194 |
"video_url": video_url,
|
| 195 |
"result": response_payload
|
|
|
|
| 208 |
# Send error via webhook if callback_url provided
|
| 209 |
if callback_url:
|
| 210 |
webhook_data = {
|
| 211 |
+
"request_id": request_id,
|
| 212 |
"input_prompt": inputs,
|
| 213 |
"video_url": video_url,
|
| 214 |
"result": error_payload
|
|
|
|
| 232 |
video_url = data.get("video", None)
|
| 233 |
parameters = data.get("parameters", {})
|
| 234 |
|
| 235 |
+
# Generate unique request ID
|
| 236 |
+
request_id = str(uuid.uuid4())
|
| 237 |
+
|
| 238 |
# Validation
|
| 239 |
if not video_url:
|
| 240 |
+
return {"error": "Missing 'video' URL.", "status": "failed", "request_id": request_id}
|
| 241 |
|
| 242 |
# --- ASYNC MODE: Return early, process in background ---
|
| 243 |
if callback_url:
|
| 244 |
+
print(f"Async mode: request_id={request_id}, will send result to {callback_url}")
|
| 245 |
|
| 246 |
# Spawn background thread for processing
|
| 247 |
thread = threading.Thread(
|
| 248 |
target=self._process_video,
|
| 249 |
+
args=(inputs, video_url, parameters, callback_url, request_id),
|
| 250 |
daemon=True # Daemon thread won't block process exit
|
| 251 |
)
|
| 252 |
thread.start()
|
| 253 |
|
| 254 |
# Return immediately with acknowledgment
|
| 255 |
return [{
|
| 256 |
+
"request_id": request_id,
|
| 257 |
"status": "accepted",
|
| 258 |
"message": "Processing started. Result will be sent to callback_url.",
|
| 259 |
"callback_url": callback_url
|
|
|
|
| 261 |
|
| 262 |
# --- SYNC MODE: Process and return result ---
|
| 263 |
else:
|
| 264 |
+
result = self._process_video(inputs, video_url, parameters, request_id=request_id)
|
| 265 |
+
result["request_id"] = request_id
|
| 266 |
return [result]
|