All MicroEvals
Optimize this Modal FastAPI app for speed: import modal impo...
Create MicroEval

Optimize this Modal FastAPI app for speed: import modal impo...

Prompt

Optimize this Modal FastAPI app for speed: import modal import os import subprocess import tempfile import json from typing import List from fastapi import FastAPI, File, UploadFile, Form, HTTPException from fastapi.responses import FileResponse from fastapi.background import BackgroundTasks # Define the Modal App app = modal.App("gpu-video-transcoder") # Create a Modal Image with FFmpeg installed. # We use an Ubuntu base image (ubuntu:24.04) instead of the default Debian # to avoid issues with recently archived Debian dependency repositories, # and to ensure we have a recent version of FFmpeg (>= 6.0) for av1_nvenc. image = ( modal.Image.from_registry("ubuntu:24.04", add_python="3.11") .apt_install("ffmpeg") .pip_install("fastapi[standard]", "python-multipart") ) # Initialize FastAPI web_app = FastAPI(title="GPU Video Transcoder") def cleanup_temp_dir(dir_path: str): """Background task to clean up temporary files after the response is sent.""" import shutil if os.path.exists(dir_path): shutil.rmtree(dir_path) @web_app.post("/convert") async def convert_video( background_tasks: BackgroundTasks, files: List[UploadFile] = File(...), file_order: str = Form(..., description='JSON array of filenames in order, e.g. ["part1.ts", "part2.ts"]'), codec: str = Form("av1", description='Target codec: "av1" or "h264"') ): # Validate codec if codec not in ["av1", "h264"]: raise HTTPException(status_code=400, detail="Codec must be 'av1' or 'h264'") # Parse the desired order try: order_list = json.loads(file_order) if not isinstance(order_list, list): raise ValueError except ValueError: raise HTTPException(status_code=400, detail="file_order must be a valid JSON list of strings") # Create a temporary directory for this job temp_dir = tempfile.mkdtemp(prefix="transcode_") # Save the uploaded files to the temp directory saved_files = {} for uploaded_file in files: file_path = os.path.join(temp_dir, uploaded_file.filename) with open(file_path, "wb") as buffer: # Read and write in chunks to manage memory while chunk := await uploaded_file.read(8192): buffer.write(chunk) saved_files[uploaded_file.filename] = file_path # Verify that all files specified in the order list were actually uploaded for filename in order_list: if filename not in saved_files: cleanup_temp_dir(temp_dir) raise HTTPException(status_code=400, detail=f"File specified in order not uploaded: {filename}") # Create the FFmpeg concat demuxer file concat_file_path = os.path.join(temp_dir, "concat_list.txt") with open(concat_file_path, "w") as f: for filename in order_list: # FFmpeg concat file requires the format: file 'path/to/file' # We use safe paths without quotes inside the temp directory f.write(f"file '{saved_files[filename]}'\n") output_path = os.path.join(temp_dir, "output.mp4") # Choose the GPU NVENC encoder based on user selection encoder = "av1_nvenc" if codec == "av1" else "h264_nvenc" # Construct the FFmpeg command # -f concat -safe 0: Reads the text list of files # -c:v <encoder>: Uses the hardware-accelerated NVIDIA encoder # -preset p6 -tune hq: High-quality presets for NVENC # -c:a aac: Standardizes audio to AAC ffmpeg_cmd = [ "ffmpeg", "-y", # Overwrite output files "-f", "concat", "-safe", "0", "-i", concat_file_path, "-c:v", encoder, "-preset", "p6", "-tune", "hq", "-c:a", "aac", "-b:a", "192k", output_path ] try: # Run FFmpeg process result = subprocess.run( ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) if result.returncode != 0: print(f"FFmpeg Error Output: {result.stderr}") raise HTTPException(status_code=500, detail="Error occurred during video transcoding.") except Exception as e: cleanup_temp_dir(temp_dir) raise HTTPException(status_code=500, detail=str(e)) # Clean up the directory *after* the file has been successfully streamed to the user background_tasks.add_task(cleanup_temp_dir, temp_dir) return FileResponse( path=output_path, media_type="video/mp4", filename=f"converted_{codec}.mp4" ) # Attach the FastAPI app to a Modal serverless endpoint # Request the L40S GPU explicitly for powerful NVENC processing @app.function(gpu="L40S", image=image, timeout=600) @modal.asgi_app() def fastapi_app(): return web_app # Typical execution block if running locally (modal serve modal_converter.py) if __name__ == "__main__": app.serve()

Drag to resize
Drag to resize