Skip to content

Fix NameError in download_weights.py and add GPU memory cleanup in gradio_app.py#96

Open
fox3nox wants to merge 1 commit into
character-ai:mainfrom
fox3nox:fix/download-weights-and-resource-leaks
Open

Fix NameError in download_weights.py and add GPU memory cleanup in gradio_app.py#96
fox3nox wants to merge 1 commit into
character-ai:mainfrom
fox3nox:fix/download-weights-and-resource-leaks

Conversation

@fox3nox

@fox3nox fox3nox commented Apr 15, 2026

Copy link
Copy Markdown

Summary

  • Critical bug fix: download_weights.py referenced args.models inside main(), but args is only defined in the __main__ block — this caused a NameError whenever main() was called programmatically or imported. Fixed by passing models as an explicit parameter to main().
  • Validation improvement: Replaced the bare assert for invalid model names with a proper ValueError and a descriptive error message.
  • GPU resource management: gradio_app.py now handles torch.cuda.OutOfMemoryError separately with a user-friendly message and immediately frees VRAM via torch.cuda.empty_cache() + gc.collect(). A finally block ensures cleanup runs after every generation, preventing VRAM leaks between calls.
  • Temp file cleanup: Partial/failed output files are now deleted on error instead of being left on disk.

Test plan

  • Run python download_weights.py --models 720x720_5s — should download without error
  • Import main from download_weights in a Python shell and call it directly — no NameError
  • Pass an invalid model name (e.g. --models invalid) — should raise ValueError with a clear message
  • Trigger an OOM error in gradio_app.py (e.g. very high resolution) — should log a clear message and not leave temp files behind
  • Run multiple generations in sequence and verify VRAM is released between runs

🤖 Generated with Claude Code

- Fix critical bug where `args.models` was referenced inside `main()` but
  only defined in the `__main__` block, causing a NameError on any import.
  `models` is now passed as an explicit parameter to `main()`.
- Replace bare `assert` with a proper `ValueError` for invalid model names.
- Add `torch.cuda.OutOfMemoryError` handling in `generate_video` to surface
  a clear message and free GPU memory instead of silently returning None.
- Clean up temp files on error and call `torch.cuda.empty_cache()` + `gc.collect()`
  in a `finally` block to prevent VRAM leaks between generations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants