cool commited on
Commit
46c3d2e
·
verified ·
1 Parent(s): e97ce18

Update install_model.py

Browse files
Files changed (1) hide show
  1. install_model.py +15 -12
install_model.py CHANGED
@@ -1,34 +1,37 @@
1
  import os
2
  import zipfile
3
  import argostranslate.package
 
4
 
5
  model_dir = "/app/models"
6
  install_dir = "/app/.local/share/argos-translate/packages"
7
 
 
 
 
 
 
 
8
  def is_valid_argosmodel(path):
9
  try:
10
- with zipfile.ZipFile(path, 'r') as z:
11
- return 'metadata.json' in z.namelist()
12
  except zipfile.BadZipFile:
13
  return False
14
 
15
- print(f"Looking for models in: {model_dir}")
16
-
17
  for filename in os.listdir(model_dir):
18
  if filename.endswith(".argosmodel"):
19
- model_path = os.path.join(model_dir, filename)
20
 
21
- if not is_valid_argosmodel(model_path):
22
- print(f"Skipping invalid model: {filename}")
23
  continue
24
 
25
  try:
26
  print(f"Installing model: {filename}")
27
- argostranslate.package.install_from_path(model_path)
28
  except Exception as e:
29
  print(f"Error installing {filename}: {e}")
30
 
31
- print("\nInstallation complete. Installed model directories:")
32
- for root, dirs, files in os.walk(install_dir):
33
- for d in dirs:
34
- print(f"Folder {os.path.join(root, d)}")
 
1
  import os
2
  import zipfile
3
  import argostranslate.package
4
+ import shutil
5
 
6
  model_dir = "/app/models"
7
  install_dir = "/app/.local/share/argos-translate/packages"
8
 
9
+ # Cleanup: Remove invalid model folder if it exists
10
+ bad_path = os.path.join(install_dir, "model")
11
+ if os.path.isdir(bad_path):
12
+ shutil.rmtree(bad_path)
13
+ print(f"Removed invalid leftover package: {bad_path}")
14
+
15
  def is_valid_argosmodel(path):
16
  try:
17
+ with zipfile.ZipFile(path, 'r') as archive:
18
+ return 'metadata.json' in archive.namelist()
19
  except zipfile.BadZipFile:
20
  return False
21
 
22
+ # Install all valid .argosmodel files
 
23
  for filename in os.listdir(model_dir):
24
  if filename.endswith(".argosmodel"):
25
+ full_path = os.path.join(model_dir, filename)
26
 
27
+ if not is_valid_argosmodel(full_path):
28
+ print(f"Skipping invalid model file: {filename}")
29
  continue
30
 
31
  try:
32
  print(f"Installing model: {filename}")
33
+ argostranslate.package.install_from_path(full_path)
34
  except Exception as e:
35
  print(f"Error installing {filename}: {e}")
36
 
37
+ print("All valid models installed.")