Update ADAM safety, UI, and model workflows
Browse filesPublishes the current polished source release with remote-access hardening, responsive UI fixes, performance improvements, expanded model/plugin workflows, portable tests, clean screenshots, and no local runtime data or settings.
This view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+36-44
- .gitignore+10-16
- Launch ADAM.bat+14-50
- README.md+365-24
- adam/assets.py+76-3
- adam/atlas.py+24-4
- adam/commands.py+42-17
- adam/config.py+9
- adam/dataset_lab.pyAdded+127
- adam/dataset_registry.pyAdded+484
- adam/eve.py+13-2
- adam/executor.py+19-27
- adam/experiment_tracker.pyAdded+387
- adam/generations.py+72
- adam/image_preferences.pyAdded+313
- adam/job_manager.py+311-10
- adam/model_inspector/__init__.pyAdded+15
- adam/model_inspector/base.pyAdded+146
- adam/model_inspector/comparison.pyAdded+160
- adam/model_inspector/ddpm.pyAdded+29
- adam/model_inspector/detector.pyAdded+35
- adam/model_inspector/flow_matching.pyAdded+25
- adam/model_inspector/generic.pyAdded+349
- adam/model_inspector/lora.pyAdded+23
- adam/model_inspector/maskgit.pyAdded+24
- adam/model_inspector/statistics.pyAdded+142
- adam/model_plugin_backend.pyAdded+38
- adam/model_plugins.pyAdded+494
- adam/model_plugins_builtin/__init__.pyAdded+1
- adam/model_plugins_builtin/ddpm/__init__.pyAdded+1
- adam/model_plugins_builtin/ddpm/manifest.pyAdded+56
- adam/model_plugins_builtin/flow_matching/__init__.pyAdded+1
- adam/model_plugins_builtin/flow_matching/manifest.pyAdded+51
- adam/model_plugins_builtin/model_template/__init__.pyAdded+1
- adam/model_plugins_builtin/model_template/manifest.pyAdded+30
- adam/model_plugins_builtin/oasis/__init__.pyAdded+1
- adam/model_plugins_builtin/oasis/manifest.pyAdded+70
- adam/model_plugins_builtin/sdxl_lora/__init__.pyAdded+1
- adam/model_plugins_builtin/sdxl_lora/manifest.pyAdded+60
- adam/model_profiles.pyAdded+93
- adam/models.py+9
- adam/oasis_dataset.pyAdded+215
- adam/orion.py+17-2
- adam/planner.py+303-49
- adam/process_control.py+36
- adam/recommendations.pyAdded+195
- adam/registry.py+52
- adam/remote_access.pyAdded
- adam/remote_api.pyAdded+147
- adam/remote_dashboard.pyAdded+141
12345678910111213141516171819202122232425262728293031323334353637383940414243444546*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textadam/__pycache__/planner.cpython-311.pyc filter=lfs diff=lfs merge=lfs -textadam/ui/__pycache__/main_window.cpython-310.pyc filter=lfs diff=lfs merge=lfs -textadam/ui/__pycache__/main_window.cpython-311.pyc filter=lfs diff=lfs merge=lfs -textadam/ui/__pycache__/studio.cpython-311.pyc filter=lfs diff=lfs merge=lfs -textassets/adam_atom.ico filter=lfs diff=lfs merge=lfs -textassets/adam_atom.png filter=lfs diff=lfs merge=lfs -textbuild/ADAM/ADAM.exe filter=lfs diff=lfs merge=lfs -textbuild/ADAM/ADAM.pkg filter=lfs diff=lfs merge=lfs -textbuild/ADAM/PYZ-00.pyz filter=lfs diff=lfs merge=lfs -textbuild/ADAM/xref-ADAM.html filter=lfs diff=lfs merge=lfs -texttests/__pycache__/test_generations.cpython-311-pytest-8.4.2.pyc filter=lfs diff=lfs merge=lfs -text1234567891011121314151617181920212223242526272829303132333435363738*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textassets/adam_atom.ico filter=lfs diff=lfs merge=lfs -textassets/adam_atom.png filter=lfs diff=lfs merge=lfs -textdocs/screenshots/command-center.png filter=lfs diff=lfs merge=lfs -text
12345678910111213141516171819202122232425262728# Python and test caches__pycache__/*.py[cod].pytest_cache/.venv/venv/# Local application state and user-connected toolsconfig/settings.jsonconfig/settings.local.jsonconfig/external_tools.json!config/external_tools.jsondata/*!data/.gitkeeplogs/# Generated datasets, models, and mediaADAM_Datasets/artifacts/build/dist/*.safetensors*.ckpt*.pt*.pth*.bin*.onnx*.tmp12345678910111213141516171819202122__pycache__/*.py[cod].pytest_cache/.venv/venv/logs/*.logdata/ADAM_Datasets/build/dist/hf-release-*/artifacts/config/settings.local.jsonconfig/settings.json*.tmp*.sqlite3*.safetensors*.ckpt*.pt*.pthLoRAModelsHere/LoRA StableDiffusionModels Here/
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061@echo offsetlocalcd /d "%~dp0"python -c "import yt_dlp, cv2, numpy" 2>nulif errorlevel 1 (echo Installing the ADAM Video Dataset Collector requirements...python -m pip install -r "%~dp0requirements.txt"if errorlevel 1 goto :adam_dependency_error)python -c "import selenium, requests, PIL" 2>nulif errorlevel 1 (echo Installing the Dataset Collector requirements for ADAM...python -m pip install -r "D:\Users\PlayRobloxAllDay\Desktop\Programs\GoogleImageDatasetCollector\requirements.txt"if errorlevel 1 goto :dependency_error)python -c "import datasets, diffusers, transformers, accelerate, torch, torchvision" 2>nulif errorlevel 1 (echo Installing the DDPM training requirements for ADAM...python -m pip install -r "D:\Users\PlayRobloxAllDay\Desktop\Programs\DDPM\requirements.txt"if errorlevel 1 goto :ddpm_dependency_error)python main.pyif errorlevel 1 (echo.echo ADAM could not start. Install the requirements with:echo python -m pip install -r requirements.txtecho.pause)endlocalexit /b:adam_dependency_errorecho.echo ADAM could not install its Video Dataset Collector requirements.echo Run this command with the same Python used to start ADAM:echo python -m pip install -r "%~dp0requirements.txt"echo.pauseendlocalexit /b:dependency_errorecho.echo ADAM could not install the Dataset Collector requirements.echo Run this command and then launch ADAM again:echo python -m pip install -r "D:\Users\PlayRobloxAllDay\Desktop\Programs\GoogleImageDatasetCollector\requirements.txt"echo.pauseendlocalexit /b:ddpm_dependency_errorecho.echo ADAM could not install the DDPM training requirements.echo Run this command and then launch ADAM again:echo python -m pip install -r "D:\Users\PlayRobloxAllDay\Desktop\Programs\DDPM\requirements.txt"echo.pauseendlocalexit /b12345678910111213141516171819202122232425@echo offsetlocalcd /d "%~dp0"python -c "import PySide6, psutil, PIL" 2>nulif errorlevel 1 (echo ADAM's desktop requirements are missing from this Python environment.echo Install them with:echo python -m pip install -r "%~dp0requirements.txt"echo.pauseexit /b 1)rem Optional trainers and collectors are connected through ADAM Settings.rem Their dependencies belong to their own environments, not startup.python main.pyif errorlevel 1 (echo.echo ADAM could not start. Check the application log for details.echo To install the application requirements, run:echo python -m pip install -r "%~dp0requirements.txt"echo.pauseexit /b 1)endlocal
9 unmodified lines10111213141516171819202122232425262728293031323334353637383940414243444546474849505152539 unmodified lines# ADAM — AI Development and Automation ManagerADAM is a local Windows desktop hub for organizing AI image-development workflows. It helps you prepare and review datasets, connect your own training tools, plan runs with explicit approval, monitor jobs, and keep a history of your own assets.## What is included- ADAM source code and the built-in tool registry.- A clean, empty external-tool registry.- No model weights, LoRAs, checkpoints, datasets, generated images, job history, personal paths, API keys, or local settings.## Requirements- Windows 10/11- Python 3.10 or later- Optional: NVIDIA GPU for compatible training workflows- Optional: Ollama for local chat assistance## Install and run```powershellgit clone https://huggingface.co/SyntheticMDProductions/AI_Development_Automation_Managercd AI_Development_Automation_Managerpython -m pip install -r requirements.txtpython main.py```On first launch, ADAM creates your personal `config/settings.json` automatically. In **Settings → Tool folders**, connect the local projects, datasets, and models that you own and want ADAM to manage. ADAM does not bundle or download model weights for you.## Notes for users- Training and collection plans require approval before ADAM starts them.- You are responsible for the licenses, permissions, and rights for any datasets, models, and third-party tools you connect.- This repository is a downloadable desktop application. It is not a hosted Hugging Face Space or an inference model.## Development```powershellpython -m pytest -q```## LicenseADAM is released under the [MIT License](LICENSE).9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933949 unmodified lines# ADAM — AI Development and Automation ManagerADAM is a local, safety-first desktop hub for orchestrating AI project tools.It includes registered dataset, DDPM, and SDXL LoRA workflows with backgroundplanning, approval gates, progress reporting, and persistent asset history.Dataset preparation, captioning, and preview placeholders remain clearly markedas demo tools. The connected Dataset Collector, DDPM trainer, and Local SDXLLoRA Trainer use real adapters and never fall back to simulated training.Existing program folders can be connected from **Settings → Tool folders**.ADAM stores only the path and scans for likely entry points; it does not copy ormodify the external project. Folder assignments can also be pasted into chat:```textDDPM Trainer: D:\AI\DDPMFlow Matching Trainer: D:\AI\FlowMatchImageGenerator```On a new computer, install `requirements.txt` in your chosen Python environmentbefore running `Launch ADAM.bat`. The launcher checks desktop dependencies anddoes not install packages automatically or depend on the developer's personaltrainer folders. Install each optional trainer's dependencies according to thattool's setup instructions before using its ADAM workflow.Remote access is disabled by default. Devices with an access token can browsedatasets, edit captions/review marks and submit work. Only enable it for trusteddevices. The desktop **Allow remote job controls and approval changes** settingalso permits remote confirmation, stopping and retrying jobs. A remote browsercan enable training auto-approval only after that desktop permission is granted;it can always turn auto-approval off. Saved token changes and disabled accesstake effect for new requests without restarting the server.Use private Tailscale access for connections beyond a trusted local network;the built-in HTTP listener does not provide transport encryption by itself.Phone URLs and QR codes contain the access token and should be treated ascredentials. Remote commands require JSON, have bounded request sizes andconnection counts, and reject cross-site browser submissions. These controlsdo not sandbox installed Python plugins or connected trainers: install onlycode you trust.Detection does not automatically authorize training. A real training adapterremains gated until its dataset, model name, run settings, and output locationare explicit.## Training agentsADAM's training lifecycle is divided into four explainable responsibilities:- **EVE** reviews dataset membership and leaves uncertain images for the user.- **ORION** reviews planned epochs, batch size, resolution, image exposures, andestimated optimizer steps. He can require approval but never silently changesthe requested settings. In the Model Creation Assistant, **ORION: apply astarting recipe** fills a conservative, editable draft from the image countand selected resolution before a plan is built.- **ATLAS** watches active training for non-finite loss, sustained critical GPUtemperature, critically low disk space, stalls, and large runtime overruns.Critical conditions pause the trainer process tree so the user can inspect it.- **NOVA** examines available post-training previews and samples for unreadablefiles and exact-looking duplicate collapse. Her report explicitly separatestechnical sample health from subjective or subject-quality review.ORION, ATLAS, and NOVA reports are stored with each durable job record and areshown in Current Plan, Active Job, and Jobs / History respectively. ATLAS'sdefault thresholds can be overridden in `config/settings.json` with the`atlas_*` settings defined in `adam/config.py`.Every new job passes through the shared preflight and ORION review before itsqueue state is chosen. Desktop plans, Remote prompts, and Remote training formsuse the same review. Remote training auto-approval still applies to ordinaryplans, but an ORION warning leaves the job awaiting explicit approval. Reviewinga plan does not change the requested training settings.## Real image collectionWhen a valid Dataset Collector folder is connected, the `dataset_collector`registry entry uses ADAM's real visible-browser adapter. After plan approval it:- opens Bing Images in a normal visible Chrome window;- waits when consent/CAPTCHA/human-verification text is detected;- resumes automatically after the user resolves the page;- downloads valid images at least 256×256;- removes exact duplicate downloads;- writes a matching `.txt` caption beside every image; and- records URLs, captions, sources, and dimensions in `metadata.csv`.No CAPTCHA or website restriction is bypassed. Closing Chrome or stopping thejob ends collection safely. A new timestamped dataset folder is used ratherthan overwriting an existing collection.ADAM keeps an incomplete DDPM request in conversation memory. A follow-up suchas `dataset folder Mario, model name Mario V2, epoch count 100, output D:\Runs`fills the pending fields and validates named datasets against the connectedcollector. It will not start if the dataset cannot be found.## Showcase videosThe **Showcase Video** workspace creates a finished MP4 directly from completedDDPM and Flow Matching models. Select and reorder the models, choose 12–24images per model, a 3-, 4-, or 5-second image duration, shared steps and aspectratio, provider-compatible samplers, seed, and 720p or 1080p output. ADAM runsthe image batches sequentially and then renders a request-list interface thattracks the active model, image number, trainer, steps, sampler, and aspect ratio.LoRA models are intentionally excluded from this streamlined workflow.When Ollama is reachable, messages that are not workflow commands receive ashort conversational answer. Ollama may explain or plan, but it still cannotbypass the registry or confirmation gates.## Web search in Chat ModeChat Mode can give local Ollama current web context without an API key. Enableit in **Settings → Planning model**, then ask naturally, for example:```textSearch the web for Dandy's World character ideas.What are the latest Ollama release notes?Look up a reference for a cyberpunk city character.```ADAM sends only that search query to Bing's public results feed, reads theresult titles and snippets,and passes up to five titles, snippets, and links to Ollama. It does not openthe result pages, download anything, or let web content run tools. Results areuntrusted reference material, so ADAM is instructed to cite the links and flaguncertainty. Disable the setting to keep Chat Mode fully local.When you explicitly ask ADAM to **read**, **open**, or **research** result links,it can read up to three public HTML/text pages and give Ollama short extracts.For example: `Search the web for Undertale character ideas and read the mostrelevant links.` Direct links can be read with `Read https://example.com/ andsummarize it.` Private/local addresses, non-web protocols, oversized pages,downloads, and more than three pages are blocked. This control can be disabledin Settings.Planning runs away from the interface thread, and conversational Ollama outputis streamed into the chat. ADAM validates training commands against a strictschema and each registered trainer's declared capabilities before offering ajob.In **Settings → Planning model**, **Chat response length** sets the maximumnumber of generated tokens for a Chat Mode reply. Higher values allow longerresearch summaries but use more time and GPU memory. The default is 1,024,which gives Qwen3 enough room to reason and still produce a visible response.ADAM stores friendly dataset/model names, paths, trainer types, epochs, andresume checkpoints in `data/assets.json`. Requests such as:```textFrom the Mario dataset, train it on a DDPM for 300 epochs.With the Mario dataset, train it on a LoRA for 100 epochs.Continue the Mario model from the DDPM for 50 epochs.```are resolved to real paths before approval. Continuation is offered only when acompatible checkpoint exists. New DDPM runs retain the latest resume checkpoint.## Run```powershellpython main.py```On Windows, you can also double-click `Launch ADAM.bat`.The app requires Python 3.10+ and PySide6. Optional integrations use `psutil`for system information and `pynvml` for NVIDIA GPU information.```powershellpython -m pip install -r requirements.txt```Try:- Click **Create a model…** in Trainer Mode for the guided Model Creation Assistant.- `Adam, train a LoRA of Hatsune Miku`- `Adam, collect a dataset of liminal spaces`- `Adam, generate previews`- `Adam, check GPU status`- `From the Mario dataset, train it on a DDPM for 300 epochs`- `With the Mario dataset, train it on a LoRA for 100 epochs`Training and large collection plans are never started until you approve theplan. All actions are recorded in `logs/adam.log`, while project artifacts liveunder `data/projects/`.The Model Creation Assistant can start from a built-in Character LoRA, StyleLoRA, DDPM, or Flow Matching preset. It can create a dataset or select aregistered one, recommends starting values, and saves personal presets. Theresult still goes through ADAM's normal validated planner and approval gate.Use **+ Add model** to build a multi-model training batch. Each wide model tabkeeps its own dataset, trainer, name, and settings; the minus button removes anunwanted model, and tabs can be dragged to change the run order. ADAM validatesall models, presents one combined approval plan, and runs them sequentially soonly one training workflow uses the GPU at a time. A failed step stops the batchbefore a later model starts.Before approval, ADAM adds checks for connected tools, dataset contents, theLoRA base model, and output-drive free space. Completed dataset and trainingjobs also include a suggested next step.### Model Batch BuilderUse **Create model batch…** to paste one requested subject per line. ADAM turnsthe list into editable model tabs, removes duplicate names, and lets the currenttrainer recipe be applied to any multi-selection of models. The batch is savedas a draft so it can be closed and resumed later.For a review-first workflow, choose **Collect missing datasets first**. Thisqueues only sequential dataset collection and leaves training in the saveddraft. After collection, reopen the draft, use **Find collected datasets**, andreview each dataset in Training Studio. **Exclude rejected** moves rejectedimages out of the training folder into a recoverable quarantine, and **Restoreexcluded** reverses it. **Keep all images** marks the whole selected dataset asaccepted in one action, after which individual bad images can still be rejected.Training remains locked until each model is explicitlymarked as reviewed and ready. If every linked dataset is acceptable as-is,**Approve all datasets** marks the entire batch ready after one confirmation;it does not inspect individual images or apply pending rejection decisions.Completed Flow Matching models can be selected in **Fine-tune**. ADAM uses thesaved Flow model folder as the continuation source, locks the continuation tothe model's original resolution, and writes the fine-tuned result to a newoutput folder. This continues the saved weights while starting a fresh optimizerand learning-rate schedule; it does not overwrite the original model.## Training StudioThe **Training Studio** turns completed work into a reviewable experiment loop:- **Datasets** provides an image gallery, keep/reject decisions, caption editing,exact duplicate detection, and visually similar duplicate candidates.- **Experiments** compares job settings and outcomes, opens outputs, marks apreferred model, and converts successful settings into reusable recipes.- **Checkpoint Lab** browses model checkpoints and output images, recordsconsistent prompt/seed evaluations, and sends preview requests through thenormal approval-aware planner.- **Recipes** preserves training starting points and can import or exportportable JSON recipe files.### EVE AI Dataset ReviewIn Training Studio → Datasets, **EVE AI Review…** performs a local reference-guided visual review. Add one or more good reference images and optional badreferences, then choose Keep and Reject confidence thresholds. EVE uses a smallDINOv2 vision model to divide the selected dataset into **Keep**, **Reject**, and**Uncertain** galleries with confidence scores. The model is downloaded once onfirst use and subsequent analysis stays local.Nothing is applied automatically. Inspect both sides, double-click images for afull view, and move selected results between the three groups before choosing**Apply EVE review**. EVE's decisions remain ordinary Training Studio reviewmarks: they can be manually changed, and rejected files are not moved until**Exclude rejected** is selected. The latest proposal is also saved under`data/eve_reviews/` for auditing. Use **Select all in current group** (orCtrl/Shift selection) to move many images at once; EVE transfers only thechosen thumbnails so manual sorting stays responsive on large datasets.Training panels show elapsed time, a progress-based ETA, recent logs, and aloss sparkline when the connected trainer reports `loss`. Preflight summariesinclude clearly labelled workload, duration, VRAM, and disk estimates. Theseestimates are planning hints rather than hardware guarantees.Create a Model also supports live training previews with a configurableepoch interval, prompt, and reproducible seed for each model tab. While atraining job is active, its newest 256×256 preview appears in the right sidebarwith the source epoch and next scheduled preview. The full-size trainer outputcan be opened from the card. Built-in adapters may publish previews directly;registered DDPM, Flow, LoRA, APVD, MaskGit, and other trainers can alsoparticipate by writing conventionally named `preview`, `sample`, or `epoch`images beneath their declared output folder.## GenerationsThe **Generations** workspace runs compatible registered image generatorswithout opening their separate desktop interfaces. The connected DDPM and FlowMatching projects can generate from completed models with a reproducible seed,sampler or ODE method, step count, image count, and aspect ratio. Generationwork uses the normal ADAM job queue, progress reporting, cancellation, andlogging.Every completed batch is stored under `data/generations/` with its images and a`generation.json` sidecar. The history gallery can open an image or batch folderand restore the exact settings for another run. DDPM creative notes are storedwith a batch for organization; they are not presented as text conditioning foran unconditional DDPM model.Generation history opens as automatic model folders. Each folder uses theregistered model name and a recent generated image as its cover. Double-clickinga folder filters history to that model and selects its provider and model in thegeneration controls, so the next batch is generated into the same existing modeldirectory. This view does not move or rewrite older generation files.**Generation Cycle…** selects multiple compatible completed models and queuesone generation step per model. Choose images per model, a shared prompt orcreative note, starting seed, slideshow duration, looping, fullscreen playback,and an optional model/trainer label. When the cycle finishes, ADAM opens theresults as a local slideshow while preserving every ordinary generation recordin history.If ADAM discovers a job interrupted by an unexpected shutdown, it offers toopen Jobs & History. The previous record remains intact and can be retried as anew approval-gated job. Job logs can also be exported for troubleshooting.## Connect an existing toolADAM supports importable Python functions and command-line Python scripts.For a no-code setup, open **Settings → External Tools → Add external tool**.Choose the program folder, select its training entry script and importantconfiguration files, then review ADAM's static compatibility and safety report.The report covers:- detected command-line options and required inputs;- likely dataset formats;- output and checkpoint behavior;- progress reporting;- resume-training support; and- potentially risky operations visible in the selected entry script.The 1–10 rating measures how clearly the script fits ADAM's safe command-linecontract. It is not a guarantee that third-party code is harmless. ADAM doesnot execute a script while scanning it, external tools cannot replace built-inregistry entries, and every external-tool run requires explicit approval.After registration, a tool can be planned with a request such as:```textRun APVD Model Trainer with dataset=D:\DreamData, epochs=20, output=D:\APVD\output```ADAM will ask for any required inputs that were omitted before it offers theapproval plan.For manual registry configuration, edit the relevant item in`config/tools.json`:```json{"backend": {"type": "python","module": "my_tools.lora","function": "train"},"demo": false}```The function receives a `ToolContext` as its first argument and keywordarguments from the approved plan. This keeps training code in one place: yourexisting GUI and ADAM can both call the same backend.For scripts:```json{"backend": {"type": "script","path": "D:/AI/LoRATrainer/train.py"},"demo": false}```ADAM invokes scripts directly with the current Python interpreter, capturesstdout/stderr, and never drives another GUI with mouse clicks.## Safety model- Plans are shown before execution.- Long, destructive, or high-volume work requires confirmation.- Unregistered tools cannot be invoked.- External paths and arguments are validated before execution.- The LLM may propose a plan, but only registered tools can execute it.- Pause, resume, and cancel controls are available for active jobs.- Every tool action and state transition is logged.## Tests```powershellpython -m pytest -q```
16 unmodified lines1718192021225 unmodified lines2829303132337 unmodified lines41424344454635 unmodified lines82838485868712 unmodified lines10010110210310410512 unmodified lines11811912012112212312412512612723 unmodified lines15115215315415515615736 unmodified lines19419519619719819919 unmodified lines2192202212222232243 unmodified lines2282292302312322332347 unmodified lines24224324424524624724824911 unmodified lines26126226326426526626726816 unmodified linesreturn re.sub(r"[^a-z0-9]+", " ", value.casefold()).strip()@dataclass(slots=True)class Asset:id: str5 unmodified linescheckpoint: str = ""epochs: int = 0created_at: str = ""@classmethoddef from_dict(cls, payload: dict[str, Any]) -> "Asset":7 unmodified linescheckpoint=str(payload.get("checkpoint", "")),epochs=int(payload.get("epochs", 0) or 0),created_at=str(payload.get("created_at") or _now()),)35 unmodified linesdataset_id: str = "",checkpoint: str = "",epochs: int = 0,persist: bool = True,) -> Asset:resolved = str(Path(path).expanduser().resolve())12 unmodified linesasset.checkpoint = checkpointasset.epochs = int(epochs)asset.created_at = asset.created_at or _now()if existing is None:self.assets.insert(0, asset)if persist:12 unmodified lineskey: item[key]for key in ("kind", "name", "path", "trainer", "dataset_id","checkpoint", "epochs",)if key in item}dataset_path = str(item.get("dataset_path", ""))if item.get("kind") == "model" and dataset_path and Path(dataset_path).is_dir():dataset = self.register(23 unmodified linesfolders = config.get("tool_folders", {})if not isinstance(folders, dict):returnapp_root = self.path.parent.parentexternal_lora_root = app_root / "LoRAModelsHere"if external_lora_root.is_dir():for path in external_lora_root.rglob("*.safetensors"):36 unmodified lines("ddpm", "ddpm_trainer", "output"),("lora", "lora_trainer", "output"),("flow", "flow_trainer", "output_flow_models"),):root = Path(str(folders.get(folder_name, ""))) / output_nameif not root.is_dir():19 unmodified lineselse -1,)elif trainer == "lora":checkpoints = sorted((path for path in folder.glob("*.safetensors")3 unmodified lines)if checkpoints:name = checkpoints[-1].stem.removesuffix("_cancelled")else:checkpoints = []try:metadata = json.loads(7 unmodified linesdataset_path = flow_datasets.get(str(folder.resolve()), "")except (OSError, ValueError, TypeError, json.JSONDecodeError):continuecheckpoint = (str(folder) if trainer == "flow" else str(checkpoints[-1]) if checkpoints else "")dataset_id = ""if dataset_path and Path(dataset_path).is_dir():11 unmodified linestrainer=trainer,dataset_id=dataset_id,checkpoint=checkpoint,persist=False,)self.save()def _flow_dataset_paths(self) -> dict[str, str]:16 unmodified lines1718192021222324252627282930315 unmodified lines373839404142437 unmodified lines5152535455565735 unmodified lines9394959697989912 unmodified lines11211311411511611711811912012112 unmodified lines13413513613713813914014114214314414514614723 unmodified lines17117217317417517617717817918018118218318418518618718836 unmodified lines22522622722822923023119 unmodified lines2512522532542552562573 unmodified lines2612622632642652662672682692702712727 unmodified lines28028128228328428528628728828929029129229329429529629729829930030130230311 unmodified lines31531631731831932032132232332432532632732832933033133233333433533633733833934034116 unmodified linesreturn re.sub(r"[^a-z0-9]+", " ", value.casefold()).strip()def _friendly_name(value: str, fallback: str) -> str:text = str(value or "").strip()if not text:return fallbackif re.search(r"^[A-Za-z]:[\\/]", text) or "/" in text or "\\" in text:return Path(text).name or fallbackreturn text@dataclass(slots=True)class Asset:id: str5 unmodified linescheckpoint: str = ""epochs: int = 0created_at: str = ""metadata: dict[str, Any] | None = None@classmethoddef from_dict(cls, payload: dict[str, Any]) -> "Asset":7 unmodified linescheckpoint=str(payload.get("checkpoint", "")),epochs=int(payload.get("epochs", 0) or 0),created_at=str(payload.get("created_at") or _now()),metadata=dict(payload.get("metadata") or {}),)35 unmodified linesdataset_id: str = "",checkpoint: str = "",epochs: int = 0,metadata: dict[str, Any] | None = None,persist: bool = True,) -> Asset:resolved = str(Path(path).expanduser().resolve())12 unmodified linesasset.checkpoint = checkpointasset.epochs = int(epochs)asset.created_at = asset.created_at or _now()if metadata:current = dict(asset.metadata or {})current.update(metadata)asset.metadata = currentif existing is None:self.assets.insert(0, asset)if persist:12 unmodified lineskey: item[key]for key in ("kind", "name", "path", "trainer", "dataset_id","checkpoint", "epochs", "metadata",)if key in item}if "trigger_word" in item:metadata = dict(values.get("metadata") or {})metadata["trigger_word"] = str(item.get("trigger_word") or "")values["metadata"] = metadatadataset_path = str(item.get("dataset_path", ""))if item.get("kind") == "model" and dataset_path and Path(dataset_path).is_dir():dataset = self.register(23 unmodified linesfolders = config.get("tool_folders", {})if not isinstance(folders, dict):returnfolders = dict(folders)app_root = self.path.parent.parentif not folders.get("oasis_trainer"):try:external = json.loads((app_root / "config" / "external_tools.json").read_text(encoding="utf-8"))except (OSError, ValueError, TypeError, json.JSONDecodeError):external = {}for entry in external.get("tools", []) if isinstance(external, dict) else []:if isinstance(entry, dict) and entry.get("id") == "external_oasis_game_trainer":root = str(entry.get("backend", {}).get("root", ""))if root:folders["oasis_trainer"] = rootexternal_lora_root = app_root / "LoRAModelsHere"if external_lora_root.is_dir():for path in external_lora_root.rglob("*.safetensors"):36 unmodified lines("ddpm", "ddpm_trainer", "output"),("lora", "lora_trainer", "output"),("flow", "flow_trainer", "output_flow_models"),("oasis", "oasis_trainer", "output_action_flow_models"),):root = Path(str(folders.get(folder_name, ""))) / output_nameif not root.is_dir():19 unmodified lineselse -1,)elif trainer == "lora":trigger_word = ""checkpoints = sorted((path for path in folder.glob("*.safetensors")3 unmodified lines)if checkpoints:name = checkpoints[-1].stem.removesuffix("_cancelled")try:metadata = json.loads((folder / "model_info.json").read_text(encoding="utf-8"))trigger_word = str(metadata.get("trigger_word") or "")except (OSError, ValueError, TypeError, json.JSONDecodeError):trigger_word = ""elif trainer == "flow":checkpoints = []try:metadata = json.loads(7 unmodified linesdataset_path = flow_datasets.get(str(folder.resolve()), "")except (OSError, ValueError, TypeError, json.JSONDecodeError):continueelse:checkpoints = []try:metadata = json.loads((folder / "action_flow_model_info.json").read_text(encoding="utf-8"))if metadata.get("model_type") != "action_conditioned_rectified_flow_video":continueif not (folder / "unet" / "config.json").is_file():continuename = _friendly_name(str(metadata.get("model_name") or metadata.get("name") or name),folder.name,)except (OSError, ValueError, TypeError, json.JSONDecodeError):continuecheckpoint = (str(folder) if trainer in {"flow", "oasis"} else str(checkpoints[-1]) if checkpoints else "")dataset_id = ""if dataset_path and Path(dataset_path).is_dir():11 unmodified linestrainer=trainer,dataset_id=dataset_id,checkpoint=checkpoint,metadata=({"trigger_word": trigger_word or name} if trainer == "lora" else None),persist=False,)try:from adam.dataset_registry import DatasetRegistrydataset_registry = DatasetRegistry(app_root, config)valid_location_ids = {location.id for location in dataset_registry.known_locations()}self.assets = [item for item in self.assetsif not (item.kind == "dataset"and isinstance(item.metadata, dict)and item.metadata.get("dataset_registry_source") in {"adam", "tool"}and item.metadata.get("dataset_location_id")and item.metadata.get("dataset_location_id") not in valid_location_ids)]dataset_registry.discover_into_assets(self, persist=False)except Exception:passself.save()def _flow_dataset_paths(self) -> dict[str, str]:
1 unmodified line234567848 unmodified lines57585960616263646566676869707172731 unmodified lineimport mathimport reimport timefrom dataclasses import dataclassfrom datetime import datetimefrom typing import Any48 unmodified linesreturn AtlasDecision("critical", f"GPU temperature remained at {temperature:.0f}°C. ATLAS paused the job.", "pause")free_disk = max(0.0, snapshot.disk_total_gb - snapshot.disk_used_gb)state["disk_samples"] = state["disk_samples"] + 1 if snapshot.disk_total_gb and free_disk <= self.critical_disk_gb else 0if state["disk_samples"] >= 2:return AtlasDecision("critical", f"Only {free_disk:.1f} GB remains on the output drive. ATLAS paused the job.", "pause")stalled_minutes = (moment - state["changed_at"]) / 60if stalled_minutes >= self.stall_minutes and snapshot.gpu_percent < 5:return AtlasDecision("warning", f"No recorded progress and little GPU activity for {stalled_minutes:.0f} minutes. Check the trainer.")if temperature is not None and temperature >= self.warning_temp:return AtlasDecision("warning", f"GPU temperature is elevated at {temperature:.0f}°C; ATLAS is watching it closely.")if snapshot.disk_total_gb and free_disk <= self.warning_disk_gb:return AtlasDecision("warning", f"Output drive space is getting low ({free_disk:.1f} GB free).")if snapshot.memory_percent >= 95:return AtlasDecision("warning", f"System memory usage is very high at {snapshot.memory_percent:.0f}%.")1 unmodified line234567891048 unmodified lines59606162636465666768697071727374757677787980818283848586878889909192931 unmodified lineimport mathimport reimport shutilimport timefrom pathlib import Pathfrom dataclasses import dataclassfrom datetime import datetimefrom typing import Any48 unmodified linesreturn AtlasDecision("critical", f"GPU temperature remained at {temperature:.0f}°C. ATLAS paused the job.", "pause")free_disk = max(0.0, snapshot.disk_total_gb - snapshot.disk_used_gb)disk_known = bool(snapshot.disk_total_gb)disk_label = "monitored drive"output = job.plan.steps[job.current_step].arguments.get("output_dir") or job.output_folderdisk_error = Falseif output:disk_label = "output drive"try:target = Path(str(output)).expanduser().resolve()while not target.exists() and target != target.parent:target = target.parentusage = shutil.disk_usage(target)free_disk = usage.free / (1024 ** 3)disk_known = Trueexcept (OSError, ValueError):disk_known = Falsedisk_error = Truestate["disk_samples"] = state["disk_samples"] + 1 if disk_known and free_disk <= self.critical_disk_gb else 0if state["disk_samples"] >= 2:return AtlasDecision("critical", f"Only {free_disk:.1f} GB remains on the {disk_label}. ATLAS paused the job.", "pause")stalled_minutes = (moment - state["changed_at"]) / 60if stalled_minutes >= self.stall_minutes and snapshot.gpu_percent < 5:return AtlasDecision("warning", f"No recorded progress and little GPU activity for {stalled_minutes:.0f} minutes. Check the trainer.")if temperature is not None and temperature >= self.warning_temp:return AtlasDecision("warning", f"GPU temperature is elevated at {temperature:.0f}°C; ATLAS is watching it closely.")if disk_error:return AtlasDecision("warning", "ATLAS could not check the output drive's free space. Check that the output location is available.")if disk_known and free_disk <= self.warning_disk_gb:return AtlasDecision("warning", f"Space on the {disk_label} is getting low ({free_disk:.1f} GB free).")if snapshot.memory_percent >= 95:return AtlasDecision("warning", f"System memory usage is very high at {snapshot.memory_percent:.0f}%.")
123456789 unmodified lines1819202122232425262728293011 unmodified lines424344454647484950515253545556575859606162636465666768697071727374757677787980818283from __future__ import annotationsfrom dataclasses import dataclassfrom typing import Anyclass CommandValidationError(ValueError):pass9 unmodified linesoutput: str = "default output"resume_from: str = ""base_model: str = ""training_options: dict[str, Any] | None = None@classmethoddef from_dict(cls, payload: dict[str, Any]) -> "TrainingCommand":allowed = {"action", "trainer", "dataset", "model_name", "epochs", "output","resume_from", "base_model","training_options",}unknown = set(payload) - allowed11 unmodified linesoutput=str(payload.get("output", "default output")).strip(),resume_from=str(payload.get("resume_from", "")).strip(),base_model=str(payload.get("base_model", "")).strip(),training_options=dict(payload.get("training_options") or {}),)except (TypeError, ValueError) as exc:raise CommandValidationError("Training command fields have invalid types.") from excif command.action not in {"train", "resume_training"}:raise CommandValidationError("Training action must be train or resume_training.")if command.trainer not in {"ddpm", "lora", "flow"}:raise CommandValidationError("Trainer must be ddpm, flow, or lora.")if not command.dataset or not command.model_name:raise CommandValidationError("Dataset and model name are required.")if not 1 <= command.epochs <= 100_000:raise CommandValidationError("Epoch count must be between 1 and 100000.")if command.action == "resume_training" and not command.resume_from:raise CommandValidationError("Resume training requires an explicit checkpoint.")command._validate_options()return commanddef _validate_options(self) -> None:options = self.training_options or {}allowed = {"ddpm": {"resolution", "batch_size", "learning_rate", "gradient_accumulation_steps","dataloader_num_workers", "mixed_precision", "save_every", "preview_steps","training_intensity", "preview_enabled", "preview_every", "preview_prompt","preview_seed",},"flow": {"resolution", "batch_size", "learning_rate", "gradient_accumulation","workers", "mixed_precision", "save_every", "preview_every", "preview_steps","gradient_checkpointing", "preview_enabled", "preview_prompt", "preview_seed",},"lora": {"preview_enabled", "preview_every", "preview_prompt", "preview_seed"},}[self.trainer]unknown = set(options) - allowedif unknown:raise CommandValidationError(f"Unsupported {self.trainer} training options: {', '.join(sorted(unknown))}")integer_ranges = {"resolution": (64, 512), "batch_size": (1, 64),"gradient_accumulation_steps": (1, 64), "gradient_accumulation": (1, 64),12345678910119 unmodified lines212223242526272829303132333411 unmodified lines464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108from __future__ import annotationsfrom dataclasses import dataclassfrom pathlib import Pathfrom typing import Anyfrom adam.model_plugins import ModelPluginRegistry, validate_settingsclass CommandValidationError(ValueError):pass9 unmodified linesoutput: str = "default output"resume_from: str = ""base_model: str = ""trigger_word: str = ""training_options: dict[str, Any] | None = None@classmethoddef from_dict(cls, payload: dict[str, Any]) -> "TrainingCommand":allowed = {"action", "trainer", "dataset", "model_name", "epochs", "output","resume_from", "base_model", "trigger_word","training_options",}unknown = set(payload) - allowed11 unmodified linesoutput=str(payload.get("output", "default output")).strip(),resume_from=str(payload.get("resume_from", "")).strip(),base_model=str(payload.get("base_model", "")).strip(),trigger_word=str(payload.get("trigger_word")or (payload.get("training_options") or {}).get("trigger_word")or "").strip(),training_options=dict(payload.get("training_options") or {}),)except (TypeError, ValueError) as exc:raise CommandValidationError("Training command fields have invalid types.") from excif command.action not in {"train", "resume_training"}:raise CommandValidationError("Training action must be train or resume_training.")plugin_schema = ModelPluginRegistry(Path.cwd()).training_schema(command.trainer)if command.trainer not in {"ddpm", "lora", "flow"} and not plugin_schema:raise CommandValidationError("Trainer must be a discovered model plugin.")if not command.dataset or not command.model_name:raise CommandValidationError("Dataset and model name are required.")if not 1 <= command.epochs <= 100_000:raise CommandValidationError("Epoch count must be between 1 and 100000.")if command.action == "resume_training" and not command.resume_from:raise CommandValidationError("Resume training requires an explicit checkpoint.")if command.trainer == "lora":trigger = command.trigger_word or command.model_nameif len(trigger) > 128 or any(char in trigger for char in '<>:"/\\|?*\x00'):raise CommandValidationError("LoRA trigger word must be short text without reserved characters.")command._validate_options()return commanddef _validate_options(self) -> None:options = self.training_options or {}schema = ModelPluginRegistry(Path.cwd()).training_schema(self.trainer)allowed = set(schema)if not allowed:allowed = {"ddpm": {"resolution", "batch_size", "learning_rate", "gradient_accumulation_steps","dataloader_num_workers", "mixed_precision", "save_every", "preview_steps","training_intensity", "preview_enabled", "preview_every", "preview_prompt","preview_seed",},"flow": {"resolution", "batch_size", "learning_rate", "gradient_accumulation","workers", "mixed_precision", "save_every", "preview_every", "preview_steps","gradient_checkpointing", "preview_enabled", "preview_prompt", "preview_seed",},"lora": {"preview_enabled", "preview_every", "preview_prompt", "preview_seed", "trigger_word"},}[self.trainer]unknown = set(options) - allowedif unknown:raise CommandValidationError(f"Unsupported {self.trainer} training options: {', '.join(sorted(unknown))}")if schema:errors = validate_settings({key: spec for key, spec in schema.items() if key in options},options,)if errors:raise CommandValidationError(" ".join(errors))returninteger_ranges = {"resolution": (64, 512), "batch_size": (1, 64),"gradient_accumulation_steps": (1, 64), "gradient_accumulation": (1, 64),
25 unmodified lines26272829303132333435363725 unmodified lines"demo_step_delay": 0.24,"max_dataset_images_without_confirmation": 100,"training_presets": {},"tool_folders": {"dataset_collector": "","caption_generator": "","lora_trainer": "","ddpm_trainer": "","flow_trainer": "","preview_generator": "",},}25 unmodified lines26272829303132333435363738394041424344454625 unmodified lines"demo_step_delay": 0.24,"max_dataset_images_without_confirmation": 100,"training_presets": {},"remote_access": {"enabled": False,"bind_address": "127.0.0.1","port": 8765,"token": "","allow_job_control": False,"auto_approve_training": False,},"tool_folders": {"dataset_collector": "","caption_generator": "","lora_trainer": "","ddpm_trainer": "","flow_trainer": "","oasis_trainer": "","preview_generator": "",},}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127from __future__ import annotations
import hashlibfrom dataclasses import asdict, dataclass, fieldfrom pathlib import Path
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp"}VIDEO_EXTENSIONS = {".mp4", ".mov", ".mkv", ".webm", ".avi"}TEXT_EXTENSIONS = {".txt", ".caption", ".jsonl", ".json"}
@dataclass(slots=True)class DatasetItem: path: str kind: str size_bytes: int width: int = 0 height: int = 0 caption_path: str = "" duplicate_key: str = ""
@dataclass(slots=True)class DatasetReport: path: str total_files: int = 0 image_count: int = 0 video_count: int = 0 text_count: int = 0 caption_count: int = 0 missing_caption_count: int = 0 duplicate_groups: int = 0 dimensions: dict[str, int] = field(default_factory=dict) extensions: dict[str, int] = field(default_factory=dict) items: list[DatasetItem] = field(default_factory=list) warnings: list[str] = field(default_factory=list)
def to_dict(self) -> dict: return asdict(self)
def _hash_file(path: Path) -> str: digest = hashlib.sha1() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest()
def scan_dataset(path: str | Path, *, limit: int = 500) -> DatasetReport: root = Path(path).expanduser().resolve() report = DatasetReport(path=str(root)) if not root.is_dir(): report.warnings.append("Dataset folder does not exist.") return report hashes: dict[str, int] = {} try: files = [item for item in root.rglob("*") if item.is_file()] except OSError as exc: report.warnings.append(f"Dataset could not be scanned: {exc}") return report report.total_files = len(files) for item in files: suffix = item.suffix.casefold() report.extensions[suffix or "(none)"] = report.extensions.get(suffix or "(none)", 0) + 1 if suffix in IMAGE_EXTENSIONS: report.image_count += 1 if not any(item.with_suffix(ext).is_file() for ext in (".txt", ".caption")): report.missing_caption_count += 1 elif suffix in VIDEO_EXTENSIONS: report.video_count += 1 if suffix in TEXT_EXTENSIONS: report.text_count += 1 if suffix in {".txt", ".caption"}: report.caption_count += 1 for item in files[: max(1, limit)]: suffix = item.suffix.casefold() kind = "other" width = height = 0 caption_path = "" duplicate_key = "" if suffix in IMAGE_EXTENSIONS: kind = "image" caption = next((item.with_suffix(ext) for ext in (".txt", ".caption") if item.with_suffix(ext).is_file()), None) caption_path = str(caption) if caption else "" try: from PIL import Image
with Image.open(item) as image: width, height = image.size label = f"{width}x{height}" report.dimensions[label] = report.dimensions.get(label, 0) + 1 except Exception: pass try: duplicate_key = _hash_file(item) hashes[duplicate_key] = hashes.get(duplicate_key, 0) + 1 except OSError: duplicate_key = "" elif suffix in VIDEO_EXTENSIONS: kind = "video" elif suffix in TEXT_EXTENSIONS: kind = "text" try: size = item.stat().st_size except OSError: size = 0 report.items.append( DatasetItem( path=str(item), kind=kind, size_bytes=size, width=width, height=height, caption_path=caption_path, duplicate_key=duplicate_key, ) ) if report.total_files > limit: report.warnings.append(f"Showing first {limit:,} files; totals still include all files.") report.duplicate_groups = sum(1 for count in hashes.values() if count > 1) if report.image_count and report.missing_caption_count: report.warnings.append(f"{report.missing_caption_count:,} sampled image(s) do not have sidecar captions.") if report.duplicate_groups: report.warnings.append(f"{report.duplicate_groups:,} duplicate image group(s) found in the sample.") return report123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484from __future__ import annotations
import hashlibimport jsonimport threadingfrom dataclasses import asdict, dataclass, fieldfrom datetime import datetime, timedelta, timezonefrom pathlib import Pathfrom typing import Any, TYPE_CHECKINGfrom uuid import uuid4
from adam.dataset_lab import IMAGE_EXTENSIONS, TEXT_EXTENSIONS, VIDEO_EXTENSIONS
if TYPE_CHECKING: from adam.assets import Asset, AssetRegistry
DATASET_MARKERS = { "dataset_manifest.json", "metadata", "frames", "videos", "captions", "actions.jsonl", "actions.csv",}SCAN_LIMIT = 20_000ASYNC_REFRESH_AFTER = timedelta(minutes=30)_scan_lock = threading.Lock()_active_scans: set[str] = set()
def _now() -> str: return datetime.now(timezone.utc).isoformat()
def _path_key(path: str | Path) -> str: resolved = str(Path(path).expanduser().resolve()) return hashlib.sha1(resolved.casefold().encode("utf-8")).hexdigest()[:16]
def _atomic_json(path: Path, payload: dict[str, Any]) -> None: path.parent.mkdir(parents=True, exist_ok=True) temporary = path.with_suffix(path.suffix + ".tmp") temporary.write_text(json.dumps(payload, indent=2), encoding="utf-8") temporary.replace(path)
def _date(value: str) -> datetime | None: try: return datetime.fromisoformat(value) except (TypeError, ValueError): return None
@dataclass(slots=True)class DatasetLocation: id: str name: str path: str source: str = "user" created_at: str = field(default_factory=_now) last_seen_at: str = "" exists: bool = True
@classmethod def from_dict(cls, payload: dict[str, Any]) -> "DatasetLocation": return cls( id=str(payload.get("id") or _path_key(str(payload.get("path", "")))), name=str(payload.get("name") or Path(str(payload.get("path", ""))).name or "Datasets"), path=str(payload.get("path", "")), source=str(payload.get("source") or "user"), created_at=str(payload.get("created_at") or _now()), last_seen_at=str(payload.get("last_seen_at") or ""), exists=bool(payload.get("exists", True)), )
@dataclass(slots=True)class DatasetRecord: id: str name: str path: str source: str = "asset" location_id: str = "" favorite: bool = False last_used_at: str = "" discovered_at: str = field(default_factory=_now) scanned_at: str = "" exists: bool = True item_count: int = 0 image_count: int = 0 video_count: int = 0 caption_count: int = 0 missing_caption_count: int = 0 sample_image: str = "" dataset_format: str = "Unknown" warnings: list[str] = field(default_factory=list)
@classmethod def from_dict(cls, payload: dict[str, Any]) -> "DatasetRecord": return cls( id=str(payload.get("id") or _path_key(str(payload.get("path", "")))), name=str(payload.get("name") or Path(str(payload.get("path", ""))).name or "Dataset"), path=str(payload.get("path", "")), source=str(payload.get("source") or "asset"), location_id=str(payload.get("location_id") or ""), favorite=bool(payload.get("favorite", False)), last_used_at=str(payload.get("last_used_at") or ""), discovered_at=str(payload.get("discovered_at") or _now()), scanned_at=str(payload.get("scanned_at") or ""), exists=bool(payload.get("exists", True)), item_count=int(payload.get("item_count", 0) or 0), image_count=int(payload.get("image_count", 0) or 0), video_count=int(payload.get("video_count", 0) or 0), caption_count=int(payload.get("caption_count", 0) or 0), missing_caption_count=int(payload.get("missing_caption_count", 0) or 0), sample_image=str(payload.get("sample_image") or ""), dataset_format=str(payload.get("dataset_format") or "Unknown"), warnings=[str(item) for item in payload.get("warnings", []) if str(item)], )
class DatasetRegistry: """Persistent ADAM-aware index of known dataset locations and datasets."""
def __init__(self, root: Path, config: Any | None = None) -> None: self.root = root.resolve() self.path = self.root / "data" / "dataset_registry.json" self.config = config self.locations: list[DatasetLocation] = [] self.datasets: dict[str, DatasetRecord] = {} self.load()
def load(self) -> None: try: payload = json.loads(self.path.read_text(encoding="utf-8")) except (OSError, ValueError, TypeError, json.JSONDecodeError): payload = {} self.locations = [ DatasetLocation.from_dict(item) for item in payload.get("locations", []) if isinstance(item, dict) ] self.datasets = { str(key): DatasetRecord.from_dict(value) for key, value in (payload.get("datasets", {}) or {}).items() if isinstance(value, dict) }
def save(self) -> None: _atomic_json( self.path, { "locations": [asdict(item) for item in self.locations], "datasets": {key: asdict(value) for key, value in self.datasets.items()}, }, )
def register_location(self, path: str | Path, *, name: str = "", source: str = "user") -> DatasetLocation: resolved = Path(path).expanduser().resolve() if not resolved.is_dir(): raise ValueError("Choose an existing dataset folder.") key = _path_key(resolved) existing = next((item for item in self.locations if item.id == key), None) if existing is None: existing = DatasetLocation( id=key, name=name.strip() or resolved.name or str(resolved), path=str(resolved), source=source, ) self.locations.insert(0, existing) else: existing.name = name.strip() or existing.name existing.path = str(resolved) existing.source = source or existing.source existing.exists = True existing.last_seen_at = _now() self.save() return existing
def remove_location(self, location_id: str) -> bool: before = len(self.locations) self.locations = [item for item in self.locations if item.id != location_id] changed = len(self.locations) != before if changed: self.save() return changed
def known_locations(self) -> list[DatasetLocation]: locations = list(self.locations) by_path = {Path(item.path).expanduser().resolve(): item for item in locations if item.path} for path, name, source in self._automatic_location_candidates(): try: resolved = path.expanduser().resolve() except OSError: continue if resolved in by_path: continue locations.append( DatasetLocation( id=_path_key(resolved), name=name or resolved.name or str(resolved), path=str(resolved), source=source, exists=resolved.is_dir(), last_seen_at=_now() if resolved.is_dir() else "", ) ) return locations
def discover_into_assets(self, assets: "AssetRegistry", *, persist: bool = False) -> list["Asset"]: discovered: list[Asset] = [] records = self.discover(asset_registry=assets, refresh_missing=False) for record in records: if not record.exists: continue asset = assets.register( kind="dataset", name=record.name, path=record.path, metadata={ "dataset_registry_source": record.source, "dataset_location_id": record.location_id, }, persist=False, ) discovered.append(asset) if persist and discovered: assets.save() return discovered
def discover( self, *, asset_registry: "AssetRegistry | None" = None, refresh_missing: bool = True, ) -> list[DatasetRecord]: self.load() changed = False locations = self.known_locations() known_location_ids = {location.id for location in locations} for key, record in list(self.datasets.items()): if record.source in {"adam", "tool"} and record.location_id and record.location_id not in known_location_ids: del self.datasets[key] changed = True for location in locations: exists = Path(location.path).is_dir() if location.source == "user": stored = next((item for item in self.locations if item.id == location.id), None) if stored: stored.exists = exists stored.last_seen_at = _now() if exists else stored.last_seen_at changed = True if not exists: continue for candidate in self._dataset_candidates(Path(location.path)): record = self._cached_or_sampled(candidate, source=location.source, location_id=location.id) self.datasets[record.id] = record changed = True if asset_registry is not None: for asset in getattr(asset_registry, "assets", []): if getattr(asset, "kind", "") != "dataset": continue record = self._cached_or_sampled(Path(asset.path), source="asset", location_id="") record.name = asset.name or record.name self.datasets[record.id] = record changed = True for record in self.datasets.values(): record.exists = Path(record.path).is_dir() if refresh_missing and record.exists and self._needs_refresh(record): self.refresh_async(record.path, source=record.source, location_id=record.location_id) if changed: self.save() return self.sorted_records()
def sorted_records(self) -> list[DatasetRecord]: records = list(self.datasets.values()) records.sort( key=lambda item: ( not item.favorite, not bool(item.last_used_at), item.last_used_at or item.discovered_at, item.name.casefold(), ), reverse=False, ) favorites = sorted([item for item in records if item.favorite], key=lambda item: item.name.casefold()) recent = sorted( [item for item in records if not item.favorite and item.last_used_at], key=lambda item: item.last_used_at, reverse=True, ) others = sorted( [item for item in records if not item.favorite and not item.last_used_at], key=lambda item: item.discovered_at, reverse=True, ) return [*favorites, *recent, *others]
def record_for_path(self, path: str | Path) -> DatasetRecord: key = _path_key(path) record = self.datasets.get(key) if record is None: record = self._cached_or_sampled(Path(path), source="asset", location_id="") self.datasets[key] = record self.save() return record
def favorite(self, path: str | Path, enabled: bool) -> DatasetRecord: record = self.record_for_path(path) record.favorite = bool(enabled) self.save() return record
def touch(self, path: str | Path) -> DatasetRecord: record = self.record_for_path(path) record.last_used_at = _now() self.save() return record
def refresh_async(self, path: str | Path, *, source: str = "asset", location_id: str = "") -> None: resolved = str(Path(path).expanduser().resolve()) key = _path_key(resolved) with _scan_lock: if key in _active_scans: return _active_scans.add(key)
def worker() -> None: try: record = self._scan(Path(resolved), source=source, location_id=location_id, limit=SCAN_LIMIT) fresh = DatasetRegistry(self.root, self.config) current = fresh.datasets.get(record.id) if current: record.favorite = current.favorite record.last_used_at = current.last_used_at record.discovered_at = current.discovered_at fresh.datasets[record.id] = record fresh.save() finally: with _scan_lock: _active_scans.discard(key)
threading.Thread(target=worker, name="ADAMDatasetScan", daemon=True).start()
def _automatic_location_candidates(self) -> list[tuple[Path, str, str]]: candidates: list[tuple[Path, str, str]] = [(self.root / "ADAM_Datasets", "ADAM Datasets", "adam")] folders = self.config.get("tool_folders", {}) if self.config is not None else {} folders = folders if isinstance(folders, dict) else {} collector_raw = str(folders.get("dataset_collector", "")).strip() if collector_raw: candidates.append((Path(collector_raw) / "Datasets", "Dataset Collector", "tool")) for tool_id in ("ddpm_trainer", "lora_trainer", "flow_trainer", "oasis_trainer"): raw_root = str(folders.get(tool_id, "")).strip() if not raw_root: continue root = Path(raw_root) label = tool_id.removesuffix("_trainer").upper() for child in ("Datasets", "datasets", "OldDatasets"): candidates.append((root / child, f"{label} {child}", "tool")) external_tools = self.root / "config" / "external_tools.json" try: payload = json.loads(external_tools.read_text(encoding="utf-8")) except (OSError, ValueError, TypeError, json.JSONDecodeError): payload = {} for entry in payload.get("tools", []) if isinstance(payload, dict) else []: if not isinstance(entry, dict): continue backend = entry.get("backend", {}) if not isinstance(backend, dict): continue raw_root = str(backend.get("root", "")).strip() if raw_root: root = Path(raw_root) candidates.append((root / "Datasets", f"{entry.get('name', 'External tool')} Datasets", "tool")) candidates.append((root / "OldDatasets", f"{entry.get('name', 'External tool')} OldDatasets", "tool")) return candidates
def _dataset_candidates(self, location: Path) -> list[Path]: candidates: list[Path] = [] try: children = [item for item in location.iterdir() if item.is_dir()] except OSError: children = [] for child in children[:1000]: if self._looks_like_dataset(child): candidates.append(child) if candidates: return candidates if self._looks_like_dataset(location): candidates.append(location) return candidates
def _looks_like_dataset(self, folder: Path) -> bool: if not folder.is_dir(): return False try: names = {item.name for item in folder.iterdir()} except OSError: return False if names & DATASET_MARKERS: return True checked = 0 for item in folder.rglob("*"): if checked >= 200: break checked += 1 if item.is_file() and item.suffix.casefold() in IMAGE_EXTENSIONS | VIDEO_EXTENSIONS: return True return False
def _cached_or_sampled(self, path: Path, *, source: str, location_id: str) -> DatasetRecord: key = _path_key(path) current = self.datasets.get(key) if current is not None: current.exists = path.is_dir() current.source = current.source or source current.location_id = current.location_id or location_id return current return self._scan(path, source=source, location_id=location_id, limit=800)
def _needs_refresh(self, record: DatasetRecord) -> bool: if not record.scanned_at: return True scanned = _date(record.scanned_at) return scanned is None or datetime.now(timezone.utc) - scanned > ASYNC_REFRESH_AFTER
def _scan(self, path: Path, *, source: str, location_id: str, limit: int) -> DatasetRecord: resolved = path.expanduser().resolve() record = DatasetRecord( id=_path_key(resolved), name=resolved.name or "Dataset", path=str(resolved), source=source, location_id=location_id, exists=resolved.is_dir(), scanned_at=_now(), ) if not resolved.is_dir(): record.warnings.append("Dataset folder is unavailable.") return record files_seen = 0 capped = False try: iterator = resolved.rglob("*") for item in iterator: if not item.is_file(): continue files_seen += 1 suffix = item.suffix.casefold() if suffix in IMAGE_EXTENSIONS: record.image_count += 1 if not record.sample_image: record.sample_image = str(item) if not any(item.with_suffix(ext).is_file() for ext in (".txt", ".caption")): record.missing_caption_count += 1 elif suffix in VIDEO_EXTENSIONS: record.video_count += 1 if suffix in TEXT_EXTENSIONS and suffix in {".txt", ".caption"}: record.caption_count += 1 if files_seen >= limit: capped = True break except OSError as exc: record.warnings.append(f"Dataset could not be scanned: {exc}") record.item_count = record.image_count + record.video_count record.dataset_format = self._format_label(resolved, record) if capped: record.warnings.append(f"Counts are sampled from the first {limit:,} files.") return record
@staticmethod def _format_label(folder: Path, record: DatasetRecord) -> str: if (folder / "actions.jsonl").is_file() or (folder / "actions.csv").is_file(): return "Oasis action dataset" if record.video_count: return "Video dataset" if record.image_count and record.caption_count: return "Captioned image dataset" if record.image_count: return "Image dataset" return "Dataset folder"86 unmodified lines878889909192939411 unmodified lines10610710810911011111211311411586 unmodified linesclass EveVisionModel:"""Lazy local DINOv2 feature extractor used by EVE."""def __init__(self, model_id: str = EVE_MODEL_ID) -> None:self.model_id = model_idself._processor = Noneself._model = Noneself._device = "cpu"11 unmodified linesraise RuntimeError("EVE needs PyTorch and Transformers. Launch ADAM with its normal Python environment.") from excself._device = "cuda" if torch.cuda.is_available() else "cpu"self._processor = AutoImageProcessor.from_pretrained(self.model_id, use_fast=True)self._model = AutoModel.from_pretrained(self.model_id).to(self._device).eval()def embed(self, paths: Sequence[str | Path], progress: Callable[[int, int], None] | None = None) -> list[list[float]]:self.load()import torch86 unmodified lines87888990919293949511 unmodified lines10710810911011111211311411511611711811912012112212312412512686 unmodified linesclass EveVisionModel:"""Lazy local DINOv2 feature extractor used by EVE."""def __init__(self, model_id: str = EVE_MODEL_ID, *, prefer_gpu: bool = True) -> None:self.model_id = model_idself.prefer_gpu = prefer_gpuself._processor = Noneself._model = Noneself._device = "cpu"11 unmodified linesraise RuntimeError("EVE needs PyTorch and Transformers. Launch ADAM with its normal Python environment.") from excself._device = "cuda" if self.prefer_gpu and torch.cuda.is_available() else "cpu"self._processor = AutoImageProcessor.from_pretrained(self.model_id, use_fast=True)self._model = AutoModel.from_pretrained(self.model_id).to(self._device).eval()def unload(self) -> None:self._processor = Noneself._model = Nonetry:import torchif torch.cuda.is_available():torch.cuda.empty_cache()except ImportError:passdef embed(self, paths: Sequence[str | Path], progress: Callable[[int, int], None] | None = None) -> list[list[float]]:self.load()import torch
11 unmodified lines1213141516175 unmodified lines232425262728298 unmodified lines383940414243444546474849505152 unmodified lines1041051061071081096 unmodified lines116117118119120121121 unmodified lines24324424524624724824925025125225325425525625725825926026126226326426526626726826927027127211 unmodified linesfrom pathlib import Pathfrom typing import Any, Callablefrom adam.registry import ToolRegistry, ToolSpec5 unmodified linespassProgressCallback = Callable[[int, str], None]LogCallback = Callable[[str], None]PreviewCallback = Callable[[dict[str, Any]], None]8 unmodified linesprogress_callback: ProgressCallbacklog_callback: LogCallbackpreview_callback: PreviewCallback = lambda _preview: Nonestep_delay: float = 0.2def log(self, message: str) -> None:self.log_callback(message)def progress(self, percent: int, message: str) -> None:self.checkpoint()self.progress_callback(max(0, min(int(percent), 100)), message)def preview(self, path: str | Path, *, epoch: int = 0, next_epoch: int = 0,52 unmodified linesprogress_callback: ProgressCallback,log_callback: LogCallback,preview_callback: PreviewCallback | None = None,) -> dict[str, Any]:spec = self.registry.get(tool_id)self._validate_arguments(spec, arguments)6 unmodified linesprogress_callback=progress_callback,log_callback=log_callback,preview_callback=preview_callback or (lambda _preview: None),step_delay=self.step_delay,)backend_type = str(spec.backend.get("type", "")).lower()121 unmodified linesdef stop_process_tree() -> None:"""Stop the script and any workers it launched."""if process_controller is not None:try:descendants = process_controller.children(recursive=True)for child in descendants:try:child.terminate()except Exception:passprocess_controller.terminate()try:import psutil_gone, alive = psutil.wait_procs(descendants, timeout=2)for child in alive:try:child.kill()except Exception:passexcept Exception:passreturnexcept Exception:passprocess.terminate()try:while True:11 unmodified lines121314151617185 unmodified lines2425262728293031323334353637388 unmodified lines4748495051525354555657585960616252 unmodified lines1151161171181191201211226 unmodified lines129130131132133134135136121 unmodified lines25825926026126226326411 unmodified linesfrom pathlib import Pathfrom typing import Any, Callablefrom adam.process_control import terminate_process_treefrom adam.registry import ToolRegistry, ToolSpec5 unmodified linespassclass ToolAdjustmentRequested(ToolExecutionError):"""A trainer stopped cleanly so a job can continue with new settings."""def __init__(self, message: str, details: dict[str, Any] | None = None) -> None:super().__init__(message)self.details = details or {}ProgressCallback = Callable[..., None]LogCallback = Callable[[str], None]PreviewCallback = Callable[[dict[str, Any]], None]8 unmodified linesprogress_callback: ProgressCallbacklog_callback: LogCallbackpreview_callback: PreviewCallback = lambda _preview: Noneadjustment_event: threading.Event | None = Noneadjustment_request: dict[str, Any] | None = Nonestep_delay: float = 0.2def log(self, message: str) -> None:self.log_callback(message)def progress(self, percent: int, message: str, **details: Any) -> None:self.checkpoint()self.progress_callback(max(0, min(int(percent), 100)), message, **details)def preview(self, path: str | Path, *, epoch: int = 0, next_epoch: int = 0,52 unmodified linesprogress_callback: ProgressCallback,log_callback: LogCallback,preview_callback: PreviewCallback | None = None,adjustment_event: threading.Event | None = None,adjustment_request: dict[str, Any] | None = None,) -> dict[str, Any]:spec = self.registry.get(tool_id)self._validate_arguments(spec, arguments)6 unmodified linesprogress_callback=progress_callback,log_callback=log_callback,preview_callback=preview_callback or (lambda _preview: None),adjustment_event=adjustment_event,adjustment_request=adjustment_request,step_delay=self.step_delay,)backend_type = str(spec.backend.get("type", "")).lower()121 unmodified linesdef stop_process_tree() -> None:"""Stop the script and any workers it launched."""terminate_process_tree(process, timeout=3)try:while True:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387from __future__ import annotations
import jsonimport reimport sqlite3from dataclasses import dataclassfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import Any
from adam.models import Job, JobStatus, SystemSnapshot
def _utc_now() -> str: return datetime.now(timezone.utc).isoformat()
def _json(value: Any) -> str: return json.dumps(value, sort_keys=True)
def _safe_json(value: str, fallback: Any) -> Any: try: parsed = json.loads(value or "") except (TypeError, ValueError, json.JSONDecodeError): return fallback return parsed if isinstance(parsed, type(fallback)) else fallback
def _safe_int(value: Any, default: int = 0) -> int: try: if isinstance(value, bool): return default return int(value) except (TypeError, ValueError): return default
def _safe_float(value: Any, default: float = 0.0) -> float: try: if isinstance(value, bool): return default return float(value) except (TypeError, ValueError): return default
def _loss_from_logs(logs: list[str]) -> float | None: for line in reversed(logs): match = re.search(r"\bloss(?:\s*[:=]\s*|\s+)(-?\d+(?:\.\d+)?(?:e[+-]?\d+)?)", line, re.I) if match: try: return float(match.group(1)) except ValueError: return None return None
def _duration_seconds(job: Job) -> int: if not job.started_at: return 0 try: start = datetime.fromisoformat(job.started_at) end = datetime.fromisoformat(job.ended_at) if job.ended_at else datetime.now(timezone.utc) return max(0, int((end - start).total_seconds())) except ValueError: return 0
def _image_count(path: str) -> int: folder = Path(path).expanduser() if not folder.is_dir(): return 0 try: return sum( 1 for item in folder.rglob("*") if item.is_file() and item.suffix.casefold() in {".png", ".jpg", ".jpeg", ".webp", ".bmp"} ) except OSError: return 0
@dataclass(slots=True)class ExperimentRun: id: str job_id: str timestamp: str model_architecture: str model_name: str trigger_word: str base_model: str dataset_path: str dataset_name: str dataset_item_count: int epochs: int batch_size: int learning_rate: float optimizer: str scheduler: str resolution: int seed: int status: str training_time_seconds: int final_loss: float | None output_folder: str checkpoint_paths: list[str] preview_images: list[str] peak_vram_gb: float | None hardware: dict[str, Any] settings: dict[str, Any] generation_settings: dict[str, Any] notes: str = "" quality_score: int | None = None
@classmethod def from_row(cls, row: sqlite3.Row) -> "ExperimentRun": payload = dict(row) for key in ("checkpoint_paths", "preview_images"): payload[key] = _safe_json(payload.get(key, "[]"), []) for key in ("hardware", "settings", "generation_settings"): payload[key] = _safe_json(payload.get(key, "{}"), {}) payload["quality_score"] = ( _safe_int(payload["quality_score"]) if payload.get("quality_score") is not None else None ) return cls(**payload)
class ExperimentStore: def __init__(self, root: Path) -> None: self.root = root.resolve() self.path = self.root / "data" / "experiments.sqlite3" self.path.parent.mkdir(parents=True, exist_ok=True) self._init_db()
def connect(self) -> sqlite3.Connection: connection = sqlite3.connect(self.path) connection.row_factory = sqlite3.Row return connection
def _init_db(self) -> None: with self.connect() as db: db.execute( """ CREATE TABLE IF NOT EXISTS experiments ( id TEXT PRIMARY KEY, job_id TEXT UNIQUE NOT NULL, timestamp TEXT NOT NULL, model_architecture TEXT NOT NULL, model_name TEXT NOT NULL, trigger_word TEXT NOT NULL DEFAULT '', base_model TEXT NOT NULL, dataset_path TEXT NOT NULL, dataset_name TEXT NOT NULL, dataset_item_count INTEGER NOT NULL, epochs INTEGER NOT NULL, batch_size INTEGER NOT NULL, learning_rate REAL NOT NULL, optimizer TEXT NOT NULL, scheduler TEXT NOT NULL, resolution INTEGER NOT NULL, seed INTEGER NOT NULL, status TEXT NOT NULL, training_time_seconds INTEGER NOT NULL, final_loss REAL, output_folder TEXT NOT NULL, checkpoint_paths TEXT NOT NULL, preview_images TEXT NOT NULL, peak_vram_gb REAL, hardware TEXT NOT NULL, settings TEXT NOT NULL, generation_settings TEXT NOT NULL, notes TEXT NOT NULL DEFAULT '', quality_score INTEGER ) """ ) self._migrate_columns(db)
@staticmethod def _migrate_columns(db: sqlite3.Connection) -> None: existing = {row["name"] for row in db.execute("PRAGMA table_info(experiments)").fetchall()} columns = { "id": "TEXT PRIMARY KEY", "job_id": "TEXT NOT NULL DEFAULT ''", "timestamp": "TEXT NOT NULL DEFAULT ''", "model_architecture": "TEXT NOT NULL DEFAULT ''", "model_name": "TEXT NOT NULL DEFAULT ''", "trigger_word": "TEXT NOT NULL DEFAULT ''", "base_model": "TEXT NOT NULL DEFAULT ''", "dataset_path": "TEXT NOT NULL DEFAULT ''", "dataset_name": "TEXT NOT NULL DEFAULT ''", "dataset_item_count": "INTEGER NOT NULL DEFAULT 0", "epochs": "INTEGER NOT NULL DEFAULT 0", "batch_size": "INTEGER NOT NULL DEFAULT 0", "learning_rate": "REAL NOT NULL DEFAULT 0", "optimizer": "TEXT NOT NULL DEFAULT ''", "scheduler": "TEXT NOT NULL DEFAULT ''", "resolution": "INTEGER NOT NULL DEFAULT 0", "seed": "INTEGER NOT NULL DEFAULT 0", "status": "TEXT NOT NULL DEFAULT ''", "training_time_seconds": "INTEGER NOT NULL DEFAULT 0", "final_loss": "REAL", "output_folder": "TEXT NOT NULL DEFAULT ''", "checkpoint_paths": "TEXT NOT NULL DEFAULT '[]'", "preview_images": "TEXT NOT NULL DEFAULT '[]'", "peak_vram_gb": "REAL", "hardware": "TEXT NOT NULL DEFAULT '{}'", "settings": "TEXT NOT NULL DEFAULT '{}'", "generation_settings": "TEXT NOT NULL DEFAULT '{}'", "notes": "TEXT NOT NULL DEFAULT ''", "quality_score": "INTEGER", } for name, definition in columns.items(): if name not in existing and name != "id": db.execute(f"ALTER TABLE experiments ADD COLUMN {name} {definition}")
def record_job(self, job: Job, snapshot: SystemSnapshot | None = None) -> ExperimentRun | None: training_steps = [step for step in job.plan.steps if step.tool_id.endswith("_trainer")] if not training_steps: return None step = training_steps[-1] args = dict(step.arguments) architecture = step.tool_id.removesuffix("_trainer") dataset_path = str(args.get("dataset_dir", "")) output_folder = str(job.output_folder or args.get("output_dir", "")) preview_images = [job.preview_path] if job.preview_path else [] checkpoints = [] if output_folder: folder = Path(output_folder) if folder.is_dir(): try: checkpoints = [ str(path) for path in sorted(folder.rglob("*")) if path.is_file() and path.suffix.casefold() in {".safetensors", ".ckpt", ".pt", ".bin"} ][-10:] discovered_previews = [ str(path) for path in sorted(folder.rglob("*")) if path.is_file() and path.suffix.casefold() in {".png", ".jpg", ".jpeg", ".webp", ".bmp"} and any(token in path.name.casefold() for token in ("preview", "sample", "epoch")) ][-12:] preview_images = list(dict.fromkeys([*preview_images, *discovered_previews])) except OSError: checkpoints = [] hardware = {} peak_vram = None if snapshot is not None: hardware = { "gpu_name": snapshot.gpu_name, "gpu_percent": snapshot.gpu_percent, "vram_used_gb": snapshot.vram_used_gb, "vram_total_gb": snapshot.vram_total_gb, "memory_used_gb": snapshot.memory_used_gb, "memory_total_gb": snapshot.memory_total_gb, "cpu_percent": snapshot.cpu_percent, "gpu_temperature": snapshot.gpu_temperature, } peak_vram = snapshot.vram_used_gb or None run = ExperimentRun( id=f"EXP-{job.id}", job_id=job.id, timestamp=job.ended_at or _utc_now(), model_architecture=architecture, model_name=str(args.get("model_name", job.plan.project_name)), trigger_word=str(args.get("trigger_word", "")), base_model=str(args.get("base_model", args.get("base_model_path", ""))), dataset_path=dataset_path, dataset_name=Path(dataset_path).name if dataset_path else "", dataset_item_count=_image_count(dataset_path), epochs=_safe_int(args.get("epochs")), batch_size=_safe_int(args.get("batch_size")), learning_rate=_safe_float(args.get("learning_rate")), optimizer=str(args.get("optimizer", "")), scheduler=str(args.get("scheduler", args.get("sampler", ""))), resolution=_safe_int(args.get("resolution")), seed=_safe_int(args.get("seed", args.get("preview_seed", 0))), status=job.status.value, training_time_seconds=_duration_seconds(job), final_loss=_loss_from_logs(job.logs), output_folder=output_folder, checkpoint_paths=checkpoints, preview_images=preview_images, peak_vram_gb=peak_vram, hardware=hardware, settings=args, generation_settings={}, ) with self.connect() as db: db.execute( """ INSERT INTO experiments ( id, job_id, timestamp, model_architecture, model_name, trigger_word, base_model, dataset_path, dataset_name, dataset_item_count, epochs, batch_size, learning_rate, optimizer, scheduler, resolution, seed, status, training_time_seconds, final_loss, output_folder, checkpoint_paths, preview_images, peak_vram_gb, hardware, settings, generation_settings ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(job_id) DO UPDATE SET timestamp=excluded.timestamp, trigger_word=excluded.trigger_word, status=excluded.status, training_time_seconds=excluded.training_time_seconds, final_loss=excluded.final_loss, output_folder=excluded.output_folder, checkpoint_paths=excluded.checkpoint_paths, preview_images=excluded.preview_images, peak_vram_gb=excluded.peak_vram_gb, hardware=excluded.hardware, settings=excluded.settings """, ( run.id, run.job_id, run.timestamp, run.model_architecture, run.model_name, run.trigger_word, run.base_model, run.dataset_path, run.dataset_name, run.dataset_item_count, run.epochs, run.batch_size, run.learning_rate, run.optimizer, run.scheduler, run.resolution, run.seed, run.status, run.training_time_seconds, run.final_loss, run.output_folder, _json(run.checkpoint_paths), _json(run.preview_images), run.peak_vram_gb, _json(run.hardware), _json(run.settings), _json(run.generation_settings), ), ) return run
def list_runs(self, search: str = "", architecture: str = "", dataset: str = "", limit: int = 200) -> list[ExperimentRun]: clauses = [] params: list[Any] = [] if search: clauses.append("(id LIKE ? OR job_id LIKE ? OR model_name LIKE ? OR dataset_name LIKE ? OR dataset_path LIKE ? OR output_folder LIKE ? OR notes LIKE ? OR status LIKE ?)") term = f"%{search}%" params.extend([term, term, term, term, term, term, term, term]) if architecture: clauses.append("model_architecture = ?") params.append(architecture) if dataset: clauses.append("dataset_name LIKE ?") params.append(f"%{dataset}%") where = " WHERE " + " AND ".join(clauses) if clauses else "" with self.connect() as db: rows = db.execute( "SELECT * FROM experiments" + where + " ORDER BY timestamp DESC LIMIT ?", [*params, int(limit)], ).fetchall() return [ExperimentRun.from_row(row) for row in rows]
def get(self, run_id: str) -> ExperimentRun | None: with self.connect() as db: row = db.execute("SELECT * FROM experiments WHERE id = ?", (run_id,)).fetchone() return ExperimentRun.from_row(row) if row else None
def update_notes(self, run_id: str, notes: str, quality_score: int | None) -> None: with self.connect() as db: db.execute( "UPDATE experiments SET notes = ?, quality_score = ? WHERE id = ?", (notes, quality_score, run_id), )
def compare(self, run_ids: list[str]) -> list[dict[str, Any]]: runs = [run for run_id in run_ids if (run := self.get(run_id)) is not None] fields = [ "model_architecture", "epochs", "final_loss", "training_time_seconds", "resolution", "batch_size", "learning_rate", "scheduler", "peak_vram_gb", "dataset_name", "quality_score", ] rows = [] for field in fields: values = {run.id: getattr(run, field) for run in runs} comparable = {str(value) for value in values.values()} rows.append({"field": field, "changed": len(comparable) > 1, **values}) return rows
def clone_request(self, run_id: str) -> str: run = self.get(run_id) if run is None: return "" options = { key: value for key, value in run.settings.items() if key not in {"dataset_dir", "model_name", "epochs", "output_dir", "resume_from"} } return ( f"From the {run.dataset_name or run.dataset_path} dataset, train a " f"{run.model_architecture.upper()} model for {run.epochs} epochs. " f"Name the model {run.model_name} Clone. " "[ADAM_TRAINING_OPTIONS:" + json.dumps(options, sort_keys=True) + "] " "[ADAM_TRAINER:" + run.model_architecture + "]" )218 unmodified lines2192202212222232246 unmodified lines2312322332342352369 unmodified lines2462472482492502516 unmodified lines258259260261262263264265266218 unmodified lines@dataclass(frozen=True, slots=True)class GenerationRecord:folder: Pathimages: tuple[Path, ...]provider_id: str6 unmodified linessampler: straspect_ratio: strcreated_at: str@classmethoddef from_metadata(cls, metadata_path: Path) -> "GenerationRecord | None":9 unmodified linesif not images:return Nonereturn cls(folder=folder,images=images,provider_id=str(payload.get("provider_id", "")),6 unmodified linessampler=str(payload.get("sampler", "")),aspect_ratio=str(payload.get("aspect_ratio", "")),created_at=str(payload.get("created_at", "")),)def generation_tools(registry: ToolRegistry) -> list[ToolSpec]:return [tool218 unmodified lines2192202212222232242256 unmodified lines2322332342352362372382399 unmodified lines2492502512522532542556 unmodified lines262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338218 unmodified lines@dataclass(frozen=True, slots=True)class GenerationRecord:metadata_path: Pathfolder: Pathimages: tuple[Path, ...]provider_id: str6 unmodified linessampler: straspect_ratio: strcreated_at: strsmart_generation: dict[str, Any]image_evaluations: dict[str, dict[str, Any]]@classmethoddef from_metadata(cls, metadata_path: Path) -> "GenerationRecord | None":9 unmodified linesif not images:return Nonereturn cls(metadata_path=metadata_path,folder=folder,images=images,provider_id=str(payload.get("provider_id", "")),6 unmodified linessampler=str(payload.get("sampler", "")),aspect_ratio=str(payload.get("aspect_ratio", "")),created_at=str(payload.get("created_at", "")),smart_generation=dict(payload.get("smart_generation") or {}),image_evaluations={str(Path(path).resolve()): dict(value)for path, value in dict(payload.get("image_evaluations") or {}).items()if isinstance(value, dict)},)@dataclass(frozen=True, slots=True)class GenerationModelFolder:"""A model-centered view over existing generation batches."""key: strmodel_name: strmodel_path: strprovider_id: strprovider_name: strrecords: tuple[GenerationRecord, ...]image_count: intcover_image: Path | Nonelatest_at: strdef generation_model_key(record: GenerationRecord) -> str:"""Keep renamed or duplicated display names separated by model identity."""raw_path = str(record.model_path or "").strip()if raw_path:try:return f"path:{Path(raw_path).expanduser().resolve()}".casefold()except OSError:return f"path:{raw_path}".casefold()return f"name:{record.provider_id}:{record.model_name}".casefold()def group_generation_records(records: list[GenerationRecord],) -> list[GenerationModelFolder]:"""Build newest-first automatic model folders without changing files."""grouped: dict[str, list[GenerationRecord]] = {}for record in records:grouped.setdefault(generation_model_key(record), []).append(record)folders: list[GenerationModelFolder] = []for key, model_records in grouped.items():newest_first = sorted(model_records,key=lambda item: item.created_at or item.folder.name,reverse=True,)latest = newest_first[0]cover = next((path for record in newest_first for path in record.images if path.is_file()),None,)folders.append(GenerationModelFolder(key=key,model_name=latest.model_name,model_path=latest.model_path,provider_id=latest.provider_id,provider_name=latest.provider_name,records=tuple(newest_first),image_count=sum(len(record.images) for record in newest_first),cover_image=cover,latest_at=latest.created_at,))folders.sort(key=lambda item: (item.latest_at, item.model_name.casefold()), reverse=True)return foldersdef generation_tools(registry: ToolRegistry) -> list[ToolSpec]:return [tool
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313from __future__ import annotations
from dataclasses import asdict, dataclass, fieldfrom datetime import datetime, timezoneimport hashlibimport jsonfrom pathlib import Pathfrom typing import Any, Callable, Sequence
from adam.eve import EveVisionModel, classify_eve_embeddings
RATINGS = {"favorite", "keep", "unsure", "reject"}POSITIVE_RATINGS = {"favorite", "keep"}NEGATIVE_RATINGS = {"reject"}
def _now() -> str: return datetime.now(timezone.utc).isoformat()
def preference_profile_id(provider_id: str, model_path: str) -> str: key = f"{provider_id}\n{str(Path(model_path).expanduser().resolve())}" return hashlib.sha1(key.encode("utf-8")).hexdigest()[:16]
def image_cache_id(path: str | Path) -> str: resolved = str(Path(path).expanduser().resolve()) return hashlib.sha1(resolved.encode("utf-8")).hexdigest()
@dataclass(slots=True)class GenerationRating: image_path: str rating: str provider_id: str model_name: str model_path: str seed: int = 0 sampler: str = "" steps: int = 0 resolution: str = "" generation_settings: dict[str, Any] = field(default_factory=dict) generation_created_at: str = "" rated_at: str = field(default_factory=_now) embedding: list[float] | None = None
@classmethod def from_dict(cls, payload: dict[str, Any]) -> "GenerationRating": rating = str(payload.get("rating", "unsure")).casefold() return cls( image_path=str(Path(str(payload.get("image_path", ""))).expanduser().resolve()), rating=rating if rating in RATINGS else "unsure", provider_id=str(payload.get("provider_id", "")), model_name=str(payload.get("model_name", "")), model_path=str(payload.get("model_path", "")), seed=int(payload.get("seed", 0) or 0), sampler=str(payload.get("sampler", "")), steps=int(payload.get("steps", 0) or 0), resolution=str(payload.get("resolution", "")), generation_settings=dict(payload.get("generation_settings") or {}), generation_created_at=str(payload.get("generation_created_at", "")), rated_at=str(payload.get("rated_at") or _now()), embedding=[float(value) for value in payload["embedding"]] if isinstance(payload.get("embedding"), list) else None, )
@dataclass(frozen=True, slots=True)class PreferenceScore: image_path: str score: float | None confidence: float category: str reason: str = ""
class PreferenceProfile: def __init__(self, root: Path, provider_id: str, model_name: str, model_path: str) -> None: self.root = root.resolve() self.provider_id = provider_id self.model_name = model_name self.model_path = str(Path(model_path).expanduser().resolve()) if model_path else "" self.id = preference_profile_id(provider_id, self.model_path) self.path = self.root / "data" / "generation_preferences" / f"{self.id}.json" self.keep_threshold = 0.70 self.reject_threshold = 0.35 self.ratings: dict[str, GenerationRating] = {} self.load()
def load(self) -> None: try: payload = json.loads(self.path.read_text(encoding="utf-8")) except (OSError, ValueError, TypeError, json.JSONDecodeError): return self.model_name = str(payload.get("model_name") or self.model_name) self.provider_id = str(payload.get("provider_id") or self.provider_id) self.model_path = str(payload.get("model_path") or self.model_path) thresholds = payload.get("thresholds", {}) if isinstance(thresholds, dict): self.keep_threshold = float(thresholds.get("keep", self.keep_threshold)) self.reject_threshold = float(thresholds.get("reject", self.reject_threshold)) ratings = payload.get("ratings", []) if isinstance(ratings, list): for item in ratings: if isinstance(item, dict): rating = GenerationRating.from_dict(item) self.ratings[rating.image_path] = rating
def save(self) -> None: self.path.parent.mkdir(parents=True, exist_ok=True) temporary = self.path.with_suffix(".tmp") temporary.write_text( json.dumps( { "version": 1, "profile_id": self.id, "provider_id": self.provider_id, "model_name": self.model_name, "model_path": self.model_path, "thresholds": { "keep": self.keep_threshold, "reject": self.reject_threshold, }, "ratings": [asdict(item) for item in self.ratings.values()], "updated_at": _now(), }, indent=2, ), encoding="utf-8", ) temporary.replace(self.path)
def set_rating( self, image_path: str | Path, rating: str, *, seed: int = 0, sampler: str = "", steps: int = 0, resolution: str = "", generation_settings: dict[str, Any] | None = None, generation_created_at: str = "", embedding: Sequence[float] | None = None, ) -> GenerationRating: clean = rating.casefold().strip() if clean not in RATINGS: raise ValueError("Generation rating must be Favorite, Keep, Unsure, or Reject.") resolved = str(Path(image_path).expanduser().resolve()) existing = self.ratings.get(resolved) record = GenerationRating( image_path=resolved, rating=clean, provider_id=self.provider_id, model_name=self.model_name, model_path=self.model_path, seed=int(seed), sampler=sampler, steps=int(steps), resolution=resolution, generation_settings=dict(generation_settings or {}), generation_created_at=generation_created_at, rated_at=_now(), embedding=[float(value) for value in embedding] if embedding is not None else ( existing.embedding if existing else None ), ) self.ratings[resolved] = record self.save() return record
def rating_for(self, image_path: str | Path) -> GenerationRating | None: return self.ratings.get(str(Path(image_path).expanduser().resolve()))
def examples(self) -> tuple[list[GenerationRating], list[GenerationRating]]: positive = [ item for item in self.ratings.values() if item.rating in POSITIVE_RATINGS and Path(item.image_path).is_file() ] negative = [ item for item in self.ratings.values() if item.rating in NEGATIVE_RATINGS and Path(item.image_path).is_file() ] return positive, negative
def has_signal(self) -> bool: positive, _negative = self.examples() return bool(positive)
class ImageEmbeddingCache: def __init__(self, root: Path, model_id: str) -> None: self.root = root.resolve() self.model_id = model_id self.folder = self.root / "data" / "image_embeddings" / hashlib.sha1(model_id.encode("utf-8")).hexdigest()[:12]
def get(self, path: str | Path) -> list[float] | None: cache_path = self.folder / f"{image_cache_id(path)}.json" try: payload = json.loads(cache_path.read_text(encoding="utf-8")) except (OSError, ValueError, TypeError, json.JSONDecodeError): return None source = Path(path).expanduser().resolve() try: stat = source.stat() except OSError: return None if payload.get("path") != str(source) or payload.get("mtime") != stat.st_mtime: return None vector = payload.get("embedding") return [float(value) for value in vector] if isinstance(vector, list) else None
def set(self, path: str | Path, embedding: Sequence[float]) -> None: source = Path(path).expanduser().resolve() try: stat = source.stat() except OSError: return self.folder.mkdir(parents=True, exist_ok=True) cache_path = self.folder / f"{image_cache_id(source)}.json" temporary = cache_path.with_suffix(".tmp") temporary.write_text( json.dumps( { "path": str(source), "mtime": stat.st_mtime, "model_id": self.model_id, "embedding": [float(value) for value in embedding], } ), encoding="utf-8", ) temporary.replace(cache_path)
class GenerationPreferenceEvaluator: """Shared EVE-backed scorer for generated images."""
def __init__( self, root: Path, vision: EveVisionModel | None = None, *, embedder: Callable[[Sequence[str | Path]], list[list[float]]] | None = None, ) -> None: self.root = root.resolve() self.vision = vision or EveVisionModel(prefer_gpu=False) self.embedder = embedder self.cache = ImageEmbeddingCache(self.root, self.vision.model_id)
def _embedding(self, path: str | Path) -> list[float]: cached = self.cache.get(path) if cached is not None: return cached vectors = self.embedder([path]) if self.embedder else self.vision.embed([path]) vector = [float(value) for value in vectors[0]] self.cache.set(path, vector) return vector
def score( self, profile: PreferenceProfile, image_paths: Sequence[str | Path], *, keep_threshold: float | None = None, reject_threshold: float | None = None, ) -> list[PreferenceScore]: positive, negative = profile.examples() if not positive: return [ PreferenceScore(str(Path(path).expanduser().resolve()), None, 0.0, "Needs Review", "No preference examples yet") for path in image_paths ] positive_vectors = [item.embedding or self._embedding(item.image_path) for item in positive] negative_vectors = [item.embedding or self._embedding(item.image_path) for item in negative] image_vectors = [self._embedding(path) for path in image_paths] keep = max(0.001, min(1.0, float(keep_threshold if keep_threshold is not None else profile.keep_threshold))) reject = max(0.0, min(float(reject_threshold if reject_threshold is not None else profile.reject_threshold), keep - 0.001)) results = classify_eve_embeddings( image_paths, image_vectors, positive_vectors, negative_vectors, keep_threshold=keep, reject_threshold=reject, ) categories = {"keep": "Strong Keep", "reject": "Likely Reject", "unreviewed": "Needs Review"} return [ PreferenceScore(result.path, result.match_score, result.decision_confidence, categories[result.suggestion]) for result in results ]
def score_generated_images( root: Path, *, provider_id: str, model_name: str, model_path: str, image_paths: Sequence[str | Path], keep_threshold: float | None = None, reject_threshold: float | None = None,) -> list[PreferenceScore]: profile = PreferenceProfile(root, provider_id, model_name, model_path) evaluator = GenerationPreferenceEvaluator(root) return evaluator.score( profile, image_paths, keep_threshold=keep_threshold, reject_threshold=reject_threshold, )4 unmodified lines567891011121314151617181920216 unmodified lines2829303132335 unmodified lines3940414243449 unmodified lines54555657585960616263646566676815 unmodified lines8485868788895 unmodified lines959697989910039 unmodified lines14014114214314414513 unmodified lines1591601611621631641651661671681691701711721731741751761771781791801811821831841851861871887 unmodified lines19619719819920020120220320431 unmodified lines2362372382392402412422432442454 unmodified lines250251252253254255127 unmodified lines38338438538638738838939039139239339439539610 unmodified lines40740840941041141219 unmodified lines4324334344354364374384392 unmodified lines4424434444454464474487 unmodified lines4564574584594604614624634644652 unmodified lines46846947047147247321 unmodified lines49549649749849950016 unmodified lines5175185195205215225235245255265275285294 unmodified linesimport mathimport reimport threadingfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import Anyfrom PySide6.QtCore import QObject, QThread, Signalfrom adam.executor import ToolCancelled, ToolExecutionError, ToolExecutorfrom adam.assets import AssetRegistryfrom adam.atlas import AtlasSupervisorfrom adam.models import ExecutionPlan, Job, JobStatus, StepStatus, utc_nowfrom adam.nova import evaluate_job_outputclass JobWorker(QThread):6 unmodified linesself.cancel_event = threading.Event()self.run_event = threading.Event()self.run_event.set()def pause(self) -> None:self.run_event.clear()5 unmodified linesself.cancel_event.set()self.run_event.set()def run(self) -> None:total_steps = len(self.job.plan.steps)try:9 unmodified lines)preview_state = {"epoch": 0, "path": ""}def on_progress(percent: int, message: str, step_index: int = index) -> None:overall = int(((step_index + percent / 100) / total_steps) * 100)self.event.emit({"type": "progress","step_percent": percent,"overall": overall,"message": message,})self._discover_external_preview(step, message, preview_state)15 unmodified linesprogress_callback=on_progress,log_callback=on_log,preview_callback=on_preview,)self.event.emit({5 unmodified linesself.event.emit({"type": "completed"})except ToolCancelled as exc:self.event.emit({"type": "cancelled", "message": str(exc)})except Exception as exc:self.event.emit({39 unmodified lines"steps": int(step.arguments.get("preview_steps", 0) or 0),})class JobManager(QObject):job_created = Signal(object)13 unmodified linesself.root = rootself.executor = executorself.logger = loggerself.jobs_path = root / "data" / "jobs.json"self.assets = AssetRegistry(root)self.atlas = AtlasSupervisor(config)self.jobs: list[Job] = []self._queue: list[str] = []self._worker: JobWorker | None = Noneself._active_job: Job | None = Noneself._load()@propertydef active_job(self) -> Job | None:return self._active_jobdef submit(self, plan: ExecutionPlan) -> Job:status = (JobStatus.AWAITING_CONFIRMATIONif plan.requires_confirmationelse JobStatus.QUEUED)job = Job(plan=plan, status=status)self.jobs.insert(0, job)self._append_log(job, f"Plan created: {plan.summary}")if plan.requires_confirmation:self._append_log(job, "Waiting for user confirmation.")else:self._queue.append(job.id)self._save()7 unmodified linesjob = self.get(job_id)if job.status != JobStatus.AWAITING_CONFIRMATION:returnjob.status = JobStatus.QUEUEDself._append_log(job, "Plan approved by user.")self._queue.append(job.id)self._save()self.job_updated.emit(job)self._start_next()31 unmodified linesif job is self._active_job and self._worker:self._append_log(job, "Cancellation requested.")self._worker.cancel()returnif job.id in self._queue:self._queue.remove(job.id)if job.status in {JobStatus.QUEUED,JobStatus.AWAITING_CONFIRMATION,JobStatus.DRAFT,4 unmodified linesself._save()self.job_updated.emit(job)def get(self, job_id: str) -> Job:for job in self.jobs:if job.id == job_id:127 unmodified linesjob.preview_total = 0job.preview_image_index = 0job.preview_image_count = 0self._append_log(job, f"Starting: {job.plan.steps[index].title}")elif event_type == "progress":job.progress = int(event["overall"])message = str(event["message"])if message and (not job.logs or message not in job.logs[-1]):self._append_log(job, message)elif event_type == "log":self._append_log(job, str(event["message"]))elif event_type == "preview":job.preview_path = str(event.get("path", "")) or Nonejob.preview_epoch = int(event.get("epoch", 0) or 0)10 unmodified lineslabel = "Denoising" if job.preview_kind == "generation" else "Training"position = f" step {job.preview_current}" if job.preview_current else f" epoch {job.preview_epoch}"self._append_log(job, f"{label} preview updated at{position}.")elif event_type == "step_finished":index = int(event["index"])job.plan.steps[index].status = StepStatus.FINISHED19 unmodified lineselif event_type == "completed":job.status = JobStatus.FINISHEDjob.progress = 100job.ended_at = utc_now()self._append_log(job, "Job finished successfully.")self.notification.emit("Job complete", job.plan.project_name)elif event_type == "cancelled":job.status = JobStatus.CANCELLED2 unmodified linesfor step in job.plan.steps:if step.status == StepStatus.RUNNING:step.status = StepStatus.SKIPPEDself.notification.emit("Job cancelled", job.plan.project_name)elif event_type == "failed":job.status = JobStatus.FAILEDjob.ended_at = utc_now()7 unmodified linesevent.get("exception"),job.error,)self.notification.emit("Job failed", job.error)self._save()self.job_updated.emit(job)def _worker_finished(self) -> None:self._worker = Noneself._active_job = None2 unmodified linesdef supervise(self, snapshot: Any) -> None:"""Let ATLAS inspect the active training run and apply critical pauses."""job = self._active_jobif job is None or job.status != JobStatus.RUNNING:return21 unmodified linesif decision.action == "pause" and job.status == JobStatus.RUNNING:self.pause(job.id)def _append_log(self, job: Job, message: str) -> None:timestamp = datetime.now().strftime("%H:%M:%S")line = f"[{timestamp}] {message}"16 unmodified linesself.jobs = []returnfor job in self.jobs:if job.status in {JobStatus.RUNNING, JobStatus.PAUSED, JobStatus.QUEUED}:job.status = JobStatus.INTERRUPTEDjob.ended_at = utc_now()job.logs.append("[startup] Previous session ended before this job. ""Review it before retrying.")self._save()def _save(self) -> None:4 unmodified lines56789101112131415161718192021222324252627282930316 unmodified lines38394041424344455 unmodified lines5152535455565758596061629 unmodified lines7273747576777879808182838485868788899091929394959615 unmodified lines1121131141151161171181195 unmodified lines12512612712812913013113239 unmodified lines17217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123213 unmodified lines2462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013027 unmodified lines31031131231331431531631731831932032131 unmodified lines3533543553563573583593603613623633643654 unmodified lines370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473127 unmodified lines60160260360460560660760860961061161261361461561661761861962062162262362462562662762862963010 unmodified lines64164264364464564664764819 unmodified lines6686696706716726736746756766776782 unmodified lines6816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117 unmodified lines7197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527532 unmodified lines75675775875976076176221 unmodified lines78478578678778878979079179279379479579679716 unmodified lines8148158168178188198208218228238248258268278288298304 unmodified linesimport mathimport reimport threadingimport timefrom datetime import datetime, timedelta, timezonefrom pathlib import Pathfrom typing import Anyfrom PySide6.QtCore import QCoreApplication, QObject, QThread, QTimer, Signalfrom adam.executor import ToolAdjustmentRequested, ToolCancelled, ToolExecutionError, ToolExecutorfrom adam.assets import AssetRegistryfrom adam.atlas import AtlasSupervisorfrom adam.experiment_tracker import ExperimentStorefrom adam.models import ExecutionPlan, Job, JobStatus, StepStatus, utc_nowfrom adam.nova import evaluate_job_outputfrom adam.training_assistant import append_preflight_summarydef _safe_int(value: Any) -> int:try:return max(0, int(value or 0))except (TypeError, ValueError):return 0class JobWorker(QThread):6 unmodified linesself.cancel_event = threading.Event()self.run_event = threading.Event()self.run_event.set()self.adjustment_event = threading.Event()self.adjustment_request: dict[str, Any] = {}def pause(self) -> None:self.run_event.clear()5 unmodified linesself.cancel_event.set()self.run_event.set()def request_adjustment(self, updates: dict[str, Any]) -> None:self.adjustment_request.clear()self.adjustment_request.update(updates)self.adjustment_event.set()self.run_event.set()def run(self) -> None:total_steps = len(self.job.plan.steps)try:9 unmodified lines)preview_state = {"epoch": 0, "path": ""}last_progress_emit = {"time": 0.0, "overall": -1, "message": ""}progress_samples: list[dict[str, float]] = []def on_progress(percent: int, message: str, step_index: int = index, **details: Any) -> None:overall = int(((step_index + percent / 100) / total_steps) * 100)now = time.monotonic()progress_eta = self._estimate_step_eta(details, progress_samples, now)changed = overall != last_progress_emit["overall"] or message != last_progress_emit["message"]terminal = percent >= 100 or overall >= 100if not terminal and (not changed or now - last_progress_emit["time"] < 0.25):returnlast_progress_emit.update({"time": now, "overall": overall, "message": message})self.event.emit({"type": "progress","step_percent": percent,"overall": overall,"message": message,**progress_eta,})self._discover_external_preview(step, message, preview_state)15 unmodified linesprogress_callback=on_progress,log_callback=on_log,preview_callback=on_preview,adjustment_event=self.adjustment_event,adjustment_request=self.adjustment_request,)self.event.emit({5 unmodified linesself.event.emit({"type": "completed"})except ToolCancelled as exc:self.event.emit({"type": "cancelled", "message": str(exc)})except ToolAdjustmentRequested as exc:self.event.emit({"type": "adjustment_ready", "message": str(exc), **exc.details})except Exception as exc:self.event.emit({39 unmodified lines"steps": int(step.arguments.get("preview_steps", 0) or 0),})@staticmethoddef _estimate_step_eta(details: dict[str, Any],samples: list[dict[str, float]],now: float,) -> dict[str, Any]:"""Estimate remaining runtime from real step cadence instead of percent alone."""current = _safe_int(details.get("current_step", details.get("step", details.get("current"))))total = _safe_int(details.get("total_steps", details.get("total")))unit = str(details.get("unit", "step") or "step")epoch = _safe_int(details.get("epoch"))total_epochs = _safe_int(details.get("total_epochs"))if (not current or not total) and epoch and total_epochs:current, total, unit = epoch, total_epochs, "epoch"payload: dict[str, Any] = {"progress_current": current,"progress_total": total,"progress_unit": unit,}if not current or not total or current >= total:return payloadlast = samples[-1] if samples else Noneif last and current <= last["current"]:return payloadsamples.append({"time": now, "current": float(current)})del samples[:-25]if len(samples) < 2:return payloadfirst = samples[0]elapsed = now - first["time"]completed = current - int(first["current"])if completed <= 0 or elapsed <= 0:return payloadlifetime_seconds_per_unit = elapsed / completedrecent = samples[-8:]recent_first = recent[0]recent_completed = current - int(recent_first["current"])recent_elapsed = now - recent_first["time"]recent_seconds_per_unit = (recent_elapsed / recent_completedif recent_completed > 0 and recent_elapsed > 0else lifetime_seconds_per_unit)seconds_per_unit = (recent_seconds_per_unit * 0.65) + (lifetime_seconds_per_unit * 0.35)remaining = max(0, int(round((total - current) * seconds_per_unit)))if remaining:payload["eta_seconds"] = remainingpayload["progress_rate"] = 1 / seconds_per_unit if seconds_per_unit > 0 else 0.0payload["estimated_completion_at"] = (datetime.now(timezone.utc) + timedelta(seconds=remaining)).isoformat()return payloadclass JobManager(QObject):job_created = Signal(object)13 unmodified linesself.root = rootself.executor = executorself.logger = loggerself.config = config if config is not None else {}self.jobs_path = root / "data" / "jobs.json"self.assets = AssetRegistry(root)self.atlas = AtlasSupervisor(config)self.experiments = ExperimentStore(root)self.jobs: list[Job] = []self._queue: list[str] = []self._worker: JobWorker | None = Noneself._active_job: Job | None = Noneself._last_snapshot = Noneself._pending_update_job_ids: set[str] = set()self._pending_update_timer_active = Falseself._load()self._schedule_timer = QTimer(self)self._schedule_timer.timeout.connect(self._release_due_scheduled)if QCoreApplication.instance() is not None:self._schedule_timer.start(15_000)QTimer.singleShot(0, self._release_due_scheduled)if self._queue:QTimer.singleShot(0, self._start_next)@propertydef active_job(self) -> Job | None:return self._active_job@propertydef pending_jobs(self) -> list[Job]:return [job for job in self.jobsif job.status in {JobStatus.AWAITING_CONFIRMATION, JobStatus.SCHEDULED, JobStatus.QUEUED}]def submit(self, plan: ExecutionPlan, scheduled_for: str | None = None) -> Job:# Every entry point must review a plan before its queue state is chosen.# UI and Remote may prepare it earlier to keep filesystem work off Qt.append_preflight_summary(plan, self.config)is_future = self._is_future(scheduled_for)status = (JobStatus.AWAITING_CONFIRMATIONif plan.requires_confirmationelse JobStatus.SCHEDULED if is_future else JobStatus.QUEUED)job = Job(plan=plan, status=status, scheduled_for=scheduled_for if is_future else None)self.jobs.insert(0, job)self._append_log(job, f"Plan created: {plan.summary}")if plan.requires_confirmation:self._append_log(job, "Waiting for user confirmation.")if is_future:self._append_log(job, f"Requested start time: {self._display_time(scheduled_for)}.")elif is_future:self._append_log(job, f"Scheduled for {self._display_time(scheduled_for)}.")else:self._queue.append(job.id)self._save()7 unmodified linesjob = self.get(job_id)if job.status != JobStatus.AWAITING_CONFIRMATION:returnjob.status = JobStatus.SCHEDULED if self._is_future(job.scheduled_for) else JobStatus.QUEUEDself._append_log(job, "Plan approved by user.")if job.status == JobStatus.SCHEDULED:self._append_log(job, f"Training will become eligible at {self._display_time(job.scheduled_for)}.")else:self._queue.append(job.id)self._save()self.job_updated.emit(job)self._start_next()31 unmodified linesif job is self._active_job and self._worker:self._append_log(job, "Cancellation requested.")self._worker.cancel()self._save()self.job_updated.emit(job)returnif job.id in self._queue:self._queue.remove(job.id)if job.status in {JobStatus.SCHEDULED,JobStatus.QUEUED,JobStatus.AWAITING_CONFIRMATION,JobStatus.DRAFT,4 unmodified linesself._save()self.job_updated.emit(job)def request_training_adjustment(self, job_id: str, updates: dict[str, Any]) -> None:"""Apply safe DDPM settings after the current epoch and resume automatically."""job = self.get(job_id)if job is not self._active_job or job.status not in {JobStatus.RUNNING, JobStatus.PAUSED} or not self._worker:raise ValueError("Only the active training job can be adjusted.")if not (0 <= job.current_step < len(job.plan.steps)):raise ValueError("The active training step is unavailable.")step = job.plan.steps[job.current_step]if step.tool_id != "ddpm_trainer":raise ValueError("Safe epoch-boundary adjustment currently supports DDPM training.")allowed = {"batch_size", "training_intensity", "gradient_accumulation_steps"}cleaned = {key: int(value) for key, value in updates.items() if key in allowed}if not cleaned or not 1 <= cleaned.get("batch_size", 1) <= 64 \or not 10 <= cleaned.get("training_intensity", 100) <= 100 \or not 1 <= cleaned.get("gradient_accumulation_steps", 1) <= 64:raise ValueError("The requested training settings are outside ADAM's safe range.")previous = {key: step.arguments.get(key) for key in cleaned}if all(previous[key] == value for key, value in cleaned.items()):raise ValueError("Those settings are already active.")job.status = JobStatus.RUNNINGself._append_log(job, f"Adjustment queued for the end of this epoch: {cleaned}.")self._worker.request_adjustment(cleaned)self._save()self.job_updated.emit(job)def safer_vram_retry(self, job_id: str) -> Job:"""Create a checkpoint-aware DDPM retry with a smaller physical batch."""original = self.get(job_id)if original.status != JobStatus.FAILED or not self._looks_like_vram_failure(original):raise ValueError("This job did not fail with a recognizable VRAM error.")plan = ExecutionPlan.from_dict(original.to_dict()["plan"])start_index = max(0, min(original.current_step, len(plan.steps) - 1))plan.steps = plan.steps[start_index:]step = plan.steps[0]old_batch = max(1, int(step.arguments.get("batch_size", 1)))if old_batch <= 1:raise ValueError("Batch size is already 1; lower resolution or enable other memory-saving options.")original_epochs = max(1, int(step.arguments.get("epochs", 1)))resume_note = self._prepare_ddpm_resume(step.arguments, step.tool_id)remaining_epochs = max(1, int(step.arguments.get("epochs", original_epochs)))completed_epochs = max(0, original_epochs - remaining_epochs) if resume_note else 0new_batch = max(1, old_batch // 2)old_accumulation = max(1, int(step.arguments.get("gradient_accumulation_steps", 1)))step.arguments["batch_size"] = new_batchstep.arguments["gradient_accumulation_steps"] = min(64, old_accumulation * max(1, math.ceil(old_batch / new_batch)))if completed_epochs:step.arguments["completed_epochs"] = completed_epochsfor item in plan.steps:item.status = StepStatus.PENDINGplan.id = original.plan.id + "-vram-retry"plan.created_at = utc_now()plan.requires_confirmation = Trueplan.confirmation_reason = "VRAM recovery reduced the physical batch and preserved the effective batch with gradient accumulation."retry = self.submit(plan)self._append_log(retry, f"VRAM recovery changed batch {old_batch} → {new_batch} and gradient accumulation {old_accumulation} → {step.arguments['gradient_accumulation_steps']}.")if resume_note:self._append_log(retry, resume_note)return retry@staticmethoddef _looks_like_vram_failure(job: Job) -> bool:text = "\n".join([job.error or "", *job.logs[-100:]]).lower()return any(token in text for token in ("out of memory", "cuda oom", "cuda error: out of memory"))@staticmethoddef _is_future(value: str | None) -> bool:if not value:return Falsetry:scheduled = datetime.fromisoformat(value)if scheduled.tzinfo is None:scheduled = scheduled.astimezone()return scheduled.astimezone(timezone.utc) > datetime.now(timezone.utc)except (TypeError, ValueError):return False@staticmethoddef _display_time(value: str | None) -> str:try:return datetime.fromisoformat(str(value)).astimezone().strftime("%b %d at %I:%M %p")except ValueError:return str(value or "the requested time")def _release_due_scheduled(self) -> None:released: list[Job] = []for job in reversed(self.jobs):if job.status == JobStatus.SCHEDULED and not self._is_future(job.scheduled_for):job.status = JobStatus.QUEUEDself._queue.append(job.id)self._append_log(job, "Scheduled start time reached; waiting for the training slot.")released.append(job)if not released:returnself._save()for job in released:self.job_updated.emit(job)self._start_next()def get(self, job_id: str) -> Job:for job in self.jobs:if job.id == job_id:127 unmodified linesjob.preview_total = 0job.preview_image_index = 0job.preview_image_count = 0job.eta_seconds = Nonejob.estimated_completion_at = Nonejob.progress_current = 0job.progress_total = 0job.progress_rate = 0.0job.progress_unit = "step"self._append_log(job, f"Starting: {job.plan.steps[index].title}")elif event_type == "progress":job.progress = int(event["overall"])job.eta_seconds = _safe_int(event.get("eta_seconds")) or Nonejob.estimated_completion_at = str(event.get("estimated_completion_at") or "") or Nonejob.progress_current = _safe_int(event.get("progress_current"))job.progress_total = _safe_int(event.get("progress_total"))job.progress_rate = float(event.get("progress_rate", 0.0) or 0.0)job.progress_unit = str(event.get("progress_unit", "step") or "step")message = str(event["message"])if message and (not job.logs or message not in job.logs[-1]):self._append_log(job, message)self._schedule_job_update(job)returnelif event_type == "log":self._append_log(job, str(event["message"]))self._schedule_job_update(job)returnelif event_type == "preview":job.preview_path = str(event.get("path", "")) or Nonejob.preview_epoch = int(event.get("epoch", 0) or 0)10 unmodified lineslabel = "Denoising" if job.preview_kind == "generation" else "Training"position = f" step {job.preview_current}" if job.preview_current else f" epoch {job.preview_epoch}"self._append_log(job, f"{label} preview updated at{position}.")self._schedule_job_update(job)returnelif event_type == "step_finished":index = int(event["index"])job.plan.steps[index].status = StepStatus.FINISHED19 unmodified lineselif event_type == "completed":job.status = JobStatus.FINISHEDjob.progress = 100job.eta_seconds = 0job.estimated_completion_at = utc_now()job.ended_at = utc_now()self._append_log(job, "Job finished successfully.")self._record_experiment(job)self.notification.emit("Job complete", job.plan.project_name)elif event_type == "cancelled":job.status = JobStatus.CANCELLED2 unmodified linesfor step in job.plan.steps:if step.status == StepStatus.RUNNING:step.status = StepStatus.SKIPPEDself._record_experiment(job)self.notification.emit("Job cancelled", job.plan.project_name)elif event_type == "adjustment_ready":index = max(0, min(job.current_step, len(job.plan.steps) - 1))remaining_steps = job.plan.steps[index:]step = remaining_steps[0]updates = dict(event.get("updates") or {})step.arguments.update(updates)checkpoint = str(event.get("checkpoint", ""))completed_epochs = max(0, int(event.get("completed_epochs", 0) or 0))total_epochs = max(1, int(step.arguments.get("epochs", 1)))step.arguments["epochs"] = max(1, total_epochs - completed_epochs)step.arguments["completed_epochs"] = completed_epochsif checkpoint:step.arguments["resume_from"] = checkpointfor pending in remaining_steps:pending.status = StepStatus.PENDINGjob.plan.steps = remaining_stepsjob.current_step = -1job.status = JobStatus.QUEUEDjob.eta_seconds = Nonejob.estimated_completion_at = Noneself._queue.insert(0, job.id)self._append_log(job, f"Epoch {completed_epochs} checkpoint is complete. Restarting with {updates}.")self.notification.emit("Training settings ready", "Restarting from the completed epoch checkpoint.")elif event_type == "failed":job.status = JobStatus.FAILEDjob.ended_at = utc_now()7 unmodified linesevent.get("exception"),job.error,)self._record_experiment(job)self.notification.emit("Job failed", job.error)self._save()self.job_updated.emit(job)def _schedule_job_update(self, job: Job) -> None:self._pending_update_job_ids.add(job.id)if QCoreApplication.instance() is None:self._flush_pending_job_updates()returnif self._pending_update_timer_active:returnself._pending_update_timer_active = TrueQTimer.singleShot(300, self._flush_pending_job_updates)def _flush_pending_job_updates(self) -> None:if not self._pending_update_job_ids:self._pending_update_timer_active = Falsereturnpending_ids = list(self._pending_update_job_ids)self._pending_update_job_ids.clear()self._pending_update_timer_active = Falseself._save()for job_id in pending_ids:try:self.job_updated.emit(self.get(job_id))except KeyError:continuedef _worker_finished(self) -> None:self._worker = Noneself._active_job = None2 unmodified linesdef supervise(self, snapshot: Any) -> None:"""Let ATLAS inspect the active training run and apply critical pauses."""self._last_snapshot = snapshotjob = self._active_jobif job is None or job.status != JobStatus.RUNNING:return21 unmodified linesif decision.action == "pause" and job.status == JobStatus.RUNNING:self.pause(job.id)def _record_experiment(self, job: Job) -> None:if not self._has_training(job):returntry:self.experiments.record_job(job, self._last_snapshot)except Exception as exc:self.logger.warning("Experiment tracking failed for %s: %s", job.id, exc)def _append_log(self, job: Job, message: str) -> None:timestamp = datetime.now().strftime("%H:%M:%S")line = f"[{timestamp}] {message}"16 unmodified linesself.jobs = []returnfor job in self.jobs:if job.status in {JobStatus.RUNNING, JobStatus.PAUSED}:job.status = JobStatus.INTERRUPTEDjob.ended_at = utc_now()job.logs.append("[startup] Previous session ended before this job. ""Review it before retrying.")elif job.status == JobStatus.QUEUED:self._queue.append(job.id)if not any("Queued job restored" in line for line in job.logs[-5:]):job.logs.append("[startup] Queued job restored and will run when ADAM is ready.")self._save()def _save(self) -> None:
123456789101112131415from __future__ import annotations
from .base import ModelComparison, ModelInspection, TensorComparison, TensorStatsfrom .comparison import compare_modelsfrom .detector import inspect_model, inspector_for
__all__ = [ "ModelComparison", "ModelInspection", "TensorComparison", "TensorStats", "compare_models", "inspect_model", "inspector_for",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146from __future__ import annotations
from dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Any, Callable, Iterable
MODEL_EXTENSIONS = {".safetensors", ".pt", ".pth", ".bin", ".ckpt"}CONFIG_FILENAMES = { "model_index.json", "config.json", "scheduler_config.json", "flow_model_info.json", "adapter_config.json",}
@dataclass(slots=True)class TensorStats: name: str shape: tuple[int, ...] dtype: str parameter_count: int memory_bytes: int minimum: float | None = None maximum: float | None = None mean: float | None = None std: float | None = None abs_mean: float | None = None l2_norm: float | None = None zero_percent: float | None = None component: str = "other" health: list[str] = field(default_factory=list)
@dataclass(slots=True)class ModelInspection: path: str resolved_path: str architecture: str confidence: float status: str size_bytes: int config_files: list[str] resolution: int | None epoch: int | None step: int | None tensor_count: int total_parameters: int trainable_parameters: int | None parameter_memory_bytes: int dtypes: dict[str, int] components: dict[str, int] largest_tensors: list[TensorStats] tensors: list[TensorStats] health: list[str] messages: list[str] lora: dict[str, Any] = field(default_factory=dict) configs: dict[str, Any] = field(default_factory=dict) histogram: dict[str, list[float]] = field(default_factory=dict) tensor_size_distribution: list[tuple[str, int]] = field(default_factory=list) checkpoints: list[str] = field(default_factory=list) loss_history: list[tuple[int, float]] = field(default_factory=list)
@dataclass(slots=True)class TensorComparison: name: str shape: tuple[int, ...] component: str mean_abs_difference: float | None relative_difference: float | None cosine_similarity: float | None l2_distance: float | None drift: float | None change_score: float | None
@dataclass(slots=True)class ModelComparison: path_a: str path_b: str architecture_a: str architecture_b: str architecture_match: bool config_differences: list[str] resolution_difference: tuple[int | None, int | None] | None parameter_count_difference: int only_a: list[str] only_b: list[str] shape_mismatches: list[str] tensor_comparisons: list[TensorComparison] group_comparisons: dict[str, dict[str, float]] messages: list[str]
ProgressCallback = Callable[[int, str], None]CancelCallback = Callable[[], bool]
class InspectorError(RuntimeError): pass
class BaseModelInspector: architecture = "Generic / Unknown"
def inspect( self, path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress: ProgressCallback | None = None, cancelled: CancelCallback | None = None, ) -> ModelInspection: raise NotImplementedError
def report(progress: ProgressCallback | None, value: int, message: str) -> None: if progress: progress(max(0, min(100, int(value))), message)
def is_cancelled(cancelled: CancelCallback | None) -> bool: return bool(cancelled and cancelled())
def parameter_count(shape: Iterable[int]) -> int: total = 1 for dim in shape: total *= int(dim) return int(total)
def dtype_size(dtype: str) -> int: lowered = dtype.casefold() if "float64" in lowered or "int64" in lowered: return 8 if "float32" in lowered or "int32" in lowered: return 4 if "float16" in lowered or "bfloat16" in lowered or "int16" in lowered: return 2 if "int8" in lowered or "uint8" in lowered or "bool" in lowered: return 1 return 4123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160from __future__ import annotations
import jsonfrom pathlib import Pathfrom typing import Any
from .base import ModelComparison, TensorComparison, is_cancelled, reportfrom .detector import inspect_modelfrom .generic import _extract_state_dict, _weight_filesfrom .statistics import component_for_name
def _iter_named_tensors(path: Path): files = _weight_files(path) for file in files: if file.suffix.casefold() == ".safetensors": from safetensors import safe_open
with safe_open(str(file), framework="pt", device="cpu") as handle: for key in handle.keys(): yield key, handle.get_tensor(key) else: import torch
payload = torch.load(str(file), map_location="cpu", weights_only=False) state = _extract_state_dict(payload) for key, value in state.items(): if hasattr(value, "shape"): yield str(key), value
def _tensor_map(path: Path) -> dict[str, Any]: return {name: tensor for name, tensor in _iter_named_tensors(path)}
def _config_differences(configs_a: dict[str, Any], configs_b: dict[str, Any], *, limit: int = 40) -> list[str]: diffs: list[str] = [] keys = sorted(set(configs_a) | set(configs_b)) for key in keys: if key not in configs_a: diffs.append(f"Only B has config {key}") elif key not in configs_b: diffs.append(f"Only A has config {key}") elif json.dumps(configs_a[key], sort_keys=True, default=str) != json.dumps(configs_b[key], sort_keys=True, default=str): diffs.append(f"Config differs: {key}") if len(diffs) >= limit: diffs.append("Additional config differences omitted.") break return diffs
def compare_models( path_a: str | Path, path_b: str | Path, *, arch_a: str = "", arch_b: str = "", settings_a: dict[str, Any] | None = None, settings_b: dict[str, Any] | None = None, progress=None, cancelled=None,) -> ModelComparison: report(progress, 2, "Inspecting first model") summary_a = inspect_model(path_a, recorded_architecture=arch_a, run_settings=settings_a, progress=progress, cancelled=cancelled) report(progress, 30, "Inspecting second model") summary_b = inspect_model(path_b, recorded_architecture=arch_b, run_settings=settings_b, progress=progress, cancelled=cancelled) report(progress, 55, "Loading comparable tensors") tensors_a = _tensor_map(Path(summary_a.resolved_path)) if is_cancelled(cancelled): raise RuntimeError("Comparison cancelled.") tensors_b = _tensor_map(Path(summary_b.resolved_path)) names_a = set(tensors_a) names_b = set(tensors_b) common = sorted(names_a & names_b) only_a = sorted(names_a - names_b)[:200] only_b = sorted(names_b - names_a)[:200] shape_mismatches = [] comparable = [] for name in common: if tuple(tensors_a[name].shape) != tuple(tensors_b[name].shape): shape_mismatches.append(name) else: comparable.append(name) comparisons: list[TensorComparison] = [] import torch
for index, name in enumerate(comparable): if is_cancelled(cancelled): raise RuntimeError("Comparison cancelled.") if index % 10 == 0: report(progress, 58 + int(36 * index / max(1, len(comparable))), f"Comparing {index + 1} of {len(comparable)} tensors") with torch.no_grad(): a = tensors_a[name].detach().to(device="cpu").float().reshape(-1) b = tensors_b[name].detach().to(device="cpu").float().reshape(-1) if a.numel() == 0: continue limit = 1_000_000 if a.numel() > limit: stride = max(1, a.numel() // limit) a = a[::stride][:limit] b = b[::stride][:limit] delta = b - a mean_abs = delta.abs().mean().item() base_abs = a.abs().mean().item() relative = mean_abs / (base_abs + 1e-12) l2 = torch.linalg.vector_norm(delta).item() norm_a = torch.linalg.vector_norm(a).item() norm_b = torch.linalg.vector_norm(b).item() cosine = torch.nn.functional.cosine_similarity(a, b, dim=0).item() if norm_a and norm_b else None drift = (norm_b - norm_a) / (norm_a + 1e-12) if norm_a else None score = relative * 0.7 + (1 - cosine if cosine is not None else 0) * 0.3 comparisons.append( TensorComparison( name=name, shape=tuple(int(dim) for dim in tensors_a[name].shape), component=component_for_name(name), mean_abs_difference=float(mean_abs), relative_difference=float(relative), cosine_similarity=float(cosine) if cosine is not None else None, l2_distance=float(l2), drift=float(drift) if drift is not None else None, change_score=float(score), ) ) comparisons.sort(key=lambda item: item.change_score or 0, reverse=True) groups: dict[str, dict[str, float]] = {} for item in comparisons: group = groups.setdefault(item.component, {"tensors": 0, "mean_change_score": 0.0, "mean_abs_difference": 0.0}) group["tensors"] += 1 group["mean_change_score"] += item.change_score or 0 group["mean_abs_difference"] += item.mean_abs_difference or 0 for group in groups.values(): count = max(1, int(group["tensors"])) group["mean_change_score"] /= count group["mean_abs_difference"] /= count messages = [ "Change Score is a statistical weight-change metric; it does not directly equal behavioral importance." ] if shape_mismatches: messages.append("Some tensor comparisons are unavailable because tensor shapes differ.") report(progress, 100, "Comparison complete") return ModelComparison( path_a=summary_a.resolved_path, path_b=summary_b.resolved_path, architecture_a=summary_a.architecture, architecture_b=summary_b.architecture, architecture_match=summary_a.architecture == summary_b.architecture, config_differences=_config_differences(summary_a.configs, summary_b.configs), resolution_difference=( (summary_a.resolution, summary_b.resolution) if summary_a.resolution != summary_b.resolution else None ), parameter_count_difference=summary_b.total_parameters - summary_a.total_parameters, only_a=only_a, only_b=only_b, shape_mismatches=shape_mismatches[:200], tensor_comparisons=comparisons[:200], group_comparisons=groups, messages=messages, )1234567891011121314151617181920212223242526272829from __future__ import annotations
from pathlib import Pathfrom typing import Any
from .generic import GenericModelInspector
class DDPMInspector(GenericModelInspector): architecture = "DDPM / Diffusers"
def inspect(self, path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress=None, cancelled=None): summary = super().inspect( path, recorded_architecture=recorded_architecture or "ddpm", run_settings=run_settings, progress=progress, cancelled=cancelled, ) if summary.architecture == "Generic / Unknown": summary.architecture = self.architecture summary.messages.insert(0, "Model recognized as DDPM from ADAM trainer metadata") expected = ("unet", "scheduler") root = Path(summary.resolved_path) existing = {part.name.casefold() for part in (root.iterdir() if root.is_dir() else [])} for component in expected: if root.is_dir() and component not in existing and not any(component in item.casefold() for item in summary.configs): summary.health.append(f"Unusual: expected DDPM component not found: {component}") return summary1234567891011121314151617181920212223242526272829303132333435from __future__ import annotations
from pathlib import Pathfrom typing import Any
from .ddpm import DDPMInspectorfrom .flow_matching import FlowMatchingInspectorfrom .generic import GenericModelInspectorfrom .lora import LoRAInspectorfrom .maskgit import MaskGITInspector
def inspector_for(path: str | Path, recorded_architecture: str = "", settings: dict[str, Any] | None = None): text = " ".join((str(path), recorded_architecture, str(settings or {}))).casefold() target = Path(path).expanduser() if "lora" in text or target.suffix.casefold() == ".safetensors" and "adapter" in target.name.casefold(): return LoRAInspector() if "maskgit" in text: return MaskGITInspector() if "flow" in text or (target / "flow_model_info.json").is_file(): return FlowMatchingInspector() if "ddpm" in text or (target / "model_index.json").is_file() or (target / "scheduler").is_dir(): return DDPMInspector() return GenericModelInspector()
def inspect_model(path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress=None, cancelled=None): inspector = inspector_for(path, recorded_architecture, run_settings) return inspector.inspect( path, recorded_architecture=recorded_architecture, run_settings=run_settings, progress=progress, cancelled=cancelled, )12345678910111213141516171819202122232425from __future__ import annotations
from pathlib import Pathfrom typing import Any
from .generic import GenericModelInspector
class FlowMatchingInspector(GenericModelInspector): architecture = "Flow Matching"
def inspect(self, path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress=None, cancelled=None): summary = super().inspect( path, recorded_architecture=recorded_architecture or "flow", run_settings=run_settings, progress=progress, cancelled=cancelled, ) summary.architecture = "Flow Matching" if summary.architecture == "Generic / Unknown" else summary.architecture if not any("flow_model_info.json" in item for item in summary.config_files): root = Path(summary.resolved_path) if root.is_dir(): summary.health.append("Unusual: Flow Matching metadata file was not found") return summary123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349from __future__ import annotations
import jsonfrom collections import Counterfrom pathlib import Pathfrom typing import Any, Iterator
from .base import BaseModelInspector, InspectorError, ModelInspection, TensorStats, is_cancelled, reportfrom .statistics import ( discover_checkpoint_paths, discover_config_files, step_from_name, tensor_stats_from_torch,)
def _folder_size(path: Path) -> int: if path.is_file(): return path.stat().st_size total = 0 try: for item in path.rglob("*"): if item.is_file(): total += item.stat().st_size except OSError: return total return total
def _read_configs(files: list[Path], root: Path) -> dict[str, Any]: configs: dict[str, Any] = {} for file in files[:40]: try: key = str(file.relative_to(root if root.is_dir() else root.parent)) except ValueError: key = file.name try: configs[key] = json.loads(file.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError, json.JSONDecodeError): configs[key] = "<unreadable>" return configs
def _resolution_from_configs(configs: dict[str, Any], settings: dict[str, Any] | None) -> int | None: for source in (settings or {}, *[value for value in configs.values() if isinstance(value, dict)]): for key in ("resolution", "sample_size", "image_size", "size"): value = source.get(key) if isinstance(source, dict) else None if isinstance(value, int): return value if isinstance(value, (list, tuple)) and value and isinstance(value[0], int): return int(value[0]) try: if value: return int(value) except (TypeError, ValueError): pass return None
def _iter_safetensors(file: Path) -> Iterator[tuple[str, Any, dict[str, Any]]]: from safetensors import safe_open
with safe_open(str(file), framework="pt", device="cpu") as handle: metadata = handle.metadata() or {} for key in handle.keys(): yield key, handle.get_tensor(key), metadata
def _extract_state_dict(payload: Any) -> dict[str, Any]: try: import torch except Exception: torch = None if torch is not None and hasattr(payload, "shape"): return {"tensor": payload} if isinstance(payload, dict): for key in ("state_dict", "model_state_dict", "model", "module", "unet", "network"): value = payload.get(key) if isinstance(value, dict) and any(hasattr(item, "shape") for item in value.values()): return value if any(hasattr(item, "shape") for item in payload.values()): return payload return {}
def _iter_torch_checkpoint(file: Path) -> Iterator[tuple[str, Any, dict[str, Any]]]: import torch
try: payload = torch.load(str(file), map_location="cpu", weights_only=True) except TypeError: payload = torch.load(str(file), map_location="cpu") except Exception: payload = torch.load(str(file), map_location="cpu", weights_only=False) state = _extract_state_dict(payload) metadata = {key: value for key, value in payload.items() if key not in state} if isinstance(payload, dict) else {} for key, tensor in state.items(): if hasattr(tensor, "shape"): yield str(key), tensor, metadata
def _weight_files(path: Path) -> list[Path]: if path.is_file(): return [path] ignored_names = {"optimizer.bin", "scheduler.bin", "scaler.pt"} names = { "diffusion_pytorch_model.safetensors", "model.safetensors", "pytorch_model.bin", "adapter_model.safetensors", "adapter_model.bin", "checkpoint.pt", "best_checkpoint.pt", } files: list[Path] = [] try: for item in path.rglob("*"): if item.is_file() and (item.name in names or item.suffix.casefold() in {".safetensors", ".pt", ".pth", ".bin", ".ckpt"}): if item.name.casefold() not in ignored_names: files.append(item) except OSError: return [] if (path / "model_index.json").is_file(): final_files = [ item for item in files if not any(part.startswith("checkpoint-") for part in item.relative_to(path).parts) ] if final_files: files = final_files return sorted(files, key=lambda item: (0 if item.name in names else 1, str(item)))
class GenericModelInspector(BaseModelInspector): architecture = "Generic / Unknown"
def inspect( self, path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress=None, cancelled=None, ) -> ModelInspection: target = Path(path).expanduser() if not target.exists(): raise InspectorError(f"Model path does not exist: {target}") target = target.resolve() report(progress, 3, "Finding model files") config_files = discover_config_files(target) configs = _read_configs(config_files, target) files = _weight_files(target) if not files: message = "Model contains no readable tensor checkpoint" return self._empty(target, recorded_architecture, run_settings, config_files, configs, message)
tensors: list[TensorStats] = [] dtypes: Counter[str] = Counter() components: Counter[str] = Counter() health: list[str] = [] messages: list[str] = [] metadata: dict[str, Any] = {} for file_index, file in enumerate(files): if is_cancelled(cancelled): raise InspectorError("Inspection cancelled.") report(progress, 8 + int(80 * file_index / max(1, len(files))), f"Reading {file.name}") try: if file.suffix.casefold() == ".safetensors": iterator = _iter_safetensors(file) else: iterator = _iter_torch_checkpoint(file) for name, tensor, file_metadata in iterator: if is_cancelled(cancelled): raise InspectorError("Inspection cancelled.") prefix = file.parent.name if len(files) > 1 else "" stat = tensor_stats_from_torch(f"{prefix}.{name}" if prefix and not name.startswith(prefix) else name, tensor) tensors.append(stat) dtypes[stat.dtype] += stat.parameter_count components[stat.component] += stat.parameter_count health.extend(f"{stat.name}: {item}" for item in stat.health) if file_metadata: metadata.update(file_metadata) except Exception as exc: health.append(f"{file.name}: unreadable checkpoint ({exc})")
if not tensors: message = "Model contains no readable tensor checkpoint" return self._empty(target, recorded_architecture, run_settings, config_files, configs, message, [*(health or []), message])
report(progress, 92, "Summarizing model") total_parameters = sum(tensor.parameter_count for tensor in tensors) parameter_memory = sum(tensor.memory_bytes for tensor in tensors) largest = sorted(tensors, key=lambda item: item.parameter_count, reverse=True)[:20] architecture, confidence, message = self._architecture_from_signals( target, recorded_architecture, configs, [tensor.name for tensor in tensors] ) messages.append(message) duplicate_count = len(tensors) - len({tensor.name for tensor in tensors}) if duplicate_count: health.append(f"Unusual: {duplicate_count} duplicate tensor names after folder merging") checkpoints = [str(item) for item in discover_checkpoint_paths(target)] return ModelInspection( path=str(path), resolved_path=str(target), architecture=architecture, confidence=confidence, status="ok", size_bytes=_folder_size(target), config_files=[str(item) for item in config_files], resolution=_resolution_from_configs(configs, run_settings), epoch=self._number_from_metadata(metadata, "epoch"), step=self._number_from_metadata(metadata, "step") or step_from_name(target.name), tensor_count=len(tensors), total_parameters=total_parameters, trainable_parameters=self._trainable_parameters(tensors, architecture), parameter_memory_bytes=parameter_memory, dtypes=dict(dtypes), components=dict(components), largest_tensors=largest, tensors=tensors, health=health or ["No invalid tensor values found in sampled statistics."], messages=messages, lora=self._lora_info(tensors, configs), configs=configs, histogram=self._histogram(tensors), tensor_size_distribution=[(tensor.name, tensor.parameter_count) for tensor in largest], checkpoints=checkpoints, loss_history=[], )
def _empty( self, target: Path, recorded_architecture: str, run_settings: dict[str, Any] | None, config_files: list[Path], configs: dict[str, Any], message: str, health: list[str] | None = None, ) -> ModelInspection: architecture, confidence, detection_message = self._architecture_from_signals(target, recorded_architecture, configs, []) return ModelInspection( path=str(target), resolved_path=str(target), architecture=architecture, confidence=confidence, status="warning", size_bytes=_folder_size(target), config_files=[str(item) for item in config_files], resolution=_resolution_from_configs(configs, run_settings), epoch=None, step=step_from_name(target.name), tensor_count=0, total_parameters=0, trainable_parameters=None, parameter_memory_bytes=0, dtypes={}, components={}, largest_tensors=[], tensors=[], health=health or [message], messages=[detection_message, message], configs=configs, checkpoints=[str(item) for item in discover_checkpoint_paths(target)], )
@staticmethod def _number_from_metadata(metadata: dict[str, Any], key: str) -> int | None: for candidate in (key, f"global_{key}", f"current_{key}"): try: value = metadata.get(candidate) if value is not None: return int(value) except (TypeError, ValueError): pass return None
@staticmethod def _trainable_parameters(tensors: list[TensorStats], architecture: str) -> int | None: if architecture == "LoRA": return sum(tensor.parameter_count for tensor in tensors) return None
@staticmethod def _architecture_from_signals( target: Path, recorded_architecture: str, configs: dict[str, Any], tensor_names: list[str], ) -> tuple[str, float, str]: recorded = recorded_architecture.casefold() joined_names = "\n".join(tensor_names).casefold() config_text = json.dumps(configs, default=str).casefold() folder_text = str(target).casefold() signals = " ".join((joined_names, config_text, folder_text)) if "lora" in recorded or "lora" in signals or "adapter_config" in signals: return "LoRA", 0.92, "Model recognized as LoRA" if "maskgit" in recorded or "maskgit" in signals: return "MaskGIT", 0.86, "Model recognized as MaskGIT" if "flow" in recorded or "rectified_flow" in signals or "flow_model_info" in signals: return "Flow Matching", 0.9, "Model recognized as Flow Matching" if "ddpm" in recorded or "diffusers" in config_text or "unet" in signals or "scheduler_config" in signals: return "DDPM / Diffusers", 0.88, "Model recognized as DDPM" return "Generic / Unknown", 0.35, "Model type uncertain - using generic tensor inspection"
@staticmethod def _lora_info(tensors: list[TensorStats], configs: dict[str, Any]) -> dict[str, Any]: lora_tensors = [tensor for tensor in tensors if "lora" in tensor.name.casefold()] if not lora_tensors: return {} down = [tensor for tensor in lora_tensors if any(token in tensor.name.casefold() for token in ("down", "lora_a"))] up = [tensor for tensor in lora_tensors if any(token in tensor.name.casefold() for token in ("up", "lora_b"))] ranks = sorted({tensor.shape[0] for tensor in down if tensor.shape}) alpha = None targets: set[str] = set() for config in configs.values(): if isinstance(config, dict): alpha = config.get("lora_alpha", config.get("alpha", alpha)) modules = config.get("target_modules") if isinstance(modules, list): targets.update(str(item) for item in modules) if not targets: for tensor in lora_tensors: parts = tensor.name.split(".") if len(parts) > 2: targets.add(parts[-3]) return { "rank": ", ".join(str(item) for item in ranks[:8]) if ranks else "unknown", "alpha": alpha if alpha is not None else "unknown", "target_modules": sorted(targets)[:20], "down_matrices": len(down), "up_matrices": len(up), "adapter_parameter_count": sum(tensor.parameter_count for tensor in lora_tensors), "average_abs_mean": ( sum(tensor.abs_mean or 0 for tensor in lora_tensors) / max(1, len(lora_tensors)) ), }
@staticmethod def _histogram(tensors: list[TensorStats]) -> dict[str, list[float]]: values = [tensor.abs_mean for tensor in tensors if tensor.abs_mean is not None] if not values: return {} buckets = [0.0] * 10 high = max(values) or 1.0 for value in values: index = min(9, int((value / high) * 10)) buckets[index] += 1 return {"abs_mean_bins": [round(high * index / 10, 6) for index in range(11)], "counts": buckets}1234567891011121314151617181920212223from __future__ import annotations
from pathlib import Pathfrom typing import Any
from .generic import GenericModelInspector
class LoRAInspector(GenericModelInspector): architecture = "LoRA"
def inspect(self, path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress=None, cancelled=None): summary = super().inspect( path, recorded_architecture=recorded_architecture or "lora", run_settings=run_settings, progress=progress, cancelled=cancelled, ) summary.architecture = "LoRA" if summary.architecture == "Generic / Unknown" else summary.architecture if summary.tensors and not summary.lora: summary.health.append("Worth inspecting: ADAM marked this as LoRA, but LoRA tensor naming was not obvious") return summary123456789101112131415161718192021222324from __future__ import annotations
from pathlib import Pathfrom typing import Any
from .generic import GenericModelInspector
class MaskGITInspector(GenericModelInspector): architecture = "MaskGIT"
def inspect(self, path: str | Path, *, recorded_architecture: str = "", run_settings: dict[str, Any] | None = None, progress=None, cancelled=None): summary = super().inspect( path, recorded_architecture=recorded_architecture or "maskgit", run_settings=run_settings, progress=progress, cancelled=cancelled, ) summary.architecture = "MaskGIT" if summary.architecture == "Generic / Unknown" else summary.architecture names = "\n".join(tensor.name for tensor in summary.tensors).casefold() if summary.tensors and not any(token in names for token in ("attention", "attn", "transformer", "embed")): summary.health.append("Worth inspecting: expected MaskGIT transformer or embedding tensors were not obvious") return summary123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142from __future__ import annotations
import mathfrom pathlib import Pathfrom typing import Any
from .base import CONFIG_FILENAMES, MODEL_EXTENSIONS, TensorStats, dtype_size, parameter_count
def bytes_label(size: int | float | None) -> str: if size is None: return "-" value = float(size) for unit in ("B", "KB", "MB", "GB", "TB"): if abs(value) < 1024 or unit == "TB": return f"{value:.1f} {unit}" if unit != "B" else f"{int(value)} B" value /= 1024 return f"{value:.1f} TB"
def component_for_name(name: str) -> str: lowered = name.casefold() mapping = ( ("down_blocks", ("down_blocks", "down.", "downsample")), ("mid_block", ("mid_block", "middle_block", "mid.")), ("up_blocks", ("up_blocks", "up.", "upsample")), ("attention", ("attn", "attention", "to_q", "to_k", "to_v", "query", "key", "value")), ("embeddings", ("embed", "embedding", "position", "token")), ("transformer blocks", ("transformer", "blocks.", "layers.", "encoder", "decoder")), ("output layers", ("out.", "output", "proj_out", "lm_head", "conv_out")), ("LoRA adapters", ("lora", "hada", "lokr", "adapter")), ("normalization", ("norm", "bn", "ln", "group_norm", "layer_norm")), ) for component, tokens in mapping: if any(token in lowered for token in tokens): return component return name.split(".", 1)[0] if "." in name else "other"
def safe_number(value: Any) -> float | None: try: number = float(value) except (TypeError, ValueError, OverflowError): return None return number if math.isfinite(number) else None
def tensor_stats_from_torch(name: str, tensor: Any, *, sample_limit: int = 1_000_000) -> TensorStats: shape = tuple(int(dim) for dim in getattr(tensor, "shape", ())) dtype = str(getattr(tensor, "dtype", "unknown")).replace("torch.", "") count = parameter_count(shape) stat = TensorStats( name=name, shape=shape, dtype=dtype, parameter_count=count, memory_bytes=count * dtype_size(dtype), component=component_for_name(name), ) if count == 0: stat.health.append("Empty tensor") return stat try: import torch
with torch.no_grad(): values = tensor.detach().to(device="cpu") if not values.is_floating_point() and not values.is_complex(): values = values.float() else: values = values.float() flat = values.reshape(-1) if flat.numel() > sample_limit: stride = max(1, flat.numel() // sample_limit) flat = flat[::stride][:sample_limit] finite = torch.isfinite(flat) if not bool(finite.all()): if bool(torch.isnan(flat).any()): stat.health.append("Invalid: NaN values found") if bool(torch.isinf(flat).any()): stat.health.append("Invalid: Inf values found") flat = flat[finite] if flat.numel() == 0: return stat stat.minimum = safe_number(flat.min().item()) stat.maximum = safe_number(flat.max().item()) stat.mean = safe_number(flat.mean().item()) stat.std = safe_number(flat.std(unbiased=False).item()) if flat.numel() > 1 else 0.0 stat.abs_mean = safe_number(flat.abs().mean().item()) stat.l2_norm = safe_number(torch.linalg.vector_norm(flat).item()) stat.zero_percent = safe_number((flat == 0).float().mean().item() * 100) except Exception as exc: stat.health.append(f"Statistics unavailable: {exc}") if stat.abs_mean is not None and stat.abs_mean > 100: stat.health.append("Unusual: very large average weight magnitude") if stat.maximum is not None and stat.minimum is not None and max(abs(stat.maximum), abs(stat.minimum)) > 1_000: stat.health.append("Unusual: very large absolute weight value") return stat
def discover_config_files(path: Path) -> list[Path]: root = path if path.is_dir() else path.parent files: list[Path] = [] try: for item in root.rglob("*"): if item.is_file() and item.name in CONFIG_FILENAMES: files.append(item) except OSError: return [] return sorted(files)
def discover_checkpoint_paths(path: Path) -> list[Path]: root = path if path.is_dir() else path.parent candidates: list[Path] = [] try: for item in root.rglob("*"): if item.is_file() and item.suffix.casefold() in MODEL_EXTENSIONS: candidates.append(item) elif item.is_dir() and item.name.startswith("checkpoint-"): candidates.append(item) except OSError: return [] return sorted(candidates, key=lambda item: (step_from_name(item.name) or -1, str(item)))
def step_from_name(name: str) -> int | None: import re
matches = re.findall(r"(?:step|checkpoint|epoch|e|s)[-_]?(\d+)", name, flags=re.I) if not matches: matches = re.findall(r"(\d+)", name) if not matches: return None try: return int(matches[-1]) except ValueError: return None
def shape_label(shape: tuple[int, ...]) -> str: return " x ".join(str(dim) for dim in shape) if shape else "scalar"1234567891011121314151617181920212223242526272829303132333435363738from __future__ import annotations
from typing import Any
from adam.executor import ToolContext, ToolExecutionErrorfrom adam.model_plugins import ModelPluginRegistry, plugin_function, validate_settings
def _plugin_for_tool(context: ToolContext, mode: str): registry = ModelPluginRegistry(context.root) for plugin in registry.all(): if mode == "training" and plugin.trainer_id == context.tool.id: return plugin if mode == "generation" and plugin.generator_id == context.tool.id: return plugin raise ToolExecutionError(f"No model plugin owns {context.tool.id}.")
def train(context: ToolContext, **settings: Any) -> dict[str, Any]: plugin = _plugin_for_tool(context, "training") errors = validate_settings(plugin.training_settings, settings) if errors: raise ToolExecutionError(" ".join(errors)) function = plugin_function(plugin, "train") if function is None: raise ToolExecutionError(f"{plugin.name} does not implement train().") return function(settings=settings, callbacks=context) or {}
def generate(context: ToolContext, **settings: Any) -> dict[str, Any]: plugin = _plugin_for_tool(context, "generation") errors = validate_settings(plugin.generation_settings, settings) if errors: raise ToolExecutionError(" ".join(errors)) function = plugin_function(plugin, "generate") if function is None: raise ToolExecutionError(f"{plugin.name} does not implement generate().") return function(settings=settings, callbacks=context) or {}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494from __future__ import annotations
import importlibimport importlib.utilimport jsonimport loggingimport pkgutilfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Any, Callable
REQUIRED_INFO_FIELDS = {"name", "version", "category", "description"}SUPPORTED_SETTING_TYPES = { "int", "float", "bool", "choice", "text", "multiline_text", "path", "folder", "slider",}
class ModelPluginError(RuntimeError): pass
@dataclass(frozen=True, slots=True)class ModelPlugin: id: str info: dict[str, Any] training_settings: dict[str, dict[str, Any]] = field(default_factory=dict) generation_settings: dict[str, dict[str, Any]] = field(default_factory=dict) training_tool: dict[str, Any] = field(default_factory=dict) generation_tool: dict[str, Any] = field(default_factory=dict) module_name: str = "" plugin_path: Path | None = None
@property def name(self) -> str: return str(self.info.get("name", self.id))
@property def trainer_id(self) -> str: return str(self.training_tool.get("id") or f"{self.id}_trainer")
@property def generator_id(self) -> str: return str(self.generation_tool.get("id") or f"{self.id}_generator")
class ModelPluginRegistry: """Discovers model plugins and validates their setting schemas."""
def __init__(self, root: Path, logger: logging.Logger | None = None) -> None: self.root = root.resolve() self.logger = logger or logging.getLogger(__name__) self.plugins: dict[str, ModelPlugin] = {} self.errors: list[str] = [] self.discover()
def discover(self) -> None: self.plugins = {} self.errors = [] for module_name in self._candidate_modules(): try: plugin = self._load_module_plugin(module_name) except Exception as exc: message = f"{module_name}: {exc}" self.errors.append(message) self.logger.warning("Model plugin failed to load: %s", message) continue if plugin.id in self.plugins: self.errors.append(f"{module_name}: duplicate model plugin id {plugin.id}") continue self.plugins[plugin.id] = plugin
def _candidate_modules(self) -> list[str | Path]: modules: list[str | Path] = [] try: package = importlib.import_module("adam.model_plugins_builtin") for item in pkgutil.iter_modules(package.__path__, package.__name__ + "."): if not item.ispkg: continue modules.append(item.name + ".manifest") except Exception as exc: self.errors.append(f"adam.model_plugins_builtin: {exc}")
models_dir = self.root / "models" if models_dir.is_dir(): for folder in sorted(models_dir.iterdir()): manifest = folder / "manifest.py" if not folder.is_dir() or not manifest.is_file(): continue modules.append(manifest) return modules
def _load_module_plugin(self, module_name: str | Path) -> ModelPlugin: if isinstance(module_name, Path): fallback_id = module_name.parent.name unique_name = f"adam_user_model_{fallback_id}_{abs(hash(str(module_name.resolve())))}" spec = importlib.util.spec_from_file_location(unique_name, module_name) if spec is None or spec.loader is None: raise ModelPluginError(f"Could not load manifest file: {module_name}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) module_label = str(module_name) else: module = importlib.import_module(module_name) fallback_id = module_name.split(".")[-2] module_label = module_name plugin_id = str(getattr(module, "PLUGIN_ID", "") or fallback_id) info = dict(getattr(module, "MODEL_INFO", {})) missing = REQUIRED_INFO_FIELDS - set(info) if missing: raise ModelPluginError( "MODEL_INFO is missing " + ", ".join(sorted(missing)) ) training_settings = self._validate_schema( dict(getattr(module, "TRAINING_SETTINGS", {})), f"{plugin_id} training", ) generation_settings = self._validate_schema( dict(getattr(module, "GENERATION_SETTINGS", {})), f"{plugin_id} generation", ) plugin_path = Path(getattr(module, "__file__", "")).resolve().parent return ModelPlugin( id=plugin_id, info=info, training_settings=training_settings, generation_settings=generation_settings, training_tool=dict(getattr(module, "TRAINING_TOOL", {})), generation_tool=dict(getattr(module, "GENERATION_TOOL", {})), module_name=module_label, plugin_path=plugin_path, )
@staticmethod def _validate_schema( schema: dict[str, Any], label: str, ) -> dict[str, dict[str, Any]]: clean: dict[str, dict[str, Any]] = {} for key, raw in schema.items(): if not isinstance(raw, dict): raise ModelPluginError(f"{label} setting {key} must be an object") spec = dict(raw) setting_type = str(spec.get("type", "text")) if setting_type not in SUPPORTED_SETTING_TYPES: raise ModelPluginError( f"{label} setting {key} has unsupported type {setting_type}" ) spec["type"] = setting_type spec.setdefault("label", key.replace("_", " ").title()) spec.setdefault("group", "Basic") if setting_type == "choice": options = spec.get("options", []) if not isinstance(options, (list, tuple)) or not options: raise ModelPluginError(f"{label} setting {key} needs options") spec["options"] = list(options) spec.setdefault("default", spec["options"][0]) clean[str(key)] = spec return clean
def get(self, plugin_id: str) -> ModelPlugin: return self.plugins[plugin_id]
def all(self) -> list[ModelPlugin]: return list(self.plugins.values())
def by_trainer(self, trainer: str) -> ModelPlugin | None: return next((plugin for plugin in self.plugins.values() if plugin.id == trainer), None)
def training_schema(self, trainer: str) -> dict[str, dict[str, Any]]: plugin = self.by_trainer(trainer) return plugin.training_settings if plugin else {}
def generation_schema_for_tool(self, tool_id: str) -> dict[str, dict[str, Any]]: for plugin in self.plugins.values(): if plugin.generator_id == tool_id: return plugin.generation_settings return {}
def training_tool_specs(self) -> list[dict[str, Any]]: return [ self._tool_spec(plugin, mode="training") for plugin in self.plugins.values() if plugin.training_tool ]
def generation_tool_specs(self) -> list[dict[str, Any]]: return [ self._tool_spec(plugin, mode="generation") for plugin in self.plugins.values() if plugin.generation_tool ]
@staticmethod def _tool_spec(plugin: ModelPlugin, *, mode: str) -> dict[str, Any]: tool = dict(plugin.training_tool if mode == "training" else plugin.generation_tool) schema = plugin.training_settings if mode == "training" else plugin.generation_settings core_arguments = ( ["dataset_dir", "model_name", "epochs", "output_dir", "resume_from"] if mode == "training" else [ "model_name", "model_path", "prompt", "image_count", "steps", "seed", "sampler", "aspect_ratio", ] ) core_required = ( ["dataset_dir", "model_name", "epochs", "output_dir"] if mode == "training" else ["model_name", "model_path", "image_count", "steps", "seed"] ) defaults = { "id": plugin.trainer_id if mode == "training" else plugin.generator_id, "name": f"{plugin.name} {'Trainer' if mode == 'training' else 'Generator'}", "description": plugin.info.get("description", ""), "category": "Training" if mode == "training" else "Output", "entry_function": "train" if mode == "training" else "generate", "arguments": [*core_arguments, *list(schema)], "required_arguments": [ *core_required, *[key for key, spec in schema.items() if bool(spec.get("required"))], ], "capabilities": ( ["fresh_training", "progress", "pause", "cancel"] if mode == "training" else ["image_generation", "progress", "cancel"] ), "requires_confirmation": mode == "training", "enabled": True, "demo": False, } defaults.update(tool) defaults["arguments"] = list(defaults.get("arguments") or [*core_arguments, *list(schema)]) defaults["required_arguments"] = list(defaults.get("required_arguments") or []) return defaults
def validate_settings( self, trainer: str, values: dict[str, Any], *, mode: str = "training", ) -> list[str]: plugin = self.by_trainer(trainer) if not plugin: return [f"Unknown model plugin: {trainer}"] schema = plugin.training_settings if mode == "training" else plugin.generation_settings return validate_settings(schema, values)
def validate_settings(schema: dict[str, dict[str, Any]], values: dict[str, Any]) -> list[str]: errors: list[str] = [] for key, spec in schema.items(): value = values.get(key, spec.get("default")) label = str(spec.get("label", key)) if spec.get("required") and (value is None or str(value).strip() == ""): errors.append(f"{label} is required.") continue if value in (None, "") and not spec.get("required"): continue setting_type = str(spec.get("type", "text")) try: if setting_type in {"int", "slider"}: if isinstance(value, bool): raise ValueError numeric = int(value) elif setting_type == "float": if isinstance(value, bool): raise ValueError numeric = float(value) else: numeric = None except (TypeError, ValueError): errors.append(f"{label} must be a number.") continue if numeric is not None: if "min" in spec and numeric < float(spec["min"]): errors.append(f"{label} must be at least {spec['min']}.") if "max" in spec and numeric > float(spec["max"]): errors.append(f"{label} must be at most {spec['max']}.") if setting_type == "choice" and "options" in spec and value not in spec["options"]: errors.append(f"{label} must be one of: {', '.join(map(str, spec['options']))}.") if setting_type == "path" and spec.get("must_exist") and not Path(str(value)).expanduser().is_file(): errors.append(f"{label} must point to an existing file.") if setting_type == "folder" and spec.get("must_exist") and not Path(str(value)).expanduser().is_dir(): errors.append(f"{label} must point to an existing folder.") return errors
def load_presets(root: Path, plugin_id: str, mode: str) -> dict[str, dict[str, Any]]: path = root.resolve() / "config" / "model_presets.json" try: payload = json.loads(path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError): return {} presets = payload.get(plugin_id, {}).get(mode, {}) return dict(presets) if isinstance(presets, dict) else {}
def save_preset( root: Path, plugin_id: str, mode: str, name: str, settings: dict[str, Any],) -> None: path = root.resolve() / "config" / "model_presets.json" try: payload = json.loads(path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError): payload = {} payload.setdefault(plugin_id, {}).setdefault(mode, {})[name] = settings temporary = path.with_suffix(".tmp") temporary.write_text(json.dumps(payload, indent=2, sort_keys=True), encoding="utf-8") temporary.replace(path)
def plugin_function(plugin: ModelPlugin, function_name: str) -> Callable[..., Any] | None: if plugin.module_name.endswith("manifest.py"): spec = importlib.util.spec_from_file_location( f"adam_user_model_{plugin.id}_{abs(hash(plugin.module_name))}", plugin.module_name, ) if spec is None or spec.loader is None: return None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) else: module = importlib.import_module(plugin.module_name) function = getattr(module, function_name, None) return function if callable(function) else None
def safe_plugin_id(name: str) -> str: cleaned = "".join( character.lower() if character.isalnum() else "_" for character in name.strip() ) cleaned = "_".join(part for part in cleaned.split("_") if part) return cleaned[:48] or "my_model"
def scaffold_model_plugin( root: Path, *, plugin_id: str, name: str, architecture: str = "custom", output_type: str = "image", include_training: bool = True, include_generation: bool = True,) -> Path: """Create a simple user-editable model plugin folder.""" plugin_id = safe_plugin_id(plugin_id) if plugin_id in {"ddpm", "flow", "lora", "model_template"}: raise ModelPluginError("Choose a plugin id that does not conflict with a built-in model.") folder = root.resolve() / "models" / plugin_id if folder.exists(): raise ModelPluginError(f"A model plugin folder already exists: {folder}") folder.mkdir(parents=True) (folder / "__init__.py").write_text( f'"""ADAM model plugin: {name}."""\n', encoding="utf-8", ) (folder / "manifest.py").write_text( _manifest_template( plugin_id=plugin_id, name=name, architecture=architecture, output_type=output_type, include_training=include_training, include_generation=include_generation, ), encoding="utf-8", ) (folder / "model.py").write_text(_model_template(), encoding="utf-8") if include_training: (folder / "trainer.py").write_text(_trainer_template(), encoding="utf-8") if include_generation: (folder / "generator.py").write_text(_generator_template(), encoding="utf-8") return folder
def _manifest_template( *, plugin_id: str, name: str, architecture: str, output_type: str, include_training: bool, include_generation: bool,) -> str: plugin_id_json = json.dumps(plugin_id) name_json = json.dumps(name) architecture_json = json.dumps(architecture) output_type_json = json.dumps(output_type) training_tool = ( "{\n" f' "id": "{plugin_id}_trainer",\n' f' "name": {json.dumps(name + " Trainer")},\n' f' "backend": {{"type": "python", "module": "models.{plugin_id}.trainer", "function": "train"}},\n' "}" if include_training else "{}" ) generation_tool = ( "{\n" f' "id": "{plugin_id}_generator",\n' f' "name": {json.dumps(name + " Generator")},\n' f' "model_trainers": ["{plugin_id}"],\n' f' "backend": {{"type": "python", "module": "models.{plugin_id}.generator", "function": "generate"}},\n' "}" if include_generation else "{}" ) return f'''PLUGIN_ID = {plugin_id_json}
MODEL_INFO = {{ "name": {name_json}, "version": "0.1", "category": "Image Generation", "description": {json.dumps("Describe what " + name + " trains or generates.")}, "architecture": {architecture_json}, "status": "experimental", "output_type": {output_type_json},}}
TRAINING_SETTINGS = {{ "resolution": {{"label": "Resolution", "type": "choice", "options": [64, 128, 256, 384, 512], "default": 256, "group": "Basic"}}, "batch_size": {{"label": "Batch size", "type": "int", "default": 1, "min": 1, "max": 64, "group": "Basic"}}, "learning_rate": {{"label": "Learning rate", "type": "float", "default": 0.0001, "min": 0.0000001, "max": 0.1, "decimals": 7, "group": "Optimization"}}, "mixed_precision": {{"label": "Precision", "type": "choice", "options": ["fp16", "no"], "default": "fp16", "group": "Optimization"}}, "preview_enabled": {{"label": "Generate previews while training", "type": "bool", "default": True, "group": "Preview"}}, "preview_every": {{"label": "Preview interval", "type": "int", "default": 5, "min": 1, "max": 100000, "group": "Preview"}}, "preview_prompt": {{"label": "Preview prompt", "type": "text", "default": "", "group": "Preview"}}, "preview_seed": {{"label": "Preview seed", "type": "int", "default": 123456789, "min": 0, "max": 2147483647, "group": "Preview"}},}}
GENERATION_SETTINGS = {{ "prompt": {{"label": "Prompt", "type": "multiline_text", "default": "", "group": "Prompt"}}, "image_count": {{"label": "Images", "type": "int", "default": 1, "min": 1, "max": 48, "group": "Generation"}}, "steps": {{"label": "Steps", "type": "int", "default": 30, "min": 1, "max": 500, "group": "Generation"}}, "seed": {{"label": "Seed", "type": "int", "default": 0, "min": 0, "max": 2147483647, "group": "Generation"}},}}
TRAINING_TOOL = {training_tool}
GENERATION_TOOL = {generation_tool}'''
def _model_template() -> str: return '''from __future__ import annotations
from typing import Any
def load_model(model_path: str, settings: dict[str, Any] | None = None) -> Any: """Load your model or inference pipeline here.""" raise NotImplementedError("Add your model loading code.")'''
def _trainer_template() -> str: return '''from __future__ import annotations
from typing import Any
def train(context, **settings: Any) -> dict[str, Any]: """Train the model and report progress back to ADAM.""" context.log("Replace this with real training code.") context.progress(100, "Training placeholder complete") return {}'''
def _generator_template() -> str: return '''from __future__ import annotations
from typing import Any
def generate(context, **settings: Any) -> dict[str, Any]: """Generate outputs and report progress back to ADAM.""" context.log("Replace this with real generation code.") context.progress(100, "Generation placeholder complete") return {}'''1"""Built-in model plugin manifests shipped with ADAM."""1"""DDPM model plugin."""1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556PLUGIN_ID = "ddpm"
MODEL_INFO = { "name": "DDPM", "version": "1.0", "category": "Image Generation", "description": "Denoising Diffusion Probabilistic Model image trainer and generator.", "architecture": "diffusion", "status": "stable", "output_type": "image", "capabilities": ["fresh_training", "resume_training", "image_generation", "smart_generation", "live_preview"], "input_formats": ["image folder"], "output_formats": ["diffusers pipeline", "checkpoint folder", "png preview"], "hardware": {"recommended_vram_gb": 6, "recommended_system_ram_gb": 16}, "vram_behavior": {"scales_with": ["resolution", "batch_size"], "estimate": "Moderate; batch size should drop quickly above 256px."},}
TRAINING_SETTINGS = { "resolution": {"label": "Resolution", "type": "choice", "options": [64, 128, 256, 384, 512], "default": 128, "group": "Basic"}, "batch_size": {"label": "Batch size", "type": "int", "default": 1, "min": 1, "max": 64, "group": "Basic"}, "learning_rate": {"label": "Learning rate", "type": "float", "default": 0.0001, "min": 0.0000001, "max": 0.1, "decimals": 7, "step": 0.00005, "group": "Optimization"}, "gradient_accumulation_steps": {"label": "Gradient accumulation", "type": "int", "default": 1, "min": 1, "max": 64, "group": "Optimization"}, "dataloader_num_workers": {"label": "Loader workers", "type": "int", "default": 4, "min": 0, "max": 16, "group": "Dataset"}, "mixed_precision": {"label": "Precision", "type": "choice", "options": ["fp16", "no"], "default": "fp16", "group": "Optimization"}, "save_every": {"label": "Save every", "type": "int", "default": 10, "min": 1, "max": 1000, "group": "Checkpoints"}, "preview_steps": {"label": "Preview steps", "type": "int", "default": 50, "min": 1, "max": 500, "group": "Preview"}, "training_intensity": {"label": "Training intensity", "type": "slider", "default": 100, "min": 10, "max": 100, "group": "Advanced", "advanced": True}, "completed_epochs": {"label": "Completed epochs", "type": "int", "default": 0, "min": 0, "max": 100000, "group": "Internal", "advanced": True}, "preview_enabled": {"label": "Generate previews while training", "type": "bool", "default": True, "group": "Preview"}, "preview_every": {"label": "Preview interval", "type": "int", "default": 5, "min": 1, "max": 100000, "group": "Preview"}, "preview_prompt": {"label": "Preview prompt", "type": "text", "default": "", "group": "Preview"}, "preview_seed": {"label": "Preview seed", "type": "int", "default": 123456789, "min": 0, "max": 2147483647, "group": "Preview"},}
GENERATION_SETTINGS = { "prompt": {"label": "Creative note", "type": "multiline_text", "default": "", "group": "Generation"}, "image_count": {"label": "Images", "type": "int", "default": 1, "min": 1, "max": 48, "group": "Generation"}, "steps": {"label": "Sampling steps", "type": "int", "default": 50, "min": 5, "max": 500, "group": "Generation"}, "sampler": {"label": "Sampler", "type": "choice", "options": ["DDIM", "DDPM"], "default": "DDIM", "group": "Generation"}, "aspect_ratio": {"label": "Aspect ratio", "type": "choice", "options": ["1:1 (Square)", "16:9 (Widescreen)", "9:16 (Portrait)", "4:3 (Classic)", "3:4 (Portrait Classic)", "3:2 (Photo)", "2:3 (Portrait Photo)"], "default": "1:1 (Square)", "group": "Generation"}, "seed": {"label": "Seed", "type": "int", "default": 0, "min": 0, "max": 2147483647, "group": "Generation"}, "reference_image": {"label": "Reference image", "type": "path", "default": "", "group": "Reference"}, "reference_strength": {"label": "Reference strength", "type": "slider", "default": 65, "min": 0, "max": 100, "group": "Reference"}, "width": {"label": "Custom width", "type": "int", "default": 0, "min": 0, "max": 2048, "group": "Advanced", "advanced": True}, "height": {"label": "Custom height", "type": "int", "default": 0, "min": 0, "max": 2048, "group": "Advanced", "advanced": True}, "preview_interval": {"label": "Steps per preview", "type": "int", "default": 0, "min": 0, "max": 500, "group": "Preview"}, "smart_generation": {"label": "Smart Generation", "type": "bool", "default": False, "group": "Smart Generation"}, "smart_wanted_results": {"label": "Wanted results", "type": "int", "default": 8, "min": 1, "max": 48, "group": "Smart Generation"}, "smart_max_candidates": {"label": "Maximum candidates", "type": "int", "default": 32, "min": 1, "max": 256, "group": "Smart Generation"}, "smart_min_score": {"label": "Minimum score", "type": "float", "default": 0.7, "min": 0, "max": 1, "group": "Smart Generation"}, "smart_mode": {"label": "Selection mode", "type": "choice", "options": ["threshold", "top_n"], "default": "threshold", "group": "Smart Generation"}, "smart_keep_rejected": {"label": "Keep rejected candidates", "type": "bool", "default": True, "group": "Smart Generation"},}
TRAINING_TOOL = {"id": "ddpm_trainer"}GENERATION_TOOL = {"id": "ddpm_generator"}1"""Flow Matching model plugin."""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051PLUGIN_ID = "flow"
MODEL_INFO = { "name": "Flow Matching", "version": "1.0", "category": "Image Generation", "description": "Rectified Flow image trainer and generator.", "architecture": "rectified_flow", "status": "stable", "output_type": "image", "capabilities": ["fresh_training", "resume_training", "image_generation", "smart_generation", "live_preview"], "input_formats": ["image folder"], "output_formats": ["diffusers unet folder", "flow metadata", "png preview"], "hardware": {"recommended_vram_gb": 8, "recommended_system_ram_gb": 16}, "vram_behavior": {"scales_with": ["resolution", "batch_size"], "estimate": "Moderate to high; flow runs usually want smaller batches at 512px."},}
TRAINING_SETTINGS = { "resolution": {"label": "Resolution", "type": "choice", "options": [64, 128, 256, 384, 512], "default": 256, "group": "Basic"}, "batch_size": {"label": "Batch size", "type": "int", "default": 8, "min": 1, "max": 64, "group": "Basic"}, "learning_rate": {"label": "Learning rate", "type": "float", "default": 0.0002, "min": 0.0000001, "max": 0.1, "decimals": 7, "step": 0.00005, "group": "Optimization"}, "gradient_accumulation": {"label": "Gradient accumulation", "type": "int", "default": 1, "min": 1, "max": 64, "group": "Optimization"}, "workers": {"label": "Loader workers", "type": "int", "default": 4, "min": 0, "max": 16, "group": "Dataset"}, "mixed_precision": {"label": "Precision", "type": "choice", "options": ["fp16", "no"], "default": "fp16", "group": "Optimization"}, "save_every": {"label": "Save every", "type": "int", "default": 10, "min": 1, "max": 1000, "group": "Checkpoints"}, "preview_steps": {"label": "Preview steps", "type": "int", "default": 10, "min": 1, "max": 500, "group": "Preview"}, "gradient_checkpointing": {"label": "Gradient checkpointing", "type": "bool", "default": False, "group": "Advanced", "advanced": True}, "preview_enabled": {"label": "Generate previews while training", "type": "bool", "default": True, "group": "Preview"}, "preview_every": {"label": "Preview interval", "type": "int", "default": 5, "min": 1, "max": 100000, "group": "Preview"}, "preview_prompt": {"label": "Preview prompt", "type": "text", "default": "", "group": "Preview"}, "preview_seed": {"label": "Preview seed", "type": "int", "default": 123456789, "min": 0, "max": 2147483647, "group": "Preview"},}
GENERATION_SETTINGS = { "prompt": {"label": "Creative note", "type": "multiline_text", "default": "", "group": "Generation"}, "image_count": {"label": "Images", "type": "int", "default": 1, "min": 1, "max": 48, "group": "Generation"}, "steps": {"label": "ODE steps", "type": "int", "default": 20, "min": 1, "max": 200, "group": "Generation"}, "sampler": {"label": "Method", "type": "choice", "options": ["Heun", "Euler"], "default": "Heun", "group": "Generation"}, "aspect_ratio": {"label": "Aspect ratio", "type": "choice", "options": ["1:1 (Square)", "4:3 (Landscape)", "3:4 (Portrait)", "3:2 (Landscape)", "2:3 (Portrait)", "16:9 (Widescreen)", "9:16 (Vertical)"], "default": "1:1 (Square)", "group": "Generation"}, "seed": {"label": "Seed", "type": "int", "default": 0, "min": 0, "max": 2147483647, "group": "Generation"}, "preview_interval": {"label": "Steps per preview", "type": "int", "default": 0, "min": 0, "max": 500, "group": "Preview"}, "smart_generation": {"label": "Smart Generation", "type": "bool", "default": False, "group": "Smart Generation"}, "smart_wanted_results": {"label": "Wanted results", "type": "int", "default": 8, "min": 1, "max": 48, "group": "Smart Generation"}, "smart_max_candidates": {"label": "Maximum candidates", "type": "int", "default": 32, "min": 1, "max": 256, "group": "Smart Generation"}, "smart_min_score": {"label": "Minimum score", "type": "float", "default": 0.7, "min": 0, "max": 1, "group": "Smart Generation"}, "smart_mode": {"label": "Selection mode", "type": "choice", "options": ["threshold", "top_n"], "default": "threshold", "group": "Smart Generation"}, "smart_keep_rejected": {"label": "Keep rejected candidates", "type": "bool", "default": True, "group": "Smart Generation"},}
TRAINING_TOOL = {"id": "flow_trainer"}GENERATION_TOOL = {"id": "flow_generator"}1"""Copyable model plugin template."""123456789101112131415161718192021222324252627282930PLUGIN_ID = "model_template"
MODEL_INFO = { "name": "Model Template", "version": "0.1", "category": "Template", "description": "Example manifest for adding a new ADAM model plugin.", "status": "example",}
TRAINING_SETTINGS = { "dataset_dir": {"label": "Dataset folder", "type": "folder", "required": True, "group": "Dataset"}, "epochs": {"label": "Epochs", "type": "int", "default": 10, "min": 1, "max": 100000, "group": "Basic"}, "learning_rate": {"label": "Learning rate", "type": "float", "default": 0.0001, "min": 0.0000001, "max": 0.1, "group": "Optimization"},}
GENERATION_SETTINGS = { "model_path": {"label": "Model file or folder", "type": "path", "required": True, "group": "Model Loading"}, "seed": {"label": "Seed", "type": "int", "default": 0, "min": 0, "max": 2147483647, "group": "Generation"},}
# A real plugin can point to its own backend:# TRAINING_TOOL = {# "backend": {"type": "python", "module": "models.my_model.trainer", "function": "train"},# }# GENERATION_TOOL = {# "backend": {"type": "python", "module": "models.my_model.generator", "function": "generate"},# }TRAINING_TOOL = {}GENERATION_TOOL = {}1"""Oasis action-conditioned world model plugin."""12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970PLUGIN_ID = "oasis"
MODEL_INFO = { "name": "Oasis Action World Model", "version": "1.0", "category": "Playable World Models", "description": "Action-conditioned playable world model trainer for gameplay frame sequences.", "architecture": "action_conditioned_rectified_flow_video", "status": "experimental", "output_type": "playable_world", "capabilities": ["fresh_training", "resume_training", "playable_inference", "live_preview"], "input_formats": ["Oasis action dataset folder", "semicolon-separated Oasis dataset folders"], "output_formats": ["action_flow_model_info.json", "diffusers unet folder", "png preview"], "hardware": {"recommended_vram_gb": 12, "recommended_system_ram_gb": 32}, "vram_behavior": { "scales_with": ["resolution", "batch_size", "sequence_context"], "estimate": "High; 256x144 with batch 2 is the conservative RTX 3060 starting point.", },}
TRAINING_SETTINGS = { "resolution": {"label": "Resolution", "type": "choice", "options": ["128x72", "256x144", "384x216", "512x288"], "default": "256x144", "group": "Basic"}, "batch_size": {"label": "Batch size", "type": "int", "default": 2, "min": 1, "max": 16, "group": "Basic"}, "learning_rate": {"label": "Learning rate", "type": "float", "default": 0.00002, "min": 0.0000001, "max": 0.01, "decimals": 7, "step": 0.00001, "group": "Optimization"}, "workers": {"label": "Loader workers", "type": "int", "default": 2, "min": 0, "max": 8, "group": "Dataset"}, "mixed_precision": {"label": "Precision", "type": "choice", "options": ["fp32", "fp16", "no"], "default": "fp32", "group": "Optimization"}, "gradient_accumulation": {"label": "Gradient accumulation", "type": "int", "default": 1, "min": 1, "max": 16, "group": "Optimization"}, "frame_gap": {"label": "Prediction horizon", "type": "int", "default": 3, "min": 1, "max": 60, "group": "Sequence"}, "sequence_context": {"label": "Context length", "type": "int", "default": 1, "min": 1, "max": 32, "group": "Sequence"}, "action_aggregation": {"label": "Action aggregation", "type": "choice", "options": ["window", "mean", "last"], "default": "window", "group": "Sequence"}, "validation_split": {"label": "Validation split", "type": "float", "default": 0.1, "min": 0.01, "max": 0.5, "decimals": 3, "step": 0.01, "group": "Dataset"}, "validation_batches": {"label": "Validation batches", "type": "int", "default": 8, "min": 0, "max": 128, "group": "Dataset"}, "save_every": {"label": "Save every", "type": "int", "default": 5, "min": 1, "max": 1000, "group": "Checkpoints"}, "preview_enabled": {"label": "Generate previews while training", "type": "bool", "default": True, "group": "Preview"}, "preview_every": {"label": "Preview interval", "type": "int", "default": 5, "min": 1, "max": 100000, "group": "Preview"}, "preview_steps": {"label": "Preview steps", "type": "int", "default": 1, "min": 1, "max": 50, "group": "Preview"}, "seed": {"label": "Random seed", "type": "int", "default": 1234, "min": 0, "max": 2147483647, "group": "Reproducibility"}, "base_model": {"label": "Base video model", "type": "folder", "default": "", "group": "Checkpoints", "advanced": True}, "condition_noise": {"label": "Condition noise", "type": "float", "default": 0.03, "min": 0.0, "max": 0.5, "decimals": 4, "step": 0.01, "group": "Advanced", "advanced": True}, "temporal_loss_weight": {"label": "Temporal loss weight", "type": "float", "default": 0.1, "min": 0.0, "max": 10.0, "decimals": 3, "step": 0.05, "group": "Advanced", "advanced": True}, "motion_loss_weight": {"label": "Motion loss weight", "type": "float", "default": 2.0, "min": 0.0, "max": 10.0, "decimals": 3, "step": 0.25, "group": "Advanced", "advanced": True}, "action_input_scale": {"label": "Action input scale", "type": "float", "default": 8.0, "min": 0.1, "max": 32.0, "decimals": 3, "step": 0.5, "group": "Advanced", "advanced": True}, "neutral_action_dropout": {"label": "Neutral action dropout", "type": "float", "default": 0.15, "min": 0.0, "max": 0.9, "decimals": 3, "step": 0.05, "group": "Advanced", "advanced": True}, "action_contrast_weight": {"label": "Action contrast weight", "type": "float", "default": 0.35, "min": 0.0, "max": 5.0, "decimals": 3, "step": 0.05, "group": "Advanced", "advanced": True}, "action_contrast_margin": {"label": "Action contrast margin", "type": "float", "default": 0.02, "min": 0.0, "max": 1.0, "decimals": 4, "step": 0.005, "group": "Advanced", "advanced": True}, "gradient_checkpointing": {"label": "Gradient checkpointing", "type": "bool", "default": False, "group": "Advanced", "advanced": True}, "balance_actions": {"label": "Balance rare actions", "type": "bool", "default": False, "group": "Advanced", "advanced": True},}
GENERATION_SETTINGS = { "starting_frame": {"label": "Starting frame", "type": "path", "default": "", "group": "Player"}, "seed": {"label": "Seed", "type": "int", "default": 0, "min": 0, "max": 2147483647, "group": "Player"},}
TRAINING_TOOL = { "id": "oasis_trainer", "name": "Oasis Action World Model Trainer", "backend": {"type": "python", "module": "adam.tools.oasis_adapter", "function": "train_oasis"}, "capabilities": ["fresh_training", "resume_training", "progress", "pause", "cancel", "live_preview"],}
GENERATION_TOOL = { "id": "oasis_player", "name": "Oasis Playable Inference", "model_trainers": ["oasis"], "arguments": ["model_name", "model_path", "starting_frame", "seed"], "required_arguments": ["model_path"], "capabilities": ["playable_inference", "keyboard_actions", "progress", "cancel"], "backend": {"type": "python", "module": "adam.tools.oasis_adapter", "function": "launch_oasis_player"},}1"""SDXL LoRA model plugin."""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960PLUGIN_ID = "lora"
MODEL_INFO = { "name": "SDXL LoRA", "version": "1.0", "category": "Image Generation", "description": "Stable Diffusion XL LoRA adapter training and base-model plus adapter generation.", "architecture": "sdxl_lora", "status": "stable", "output_type": "image", "dependencies": ["diffusers", "safetensors"], "capabilities": ["fresh_training", "resume_training", "lora_adapter", "image_generation", "reference_image"], "input_formats": ["captioned image folder", "SDXL checkpoint"], "output_formats": ["safetensors", "png generation"], "hardware": {"recommended_vram_gb": 8, "recommended_system_ram_gb": 16}, "vram_behavior": {"scales_with": ["base_model_size", "resolution", "batch_size"], "estimate": "High; SDXL LoRA usually starts safely at batch 1 on 8-12 GB GPUs."},}
TRAINING_SETTINGS = { "base_model": {"label": "Base model", "type": "path", "default": "", "required": True, "must_exist": True, "group": "Basic"}, "trigger_word": {"label": "Trigger word", "type": "text", "default": "", "group": "LoRA"}, "resolution": {"label": "Resolution", "type": "choice", "options": [512, 768, 1024], "default": 1024, "group": "Basic"}, "rank": {"label": "Rank", "type": "int", "default": 16, "min": 1, "max": 256, "group": "LoRA"}, "alpha": {"label": "Alpha", "type": "int", "default": 16, "min": 1, "max": 256, "group": "LoRA"}, "learning_rate": {"label": "Learning rate", "type": "float", "default": 0.0001, "min": 0.0000001, "max": 0.01, "decimals": 7, "step": 0.00005, "group": "Optimization"}, "batch_size": {"label": "Batch size", "type": "int", "default": 1, "min": 1, "max": 16, "group": "Basic"}, "gradient_accumulation_steps": {"label": "Gradient accumulation", "type": "int", "default": 1, "min": 1, "max": 64, "group": "Optimization"}, "mixed_precision": {"label": "Precision", "type": "choice", "options": ["fp16", "bf16", "no"], "default": "fp16", "group": "Optimization"}, "caption_extension": {"label": "Caption extension", "type": "choice", "options": [".txt", ".caption"], "default": ".txt", "group": "Dataset"}, "save_every": {"label": "Save every", "type": "int", "default": 10, "min": 1, "max": 1000, "group": "Checkpoints"}, "optimizer": {"label": "Optimizer", "type": "choice", "options": ["AdamW", "AdamW8bit"], "default": "AdamW", "group": "Optimization", "advanced": True}, "gradient_clip_norm": {"label": "Gradient clipping", "type": "float", "default": 1.0, "min": 0.0, "max": 10.0, "decimals": 3, "step": 0.1, "group": "Advanced", "advanced": True}, "preview_enabled": {"label": "Generate previews while training", "type": "bool", "default": True, "group": "Preview"}, "preview_every": {"label": "Preview interval", "type": "int", "default": 5, "min": 1, "max": 100000, "group": "Preview"}, "preview_prompt": {"label": "Preview prompt", "type": "text", "default": "", "group": "Preview"}, "preview_seed": {"label": "Preview seed", "type": "int", "default": 123456789, "min": 0, "max": 2147483647, "group": "Preview"},}
GENERATION_SETTINGS = { "base_model_path": {"label": "Base model", "type": "path", "default": "", "required": True, "must_exist": True, "group": "Model Loading"}, "model_path": {"label": "LoRA adapter", "type": "path", "default": "", "required": True, "must_exist": True, "group": "Model Loading"}, "prompt": {"label": "Prompt", "type": "multiline_text", "default": "", "required": True, "group": "Prompt"}, "negative_prompt": {"label": "Negative prompt", "type": "multiline_text", "default": "", "group": "Prompt"}, "lora_strength": {"label": "LoRA strength", "type": "float", "default": 1.0, "min": 0.0, "max": 2.0, "decimals": 2, "step": 0.05, "group": "Generation"}, "image_count": {"label": "Images", "type": "int", "default": 1, "min": 1, "max": 48, "group": "Generation"}, "steps": {"label": "Steps", "type": "int", "default": 30, "min": 1, "max": 150, "group": "Generation"}, "cfg_scale": {"label": "CFG scale", "type": "float", "default": 7.0, "min": 0.1, "max": 30.0, "decimals": 2, "step": 0.5, "group": "Generation"}, "sampler": {"label": "Sampler", "type": "choice", "options": ["DPM++ 2M", "DPM++ SDE", "Euler", "Euler a", "DDIM"], "default": "DPM++ 2M", "group": "Generation"}, "aspect_ratio": {"label": "Aspect ratio", "type": "choice", "options": ["1:1 (Square)", "4:3 (Landscape)", "3:4 (Portrait)", "3:2 (Landscape)", "2:3 (Portrait)", "16:9 (Widescreen)", "9:16 (Vertical)"], "default": "1:1 (Square)", "group": "Generation"}, "width": {"label": "Width", "type": "int", "default": 1024, "min": 256, "max": 2048, "group": "Generation"}, "height": {"label": "Height", "type": "int", "default": 1024, "min": 256, "max": 2048, "group": "Generation"}, "seed": {"label": "Seed", "type": "int", "default": 0, "min": 0, "max": 2147483647, "group": "Generation"}, "reference_image": {"label": "Reference image", "type": "path", "default": "", "group": "Reference"}, "denoise_strength": {"label": "Denoise strength", "type": "float", "default": 0.45, "min": 0.0, "max": 1.0, "decimals": 2, "step": 0.05, "group": "Reference"}, "prompt_weighting": {"label": "Use prompt weights", "type": "bool", "default": True, "group": "Advanced", "advanced": True}, "preview_interval": {"label": "Steps per preview", "type": "int", "default": 0, "min": 0, "max": 500, "group": "Preview"},}
TRAINING_TOOL = {"id": "lora_trainer"}GENERATION_TOOL = {"id": "lora_generator"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293from __future__ import annotations
from dataclasses import asdict, dataclass, fieldfrom typing import Any
from adam.model_plugins import ModelPlugin, ModelPluginRegistry
@dataclass(frozen=True, slots=True)class ModelProfile: """Normalized model profile built from ADAM's plugin manifests."""
id: str name: str category: str architecture: str version: str description: str status: str = "experimental" output_type: str = "image" training: dict[str, dict[str, Any]] = field(default_factory=dict) generation: dict[str, dict[str, Any]] = field(default_factory=dict) trainer_module: str = "" generator_module: str = "" trainer_tool: str = "" generator_tool: str = "" capabilities: list[str] = field(default_factory=list) hardware: dict[str, Any] = field(default_factory=dict) vram_behavior: dict[str, Any] = field(default_factory=dict) input_formats: list[str] = field(default_factory=list) output_formats: list[str] = field(default_factory=list)
def to_dict(self) -> dict[str, Any]: return asdict(self)
def profile_from_plugin(plugin: ModelPlugin) -> ModelProfile: info = dict(plugin.info) training_tool = dict(plugin.training_tool) generation_tool = dict(plugin.generation_tool) capabilities = list( dict.fromkeys( [ *info.get("capabilities", []), *training_tool.get("capabilities", []), *generation_tool.get("capabilities", []), ] ) ) trainer_backend = dict(training_tool.get("backend", {})) generator_backend = dict(generation_tool.get("backend", {})) return ModelProfile( id=plugin.id, name=str(info.get("name", plugin.name)), category=str(info.get("category", "")), architecture=str(info.get("architecture", plugin.id)), version=str(info.get("version", "")), description=str(info.get("description", "")), status=str(info.get("status", "experimental")), output_type=str(info.get("output_type", "image")), training=plugin.training_settings, generation=plugin.generation_settings, trainer_module=str(trainer_backend.get("module", "")), generator_module=str(generator_backend.get("module", "")), trainer_tool=plugin.trainer_id if plugin.training_settings else "", generator_tool=plugin.generator_id if plugin.generation_settings else "", capabilities=capabilities, hardware=dict(info.get("hardware", {})), vram_behavior=dict(info.get("vram_behavior", {})), input_formats=list(info.get("input_formats", [])), output_formats=list(info.get("output_formats", [])), )
class ModelProfileRegistry: """Read-only view over plugin manifests for UI and automation features."""
def __init__(self, plugins: ModelPluginRegistry) -> None: self.plugins = plugins
def all(self) -> list[ModelProfile]: return [ profile_from_plugin(plugin) for plugin in self.plugins.all() if plugin.info.get("category") != "Template" ]
def get(self, profile_id: str) -> ModelProfile | None: plugin = self.plugins.by_trainer(profile_id) return profile_from_plugin(plugin) if plugin else None
def as_catalog(self) -> list[dict[str, Any]]: return [profile.to_dict() for profile in self.all()]13 unmodified lines14151617181953 unmodified lines73747576777810 unmodified lines899091929394959613 unmodified linesclass JobStatus(str, Enum):DRAFT = "Draft"AWAITING_CONFIRMATION = "Awaiting confirmation"QUEUED = "Queued"RUNNING = "Running"PAUSED = "Paused"53 unmodified linesprogress: int = 0current_step: int = -1created_at: str = field(default_factory=utc_now)started_at: str | None = Noneended_at: str | None = Noneoutput_folder: str | None = None10 unmodified linespreview_total: int = 0preview_image_index: int = 0preview_image_count: int = 0atlas_report: dict[str, Any] = field(default_factory=dict)nova_report: dict[str, Any] = field(default_factory=dict)def to_dict(self) -> dict[str, Any]:payload = asdict(self)13 unmodified lines1415161718192053 unmodified lines7475767778798010 unmodified lines91929394959697989910010110210310410513 unmodified linesclass JobStatus(str, Enum):DRAFT = "Draft"AWAITING_CONFIRMATION = "Awaiting confirmation"SCHEDULED = "Scheduled"QUEUED = "Queued"RUNNING = "Running"PAUSED = "Paused"53 unmodified linesprogress: int = 0current_step: int = -1created_at: str = field(default_factory=utc_now)scheduled_for: str | None = Nonestarted_at: str | None = Noneended_at: str | None = Noneoutput_folder: str | None = None10 unmodified linespreview_total: int = 0preview_image_index: int = 0preview_image_count: int = 0eta_seconds: int | None = Noneestimated_completion_at: str | None = Noneprogress_current: int = 0progress_total: int = 0progress_rate: float = 0.0progress_unit: str = "step"atlas_report: dict[str, Any] = field(default_factory=dict)nova_report: dict[str, Any] = field(default_factory=dict)metadata: dict[str, Any] = field(default_factory=dict)def to_dict(self) -> dict[str, Any]:payload = asdict(self)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215from __future__ import annotations
import jsonimport refrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Any
from PIL import Image
BINARY_ACTIONS = { "w", "a", "s", "d", "jump", "arrow_up", "arrow_left", "arrow_down", "arrow_right", "enter", "shift", "ctrl", "alt", "tab", "escape", "q", "e", "r", "f", "z", "x", "c", "v", "key_1", "key_2", "key_3", "key_4", "mouse_left", "mouse_middle", "mouse_right",}CONTINUOUS_ACTIONS = {"mouse_dx", "mouse_dy", "zoom"}DERIVED_ACTIONS = { "move_x", "move_y", "right_mouse", "camera_active", "camera_yaw_delta_degrees", "camera_pitch_delta_degrees", "mouse_raw_dx", "mouse_raw_dy",}SUPPORTED_ACTIONS = BINARY_ACTIONS | CONTINUOUS_ACTIONS | DERIVED_ACTIONSREQUIRED_CANONICAL_ACTIONS = {"w", "a", "s", "d", "jump"}IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp"}METADATA_FIELDS = { "session_id", "session_started_at", "frame_index", "filename", "timestamp_seconds", "camera_encoding",}
@dataclass(slots=True)class OasisDatasetReport: dataset_folders: list[str] = field(default_factory=list) frames: int = 0 metadata_rows: int = 0 valid_rows: int = 0 valid_transitions: int = 0 sessions: int = 0 resolution: str = "" action_counts: dict[str, int] = field(default_factory=dict) errors: list[str] = field(default_factory=list) warnings: list[str] = field(default_factory=list)
@property def ok(self) -> bool: return not self.errors
def dataset_directories(value: str | list[str] | tuple[str, ...]) -> list[Path]: entries = value if isinstance(value, (list, tuple)) else str(value or "").split(";") directories: list[Path] = [] for entry in entries: text = str(entry).strip().strip('"') if not text: continue path = Path(text).expanduser() if path not in directories: directories.append(path) return directories
def _numeric_frame_index(path: Path) -> int | None: match = re.search(r"frame_(\d+)", path.stem, re.I) or re.search(r"(\d+)", path.stem) return int(match.group(1)) if match else None
def validate_oasis_dataset(value: str | list[str] | tuple[str, ...], *, frame_gap: int = 1) -> OasisDatasetReport: report = OasisDatasetReport() frame_gap = max(1, int(frame_gap)) directories = dataset_directories(value) if not directories: report.errors.append("Select at least one Oasis action dataset folder.") return report seen_resolution: tuple[int, int] | None = None action_counts = {name: 0 for name in sorted(REQUIRED_CANONICAL_ACTIONS | CONTINUOUS_ACTIONS)} transition_total = 0 session_ids: set[str] = set()
for directory in directories: resolved = directory.resolve() report.dataset_folders.append(str(resolved)) if not directory.is_dir(): report.errors.append(f"Dataset folder does not exist: {directory}") continue frames_dir = directory / "frames" actions_path = directory / "actions.jsonl" if not frames_dir.is_dir(): report.errors.append(f"{directory} is missing a frames folder.") continue if not actions_path.is_file(): report.errors.append(f"{directory} is missing actions.jsonl.") continue frame_files = sorted(path for path in frames_dir.iterdir() if path.is_file() and path.suffix.casefold() in IMAGE_EXTENSIONS) report.frames += len(frame_files) if not frame_files: report.errors.append(f"{directory} has no image frames.") indexed_frames = [index for index in (_numeric_frame_index(path) for path in frame_files) if index is not None] frame_paths_by_index: dict[int, list[Path]] = {} for frame_file in frame_files: index = _numeric_frame_index(frame_file) if index is not None: frame_paths_by_index.setdefault(index, []).append(frame_file) if indexed_frames: gaps = [ (left, right) for left, right in zip(indexed_frames, indexed_frames[1:]) if right != left + 1 ] if gaps: report.warnings.append(f"{directory} has frame ordering gaps such as {gaps[0][0]} to {gaps[0][1]}; invalid transitions will be skipped.") rows_by_session: dict[str, list[dict[str, Any]]] = {} seen_keys: set[tuple[str, int]] = set() for line_number, line in enumerate(actions_path.read_text(encoding="utf-8").splitlines(), 1): line = line.strip() if not line: continue report.metadata_rows += 1 try: row = json.loads(line) except json.JSONDecodeError: report.errors.append(f"{actions_path.name} line {line_number} is not valid JSON.") continue filename = str(row.get("filename", "")).strip() if not filename: report.errors.append(f"{actions_path.name} line {line_number} has no frame filename.") continue frame_path = frames_dir / filename if not frame_path.is_file(): try: frame_index = int(row.get("frame_index")) except (TypeError, ValueError): report.warnings.append( f"{actions_path.name} line {line_number} points to missing frame {filename}; skipping row." ) continue candidates = frame_paths_by_index.get(frame_index, []) if len(candidates) == 1: frame_path = candidates[0] report.warnings.append( f"{actions_path.name} line {line_number} uses {frame_path.name} for missing legacy filename {filename}." ) else: report.warnings.append( f"{actions_path.name} line {line_number} points to missing frame {filename}; skipping row." ) continue try: with Image.open(frame_path) as image: image.verify() with Image.open(frame_path) as image: size = image.size except Exception as exc: report.errors.append(f"Broken image file {frame_path.name}: {exc}") continue if seen_resolution is None: seen_resolution = size report.resolution = f"{size[0]}x{size[1]}" elif size != seen_resolution: report.errors.append(f"Inconsistent frame resolution: {frame_path.name} is {size[0]}x{size[1]}, expected {seen_resolution[0]}x{seen_resolution[1]}.") unexpected = sorted(set(row) - SUPPORTED_ACTIONS - METADATA_FIELDS) if unexpected: report.errors.append(f"{actions_path.name} line {line_number} contains unsupported action field(s): {', '.join(unexpected[:6])}.") missing = sorted(name for name in REQUIRED_CANONICAL_ACTIONS if name not in row) if missing: report.errors.append(f"{actions_path.name} line {line_number} is missing action label(s): {', '.join(missing)}.") continue try: frame_index = int(row.get("frame_index")) except (TypeError, ValueError): report.errors.append(f"{actions_path.name} line {line_number} has an invalid frame_index.") continue session_id = str(row.get("session_id") or f"legacy-{directory.name}").strip() key = (session_id, frame_index) if key in seen_keys: report.errors.append(f"{actions_path.name} repeats frame_index {frame_index} in session {session_id}.") continue seen_keys.add(key) for name in action_counts: try: value = float(row.get(name, 0)) except (TypeError, ValueError): report.errors.append(f"{actions_path.name} line {line_number} has invalid {name} action value.") value = 0.0 if abs(value) > (0.5 if name in BINARY_ACTIONS else 0.02): action_counts[name] += 1 row["_session_id"] = session_id rows_by_session.setdefault(session_id, []).append(row) session_ids.add(f"{resolved}:{session_id}") report.valid_rows += 1 for session_id, rows in rows_by_session.items(): if not rows: report.errors.append(f"{directory} has an empty sequence {session_id}.") continue rows.sort(key=lambda item: int(item["frame_index"])) for left, right in zip(rows, rows[1:]): if int(right["frame_index"]) != int(left["frame_index"]) + 1: report.warnings.append(f"{directory} session {session_id} has an ordering gap at frame {left['frame_index']}; invalid transitions will be skipped.") transition_total += sum( 1 for left, right in zip(rows, rows[frame_gap:]) if int(right["frame_index"]) == int(left["frame_index"]) + frame_gap )
report.sessions = len(session_ids) report.action_counts = action_counts report.valid_transitions = transition_total if report.metadata_rows != report.frames: report.warnings.append(f"Frame and label counts differ: {report.frames} frame files, {report.metadata_rows} action rows.") if report.valid_rows < 2: report.errors.append("The dataset needs at least two valid labelled frames.") if report.valid_transitions < 1: report.errors.append(f"No valid frame transitions were found for prediction horizon {frame_gap}.") if report.valid_rows and not any(action_counts.values()): report.errors.append("No non-idle action labels were found. Record idle plus at least one active control.") return report9 unmodified lines10111213141520 unmodified lines3637383940414 unmodified lines4647484950515278 unmodified lines1311321331341351361379 unmodified linesdef dataset_image_count(raw_path: object) -> int:path = Path(str(raw_path or "")).expanduser()if not path.is_dir():return 020 unmodified linesreturn Nonedef recommend_training_settings(trainer: str,image_count: int,4 unmodified lines"""Return an explainable, conservative starting recipe for manual review."""trainer = str(trainer).casefold()images = max(10, int(image_count))resolution = max(64, min(512, int(resolution)))vram = _available_vram_gb() if vram_gb is None else vram_gbcpu_workers = max(2, min(8, (os.cpu_count() or 4) // 2))78 unmodified lines1,int(args.get("gradient_accumulation_steps", args.get("gradient_accumulation", 1)) or 1),)resolution = max(64, int(args.get("resolution", 256) or 256))exposures = images * epochs if images else 0optimizer_steps = math.ceil(images / batch / accumulation) * epochs if images else 0total_steps += optimizer_steps9 unmodified lines101112131415161720 unmodified lines383940414243444546474849505152535455564 unmodified lines6162636465666778 unmodified lines1461471481491501511529 unmodified linesdef dataset_image_count(raw_path: object) -> int:if not str(raw_path or "").strip():return 0path = Path(str(raw_path or "")).expanduser()if not path.is_dir():return 020 unmodified linesreturn Nonedef _resolution_extent(value: object, fallback: int = 256) -> int:raw = str(value or fallback).strip().lower()if "x" in raw:try:return max(int(part.strip()) for part in raw.split("x", 1))except ValueError:return fallbacktry:return int(raw)except ValueError:return fallbackdef recommend_training_settings(trainer: str,image_count: int,4 unmodified lines"""Return an explainable, conservative starting recipe for manual review."""trainer = str(trainer).casefold()images = max(10, int(image_count))resolution = max(64, min(512, _resolution_extent(resolution)))vram = _available_vram_gb() if vram_gb is None else vram_gbcpu_workers = max(2, min(8, (os.cpu_count() or 4) // 2))78 unmodified lines1,int(args.get("gradient_accumulation_steps", args.get("gradient_accumulation", 1)) or 1),)resolution = max(64, _resolution_extent(args.get("resolution", 256)))exposures = images * epochs if images else 0optimizer_steps = math.ceil(images / batch / accumulation) * epochs if images else 0total_steps += optimizer_steps
42 unmodified lines434445464748269 unmodified lines318319320321322323232 unmodified lines5565575585595605615625635645658 unmodified lines57457557657757857958038 unmodified lines61962062162262362462516 unmodified lines6426436446456466476484 unmodified lines65365465565665765865966066166214 unmodified lines67767867968068168268368468568668768868969069169269369469569669733 unmodified lines73173273373473573673749 unmodified lines78778878979079179279379479579679779879980080180280380480580680780880974 unmodified lines8848858868878888898909 unmodified lines90090190290390490511 unmodified lines9179189199209219229239249259269279289299309319329339349359369379389399409412 unmodified lines94494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397412 unmodified lines9879889899909919929932 unmodified lines9969979989991000100113 unmodified lines10151016101710181019102026 unmodified lines104710481049105010511052105310541055105610571058105910601061106210631064106579 unmodified lines114511461147114811491150115111521153115411551156318 unmodified lines14751476147714781479148058 unmodified lines153915401541154215431544154515461547154815491550155115521553155415558 unmodified lines1564156515661567156815691570157138 unmodified lines16101611161216131614161542 unmodified linesreturn f"{safe.title()} {suffix}".strip()[:64]def _collection_mode(request: str) -> str:"""Return the user's requested stopping rule for internet collection."""return (269 unmodified linesdef _deterministic_plan(self, request: str) -> ExecutionPlan | None:lowered = request.lower()youtube_plan = self._youtube_dataset_plan(request)if youtube_plan:return youtube_plan232 unmodified linesif not re.search(r"\b(train|fine[- ]?tune|retrain|continue|resume)\b", lowered):return Nonefine_tune_payload = self._fine_tune_payload(request)trainer = str(fine_tune_payload.get("trainer", "")) or ("lora" if re.search(r"\blora\b", lowered)else "ddpm" if re.search(r"\bddpm\b", lowered)else "flow" if re.search(r"\bflow(?:\s+matching)?\b", lowered)else "")action = (8 unmodified linesmodel_query = ""resume_match = re.search(r"\b(?:fine[- ]?tune|retrain|continue|resume)\s+(?:the\s+)?(.+?)"r"(?:\s+model)?\s+(?:from|on|with)\s+(?:the\s+)?(?:ddpm|lora)\b",request,re.I,)38 unmodified linesif action == "resume_training":candidates: list[Asset] = []if model_query:candidates = self.assets.find("model", model_query, trainer=trainer)if not candidates:return ExecutionPlan(request=request,16 unmodified linestrainer = trainer or model.trainerddpm_pipeline = trainer == "ddpm" and (Path(model.path) / "model_index.json").is_file()flow_model = trainer == "flow" and self._valid_flow_model(Path(model.path))if (not model.checkpoint or not Path(model.checkpoint).exists()) and not ddpm_pipeline and not flow_model:return ExecutionPlan(request=request,summary=(4 unmodified lines),steps=[],project_name="Resume training",)dataset_mode = str(fine_tune_payload.get("dataset_mode", "original"))if dataset_mode == "existing":dataset = self._asset_dataset(str(fine_tune_payload.get("dataset_name", "")))else:dataset = self._dataset_for_model(model)if dataset_mode == "new":14 unmodified linessteps=[],project_name="Resume training",)command = TrainingCommand.from_dict({"action": "resume_training","trainer": trainer,"dataset": dataset.path,"model_name": model.name,"epochs": epochs,"output": (str(self._training_output(trainer, f"{model.name} Fine Tune") or model.path)if trainer == "flow" else model.path),# The DDPM adapter can safely branch from a complete pipeline when# its exact Accelerate checkpoint has been cleaned up."resume_from": model.checkpoint or model.path,"base_model": self._lora_base_model() if trainer == "lora" else "","training_options": training_options,})33 unmodified lines"model_name": model_name,"epochs": epochs,"output": str(output),"base_model": self._lora_base_model() if trainer == "lora" else "","training_options": training_options,})49 unmodified linesif dataset_dir.exists():dataset_dir = dataset_dir.with_name(f"{dataset_dir.name} {datetime.now().strftime('%Y%m%d_%H%M%S')}")image_count = max(10, min(int(payload.get("image_count", 60)), 5000))arguments: dict[str, Any] = {"dataset_dir": str(dataset_dir), "model_name": model.name,"epochs": epochs,"output_dir": (str(self._training_output(trainer, f"{model.name} Fine Tune") or model.path)if trainer == "flow" else model.path),"resume_from": model.checkpoint or model.path, **training_options,}if trainer == "lora":base_model = self._lora_base_model()if not base_model or not Path(base_model).is_file():return ExecutionPlan(request=request, summary="Choose a valid SDXL base model in the LoRA app before fine-tuning.", steps=[], project_name="LoRA training")arguments["base_model"] = base_modelreturn ExecutionPlan(request=request,summary=f"Collect {image_count} new images for {subject}, then continue {model.name} for {epochs} additional epochs.",steps=[PlanStep("dataset_collector", "Collect new fine-tune dataset", "Collect and save a reviewable dataset.", {"subject": subject, "image_count": image_count, "collection_mode": "target", "project_name": project, "output_dir": str(dataset_dir)}),PlanStep(f"{trainer}_trainer", f"Fine-tune {trainer.upper()} model", "Continue from the selected saved model using the newly collected dataset.", arguments),74 unmodified linesdef _dataset_for_phrase(self, phrase: str) -> Asset | None:"""Resolve a friendly dataset phrase, preferring the shortest clear folder match."""direct = self._asset_dataset(phrase)if direct:return directwanted = re.sub(r"[^a-z0-9]+", " ", phrase.casefold()).strip()9 unmodified linescandidates.sort(key=lambda item: (len(item.name), item.name.casefold()))return candidates[0]def _plan_training_command(self,request: str,11 unmodified linessteps=[],project_name="Unsupported training request",)dataset_path = Path(command.dataset).expanduser()if not dataset_path.is_dir():raise PlanningError("The validated training dataset does not exist.")trainer_folder = self._configured_tool_folder(tool_id)if not trainer_folder:raise PlanningError(f"The {spec.name} folder is not connected.")output_folder = "output_flow_models" if command.trainer == "flow" else "output"output_root = (Path(trainer_folder) / output_folder).resolve()output_path = Path(command.output).expanduser().resolve()try:output_path.relative_to(output_root)except ValueError as exc:raise PlanningError(f"{spec.name} outputs must stay inside {output_root}.") from excif command.resume_from and not Path(command.resume_from).exists():raise PlanningError("The validated resume checkpoint does not exist.")arguments: dict[str, Any] = {"dataset_dir": str(dataset_path.resolve()),"model_name": command.model_name,"epochs": command.epochs,"output_dir": str(output_path),2 unmodified linesif command.resume_from:arguments["resume_from"] = command.resume_fromif command.trainer == "lora":if not command.base_model or not Path(command.base_model).is_file():return ExecutionPlan(request=request,summary=("I found the LoRA dataset, but the connected LoRA trainer has no ""valid SDXL base model selected. Choose one in the LoRA app first."),steps=[],project_name="LoRA training",)arguments["base_model"] = command.base_modelverb = "Continue" if command.action == "resume_training" else "Train"epoch_kind = "additional epochs" if command.action == "resume_training" else "epochs"return ExecutionPlan(request=request,summary=(f"{verb} {command.model_name} with the registered {command.trainer.upper()} "f"trainer for {command.epochs} {epoch_kind}. Dataset: {command.dataset}. "f"Output: {command.output}."+ (f" Training options: {command.training_options}." if command.training_options else "")),steps=[PlanStep(tool_id,f"{verb} {command.trainer.upper()} model","Launch the connected trainer with validated paths and stream progress.",arguments,)12 unmodified lines# causing a valid Flow Matching request to fall back to its old# clarification screen instead of creating a training plan.explicit_path = re.search(r"\bfrom\s+(?:the\s+)?([A-Za-z]:[\\/].+?)\s+dataset\s*"r"(?=[,.;]?\s*(?:train|continue|resume|name|call|save|output|put)\b)",request,re.I,2 unmodified linesreturn explicit_path.group(1).strip()patterns = (r"\btrain\s+(?:the\s+)?(.+?)\s+dataset\s+(?:on|with|for)\b",r"\bfrom\s+(?:the\s+)?(.+?)\s+dataset\b",r"\bwith\s+(?:the\s+)?(.+?)\s+dataset\b",r"\b(?:the\s+)?(.+?)\s+dataset\s*,?\s+(?:train|use)\b",13 unmodified lines# metadata before looking for the user-facing name.request = re.sub(r"\s*\[ADAM_TRAINING_OPTIONS:\{.*?\}\]", "", request, flags=re.I | re.S)match = re.search(r"\b(?:name|call)\s+(?:the\s+)?model\s+(.+?)(?:[,\[\{]|$)", request, re.I)return _clean_subject(match.group(1)) if match else ""def _asset_dataset(self, name: str) -> Asset | None:26 unmodified linesreturn ranked[0]return Nonedef _training_output(self, trainer: str, model_name: str) -> Path | None:folder = self._configured_tool_folder(f"{trainer}_trainer")if not folder:return Nonesafe = re.sub(r"[^A-Za-z0-9._-]+", "_", model_name).strip("._") or "model"output_root = "output_flow_models" if trainer == "flow" else "output"candidate = (Path(folder) / output_root / safe).resolve()if candidate.exists():candidate = candidate.with_name(f"{candidate.name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}")return candidate@staticmethoddef _valid_flow_model(folder: Path) -> bool:try:79 unmodified lines@staticmethoddef _training_options_from_request(request: str) -> dict[str, Any]:match = re.search(r"\[ADAM_TRAINING_OPTIONS:(\{.*?\})\]", request, re.S)if not match:return {}try:options = json.loads(match.group(1))except json.JSONDecodeError as exc:raise PlanningError("Training options could not be read safely.") from excif not isinstance(options, dict):raise PlanningError("Training options must be a settings object.")return options318 unmodified linesproject_name="DDPM training",)@staticmethoddef _missing_ddpm_message(fields: dict[str, Any]) -> str:missing = [58 unmodified linesdef _configured_tool_folder(self, tool_id: str) -> str:folders = self.config.get("tool_folders", {})if not isinstance(folders, dict):return ""raw_path = str(folders.get(tool_id, "")).strip()return raw_path if raw_path and Path(raw_path).is_dir() else ""def _lora_plan(self, request: str, subject: str) -> ExecutionPlan:requested_name = self._model_name_from_request(request)model_name = requested_name or subjectproject = _project_name(model_name, "LoRA")collector_root = self._configured_tool_folder("dataset_collector")trainer_root = self._configured_tool_folder("lora_trainer")base_model = self._lora_base_model()if not collector_root or not trainer_root:return ExecutionPlan(request=request,8 unmodified linesreturn ExecutionPlan(request=request,summary=("Select a valid SDXL base model in the connected LoRA app first. ""ADAM will reuse that reviewed setting."),steps=[],project_name="LoRA training",38 unmodified lines"epochs": epochs,"output_dir": str(output_dir),"base_model": base_model,},),]42 unmodified lines43444546474849505152535455565758596061626364269 unmodified lines334335336337338339340341342343232 unmodified lines5765775785795805815825835845855865875885895905915928 unmodified lines60160260360460560660738 unmodified lines64664764864965065165216 unmodified lines6696706716726736746756766776786796806814 unmodified lines68668768868969069169269369469514 unmodified lines71071171271371471571671771871972072172272372472572672772872973073173273373433 unmodified lines76876977077177277377477577677749 unmodified lines82782882983083183283383483583683783883984084184284384484584684784884985074 unmodified lines9259269279289299309319 unmodified lines94194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597611 unmodified lines9889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410352 unmodified lines103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107412 unmodified lines10871088108910901091109210932 unmodified lines109610971098109911001101110213 unmodified lines11161117111811191120112111221123112426 unmodified lines1151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121879 unmodified lines1298129913001301130213031304130513061307130813091310131113121313131413151316131713181319318 unmodified lines163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171958 unmodified lines17781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718088 unmodified lines1817181818191820182118221823182438 unmodified lines186318641865186618671868186942 unmodified linesreturn f"{safe.title()} {suffix}".strip()[:64]def _trainer_label(trainer: str) -> str:return {"ddpm": "DDPM","flow": "Flow Matching","lora": "LoRA","oasis": "Oasis Action World Model",}.get(trainer, trainer.replace("_", " ").title())def _friendly_model_name(asset: Asset) -> str:name = str(asset.name or "").strip()if re.search(r"^[A-Za-z]:[\\/]", name) or "/" in name or "\\" in name:return Path(asset.path).namereturn name or Path(asset.path).namedef _collection_mode(request: str) -> str:"""Return the user's requested stopping rule for internet collection."""return (269 unmodified linesdef _deterministic_plan(self, request: str) -> ExecutionPlan | None:lowered = request.lower()oasis_player = self._oasis_player_plan(request)if oasis_player:return oasis_playeryoutube_plan = self._youtube_dataset_plan(request)if youtube_plan:return youtube_plan232 unmodified linesif not re.search(r"\b(train|fine[- ]?tune|retrain|continue|resume)\b", lowered):return Nonefine_tune_payload = self._fine_tune_payload(request)marker = re.search(r"\[ADAM_TRAINER:([A-Za-z0-9_ -]+)\]", request, re.I)trainer = str(fine_tune_payload.get("trainer", "")) or (marker.group(1).strip().casefold().replace(" ", "_") if marker else "") or ("lora" if re.search(r"\blora\b", lowered)else "ddpm" if re.search(r"\bddpm\b", lowered)else "flow" if re.search(r"\bflow(?:\s+matching)?\b", lowered)else "oasis" if re.search(r"\b(oasis|action[- ]conditioned|playable\s+ai\s+games?|world\s+models?|gameplay[- ]frame|wasd|w/a/s/d)\b",lowered,)else "")action = (8 unmodified linesmodel_query = ""resume_match = re.search(r"\b(?:fine[- ]?tune|retrain|continue|resume)\s+(?:the\s+)?(.+?)"r"(?:\s+model)?\s+(?:from|on|with)\s+(?:the\s+)?(?:ddpm|lora|oasis)\b",request,re.I,)38 unmodified linesif action == "resume_training":candidates: list[Asset] = []if model_query:candidates = self._model_candidates(model_query, trainer=trainer)if not candidates:return ExecutionPlan(request=request,16 unmodified linestrainer = trainer or model.trainerddpm_pipeline = trainer == "ddpm" and (Path(model.path) / "model_index.json").is_file()flow_model = trainer == "flow" and self._valid_flow_model(Path(model.path))oasis_model = trainer == "oasis" and self._valid_oasis_model(Path(model.path))if ((not model.checkpoint or not Path(model.checkpoint).exists())and not ddpm_pipelineand not flow_modeland not oasis_model):return ExecutionPlan(request=request,summary=(4 unmodified lines),steps=[],project_name="Resume training",)dataset_mode = str(fine_tune_payload.get("dataset_mode", "original"))if dataset_mode == "existing":dataset = self._resolve_dataset_asset(str(fine_tune_payload.get("dataset_name", "")))else:dataset = self._dataset_for_model(model)if dataset_mode == "new":14 unmodified linessteps=[],project_name="Resume training",)resumed_model_name = _friendly_model_name(model)command = TrainingCommand.from_dict({"action": "resume_training","trainer": trainer,"dataset": dataset.path,"model_name": resumed_model_name,"epochs": epochs,"output": (str(self._training_output(trainer, f"{resumed_model_name} Fine Tune") or model.path)if trainer == "flow" else model.path),# The DDPM adapter can safely branch from a complete pipeline when# its exact Accelerate checkpoint has been cleaned up."resume_from": model.checkpoint or model.path,"base_model": (str(training_options.get("base_model") or self._lora_base_model())if trainer == "lora" else ""),"training_options": training_options,})33 unmodified lines"model_name": model_name,"epochs": epochs,"output": str(output),"base_model": (str(training_options.get("base_model") or self._lora_base_model())if trainer == "lora" else ""),"training_options": training_options,})49 unmodified linesif dataset_dir.exists():dataset_dir = dataset_dir.with_name(f"{dataset_dir.name} {datetime.now().strftime('%Y%m%d_%H%M%S')}")image_count = max(10, min(int(payload.get("image_count", 60)), 5000))model_name = _friendly_model_name(model)arguments: dict[str, Any] = {"dataset_dir": str(dataset_dir), "model_name": model_name,"epochs": epochs,"output_dir": (str(self._training_output(trainer, f"{model_name} Fine Tune") or model.path)if trainer == "flow" else model.path),"resume_from": model.checkpoint or model.path, **training_options,}if trainer == "lora":base_model = str(training_options.get("base_model") or self._lora_base_model())if not base_model or not Path(base_model).is_file():return ExecutionPlan(request=request, summary="Choose a valid SDXL base model in the LoRA app before fine-tuning.", steps=[], project_name="LoRA training")arguments["base_model"] = base_modelreturn ExecutionPlan(request=request,summary=f"Collect {image_count} new images for {subject}, then continue {model_name} for {epochs} additional epochs.",steps=[PlanStep("dataset_collector", "Collect new fine-tune dataset", "Collect and save a reviewable dataset.", {"subject": subject, "image_count": image_count, "collection_mode": "target", "project_name": project, "output_dir": str(dataset_dir)}),PlanStep(f"{trainer}_trainer", f"Fine-tune {trainer.upper()} model", "Continue from the selected saved model using the newly collected dataset.", arguments),74 unmodified linesdef _dataset_for_phrase(self, phrase: str) -> Asset | None:"""Resolve a friendly dataset phrase, preferring the shortest clear folder match."""direct = self._resolve_dataset_asset(phrase)if direct:return directwanted = re.sub(r"[^a-z0-9]+", " ", phrase.casefold()).strip()9 unmodified linescandidates.sort(key=lambda item: (len(item.name), item.name.casefold()))return candidates[0]def _model_candidates(self, query: str, *, trainer: str = "") -> list[Asset]:raw_query = str(query).strip().strip('"').replace("\\_", "_")path = Path(raw_query).expanduser()if path.exists():resolved = path.resolve()matches = [asset for asset in self.assets.assetsif asset.kind == "model"and (not trainer or asset.trainer == trainer)and Path(asset.path).expanduser().resolve() == resolved]if matches:return matchespath_like = re.search(r"^[A-Za-z]:[\\/]", raw_query) or "/" in raw_query or "\\" in raw_queryif path_like and path.name:matches = self.assets.find("model", path.name, trainer=trainer)existing = [asset for asset in matches if Path(asset.path).expanduser().exists()]if trainer == "oasis":valid = [asset for asset in existingif self._valid_oasis_model(Path(asset.path).expanduser())]if valid:return validif existing:return existingif matches:return matchesreturn self.assets.find("model", raw_query, trainer=trainer)def _plan_training_command(self,request: str,11 unmodified linessteps=[],project_name="Unsupported training request",)if command.trainer == "oasis":dataset_paths = self._oasis_dataset_paths(command.dataset)if not dataset_paths:raise PlanningError("The validated Oasis dataset does not exist or contains no action dataset folders.")dataset_path = dataset_paths[0]resolved_dataset_paths = [str(path.resolve()) for path in dataset_paths]dataset_argument = (resolved_dataset_paths[0]if len(resolved_dataset_paths) == 1else resolved_dataset_paths)dataset_label = ";".join(resolved_dataset_paths)else:dataset_path = Path(command.dataset).expanduser()if not dataset_path.is_dir():raise PlanningError("The validated training dataset does not exist.")dataset_argument = str(dataset_path.resolve())dataset_label = dataset_argumenttrainer_folder = self._configured_tool_folder(tool_id)plugin = self.registry.model_plugins.by_trainer(command.trainer)custom_plugin = plugin is not None and command.trainer not in {"ddpm", "flow", "lora", "oasis"}if not trainer_folder and not custom_plugin:raise PlanningError(f"The {spec.name} folder is not connected.")output_path = Path(command.output).expanduser().resolve()if custom_plugin:output_root = (self.root / "data" / "model_plugin_outputs" / command.trainer).resolve()output_root.mkdir(parents=True, exist_ok=True)else:output_folder = self._trainer_output_folder(command.trainer)output_root = (Path(trainer_folder) / output_folder).resolve()try:output_path.relative_to(output_root)except ValueError as exc:raise PlanningError(f"{spec.name} outputs must stay inside {output_root}.") from excif command.resume_from and not Path(command.resume_from).exists():raise PlanningError("The validated resume checkpoint does not exist.")arguments: dict[str, Any] = {"dataset_dir": dataset_argument,"model_name": command.model_name,"epochs": command.epochs,"output_dir": str(output_path),2 unmodified linesif command.resume_from:arguments["resume_from"] = command.resume_fromif command.trainer == "lora":base_model = command.base_model or str((command.training_options or {}).get("base_model", ""))if not base_model or not Path(base_model).is_file():return ExecutionPlan(request=request,summary=("I found the LoRA dataset, but no valid SDXL base model is selected. ""Choose one in the generated LoRA settings before training."),steps=[],project_name="LoRA training",)arguments["base_model"] = base_modelarguments["trigger_word"] = (command.trigger_wordor str((command.training_options or {}).get("trigger_word") or "")or command.model_name)verb = "Continue" if command.action == "resume_training" else "Train"epoch_kind = "additional epochs" if command.action == "resume_training" else "epochs"return ExecutionPlan(request=request,summary=(f"{verb} {command.model_name} with the registered {_trainer_label(command.trainer)} "f"trainer for {command.epochs} {epoch_kind}. Dataset: {dataset_label}. "f"Output: {command.output}."+ (f" Training options: {command.training_options}." if command.training_options else "")),steps=[PlanStep(tool_id,f"{verb} {_trainer_label(command.trainer)} model","Launch the connected trainer with validated paths and stream progress.",arguments,)12 unmodified lines# causing a valid Flow Matching request to fall back to its old# clarification screen instead of creating a training plan.explicit_path = re.search(r"\b(?:from|use)\s+(?:the\s+)?([A-Za-z]:[\\/].+?)\s+dataset\s*"r"(?=[,.;]?\s*(?:train|continue|resume|name|call|save|output|put)\b)",request,re.I,2 unmodified linesreturn explicit_path.group(1).strip()patterns = (r"\btrain\s+(?:the\s+)?(.+?)\s+dataset\s+(?:on|with|for)\b",r"\buse\s+(?:the\s+)?(.+?)\s+dataset\b",r"\bfrom\s+(?:the\s+)?(.+?)\s+dataset\b",r"\bwith\s+(?:the\s+)?(.+?)\s+dataset\b",r"\b(?:the\s+)?(.+?)\s+dataset\s*,?\s+(?:train|use)\b",13 unmodified lines# metadata before looking for the user-facing name.request = re.sub(r"\s*\[ADAM_TRAINING_OPTIONS:\{.*?\}\]", "", request, flags=re.I | re.S)match = re.search(r"\b(?:name|call)\s+(?:the\s+)?model\s+(.+?)(?:[,\[\{]|$)", request, re.I)if match:return _clean_subject(match.group(1))match = re.search(r"\b(?:model|checkpoint)\s+called\s+(.+?)(?:[,\[\{]|$)", request, re.I)return _clean_subject(match.group(1)) if match else ""def _asset_dataset(self, name: str) -> Asset | None:26 unmodified linesreturn ranked[0]return Nonedef _resolve_dataset_asset(self, value: str) -> Asset | None:path = self._resolve_dataset(value)if path:return self.assets.register(kind="dataset",name=path.name,path=str(path),persist=False,)return None@staticmethoddef _is_oasis_dataset_folder(path: Path) -> bool:return path.is_dir() and (path / "frames").is_dir() and (path / "actions.jsonl").is_file()def _oasis_dataset_paths(self, value: str) -> list[Path]:paths: list[Path] = []for raw in str(value or "").split(";"):text = raw.strip().strip('"')if not text:continuepath = Path(text).expanduser()if self._is_oasis_dataset_folder(path):paths.append(path.resolve())continueif path.is_dir():children = [child.resolve()for child in sorted(path.rglob("*"), key=lambda item: str(item).casefold())if self._is_oasis_dataset_folder(child)]paths.extend(children)unique: list[Path] = []for path in paths:if path not in unique:unique.append(path)return uniquedef _training_output(self, trainer: str, model_name: str) -> Path | None:folder = self._configured_tool_folder(f"{trainer}_trainer")plugin = self.registry.model_plugins.by_trainer(trainer)if not folder and plugin is None:return Nonesafe = re.sub(r"[^A-Za-z0-9._-]+", "_", model_name).strip("._") or "model"if folder:output_root = self._trainer_output_folder(trainer)candidate = (Path(folder) / output_root / safe).resolve()else:candidate = (self.root / "data" / "model_plugin_outputs" / trainer / safe).resolve()if candidate.exists():candidate = candidate.with_name(f"{candidate.name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}")return candidate@staticmethoddef _trainer_output_folder(trainer: str) -> str:return {"flow": "output_flow_models","oasis": "output_action_flow_models",}.get(trainer, "output")@staticmethoddef _valid_flow_model(folder: Path) -> bool:try:79 unmodified lines@staticmethoddef _training_options_from_request(request: str) -> dict[str, Any]:match = re.search(r"\[ADAM_TRAINING_OPTIONS:(\{.*?\})\]", request, re.S)options: dict[str, Any] = {}if match:try:parsed = json.loads(match.group(1))except json.JSONDecodeError as exc:raise PlanningError("Training options could not be read safely.") from excif not isinstance(parsed, dict):raise PlanningError("Training options must be a settings object.")options = parsedtrigger_match = re.search(r"\b(?:trigger\s+word|concept\s+token)(?:\s+of|\s*=|\s*:)?\s*['\"\u201c\u201d]?([A-Za-z0-9_.-]{1,128})",request,re.I,)if trigger_match and "trigger_word" not in options:options["trigger_word"] = trigger_match.group(1).strip()if not isinstance(options, dict):raise PlanningError("Training options must be a settings object.")return options318 unmodified linesproject_name="DDPM training",)def _oasis_player_plan(self, request: str) -> ExecutionPlan | None:lowered = request.casefold()if not re.search(r"\b(launch|start|play|open|run)\b", lowered):return Noneif not re.search(r"\b(oasis|playable\s+ai\s+game|action\s+player|world\s+model)\b", lowered):return Noneself.assets.discover(self.config)path_match = re.search(r"([A-Za-z]:[\\/][^,\n]+)", request)model_path = Path(path_match.group(1).strip().strip("\"'")) if path_match else Nonemodel_name = ""if model_path is not None and not self._valid_oasis_model(model_path):return ExecutionPlan(request=request,summary="I need a valid Oasis action model folder to launch the playable window.",steps=[],project_name="Oasis player",)if model_path is None:query_text = re.sub(r"\b(?:with\s+)?seed\s+\d+\b", " ", request, flags=re.I)query_text = re.sub(r"\b(?:starting|reference)\s+frame\s*(?:is|:|=)?\s*[A-Za-z]:[\\/][^,\n]+", " ", query_text, flags=re.I)query = _clean_subject(re.sub(r"\b(launch|start|play|open|run|oasis|action player|world model)\b", " ", query_text, flags=re.I))candidates = self.assets.find("model", query, trainer="oasis") if query else [asset for asset in self.assets.assets if asset.kind == "model" and asset.trainer == "oasis"]candidates = [asset for asset in candidates if Path(asset.path).is_dir()]if len(candidates) != 1:examples = ", ".join(asset.name for asset in candidates[:4])return ExecutionPlan(request=request,summary=("I need one Oasis checkpoint folder to launch."+ (f" Matching models: {examples}." if examples else "")),steps=[],project_name="Oasis player",)model_name = candidates[0].namemodel_path = Path(candidates[0].path)starting_frame = ""start_match = re.search(r"\b(?:starting|reference)\s+frame\s*(?:is|:|=)?\s*([A-Za-z]:[\\/][^,\n]+)", request, re.I)if start_match:starting_frame = start_match.group(1).strip().strip("\"'")seed_match = re.search(r"\bseed\s+(\d+)", request, re.I)return ExecutionPlan(request=request,summary=f"Launch the Oasis playable window for {model_name or model_path.name}.",steps=[PlanStep("oasis_player","Launch Oasis player","Open the existing Oasis playable inference window in its own process.",{"model_name": model_name or model_path.name,"model_path": str(model_path.resolve()),"starting_frame": starting_frame,"seed": int(seed_match.group(1)) if seed_match else 0,},)],requires_confirmation=False,project_name="Oasis player",)@staticmethoddef _valid_oasis_model(folder: Path) -> bool:try:metadata = json.loads((folder / "action_flow_model_info.json").read_text(encoding="utf-8"))return (metadata.get("model_type") == "action_conditioned_rectified_flow_video"and (folder / "unet" / "config.json").is_file())except (OSError, ValueError, TypeError, json.JSONDecodeError):return False@staticmethoddef _missing_ddpm_message(fields: dict[str, Any]) -> str:missing = [58 unmodified linesdef _configured_tool_folder(self, tool_id: str) -> str:folders = self.config.get("tool_folders", {})if not isinstance(folders, dict):folders = {}raw_path = str(folders.get(tool_id, "")).strip()if raw_path and Path(raw_path).is_dir():return raw_pathif tool_id == "oasis_trainer":external = self.root / "config" / "external_tools.json"try:payload = json.loads(external.read_text(encoding="utf-8"))except (OSError, ValueError, TypeError, json.JSONDecodeError):return ""for entry in payload.get("tools", []):if isinstance(entry, dict) and entry.get("id") == "external_oasis_game_trainer":candidate = Path(str(entry.get("backend", {}).get("root", ""))).expanduser()if candidate.is_dir():return str(candidate)return ""def _lora_plan(self, request: str, subject: str) -> ExecutionPlan:requested_name = self._model_name_from_request(request)model_name = requested_name or subjecttraining_options = self._training_options_from_request(request)project = _project_name(model_name, "LoRA")collector_root = self._configured_tool_folder("dataset_collector")trainer_root = self._configured_tool_folder("lora_trainer")base_model = str(training_options.get("base_model") or self._lora_base_model())if not collector_root or not trainer_root:return ExecutionPlan(request=request,8 unmodified linesreturn ExecutionPlan(request=request,summary=("Choose a valid SDXL base model in the generated LoRA settings ""before training."),steps=[],project_name="LoRA training",38 unmodified lines"epochs": epochs,"output_dir": str(output_dir),"base_model": base_model,**training_options,},),]
12345612 unmodified lines192021from __future__ import annotationsimport subprocessdef set_process_tree_paused(process: subprocess.Popen, paused: bool) -> bool:12 unmodified linesreturn Trueexcept Exception:return False123456712 unmodified lines2021222324252627282930313233343536373839404142434445464748495051525354555657from __future__ import annotationsimport subprocessfrom typing import Anydef set_process_tree_paused(process: subprocess.Popen, paused: bool) -> bool:12 unmodified linesreturn Trueexcept Exception:return Falsedef terminate_process_tree(process: Any, *, timeout: float = 3.0) -> None:"""Terminate a process and any children it launched."""try:import psutilparent = psutil.Process(process.pid)children = parent.children(recursive=True)targets = [*children, parent]for target in targets:try:target.terminate()except (psutil.NoSuchProcess, psutil.AccessDenied):continue_gone, alive = psutil.wait_procs(targets, timeout=timeout)for target in alive:try:target.kill()except (psutil.NoSuchProcess, psutil.AccessDenied):continuereturnexcept Exception:passtry:process.terminate()except Exception:returntry:process.wait(timeout=timeout)except Exception:try:process.kill()except Exception:pass
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195from __future__ import annotations
import mathimport osfrom dataclasses import asdict, dataclass, fieldfrom typing import Any
from adam.model_profiles import ModelProfilefrom adam.models import SystemSnapshot
@dataclass(slots=True)class SettingsRecommendation: profile_id: str epochs: int settings: dict[str, Any] = field(default_factory=dict) reasons: list[str] = field(default_factory=list) warnings: list[str] = field(default_factory=list) summary: str = "" estimated_vram_gb: float | None = None risk_level: str = "normal" confidence: str = "conservative"
def to_dict(self) -> dict[str, Any]: return asdict(self)
def _field_default(profile: ModelProfile, key: str, fallback: Any) -> Any: return profile.training.get(key, {}).get("default", fallback)
def _clamp_to_schema(profile: ModelProfile, key: str, value: Any) -> Any: spec = profile.training.get(key, {}) kind = str(spec.get("type", "text")) try: if kind in {"int", "slider"}: numeric = int(value) return max(int(spec.get("min", numeric)), min(numeric, int(spec.get("max", numeric)))) if kind == "float": numeric = float(value) return max(float(spec.get("min", numeric)), min(numeric, float(spec.get("max", numeric)))) except (TypeError, ValueError): return spec.get("default", value) if kind == "choice": options = list(spec.get("options", [])) return value if value in options else (options[0] if options else value) return value
def estimate_vram_gb(profile: ModelProfile, resolution: int | str, batch_size: int, base_model_gb: float = 0.0) -> float: """Broad VRAM estimate used only for warnings and conservative defaults.""" architecture = profile.architecture.casefold() pixels = (max(64, resolution) / 512) ** 2 if profile.id == "lora" or "lora" in architecture: base = max(6.0, base_model_gb * 1.8) return base + pixels * max(1, batch_size) * 1.2 if profile.id == "oasis" or "action_conditioned" in architecture: width, height = (resolution, resolution) if isinstance(resolution, str) and "x" in resolution: try: width, height = (int(part) for part in resolution.lower().split("x", 1)) except ValueError: width, height = (256, 144) pixels = (max(width, height) / 512) ** 2 return 4.0 + pixels * max(1, batch_size) * 2.4 if "flow" in architecture: return 2.8 + pixels * max(1, batch_size) * 1.0 if "diffusion" in architecture: return 2.2 + pixels * max(1, batch_size) * 0.9 return 3.0 + pixels * max(1, batch_size) * 0.8
def recommend_for_profile( profile: ModelProfile, *, dataset_items: int, resolution: int | str | None = None, snapshot: SystemSnapshot | None = None, base_model_gb: float = 0.0,) -> SettingsRecommendation: images = max(10, int(dataset_items or 10)) raw_resolution = resolution or _field_default(profile, "resolution", 256) or 256 if isinstance(raw_resolution, str) and "x" in raw_resolution: resolution = int(raw_resolution.lower().split("x", 1)[0]) else: resolution = int(raw_resolution) reasons: list[str] = [] vram_total = snapshot.vram_total_gb if snapshot and snapshot.vram_total_gb else None available_vram = ( max(0.0, snapshot.vram_total_gb - snapshot.vram_used_gb) if snapshot and snapshot.vram_total_gb else vram_total ) architecture = profile.architecture.casefold() target_exposures = 80_000 if profile.id == "lora" else 180_000 if "diffusion" in architecture else 120_000 max_epochs = 220 if profile.id == "lora" else 600 if "diffusion" in architecture else 300 epochs = max(10 if profile.id == "lora" else 25, min(max_epochs, round(target_exposures / images))) reasons.append( f"Epochs target roughly {target_exposures:,} image exposures, then clamp to the profile's safe range." )
batch_defaults = { 64: 16, 128: 12, 256: 4, 384: 2, 512: 1, 768: 1, 1024: 1, } if profile.id == "flow": batch_defaults.update({64: 12, 128: 8, 256: 4}) if profile.id == "oasis": batch_defaults.update({128: 4, 256: 2, 384: 1, 512: 1}) if profile.id == "lora": batch_defaults.update({512: 2, 768: 1, 1024: 1}) nearest = min(batch_defaults, key=lambda size: abs(size - resolution)) batch_size = batch_defaults[nearest] reasons.append(f"Batch starts from the closest resolution preset ({nearest}px).") if available_vram is not None and available_vram < 8: batch_size = max(1, batch_size // 2) reasons.append("Available VRAM is below 8 GB, so batch size is reduced conservatively.")
settings: dict[str, Any] = {} for key in ("resolution", "batch_size"): if key in profile.training: value = batch_size if key == "resolution": value = raw_resolution if profile.id == "oasis" else resolution settings[key] = _clamp_to_schema( profile, key, value, ) if "learning_rate" in profile.training: settings["learning_rate"] = _clamp_to_schema( profile, "learning_rate", 0.00002 if profile.id == "oasis" else 0.0001 if profile.id in {"ddpm", "lora"} else 0.0002, ) workers = max(1, min(8, (os.cpu_count() or 4) // 2)) for key in ("dataloader_num_workers", "workers"): if key in profile.training: settings[key] = _clamp_to_schema(profile, key, workers) for key, value in { "gradient_accumulation_steps": 1, "gradient_accumulation": 1, "mixed_precision": "fp32" if profile.id == "oasis" else "fp16", "save_every": max(5, min(25, max(1, epochs // 10))), "preview_every": max(5, min(50, max(1, epochs // 10))), "training_intensity": 100, "gradient_checkpointing": resolution >= 384 or (available_vram is not None and available_vram < 8), "rank": 16, "alpha": 16, "frame_gap": 3, "sequence_context": 1, "preview_steps": 1 if profile.id == "oasis" else 50 if profile.id == "ddpm" else 10, }.items(): if key in profile.training: settings[key] = _clamp_to_schema(profile, key, value)
estimated = estimate_vram_gb(profile, resolution, int(settings.get("batch_size", batch_size)), base_model_gb) warnings: list[str] = [] if available_vram is not None and estimated > available_vram * 0.9: warnings.append( f"Estimated VRAM need is about {estimated:.1f} GB, above the conservative {available_vram * 0.9:.1f} GB working limit." ) if "batch_size" in settings and int(settings["batch_size"]) > 1: settings["batch_size"] = max(1, int(settings["batch_size"]) // 2) estimated = estimate_vram_gb(profile, resolution, int(settings["batch_size"]), base_model_gb) warnings.append(f"Batch size was reduced to {settings['batch_size']} for a safer first run.") reasons.append("The initial VRAM estimate was high, so ADAM reduced the batch before applying the recipe.") if images < 20: warnings.append("Dataset is very small; expect overfitting unless this is just a smoke test.") reasons.append("Very small datasets get a warning because quality usually depends more on data cleanup than long training.") risk_level = "risky" if warnings else "normal"
memory_note = ( f" using about {available_vram:.1f} GB available VRAM" if available_vram is not None else " without detected VRAM" ) summary = ( f"Recommended {epochs:,} epochs for {images:,} item(s), " f"batch {settings.get('batch_size', batch_size)} at {resolution}px{memory_note}. " "Treat this as a starting recipe, not a guarantee." ) return SettingsRecommendation( profile_id=profile.id, epochs=epochs, settings=settings, reasons=reasons, warnings=warnings, summary=summary, estimated_vram_gb=estimated, risk_level=risk_level, )1234567891049 unmodified lines60616263646546 unmodified lines11211311411511611726 unmodified lines144145146from __future__ import annotationsimport jsonfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Anyclass RegistryError(RuntimeError):pass49 unmodified linesself.root = root.resolve()self.path = self.root / "config" / "tools.json"self._tools: dict[str, ToolSpec] = {}self.load()def load(self) -> None:46 unmodified linessafe_entry["demo"] = Falsespec = ToolSpec.from_dict(safe_entry)loaded[spec.id] = specself._tools = loadeddef get(self, tool_id: str, *, require_enabled: bool = True) -> ToolSpec:26 unmodified lines}for tool in self.enabled()]1234567891011121349 unmodified lines6364656667686946 unmodified lines11611711811912012112212312412512612712812913013113213313413513613713813914014126 unmodified lines168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198from __future__ import annotationsimport jsonimport loggingfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Anyfrom adam.model_plugins import ModelPluginRegistryclass RegistryError(RuntimeError):pass49 unmodified linesself.root = root.resolve()self.path = self.root / "config" / "tools.json"self._tools: dict[str, ToolSpec] = {}self.model_plugins = ModelPluginRegistry(self.root, logging.getLogger(__name__))self.load()def load(self) -> None:46 unmodified linessafe_entry["demo"] = Falsespec = ToolSpec.from_dict(safe_entry)loaded[spec.id] = specfor entry in self.model_plugins.training_tool_specs() + self.model_plugins.generation_tool_specs():if not isinstance(entry, dict):continuesafe_entry = dict(entry)backend = dict(safe_entry.get("backend", {}))if not backend:safe_entry["backend"] = {"type": "python","module": "adam.model_plugin_backend","function": "train" if safe_entry.get("category") == "Training" else "generate",}try:spec = ToolSpec.from_dict(safe_entry)except RegistryError as exc:self.model_plugins.errors.append(f"{safe_entry.get('id', 'unknown')}: {exc}")continueif spec.id in loaded:loaded[spec.id] = _merge_tool_specs(loaded[spec.id], spec)else:loaded[spec.id] = specself._tools = loadeddef get(self, tool_id: str, *, require_enabled: bool = True) -> ToolSpec:26 unmodified lines}for tool in self.enabled()]def _merge_tool_specs(existing: ToolSpec, plugin: ToolSpec) -> ToolSpec:"""Keep the existing backend while accepting plugin-declared schema arguments."""arguments = tuple(dict.fromkeys([*existing.arguments, *plugin.arguments]))required_arguments = existing.required_arguments or plugin.required_argumentscapabilities = tuple(dict.fromkeys([*existing.capabilities, *plugin.capabilities]))model_trainers = tuple(dict.fromkeys([*existing.model_trainers, *plugin.model_trainers]))generation_options = dict(existing.generation_options)generation_options.update(plugin.generation_options)return ToolSpec(id=existing.id,name=existing.name,description=existing.description,category=existing.category,entry_function=existing.entry_function,arguments=arguments,required_arguments=required_arguments,capabilities=capabilities,model_trainers=model_trainers,generation_options=generation_options,requires_confirmation=existing.requires_confirmation,enabled=existing.enabled,demo=existing.demo,backend=existing.backend,)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147from __future__ import annotations
import jsonimport mathfrom dataclasses import dataclassfrom typing import Any
class RemoteApiError(ValueError): def __init__(self, message: str, *, status: int = 400) -> None: super().__init__(message) self.status = status
@dataclass(frozen=True, slots=True)class RemoteResponse: status: int body: bytes content_type: str = "application/json" headers: dict[str, str] | None = None
def json_response(payload: dict[str, Any], *, status: int = 200) -> RemoteResponse: return RemoteResponse( status=status, body=json.dumps(payload, separators=(",", ":")).encode("utf-8"), content_type="application/json", )
def error_response(message: str, *, status: int = 400) -> RemoteResponse: return json_response({"ok": False, "error": message}, status=status)
def media_response(body: bytes, content_type: str, *, cache_seconds: int = 86400) -> RemoteResponse: return RemoteResponse( status=200, body=body, content_type=content_type, headers={"Cache-Control": f"private, max-age={max(0, int(cache_seconds))}"}, )
def bounded_text(value: Any, *, max_length: int, label: str, required: bool = False) -> str: if value is None: value = "" if not isinstance(value, str): value = str(value) text = value.strip() if required and not text: raise RemoteApiError(f"{label} is required.") if len(text) > max_length: raise RemoteApiError(f"{label} must be {max_length} characters or shorter.") return text
def bounded_int( value: Any, *, minimum: int, maximum: int, default: int, label: str,) -> int: if value in (None, ""): return default try: if isinstance(value, bool): raise ValueError number = int(value) except (TypeError, ValueError) as exc: raise RemoteApiError(f"{label} must be a whole number.") from exc if number < minimum or number > maximum: raise RemoteApiError(f"{label} must be between {minimum} and {maximum}.") return number
def bounded_float( value: Any, *, minimum: float, maximum: float, default: float, label: str,) -> float: if value in (None, ""): return default try: if isinstance(value, bool): raise ValueError number = float(value) except (TypeError, ValueError) as exc: raise RemoteApiError(f"{label} must be a number.") from exc if not math.isfinite(number) or number < minimum or number > maximum: raise RemoteApiError(f"{label} must be between {minimum:g} and {maximum:g}.") return number
def parse_pagination(query: dict[str, list[str]], *, default_size: int = 24, max_size: int = 80) -> dict[str, int]: page = bounded_int( (query.get("page") or ["1"])[0], minimum=1, maximum=1_000_000, default=1, label="Page", ) page_size = bounded_int( (query.get("page_size") or [str(default_size)])[0], minimum=1, maximum=max_size, default=default_size, label="Page size", ) return { "page": page, "page_size": page_size, "offset": (page - 1) * page_size, "limit": page_size, }
def coerce_json_object(payload: Any) -> dict[str, Any]: if not isinstance(payload, dict): raise RemoteApiError("Send a JSON object.") return payload
def sanitized_arguments(arguments: dict[str, Any]) -> dict[str, Any]: """Return client-safe arguments without absolute filesystem paths.""" hidden = { "dataset_dir", "output_dir", "model_path", "base_model", "base_model_path", "resume_from", "reference_image", } clean: dict[str, Any] = {} for key, value in arguments.items(): if key in hidden: text = str(value or "") clean[f"{key}_name"] = text.replace("\\", "/").rstrip("/").rsplit("/", 1)[-1] if text else "" continue if isinstance(value, (str, int, float, bool)) or value is None: clean[key] = value return clean123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141from __future__ import annotations
def remote_dashboard_app_html() -> str:
return """<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>ADAM Remote</title>
<style>
:root{color-scheme:dark;--bg:#050911;--panel:#111823;--panel2:#171f2b;--line:#26374b;--text:#f4f8ff;--muted:#94a2b1;--blue:#3988ff;--blue2:#74bdff;--gold:#f5c84d;--warn:#f8c35d;--bad:#ff6d86}
*{box-sizing:border-box}body{margin:0;background:radial-gradient(circle at 50% -20%,#102747 0,#07101c 34%,#02060b 100%);color:var(--text);font:15px/1.38 system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}button,input,select,textarea{font:inherit}button{border:1px solid #32475d;border-radius:8px;background:#1a2431;color:var(--text);padding:10px 12px;font-weight:750;min-height:42px}button.primary{background:linear-gradient(180deg,#438dff,#2467df);color:#fff;border-color:#4a97ff;box-shadow:0 8px 22px rgba(36,103,223,.28)}button.ghost{background:#121923;color:#c5e4ff;border-color:#2d76c9}button.warn{background:#352816;color:#ffe3a3}button.bad{background:#391d29;color:#ffd2db}button.icon{width:40px;min-width:40px;padding:0;color:var(--gold);font-size:22px;background:transparent;border:0}button:disabled{opacity:.46}input,select,textarea{width:100%;border:1px solid var(--line);border-radius:8px;background:#101722;color:var(--text);min-height:44px;padding:10px 12px}textarea{resize:vertical;min-height:86px}.app{min-height:100vh;padding:12px 14px 88px}.top,.titlebar{display:grid;grid-template-columns:42px 1fr 42px;align-items:center;margin:2px 0 14px}.brand{text-align:center}.brand b{display:block;font-size:22px}.brand span,.muted{color:var(--muted);font-size:13px}.titlebar h1{margin:0;text-align:center;font-size:22px;letter-spacing:0}.pill{border:1px solid var(--line);border-radius:999px;padding:6px 10px;color:var(--blue2);background:#0d1721;font-size:12px}.tabs{position:fixed;left:0;right:0;bottom:0;display:grid;grid-template-columns:repeat(5,1fr);background:rgba(4,8,13,.97);border-top:1px solid var(--line);z-index:20}.tabs button{border-radius:0;border:0;border-left:1px solid #111b25;background:transparent;color:var(--muted);font-size:12px;padding:9px 4px}.tabs button b{display:block;font-size:20px;line-height:1}.tabs button.active{color:var(--blue2);background:#0b1421}.view{display:none}.view.active{display:block}.grid{display:grid;gap:10px}.two{grid-template-columns:1fr 1fr}.card{background:rgba(17,24,35,.92);border:1px solid var(--line);border-radius:8px;padding:12px}.panel{background:transparent;border:0;padding:0}.section-row{display:flex;justify-content:space-between;align-items:center;margin:16px 4px 8px}.section{font-size:17px;color:var(--text);font-weight:850;margin:0}.view-all{color:var(--blue2);font-size:13px;font-weight:800}.row{display:flex;gap:8px;align-items:center;flex-wrap:wrap}.row>*{flex:1 1 auto}.status{min-height:20px;color:var(--muted);font-size:13px;white-space:pre-wrap}.progress{height:9px;border-radius:999px;background:#05080c;overflow:hidden;margin-top:10px}.bar{height:100%;width:0;background:linear-gradient(90deg,var(--blue),var(--blue2))}.dataset-list{display:grid;gap:8px}.dataset-card{display:grid;grid-template-columns:82px 1fr 36px;gap:10px;align-items:center;border:1px solid rgba(88,112,140,.35);border-radius:8px;background:linear-gradient(180deg,#171f2b,#111923);padding:8px;width:100%;box-shadow:0 10px 24px rgba(0,0,0,.18)}.dataset-card.compact{grid-template-columns:54px 1fr 22px}.dataset-card.unavailable{opacity:.62}.dataset-card img{width:82px;height:74px;object-fit:cover;border-radius:7px;background:#05080c}.dataset-card.compact img{width:54px;height:54px}.dataset-card b{font-size:16px;overflow-wrap:anywhere}.dataset-actions{grid-column:2/4;display:grid;grid-template-columns:1fr 1.15fr;gap:8px}.dataset-actions button{min-height:36px;padding:7px 10px}.meta{color:var(--muted);font-size:12px}.search-wrap{position:relative}.search-wrap span{position:absolute;left:12px;top:11px;color:var(--muted)}.search-wrap input{padding-left:38px}.chips{display:flex;gap:6px;overflow:auto;padding-bottom:2px}.chip{white-space:nowrap;border:1px solid var(--line);border-radius:999px;background:#0b1219;color:var(--muted);padding:7px 10px}.chip.active{color:#fff;background:#173b63;border-color:#3f8fff}.thumbs{display:grid;grid-template-columns:repeat(auto-fill,minmax(92px,1fr));gap:8px}.thumb{padding:0;overflow:hidden;text-align:left;background:#0d151e}.thumb img{display:block;width:100%;aspect-ratio:1;object-fit:cover;background:#05080c}.thumb div{padding:6px;color:var(--muted);font-size:12px;overflow-wrap:anywhere}.preview{display:block;width:100%;max-height:54vh;object-fit:contain;border-radius:8px;background:#05080c}.form{display:grid;gap:18px}.form-row{display:grid;grid-template-columns:112px 1fr;gap:10px;align-items:center}.form-row.stack{grid-template-columns:1fr}.form-row label,.field label{color:var(--text);font-size:15px;font-weight:780}.hint{color:var(--muted);font-size:12px;margin-top:5px}.toggle{display:grid;grid-template-columns:1fr 58px;gap:10px;align-items:center}.toggle input{width:58px;min-height:32px;accent-color:var(--blue)}.settings-grid{display:grid;gap:12px}.details summary{cursor:pointer;color:var(--blue2);font-weight:800;padding:8px 0}.queue-item{border:1px solid var(--line);border-radius:8px;background:#0d151e;padding:10px}.bad-text{color:var(--bad)}pre{overflow:auto}.big-action{width:100%;min-height:58px;font-size:22px}.selected-note{margin-top:6px;color:var(--muted);font-size:13px}
@media(min-width:760px){.app{max-width:1020px;margin:0 auto}.wide{grid-template-columns:1.05fr .95fr}.settings-grid{grid-template-columns:1fr 1fr}.tabs{left:50%;transform:translateX(-50%);max-width:1020px;border-left:1px solid var(--line);border-right:1px solid var(--line)}}
</style>
</head>
<body>
<main class="app">
<div class="top"><button id="refresh" class="ghost">Refresh</button><div class="brand"><b>ADAM Remote</b><span id="connection">Connecting</span></div><span></span></div>
<section id="home" class="view active">
<div class="grid wide">
<article class="card"><div class="section">Active Job</div><div id="activeJob">Checking ADAM...</div><div class="progress"><div id="activeBar" class="bar"></div></div><div class="row" style="margin-top:10px"><span class="pill">Time Left <b id="timeLeft">-</b></span><span class="pill">Finish <b id="finishTime">-</b></span></div><div class="row" style="margin-top:10px"><button data-action="pause">Pause</button><button data-action="resume">Resume</button><button class="bad" data-action="cancel">Cancel</button></div></article>
<article class="card"><div class="section">Live Preview</div><img id="preview" class="preview" alt="Live preview" style="display:none"><div id="previewNote" class="status">Waiting for a preview.</div></article>
<article class="card"><div class="section">Prompt ADAM</div><textarea id="prompt" placeholder="Ask ADAM naturally."></textarea><div class="row"><button class="primary" id="sendPrompt">Send</button><button class="ghost" id="askCreate">Create Model</button><button class="ghost" id="askDataset">Collect Dataset</button><button class="ghost" id="askQuick">Quick</button></div><div id="promptStatus" class="status"></div></article>
<article class="card"><div class="section">Latest Generation</div><div id="latestGeneration" class="thumbs"></div></article>
</div>
</section>
<section id="datasetsView" class="view">
<div class="titlebar"><span></span><h1>Datasets</h1><button id="filterDatasets" class="icon" title="Filter">+</button></div>
<div class="search-wrap"><span>Q</span><input id="datasetSearch" placeholder="Search datasets..."></div>
<div id="locationChips" class="chips" style="margin-top:10px"></div>
<div class="section-row"><div class="section">Favorites</div><div class="view-all">View All</div></div><div id="favoriteDatasets" class="dataset-list"></div>
<div class="section-row"><div class="section">Recently Used</div><div class="view-all">View All</div></div><div id="recentDatasets" class="dataset-list"></div>
<div class="section-row"><div class="section">Locations</div></div><div id="locationsListInline" class="dataset-list"></div>
<div class="section-row"><div class="section">All Remembered</div></div><div id="allDatasets" class="dataset-list"></div>
<article id="datasetDetail" class="card" style="display:none;margin-top:12px">
<div class="section">Preview</div>
<div id="datasetTitle"></div><div id="datasetStats" class="meta"></div>
<div class="row" style="margin:10px 0"><button id="useDataset" class="primary">Use Dataset</button><button id="selectDatasetForCreate" class="ghost">Select For Create</button></div>
<div id="datasetGrid" class="thumbs"></div>
<div class="row" style="margin-top:10px"><button id="prevPage">Previous</button><span id="pageLabel" class="pill">Page 1</span><button id="nextPage">Next</button></div>
</article>
<article id="imageDetail" class="card" style="display:none;margin-top:12px"><img id="detailImage" class="preview" alt="Dataset image"><div id="detailName"></div><div id="detailDims" class="meta"></div><div class="field"><label>Caption</label><textarea id="captionEditor"></textarea></div><div class="row"><button id="saveCaption">Save</button><button id="keepImage">Keep</button><button class="warn" id="rejectImage">Reject</button><button id="unreviewImage">Unreview</button></div><div id="imageStatus" class="status"></div></article>
</section>
<section id="createView" class="view">
<div class="titlebar"><button id="createBack" class="icon" title="Back"><</button><h1>Create Model</h1><button id="createHelp" class="icon" title="Help">?</button></div>
<article class="panel">
<div class="form">
<div class="form-row"><label>Preset</label><div><select id="preset"></select><div class="hint">Save time with a preset configuration.</div></div></div>
<div class="form-row"><label>Trainer</label><select id="trainer"></select></div>
<div class="form-row stack"><label>Model Name</label><input id="modelName" placeholder="Minecraft Oasis V3"></div>
<div class="form-row stack"><label>Dataset</label><button id="chooseDataset" class="ghost">Choose Dataset</button><select id="trainDataset" style="display:none"></select><div id="selectedDatasetNote" class="selected-note">No dataset selected</div></div>
<div id="baseModelWrap" class="form-row stack"><label>Base model</label><select id="baseModel"></select></div>
<div id="basicSettings" class="settings-grid"></div>
<details class="details"><summary>Advanced Settings</summary><div id="advancedSettings" class="settings-grid"></div></details>
<button id="startTraining" class="primary big-action">Start Training</button>
<button id="askInstead" class="ghost big-action">Ask ADAM Instead</button>
<button id="reviewPlan" class="ghost">Review Plan</button>
<pre id="trainingReview" class="status"></pre>
</div>
</article>
</section>
<section id="jobsView" class="view"><article class="card"><div class="section">Jobs</div><div id="queues" class="grid"></div></article></section>
<section id="settingsView" class="view"><div class="grid"><article class="card"><div class="section">Remote Control</div><label class="toggle"><span>Auto-approve remote training</span><input id="autoApproveTraining" type="checkbox"></label><label class="toggle"><span>Keep screen updated</span><input id="keepAwake" type="checkbox"></label><div id="settingsStatus" class="status"></div></article><article class="card"><div class="section">Remembered Locations</div><div id="locationsList" class="dataset-list"></div></article><article class="card"><div class="section">System</div><div id="system" class="grid two"></div></article></div></section>
</main>
<nav class="tabs"><button class="active" data-view="home"><b>^</b>Home</button><button data-view="datasetsView"><b>O</b>Datasets</button><button data-view="createView"><b>+</b>Create</button><button data-view="jobsView"><b>/</b>Jobs</button><button data-view="settingsView"><b>*</b>Settings</button></nav>
<script>
(function(){
var queryString=window.location.search||"";
var state={datasets:[],locations:[],trainingSchema:{trainers:[],base_models:[],presets:[]},selectedDataset:"",datasetPage:1,selectedItem:null,activeJobId:"",refreshMs:3000,locationFilter:""};
var timer=null;
function $(id){return document.getElementById(id)}
function list(v){return Array.isArray(v)?v:[]}
function clear(n){if(!n)return;while(n.firstChild)n.removeChild(n.firstChild)}
function text(id,v){var n=$(id);if(n)n.textContent=v==null?"":String(v)}
function authUrl(path,extra){var token=queryString.replace(/^\\?/,"");var url=path;if(token)url+=(url.indexOf("?")>=0?"&":"?")+token;if(extra)url+=(url.indexOf("?")>=0?"&":"?")+extra;return url}
function errorMessage(e){return e&&e.message?e.message:String(e||"Remote request failed.")}
function requestJson(path,method,payload){return new Promise(function(resolve,reject){var x=new XMLHttpRequest();x.open(method||"GET",authUrl(path),true);x.timeout=25000;x.setRequestHeader("Accept","application/json");if(payload!==undefined)x.setRequestHeader("Content-Type","application/json");x.onreadystatechange=function(){if(x.readyState!==4)return;var p={};try{p=x.responseText?JSON.parse(x.responseText):{}}catch(_e){reject(new Error("ADAM returned an unreadable response."));return}if(x.status<200||x.status>=300){reject(new Error(p.error||"Remote request failed ("+x.status+")."));return}resolve(p)};x.onerror=function(){reject(new Error("Connection failed. Check that ADAM Remote is still running."))};x.ontimeout=function(){reject(new Error("Connection timed out. The PC may be busy."))};x.send(payload===undefined?null:JSON.stringify(payload))})}
function getJson(path){return requestJson(path,"GET")}function postJson(path,payload){return requestJson(path,"POST",payload||{})}
function option(select,label,value){var o=document.createElement("option");o.textContent=label||"";o.value=value||"";select.appendChild(o)}
function appendText(parent,tag,value,cls){var node=document.createElement(tag);node.textContent=value==null?"":String(value);if(cls)node.className=cls;parent.appendChild(node);return node}
function fieldId(key){return "setting_"+key.replace(/[^A-Za-z0-9_]/g,"_")}
function activeTrainer(){var id=$("trainer").value;return list(state.trainingSchema.trainers).filter(function(t){return t.id===id})[0]||{settings:{}}}
function schemaFor(key){return activeTrainer().settings[key]||{}}
function closestButton(n){while(n&&n!==document.body){if(n.tagName&&n.tagName.toLowerCase()==="button")return n;n=n.parentNode}return null}
function switchView(id){var nodes=document.querySelectorAll(".view,.tabs button");for(var i=0;i<nodes.length;i++)nodes[i].classList.remove("active");if($(id))$(id).classList.add("active");var tab=document.querySelector('[data-view="'+id+'"]');if(tab)tab.classList.add("active")}
document.body.addEventListener("click",function(ev){var b=closestButton(ev.target);if(!b)return;var view=b.getAttribute("data-view");if(view)switchView(view);var action=b.getAttribute("data-action");if(action&&state.activeJobId){postJson("/api/job",{job_id:state.activeJobId,action:action}).then(load).catch(function(e){text("promptStatus",errorMessage(e))})}});
window.addEventListener("error",function(e){text("connection","Phone app error: "+(e.message||"unknown"))});
window.addEventListener("unhandledrejection",function(e){text("connection","Remote request error: "+errorMessage(e.reason))});
function img(src,alt,note){var im=document.createElement("img");im.alt=alt||"";if(src)im.src=authUrl(src);im.onerror=function(){im.style.display="none";if(note)note.textContent="Preview unavailable"};return im}
function renderStatus(p){p=p||{};text("connection",(p.app||"ADAM")+" online - "+(p.scope||"remote"));var perms=p.permissions||{};var job=p.active_job||null;state.activeJobId=job&&job.id?job.id:"";if(job){text("activeJob",(job.project||"Active job")+" - "+(job.status||"")+" - "+String(job.progress||0)+"% - Time left "+((job.timing||{}).remaining_label||""));text("timeLeft",(job.timing||{}).remaining_label||"-");text("finishTime",(job.timing||{}).finish_label||"-");$("activeBar").style.width=String(job.progress||0)+"%"}else{text("activeJob","No active job.");text("timeLeft","-");text("finishTime","-");$("activeBar").style.width="0%"}var buttons=document.querySelectorAll("[data-action]");for(var i=0;i<buttons.length;i++)buttons[i].disabled=!job||!perms.job_control;$("autoApproveTraining").checked=!!perms.auto_approve_training;$("autoApproveTraining").disabled=!perms.job_control&&!perms.auto_approve_training;renderPreview(p.preview||{});renderLatest(p.latest_generation||{});renderSystem(p.system||{});renderQueues(p)}
function renderPreview(info){var p=$("preview");if(info.available){p.style.display="block";p.src=authUrl("/api/preview","t="+Date.now());text("previewNote",(info.kind||"preview")+" "+(info.current||info.epoch||"")+"/"+(info.total||""))}else{p.style.display="none";p.removeAttribute("src");text("previewNote","Waiting for a preview.")}}
function renderLatest(latest){var root=$("latestGeneration");clear(root);var images=list(latest.images).slice(0,8);if(!latest.available||!images.length){var d=document.createElement("div");d.className="status";d.textContent="Finished generated images will appear here.";root.appendChild(d);return}images.forEach(function(item){var b=document.createElement("button");b.className="thumb";b.type="button";var n=document.createElement("div");n.textContent=latest.model_name||"Generated image";b.appendChild(img(item.url,"Generated image",n));b.appendChild(n);b.onclick=function(){window.open(authUrl(item.url,"t="+Date.now()),"_blank","noopener,noreferrer")};root.appendChild(b)})}
function renderSystem(sys){var root=$("system");clear(root);[["CPU",sys.cpu_percent],["RAM",sys.memory_percent],["GPU",sys.gpu_percent],["VRAM",sys.vram_percent]].forEach(function(m){var d=document.createElement("div");d.className="card";appendText(d,"div",m[0],"meta");appendText(d,"b",m[1]==null?"-":m[1]+"%");root.appendChild(d)})}
function renderQueues(p){var root=$("queues");clear(root);var jobs=list(p.queue).concat(list(p.completed_jobs),list(p.failed_jobs)).slice(0,40);if(!jobs.length){text("queues","No jobs yet.");return}jobs.forEach(function(job){var d=document.createElement("div");d.className="queue-item";appendText(d,"b",job.project||"ADAM Job");appendText(d,"div",(job.status||"")+" - "+String(job.progress||0)+"%","meta");if(job.current_step_title){var s=document.createElement("div");s.className="meta";s.textContent=job.current_step_title;d.appendChild(s)}if(job.error){var e=document.createElement("div");e.className="bad-text";e.textContent=job.error;d.appendChild(e)}root.appendChild(d)})}
function load(){return getJson("/api/status").then(renderStatus).catch(function(e){text("connection","Offline: "+errorMessage(e))})}
function loadData(){return Promise.all([
getJson("/api/v1/datasets").then(function(p){state.datasets=list(p.datasets);renderDatasets();fillDatasets()}),
getJson("/api/v1/datasets/locations").then(function(p){state.locations=list(p.locations);renderLocations()}),
getJson("/api/v1/training/schema").then(function(p){state.trainingSchema=p||{trainers:[]};fillCreate()})
]).catch(function(e){text("trainingReview",errorMessage(e))})}
function datasetMatches(d){var q=($("datasetSearch").value||"").toLowerCase();if(q&&(d.name||"").toLowerCase().indexOf(q)<0)return false;if(state.locationFilter&&d.location_id!==state.locationFilter)return false;return true}
function datasetImage(d){return d.thumbnail_url?img(d.thumbnail_url,"Dataset thumbnail"):document.createElement("span")}
function useDataset(id){if(!id)return;postJson("/api/v1/datasets/"+encodeURIComponent(id)+"/use",{}).then(function(p){var d=p.dataset||{};state.selectedDataset=d.id||id;fillDatasets();if(!$("modelName").value)$("modelName").value=d.name||"";updateSelectedDatasetNote();switchView("createView");loadData()}).catch(function(e){text("datasetStats",errorMessage(e))})}
function datasetCard(d,compact){var row=document.createElement("div");row.className="dataset-card"+(compact?" compact":"")+(d.available?"":" unavailable");var image=datasetImage(d);var body=document.createElement("div");var name=document.createElement("b");name.textContent=d.name||"Dataset";body.appendChild(name);var meta=document.createElement("div");meta.className="meta";meta.textContent=(d.available?String(d.image_count||d.item_count||0)+" images":"Unavailable")+" - "+(d.dataset_format||"Dataset");body.appendChild(meta);var star=document.createElement("button");star.type="button";star.className="icon";star.textContent=d.favorite?"*":"+";star.title=d.favorite?"Remove favorite":"Favorite";star.onclick=function(ev){ev.stopPropagation();postJson("/api/v1/datasets/"+encodeURIComponent(d.id)+"/favorite",{favorite:!d.favorite}).then(loadData)};row.appendChild(image);row.appendChild(body);row.appendChild(star);if(compact){row.onclick=function(){useDataset(d.id)};return row}var actions=document.createElement("div");actions.className="dataset-actions";var preview=document.createElement("button");preview.type="button";preview.className="ghost";preview.textContent="Preview";preview.onclick=function(){openDataset(d.id,1)};var use=document.createElement("button");use.type="button";use.className="primary";use.textContent="Use Dataset";use.onclick=function(){useDataset(d.id)};actions.appendChild(preview);actions.appendChild(use);row.appendChild(actions);return row}
function fillList(id,items,empty,compact){var root=$(id);clear(root);items=items.filter(datasetMatches);if(!items.length){var e=document.createElement("div");e.className="status";e.textContent=empty;root.appendChild(e);return}items.forEach(function(d){root.appendChild(datasetCard(d,compact))})}
function renderDatasets(){var all=state.datasets;fillList("favoriteDatasets",all.filter(function(d){return d.favorite}),"No favorites yet.",false);fillList("recentDatasets",all.filter(function(d){return d.last_used_at}),"Recently used datasets will appear here.",true);fillList("allDatasets",all,"No remembered datasets found. Add locations from desktop Training Studio.",false)}
function renderLocations(){var chips=$("locationChips");clear(chips);var all=document.createElement("button");all.className="chip"+(state.locationFilter?"":" active");all.textContent="All locations";all.onclick=function(){state.locationFilter="";renderLocations();renderDatasets()};chips.appendChild(all);var listRoot=$("locationsList");var inlineRoot=$("locationsListInline");clear(listRoot);clear(inlineRoot);state.locations.forEach(function(loc){var c=document.createElement("button");c.className="chip"+(state.locationFilter===loc.id?" active":"");c.textContent=loc.name+(loc.available?"":" (unavailable)");c.onclick=function(){state.locationFilter=loc.id;renderLocations();renderDatasets()};chips.appendChild(c);var item=document.createElement("div");item.className="queue-item";appendText(item,"b",loc.name);appendText(item,"div",(loc.available?"available":"unavailable")+" - "+loc.source,"meta");listRoot.appendChild(item.cloneNode(true));inlineRoot.appendChild(item)})}
function fillDatasets(){var current=$("trainDataset")?$("trainDataset").value:"";if(!$("trainDataset"))return;clear($("trainDataset"));state.datasets.filter(function(d){return d.available}).forEach(function(d){option($("trainDataset"),d.name+" ("+String(d.image_count||d.item_count||0)+")",d.id)});if(current)$("trainDataset").value=current;if(state.selectedDataset)$("trainDataset").value=state.selectedDataset;updateSelectedDatasetNote()}
function updateSelectedDatasetNote(){var id=state.selectedDataset||($("trainDataset")&&$("trainDataset").value)||"";var d=state.datasets.filter(function(item){return item.id===id})[0];text("selectedDatasetNote",d?d.name+" - "+String(d.image_count||d.item_count||0)+" images":"No dataset selected")}
function openDataset(id,page){state.selectedDataset=id;state.datasetPage=page;text("datasetStats","Loading preview...");$("datasetDetail").style.display="block";getJson("/api/v1/datasets/"+encodeURIComponent(id)+"/items?page="+encodeURIComponent(page)+"&page_size=24").then(function(p){var d=p.dataset||{};text("datasetTitle",d.name||"Dataset");text("datasetStats",String(d.image_count||d.item_count||0)+" items - "+String(d.caption_count||0)+" captions - "+String(d.missing_caption_count||0)+" missing captions - "+(d.dataset_format||"Dataset"));text("pageLabel","Page "+String((p.pagination||{}).page||page));$("prevPage").disabled=page<=1;$("nextPage").disabled=!(p.pagination||{}).has_next;var grid=$("datasetGrid");clear(grid);list(p.items).forEach(function(item){var b=document.createElement("button");b.type="button";b.className="thumb";var note=document.createElement("div");note.textContent=(item.display_name||"Image")+"\\n"+(item.decision||"unreviewed");b.appendChild(img(item.thumbnail_url,"Dataset image",note));b.appendChild(note);b.onclick=function(){showImage(item)};grid.appendChild(b)})}).catch(function(e){text("datasetStats",errorMessage(e))})}
function showImage(item){state.selectedItem=item;$("imageDetail").style.display="block";$("detailImage").src=authUrl(item.preview_url);text("detailName",item.display_name||"Image");text("detailDims",String((item.dimensions||{}).width||0)+" x "+String((item.dimensions||{}).height||0));$("captionEditor").value=item.caption||"";text("imageStatus",item.decision||"unreviewed")}
function fillCreate(){clear($("trainer"));list(state.trainingSchema.trainers).forEach(function(t){option($("trainer"),t.name,t.id)});clear($("preset"));option($("preset"),"Custom","");list(state.trainingSchema.presets).forEach(function(p){option($("preset"),p.name,p.id)});clear($("baseModel"));list(state.trainingSchema.base_models).forEach(function(m){option($("baseModel"),m.name,m.id)});fillDatasets();renderSettings();ensureEpochField()}
function makeSetting(key,spec,advanced){if(key==="base_model")return null;var wrap=document.createElement("div");wrap.className=spec.type==="bool"?"toggle":"form-row";var input;if(spec.type==="bool"){var label=document.createElement("div");appendText(label,"b",spec.label||key);appendText(label,"div",spec.description||"Enable this setting","hint");input=document.createElement("input");input.type="checkbox";input.checked=spec.default!==false;wrap.appendChild(label);wrap.appendChild(input)}else{var label=document.createElement("label");label.textContent=spec.label||key;wrap.appendChild(label);if(spec.type==="choice"){input=document.createElement("select");list(spec.options).forEach(function(v){option(input,String(v),String(v))})}else{input=document.createElement("input");input.type=(spec.type==="int"||spec.type==="float"||spec.type==="slider")?"number":"text";if(spec.min!==undefined)input.min=spec.min;if(spec.max!==undefined)input.max=spec.max;if(spec.step!==undefined)input.step=spec.step}input.value=spec.default!==undefined?String(spec.default):"";wrap.appendChild(input)}input.id=fieldId(key);input.setAttribute("data-setting-key",key);input.setAttribute("data-setting-type",spec.type||"text");input.setAttribute("data-advanced",advanced?"1":"0");return wrap}
function renderSettings(){var t=activeTrainer();var schema=t.settings||{};var basic=$("basicSettings"),advanced=$("advancedSettings");clear(basic);clear(advanced);var basicKeys=["resolution","batch_size","preview_enabled"];Object.keys(schema).forEach(function(key){var spec=schema[key]||{};var isAdvanced=!!spec.advanced||basicKeys.indexOf(key)<0;var node=makeSetting(key,spec,isAdvanced);if(!node)return;(isAdvanced?advanced:basic).appendChild(node)});$("baseModelWrap").style.display=schema.base_model?"grid":"none"}
function settingValue(node){var type=node.getAttribute("data-setting-type");if(type==="bool")return !!node.checked;if(type==="int"||type==="slider")return Number(node.value||0);if(type==="float")return Number(node.value||0);var spec=schemaFor(node.getAttribute("data-setting-key"));if(spec.type==="choice"){var sample=list(spec.options)[0];if(typeof sample==="number")return Number(node.value)}return node.value}
function trainingPayload(){var settings={};var nodes=document.querySelectorAll("[data-setting-key]");for(var i=0;i<nodes.length;i++){var key=nodes[i].getAttribute("data-setting-key");if(key==="epochs")continue;settings[key]=settingValue(nodes[i])}return{trainer:$("trainer").value,dataset_id:state.selectedDataset||$("trainDataset").value,base_model_id:$("baseModel").value,model_name:$("modelName").value,trigger_word:settings.trigger_word||"",epochs:Number($(fieldId("epochs"))&&$(fieldId("epochs")).value||10),settings:settings}}
function applyPreset(){var id=$("preset").value;var preset=list(state.trainingSchema.presets).filter(function(p){return p.id===id})[0];if(!preset)return;if(preset.trainer){$("trainer").value=preset.trainer;renderSettings();ensureEpochField()}if(preset.epochs&&$(fieldId("epochs")))$(fieldId("epochs")).value=preset.epochs;Object.keys(preset.settings||{}).forEach(function(key){var n=$(fieldId(key));if(!n)return;if(n.type==="checkbox")n.checked=!!preset.settings[key];else n.value=String(preset.settings[key])})}
function ensureEpochField(){if(!$(fieldId("epochs"))){var node=makeSetting("epochs",{label:"Epochs",type:"int",default:10,min:1,max:100000},false);$("basicSettings").appendChild(node)}}
$("trainer").onchange=function(){renderSettings();ensureEpochField()};$("preset").onchange=applyPreset;$("datasetSearch").oninput=renderDatasets;$("filterDatasets").onclick=function(){var chips=$("locationChips");chips.style.display=chips.style.display==="none"?"flex":"none"};$("chooseDataset").onclick=function(){switchView("datasetsView")};$("createBack").onclick=function(){switchView("home")};$("createHelp").onclick=function(){text("trainingReview","Choose a preset, trainer, model name, and dataset. Advanced settings come from ADAM's trainer registry.")};$("askInstead").onclick=function(){switchView("home");$("prompt").focus()};$("useDataset").onclick=function(){useDataset(state.selectedDataset)};$("selectDatasetForCreate").onclick=$("useDataset").onclick;
$("reviewPlan").onclick=function(){postJson("/api/v1/training/plan",trainingPayload()).then(function(p){$("trainingReview").textContent=JSON.stringify(p.plan||p,null,2)}).catch(function(e){text("trainingReview",errorMessage(e))})};
$("startTraining").onclick=function(){if(!state.selectedDataset&&!$("trainDataset").value){text("trainingReview","Choose a dataset first.");return}if(!confirm("Start this training job on the PC?"))return;postJson("/api/v1/training/start",trainingPayload()).then(function(p){text("trainingReview",p.message||"Training queued.");load();loadData()}).catch(function(e){text("trainingReview",errorMessage(e))})};
$("prevPage").onclick=function(){openDataset(state.selectedDataset,Math.max(1,state.datasetPage-1))};$("nextPage").onclick=function(){openDataset(state.selectedDataset,state.datasetPage+1)};
$("saveCaption").onclick=function(){if(!state.selectedDataset||!state.selectedItem)return;postJson("/api/v1/datasets/"+encodeURIComponent(state.selectedDataset)+"/items/"+encodeURIComponent(state.selectedItem.id)+"/caption",{caption:$("captionEditor").value}).then(function(p){text("imageStatus",p.message||"Saved.")}).catch(function(e){text("imageStatus",errorMessage(e))})};
function decide(decision){if(!state.selectedDataset||!state.selectedItem)return;postJson("/api/v1/datasets/"+encodeURIComponent(state.selectedDataset)+"/items/"+encodeURIComponent(state.selectedItem.id)+"/decision",{decision:decision}).then(function(p){text("imageStatus",p.message||decision);openDataset(state.selectedDataset,state.datasetPage)}).catch(function(e){text("imageStatus",errorMessage(e))})}
$("keepImage").onclick=function(){decide("keep")};$("rejectImage").onclick=function(){decide("reject")};$("unreviewImage").onclick=function(){decide("unreviewed")};
$("sendPrompt").onclick=function(){postJson("/api/prompt",{prompt:$("prompt").value}).then(function(p){text("promptStatus",p.message||"Sent.");$("prompt").value="";load()}).catch(function(e){text("promptStatus",errorMessage(e))})};$("askCreate").onclick=function(){switchView("createView")};$("askDataset").onclick=function(){$("prompt").value="Collect a new dataset for Example."};$("askQuick").onclick=function(){$("prompt").value="Generate an image of Example."};
$("refresh").onclick=function(){load();loadData()};$("autoApproveTraining").onchange=function(e){postJson("/api/remote-settings",{auto_approve_training:e.target.checked}).then(function(p){text("settingsStatus",p.message||"Saved.");load()}).catch(function(err){text("settingsStatus",errorMessage(err))})};$("keepAwake").onchange=function(e){state.refreshMs=e.target.checked?1500:5000;startTimer();text("settingsStatus",e.target.checked?"Fast refresh is on.":"Quiet refresh is on.")};
function startTimer(){if(timer)clearInterval(timer);timer=setInterval(load,state.refreshMs)}
load();loadData();startTimer();
}());
</script>
</body>
</html>""".gitattributes+36-44
.gitignore+10-16
Launch ADAM.bat+14-50
README.md+365-24
adam/assets.py+76-3
adam/atlas.py+24-4
adam/commands.py+42-17
adam/config.py+9
adam/dataset_lab.pyAdded+127
adam/dataset_registry.pyAdded+484
adam/eve.py+13-2
adam/executor.py+19-27
adam/experiment_tracker.pyAdded+387
adam/generations.py+72
adam/image_preferences.pyAdded+313
adam/job_manager.py+311-10
adam/model_inspector/__init__.pyAdded+15
adam/model_inspector/base.pyAdded+146
adam/model_inspector/comparison.pyAdded+160
adam/model_inspector/ddpm.pyAdded+29
adam/model_inspector/detector.pyAdded+35
adam/model_inspector/flow_matching.pyAdded+25
adam/model_inspector/generic.pyAdded+349
adam/model_inspector/lora.pyAdded+23
adam/model_inspector/maskgit.pyAdded+24
adam/model_inspector/statistics.pyAdded+142
adam/model_plugin_backend.pyAdded+38
adam/model_plugins.pyAdded+494
adam/model_plugins_builtin/__init__.pyAdded+1
adam/model_plugins_builtin/ddpm/__init__.pyAdded+1
adam/model_plugins_builtin/ddpm/manifest.pyAdded+56
adam/model_plugins_builtin/flow_matching/__init__.pyAdded+1
adam/model_plugins_builtin/flow_matching/manifest.pyAdded+51
adam/model_plugins_builtin/model_template/__init__.pyAdded+1
adam/model_plugins_builtin/model_template/manifest.pyAdded+30
adam/model_plugins_builtin/oasis/__init__.pyAdded+1
adam/model_plugins_builtin/oasis/manifest.pyAdded+70
adam/model_plugins_builtin/sdxl_lora/__init__.pyAdded+1
adam/model_plugins_builtin/sdxl_lora/manifest.pyAdded+60
adam/model_profiles.pyAdded+93
adam/models.py+9
adam/oasis_dataset.pyAdded+215
adam/orion.py+17-2
adam/planner.py+303-49
adam/process_control.py+36
adam/recommendations.pyAdded+195
adam/registry.py+52
adam/remote_access.pyAdded
adam/remote_api.pyAdded+147
adam/remote_dashboard.pyAdded+141