-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_python.py
More file actions
196 lines (175 loc) · 5.02 KB
/
build_python.py
File metadata and controls
196 lines (175 loc) · 5.02 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
PyInstaller Build Script for Faces AI Python Dependencies
Builds standalone executables for YOLO, Kokoro, Whisper, Piper, and Local STT
"""
import subprocess
import sys
import os
import shutil
# Ensure PyInstaller is installed
try:
import PyInstaller
except ImportError:
print("Installing PyInstaller...")
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pyinstaller'])
import PyInstaller.__main__
# Build configuration for each Python backend
BUILDS = [
{
'script': 'python/yolo_tracker.py',
'name': 'yolo_tracker',
'hidden_imports': [
'ultralytics',
'ultralytics.nn',
'ultralytics.utils',
'ultralytics.engine',
'torch',
'torchvision',
'cv2',
'PIL',
'numpy',
],
'collect_all': ['ultralytics'],
'data_files': [
('yolov11n.pt', '.'),
],
},
{
'script': 'python/kokoro_tts.py',
'name': 'kokoro_tts',
'hidden_imports': [
'kokoro',
'torch',
'numpy',
'soundfile',
],
'collect_all': ['kokoro'],
'data_files': [],
},
{
'script': 'python/local_whisper.py',
'name': 'local_whisper',
'hidden_imports': [
'faster_whisper',
'ctranslate2',
'torch',
'numpy',
'speech_recognition',
],
'collect_all': ['faster_whisper'],
'data_files': [],
},
{
'script': 'python/piper_tts.py',
'name': 'piper_tts',
'hidden_imports': [
'piper',
'onnxruntime',
'numpy',
],
'collect_all': [],
'data_files': [
('python/models', 'models'),
],
},
{
'script': 'python/local_stt.py',
'name': 'local_stt',
'hidden_imports': [
'faster_whisper',
'torch',
'numpy',
],
'collect_all': ['faster_whisper'],
'data_files': [],
},
]
def clean_build_dirs():
"""Clean previous build artifacts"""
dirs_to_clean = ['python-dist', 'python-build']
for d in dirs_to_clean:
if os.path.exists(d):
print(f"Cleaning {d}...")
shutil.rmtree(d)
def build_executable(config):
"""Build a single Python script into an executable"""
script = config['script']
name = config['name']
if not os.path.exists(script):
print(f"Warning: {script} not found, skipping...")
return False
print(f"\n{'='*60}")
print(f"Building {name}...")
print(f"{'='*60}\n")
# Base PyInstaller arguments
args = [
script,
'--onedir',
'--name', name,
'--distpath', 'python-dist',
'--workpath', 'python-build',
'--specpath', 'python-build',
'--noconfirm',
'--clean',
]
# Add hidden imports
for imp in config.get('hidden_imports', []):
args.extend(['--hidden-import', imp])
# Add collect-all for packages that need all submodules
for pkg in config.get('collect_all', []):
args.extend(['--collect-all', pkg])
# Add data files
for src, dst in config.get('data_files', []):
if os.path.exists(src):
args.extend(['--add-data', f'{src};{dst}'])
else:
print(f"Warning: Data file {src} not found")
try:
PyInstaller.__main__.run(args)
print(f"Successfully built {name}")
return True
except Exception as e:
print(f"Error building {name}: {e}")
return False
def build_all():
"""Build all Python backends"""
print("Faces AI - Python Dependency Builder")
print("="*60)
# Clean previous builds
clean_build_dirs()
# Build each executable
results = {}
for config in BUILDS:
success = build_executable(config)
results[config['name']] = success
# Summary
print("\n" + "="*60)
print("BUILD SUMMARY")
print("="*60)
for name, success in results.items():
status = "SUCCESS" if success else "FAILED"
print(f" {name}: {status}")
# Calculate total size
if os.path.exists('python-dist'):
total_size = 0
for root, dirs, files in os.walk('python-dist'):
for f in files:
total_size += os.path.getsize(os.path.join(root, f))
print(f"\nTotal size: {total_size / (1024*1024):.1f} MB")
print("\nBuild complete! Executables are in python-dist/")
print("Run 'npm run package:win' to build the final Electron app")
def build_single(name):
"""Build a single executable by name"""
for config in BUILDS:
if config['name'] == name:
build_executable(config)
return
print(f"Unknown build target: {name}")
print(f"Available: {', '.join(c['name'] for c in BUILDS)}")
if __name__ == '__main__':
if len(sys.argv) > 1:
# Build specific target
build_single(sys.argv[1])
else:
# Build all
build_all()