Spaces:
Runtime error
Runtime error
Merge branch 'content-moderation-implementation' into 'main'
Browse filesAdd video processing functionality with OpenCV and Modal integration
See merge request sonne-technology/bsod-tv/waveform-matching-gradio-front-end!21
Files changed (2)
- app.py+89-21
- requirements.txt+2-1
5 unmodified lines6789101112136 unmodified lines202122232425262728293019 unmodified lines50515253545556575859606162636465666768697071727374757677785 unmodified linesimport osimport timeimport modalimport loggingimport gradio as gr6 unmodified linesfile upload to presigned URLs, and triggering the processing."""# 1. Check the duration of both audio files.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']processing_id = str(int(time.time()))try:bsodtv_storage = modal.Volume.from_name(modal_volume)19 unmodified linesreturn "Processing started. Results will be emailed to you shortly."def process_video(video_path, notes, email, company_name):"""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."""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 componentreturn Nonereturn video_pathexcept Exception as e:logger.error(f"Error during video processing: {e}")return None# Create a professional Gradio interface using the Golden ratio (1.618) for proportions# Define custom CSS for a professional look5 unmodified lines6789101112131415166 unmodified lines232425262728293031323319 unmodified lines53545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451465 unmodified linesimport osimport cv2import timeimport uuidimport modalimport shutilimport loggingimport gradio as gr6 unmodified linesfile upload to presigned URLs, and triggering the processing."""# 1. Check the duration of both audio files.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['WAVEFORM_MODAL_VOLUME']processing_id = str(int(time.time()))try:bsodtv_storage = modal.Volume.from_name(modal_volume)19 unmodified linesreturn "Processing started. Results will be emailed to you shortly."def process_video(video_path, notes, email, company_name) -> str:"""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 inputsif 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 heightdef _get_video_dimensions(path: str):try:# type: ignorecap = 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, heighttry:# 1. Setup Modal app and volumemoderation_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 pathsprocessing_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 Volumevolume = 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 dimensionswidth, height = _get_video_dimensions(video_path)# 4. Call Modal function synchronouslytry: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 filenamelocal_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 pathwith 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 pathreturn local_output_pathexcept 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 look
12modalgradio123modalgradioopencv-python-headless
app.py+89-21
requirements.txt+2-1