Spaces:
Runtime error
Runtime error
Commit ·
b5e3557
1
Parent(s): b313b80
Add video processing functionality with OpenCV and Modal integration
Browse filesIncludes:
- Validation and upload of videos to Modal Volume.
- Remote processing via Modal function.
- Local download of processed videos with dynamic paths.
- OpenCV for video dimension extraction.
- Updated dependencies in `requirements.txt`.
Files changed (2)
- app.py+89-21
- requirements.txt+2-1
5 unmodified lines6789101112131415166 unmodified lines2324252326272829273031323319 unmodified lines5354555356575556575859606162635859606162636465666768697071727374757677787980818283846667686970717273748586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451465 unmodified lines
import osimport cv2import timeimport uuidimport modalimport shutilimport loggingimport gradio as gr
6 unmodified lines file upload to presigned URLs, and triggering the processing. """ # 1. Check the duration of both audio files. app = modal.App("Waveform-Matching") waveform_app = modal.App("Waveform-Matching") modal_token_id = os.environ['MODAL_TOKEN_ID'] modal_token_secret = os.environ['MODAL_TOKEN_SECRET'] modal_environment = os.environ['MODAL_ENVIRONMENT'] modal_volume = os.environ['MODAL_VOLUME'] modal_volume = os.environ['WAVEFORM_MODAL_VOLUME'] processing_id = str(int(time.time())) try: bsodtv_storage = modal.Volume.from_name(modal_volume)19 unmodified lines return "Processing started. Results will be emailed to you shortly."
def process_video(video_path, notes, email, company_name):def process_video(video_path, notes, email, company_name) -> str: """ Process the input video for content moderation. Inputs: - video_path: path to the uploaded video file (str) - notes: department notes (str) - email: contact email (str) - company_name: company name (str) Output: - path to the output video file (str) For now, this is a placeholder that returns the input video unchanged. Process the input video for content moderation using Modal. Steps: 1. Upload the provided video to the configured Modal Volume. 2. Obtain the video dimensions (width, height). 3. Call the Content-Moderation reception_function via Modal (synchronously with .remote). 4. Download the processed video returned by the function to /tmp with a random UUID filename. 5. Return the local path to the downloaded video. """ # Validate inputs if not video_path or not os.path.exists(video_path): logger.error("Invalid video path provided to process_video.") return "Invalid video path."
# Helper to obtain width and height def _get_video_dimensions(path: str): try: # type: ignore cap = cv2.VideoCapture(path) if cap.isOpened(): width = int(cap.get(3)) height = int(cap.get(4)) cap.release() except Exception as e: logger.debug(f"OpenCV not available or failed to read video dimensions: {e}") return width, height
try: # In a real implementation, you would trigger your backend processing here. # For this demo, we simply return the original video path as the output. if not video_path: # Gradio expects None to clear the output component return None return video_path except Exception as e: logger.error(f"Error during video processing: {e}") return None # 1. Setup Modal app and volume moderation_app = modal.App("Content-Moderation") _ = os.environ.get('MODAL_TOKEN_ID') # Read to ensure environment readiness (kept for parity with process_audio) _ = os.environ.get('MODAL_TOKEN_SECRET') _ = os.environ.get('MODAL_ENVIRONMENT') modal_volume_name = os.environ['MODERATION_MODAL_VOLUME']
# Unique processing folder and paths processing_id = str(int(time.time())) ext = os.path.splitext(video_path)[1] remote_input_path = f"/{processing_id}/input_video{ext}"
# 2. Upload video to Modal Volume volume = modal.Volume.from_name(modal_volume_name) try: with volume.batch_upload() as batch: batch.put_file(video_path, remote_input_path) except Exception as e: logger.error(f"Error uploading video to Modal Storage: {e}") return "Error uploading video to Cloud Storage."
# 3. Obtain video dimensions width, height = _get_video_dimensions(video_path)
# 4. Call Modal function synchronously try: moderation_function = modal.Function.from_name("Content-Moderation", "reception_function") processed_remote_path = moderation_function.remote( input_text=str(notes) if notes is not None else "", video_path=remote_input_path, size=(int(width), int(height)), ) except Exception as e: logger.error(f"Error calling Modal reception_function: {e}") return "Error calling Outpost to trigger processing."
if not processed_remote_path or not isinstance(processed_remote_path, str): logger.error("Modal function did not return a valid path to the processed video.") return "Processing failed to return an output path."
# 5. Download the processed video to /tmp with UUID filename local_ext = os.path.splitext(processed_remote_path)[1] or ext or ".mp4" local_output_path = f"/tmp/{uuid.uuid4().hex}{local_ext}" try: # Use Modal Volume.read_file to stream the remote file to the local path with open(local_output_path, "wb") as dst: for chunk in volume.read_file(processed_remote_path): if chunk: dst.write(chunk) except Exception as e: logger.error(f"Error downloading processed video from Modal Storage using read_file: {e}") return "Error downloading processed video from Cloud Storage."
# 6. Return local path return local_output_path
except Exception as e: logger.error(f"Unexpected error in process_video: {e}") return "Unexpected error during video processing."
# Create a professional Gradio interface using the Golden ratio (1.618) for proportions# Define custom CSS for a professional look1223modalgradiogradioopencv-python-headlessapp.py+89-21
requirements.txt+2-1