All MicroEvals
Act as a Staff AI Research Engineer and Senior Backend Archi...
Create MicroEval
Header image for Act as a Staff AI Research Engineer and Senior Backend Archi...

Act as a Staff AI Research Engineer and Senior Backend Archi...

Prompt

Act as a Staff AI Research Engineer and Senior Backend Architect. Write a comprehensive, production-grade Python solution using FastAPI and the Hugging Face `diffusers` library. This system will power advanced "Clone" and "Redesign" features within a professional graphic design suite. The system must expose a POST endpoint `/api/v1/advanced-edit` that accepts: 1. `image`: The source image file (Multipart/Form-Data). 2. `mode`: A string enum ("clone" or "redesign"). 3. `prompt`: The textual instruction from the user. 4. `control_scale`: An optional float (0.0 to 1.0) to manually override the structural weight. Implement the following advanced engineering architectures based on the `mode`: - For "CLONE" (Exact structural preservation with texture/color swap): * Use a `StableDiffusionControlNetImg2ImgPipeline` combined with a Canny Edge or Lineart ControlNet model (e.g., "lllyasviel/control_v11p_sd15_canny"). * Process the source image to extract its edge maps. * Set `controlnet_conditioning_scale` high (0.75 - 0.9) to enforce strict boundary preservation. * Set the Img2Img `strength` low (0.25 - 0.35) to prevent structural drifting, ensuring the generated output is an exact geometric replica but with modified elements. - For "REDESIGN" (Layout-guided reimagining / style transfer): * Use the same ControlNet pipeline but switch to an Depth or OpenPose model, or use an IP-Adapter (Image Prompt Adapter) for holistic style transfer. * Set `controlnet_conditioning_scale` lower (0.4 - 0.5) to treat the source image as a loose spatial guide rather than a hard constraint. * Set the Img2Img `strength` higher (0.65 - 0.8) to give the diffusion model maximum creative freedom to redesign textures, lighting, backgrounds, and aesthetic details while respecting the macro layout. Technical Constraints & Robustness Requirements: - Hardware Optimization: Enable xformers memory efficient attention (`pipe.enable_xformers_memory_efficient_attention()`) and CPU offloading (`pipe.enable_model_cpu_offload()`) if necessary to manage VRAM. Run the core pipeline in `torch.float16` on `cuda`. - Dynamic Resolution: Include a utility function to automatically downscale/upscale input images to the nearest multiple of 8 (e.g., maintaining aspect ratio while fitting within 512x512 or 1024x1024 bounds). - Concurrency & Queueing: Since diffusion inference is compute-intensive, wrap the generation logic inside a thread pool or an async background task queue using a basic locking mechanism to prevent concurrent API requests from crashing the GPU VRAM. - Error Handling: Provide explicit error handling for PIL image corruption, PyTorch CUDA Out-of-Memory (OOM) triggers, and missing model checkpoints. - Response: Stream the final generated image back as a PNG using `StreamingResponse`. Provide the complete file architecture including `main.py`, `utils.py` for image processing, and a `requirements.txt` file. Do not use pseudo-code or omit implementation details.