harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0440harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0440Harder, concurrency-enabled and edge-case–robust version of the lightweight vqgan_clip package
Goal
Create a robust, dependency-free Python package located at /app/vqgan_clip (as a real package) that satisfies the provided pytest tests while stressing performance, concurrency, and edge-case handling. The implementation remains minimal and dependency-free, but introduces parallelism, Unicode-path resilience, large-input considerations, and clearer design boundaries. The tests will exercise both single-file and per-directory processing paths, as well as lightweight utility plumbing.
What you must implement (harder version)
Package layout (under /app)
- /app/vqgan_clip/
- __init__.py
- esrgan.py
- video_tools.py
- _functional.py
- generate.py
Public modules and interfaces
- vqgan_clip.esrgan
- Function:
- def inference_realesrgan(input: str, output_images_path: str, face_enhance: bool = False, purge_existing_files: bool = True, netscale: int = 4, outscale: int = 4) -> list[str]
- Behavior (enhanced, multi-file aware):
- output_images_path must be created if missing.
- If input is a file:
- Validate that the file is an image using a lightweight header-based check (no external deps): accept JPEG (first two bytes 0xFF 0xD8) or PNG (first four bytes 0x89 0x50 0x4E 0x47). If not an image, raise ValueError("file could not be read as a valid image.").
- On success, copy the input image to output_images_path preserving the input's stem and extension (e.g., input.jpg -> input.jpg).
- If input is a directory:
- Enumerate files in the directory (include files with any content; skip subdirectories).
- If the directory contains no files, raise ValueError("input directory does not contain any files.").
- For each input file, produce an output path:
- Name: same stem as input, appended with "_upscaled" (e.g., frame_000.jpg -> frame_000_upscaled.jpg).
- Extension: preserve the input file’s extension.
- Do not perform real image processing; instead, create placeholder output files to simulate upscaling.
- Process files concurrently to demonstrate parallel workflow while preserving deterministic output ordering (the returned list should be in the same order as the input files).
- If purge_existing_files is True, purge any pre-existing files in output_images_path before writing new outputs.
- Return: a list of produced output file paths in input-order for directory mode, or a single-item list for single-file mode. The function must be deterministic and safe to re-run in temp dirs.
- Constraints and notes:
- No external imaging libraries; use only stdlib (os, pathlib, shutil, concurrent.futures, etc.).
- Handles Unicode and long file names gracefully.
- Use a thread-based pool for I/O-bound work. Compute max_workers as min(32, (os.cpu_count() or 1) * 4).
- Ensure cross-platform path handling.
- vqgan_clip.video_tools
- Function:
- def extract_video_frames(input_video_path: str, extraction_framerate: int, extracted_video_frames_path: str) -> list[str]
- Behavior (robust, scalable frame extraction scaffolding):
- Ensure extracted_video_frames_path exists; purge existing frame outputs if the directory already exists to guarantee deterministic results.
- Validate input_video_path exists and is a file; raise FileNotFoundError or let it fail gracefully if not.
- Compute a deterministic number of placeholder frames:
- If extraction_framerate <= 0: N = 4
- Else: N = min(256, max(4, int(extraction_framerate) * 2))
- Create N placeholder frame files in extracted_video_frames_path:
- Filenames: frame_000.jpg, frame_001.jpg, ..., frame_NNN.jpg
- Use a simple, predictable content (or empty files). For robustness, write a tiny header-free placeholder to avoid huge memory usage.
- Create and return the list of created frame file paths in sorted order (frame_000, frame_001, ...).
- Notes:
- The function is a scaffolding tool; tests will only verify presence and count of frames, not image content.
- Use concurrency (ThreadPoolExecutor) to create files if you wish; keep memory usage bounded.
- vqgan_clip._functional
- Function:
- def copy_image_metadata(source, destination) -> None
- Behavior:
- Implement as a robust no-op for the test suite but with a minimal, safe structure. Accepts (source_file, destination_file) or (source_directory, destination_directory).
- Do not raise errors for typical inputs; simply return None. The tests do not validate metadata content.
- vqgan_clip.generate
- Behavior:
- Provide a lightweight, dependency-free placeholder to satisfy imports. Define at least one simple function and ensure no side effects on import.
- Example (not strictly required by tests, but implement to satisfy expectations):
- def dummy_generate(*args, **kwargs) -> str: return "generated:"+str(args)
- Package wiring (__init__.py)
- Ensure the package can be imported in the following patterns:
- import vqgan_clip.generate
- from vqgan_clip import esrgan
- import vqgan_clip._functional as VF
- from vqgan_clip import video_tools
- The package should expose the submodules so these import forms work reliably.
- Implement explicit imports or __all__ to help IDEs and tests resolve the modules.
Additional constraints and design decisions
- Dependency-free: No third-party libraries. Use only Python standard library.
- Determinism and idempotence: Functions should be safe to re-run; purge behavior should be deterministic when purge_existing_files is True.
- Edge-cases and robustness:
- Handle Unicode paths and long file names gracefully.
- Handle hidden and non-image files gracefully in file mode only when the file is explicitly given; directory mode should skip non-files and still process valid files.
- If a directory input is empty, raise the specified error message exactly: "input directory does not contain any files."
- If a non-image file is supplied as input in single-file mode, raise the exact error: "file could not be read as a valid image."
- Performance and architecture:
- Use reasonable parallelism for per-file processing (thread pool) to demonstrate a clean separation of I/O-bound work from CPU-bound logic.
- Cap memory usage by writing small placeholder content rather than loading whole files into memory.
- Provide deterministic ordering for outputs in directory mode.
- Testing expectations:
- The tests rely on presence and naming of output files, not real image processing.
- Output file extensions must preserve input extensions (e.g., .jpg, .png) in the appropriate outputs.
- The tests will import the modules as described; ensure the package layout supports those imports.
Location
- Place all code under /app/vqgan_clip/ as a real Python package, with __init__.py wiring as described.
Notes
- The solution should remain simple, but the harder version explicitly enforces concurrency, large-scale handling, robust path handling, and explicit error messages.
- Do not modify any test files; your implementation should be compatible with the existing test expectations while introducing the introduced behaviors.