-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrun.py
More file actions
67 lines (55 loc) · 1.72 KB
/
run.py
File metadata and controls
67 lines (55 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""
AutoShorts - Entry point script.
Run this from the project root to generate shorts.
"""
import sys
from pathlib import Path
import signal
import logging
import gc
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
from shorts import main
def _cleanup_models():
"""Attempt to clear model singletons and free GPU memory."""
logging.info("Cleaning up models and GPU resources...")
# Try clearing Qwen TTS singleton if present
try:
from tts_generator import QwenTTS
QwenTTS.clear_instance()
logging.info("Cleared QwenTTS instance")
except Exception as e:
logging.debug(f"No QwenTTS to clear or clear failed: {e}")
# Try to free torch CUDA memory if torch is available
try:
import torch
if torch.cuda.is_available():
torch.cuda.empty_cache()
logging.info("Emptied torch.cuda cache")
except Exception as e:
logging.debug(f"torch cleanup failed: {e}")
# Force garbage collection
try:
gc.collect()
logging.info("Garbage collection complete")
except Exception:
pass
def _signal_handler(signum, frame):
logging.info(f"Received signal {signum}, shutting down...")
_cleanup_models()
# Exit cleanly
sys.exit(0)
# Register signal handlers for graceful shutdown
signal.signal(signal.SIGINT, _signal_handler)
signal.signal(signal.SIGTERM, _signal_handler)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logging.info("Keyboard interrupt received, cleaning up...")
_cleanup_models()
sys.exit(0)
finally:
# Ensure cleanup on normal exit as well
_cleanup_models()