-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_downloader.py
More file actions
407 lines (328 loc) · 12.8 KB
/
Copy pathmodel_downloader.py
File metadata and controls
407 lines (328 loc) · 12.8 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python3
"""
OffGallery - Model Downloader
Scarica i modelli AI dal repository HuggingFace congelato.
Tutti i modelli sono hostati su un repo controllato per garantire stabilità.
"""
import sys
import os
from pathlib import Path
# Determina directory app
def get_app_dir() -> Path:
if getattr(sys, 'frozen', False):
return Path(sys.executable).parent
else:
return Path(__file__).parent
APP_DIR = get_app_dir()
def load_config():
"""Carica configurazione da config_new.yaml"""
import yaml
config_path = APP_DIR / 'config_new.yaml'
if not config_path.exists():
print(f"[ERRORE] Config non trovato: {config_path}")
return None
with open(config_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
def print_header():
print()
print("=" * 60)
print(" OFFGALLERY - DOWNLOAD MODELLI AI")
print("=" * 60)
print()
def print_ok(msg):
print(f"[OK] {msg}")
def print_error(msg):
print(f"[ERRORE] {msg}")
def print_info(msg):
print(f"[...] {msg}")
def get_models_dir(config=None) -> Path:
"""Restituisce il percorso assoluto della directory modelli."""
if config is None:
config = load_config() or {}
rel = config.get('models_repository', {}).get('models_dir', 'Models')
p = Path(rel)
return p if p.is_absolute() else APP_DIR / p
def check_models_exist(config=None):
"""Verifica se i modelli sono già presenti nella models_dir."""
models_dir = get_models_dir(config)
aesthetic_dir = models_dir / 'aesthetic'
clip_dir = models_dir / 'clip'
dinov2_dir = models_dir / 'dinov2'
if not (aesthetic_dir.exists() and (
(aesthetic_dir / 'model.safetensors').exists() or
(aesthetic_dir / 'pytorch_model.bin').exists()
)):
return False
if not (clip_dir.exists() and (clip_dir / 'config.json').exists()):
return False
if not (dinov2_dir.exists() and (dinov2_dir / 'config.json').exists()):
return False
return True
def download_from_hf_repo(repo_id: str, subfolder: str, local_dir: Path, description: str):
"""
Scarica un modello/sottocartella dal repo HuggingFace congelato.
Args:
repo_id: ID del repo (es. "username/OffGallery-models")
subfolder: Sottocartella nel repo (es. "clip", "dinov2")
local_dir: Directory locale dove salvare
description: Descrizione per il log
"""
try:
from huggingface_hub import snapshot_download
print()
print("-" * 60)
print_info(f"Download {description}")
print_info(f"Da: {repo_id}/{subfolder}")
print_info(f"A: {local_dir}")
# Scarica la sottocartella specifica
snapshot_download(
repo_id=repo_id,
allow_patterns=f"{subfolder}/*",
local_dir=local_dir,
local_dir_use_symlinks=False,
)
print_ok(f"{description} scaricato")
return True
except Exception as e:
print_error(f"{description}: {e}")
return False
def download_all_models(repo_id: str, models_config: dict, models_dir: Path):
"""
Scarica tutti i modelli dal repository HuggingFace congelato in models_dir.
Il repo deve avere questa struttura:
repo_id/
├── clip/ (modello CLIP completo)
├── dinov2/ (modello DINOv2 completo)
├── aesthetic/ (modello CLIP per aesthetic)
├── bioclip/ (modello BioCLIP v2)
├── treeoflife/ (dataset TreeOfLife per BioCLIP)
└── argos-it-en/ (pacchetto traduzione Argos)
"""
try:
from huggingface_hub import snapshot_download
except ImportError:
print_error("huggingface_hub non installato")
return False
models_dir.mkdir(parents=True, exist_ok=True)
success = True
print(f"Repository congelato: {repo_id}")
print(f"Destinazione modelli: {models_dir}")
print()
# --- CLIP ---
print()
print("-" * 60)
print_info("Download CLIP (ricerca semantica) - ~580 MB")
try:
clip_subfolder = models_config.get('clip', 'clip')
clip_dir = models_dir / clip_subfolder
if clip_dir.exists() and (clip_dir / 'config.json').exists():
print_ok("CLIP già presente")
else:
snapshot_download(
repo_id=repo_id,
allow_patterns=f"{clip_subfolder}/**",
local_dir=str(models_dir),
local_dir_use_symlinks=False,
)
print_ok("CLIP scaricato")
except Exception as e:
print_error(f"CLIP: {e}")
success = False
# --- DINOv2 ---
print()
print("-" * 60)
print_info("Download DINOv2 (similarità visiva) - ~330 MB")
try:
dinov2_subfolder = models_config.get('dinov2', 'dinov2')
dinov2_dir = models_dir / dinov2_subfolder
if dinov2_dir.exists() and (dinov2_dir / 'config.json').exists():
print_ok("DINOv2 già presente")
else:
snapshot_download(
repo_id=repo_id,
allow_patterns=f"{dinov2_subfolder}/**",
local_dir=str(models_dir),
local_dir_use_symlinks=False,
)
print_ok("DINOv2 scaricato")
except Exception as e:
print_error(f"DINOv2: {e}")
success = False
# --- Aesthetic ---
print()
print("-" * 60)
print_info("Download Aesthetic (score estetico) - ~1.6 GB")
try:
aesthetic_subfolder = models_config.get('aesthetic', 'aesthetic')
aesthetic_dir = models_dir / aesthetic_subfolder
if aesthetic_dir.exists() and (
(aesthetic_dir / 'model.safetensors').exists() or
(aesthetic_dir / 'pytorch_model.bin').exists()
):
print_ok("Aesthetic già presente")
else:
aesthetic_dir.mkdir(exist_ok=True)
snapshot_download(
repo_id=repo_id,
allow_patterns=f"{aesthetic_subfolder}/**",
local_dir=str(models_dir),
local_dir_use_symlinks=False,
)
print_ok("Aesthetic scaricato")
except Exception as e:
print_error(f"Aesthetic: {e}")
success = False
# --- BioCLIP v2 + TreeOfLife ---
print()
print("-" * 60)
print_info("Download BioCLIP v2 + TreeOfLife (classificazione natura) - ~4.2 GB")
try:
bioclip_subfolder = models_config.get('bioclip', 'bioclip')
treeoflife_subfolder = models_config.get('treeoflife', 'treeoflife')
bioclip_dir = models_dir / bioclip_subfolder
treeoflife_dir = models_dir / treeoflife_subfolder
if bioclip_dir.exists() and (bioclip_dir / 'open_clip_model.safetensors').exists():
print_ok("BioCLIP v2 già presente")
else:
snapshot_download(
repo_id=repo_id,
allow_patterns=f"{bioclip_subfolder}/**",
local_dir=str(models_dir),
local_dir_use_symlinks=False,
)
print_ok("BioCLIP v2 scaricato")
if treeoflife_dir.exists() and (treeoflife_dir / 'txt_emb_species.npy').exists():
print_ok("TreeOfLife già presente")
else:
snapshot_download(
repo_id=repo_id,
allow_patterns=f"{treeoflife_subfolder}/**",
local_dir=str(models_dir),
local_dir_use_symlinks=False,
)
print_ok("TreeOfLife scaricato")
except Exception as e:
print_error(f"BioCLIP: {e}")
success = False
return success
def download_argos_from_hf(repo_id: str, models_config: dict):
"""
Scarica il pacchetto Argos Translate IT->EN dal repo HuggingFace.
Il pacchetto è un file .argosmodel che viene installato localmente.
"""
print()
print("-" * 60)
print_info("Download Argos Translate IT->EN - ~92 MB")
try:
import argostranslate.package
# Verifica se già installato
installed = argostranslate.package.get_installed_packages()
for pkg in installed:
if pkg.from_code == 'it' and pkg.to_code == 'en':
print_ok("Argos IT->EN già installato")
return True
# Scarica da HuggingFace
from huggingface_hub import hf_hub_download
argos_subfolder = models_config.get('argos_it_en', 'argos-it-en')
# Cerca il file .argosmodel nel repo
print_info("Download pacchetto da HuggingFace...")
# Il file dovrebbe essere qualcosa come argos-it-en/translate-it_en.argosmodel
local_path = hf_hub_download(
repo_id=repo_id,
filename=f"{argos_subfolder}/translate-it_en.argosmodel",
local_dir=str(APP_DIR / 'temp_argos'),
)
# Installa il pacchetto
print_info("Installazione pacchetto...")
argostranslate.package.install_from_path(local_path)
# Pulizia
import shutil
temp_dir = APP_DIR / 'temp_argos'
if temp_dir.exists():
shutil.rmtree(temp_dir)
print_ok("Argos IT->EN installato da HuggingFace")
return True
except Exception as e:
print_error(f"Argos da HF: {e}")
# Fallback: prova dal server Argos originale
print_info("Tentativo fallback da server Argos...")
return download_argos_fallback()
def download_argos_fallback():
"""Fallback: scarica Argos dal server originale"""
try:
import argostranslate.package
import argostranslate.translate
argostranslate.package.update_package_index()
available = argostranslate.package.get_available_packages()
pkg_to_install = None
for pkg in available:
if pkg.from_code == 'it' and pkg.to_code == 'en':
pkg_to_install = pkg
break
if pkg_to_install is None:
print_error("Pacchetto IT->EN non trovato")
return False
download_path = pkg_to_install.download()
argostranslate.package.install_from_path(download_path)
print_ok("Argos IT->EN installato (fallback)")
return True
except Exception as e:
print_error(f"Argos fallback: {e}")
return False
def run_download(force=False):
"""Esegue il download dei modelli"""
print_header()
# Carica config
config = load_config()
if config is None:
return False
repo_config = config.get('models_repository', {})
repo_id = repo_config.get('huggingface_repo', '')
auto_download = repo_config.get('auto_download', True)
models_config = repo_config.get('models', {})
models_dir = get_models_dir(config)
if not repo_id:
print_error("huggingface_repo non configurato in config_new.yaml")
print_info("Imposta models_repository.huggingface_repo nel config")
return False
if not auto_download and not force:
print_info("Auto-download disabilitato in config")
return True
# Verifica se già scaricati
if not force and check_models_exist(config):
print_ok("Modelli già presenti, skip download")
print_info("Usa --force per forzare il re-download")
return True
print(f"Repository congelato: {repo_id}")
print(f"Destinazione modelli: {models_dir}")
print()
print("NOTA: I modelli sono congelati sul tuo repository HuggingFace")
print(" per garantire compatibilità futura.")
print()
# Download
hf_ok = download_all_models(repo_id, models_config, models_dir)
argos_ok = download_argos_from_hf(repo_id, models_config)
# Riepilogo
print()
print("=" * 60)
if hf_ok and argos_ok:
print_ok("Tutti i modelli scaricati con successo!")
print()
print("Ora puoi avviare OffGallery.exe")
else:
print("[!!] Alcuni modelli non sono stati scaricati")
print(" Riprova eseguendo: python model_downloader.py --force")
print("=" * 60)
return hf_ok and argos_ok
def main():
"""Entry point per esecuzione standalone"""
import argparse
parser = argparse.ArgumentParser(description='OffGallery Model Downloader')
parser.add_argument('--force', action='store_true', help='Forza re-download')
args = parser.parse_args()
success = run_download(force=args.force)
if not getattr(sys, 'frozen', False):
input("\nPremi INVIO per chiudere...")
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()