gpaasch commited on
Commit
6afe259
·
1 Parent(s): db0b943

1. Only download the specific `.gguf` file we need

Browse files

2. Use less memory during download
3. Avoid the "Killed" error from system memory exhaustion

Files changed (1) hide show
  1. src/app.py +14 -18
src/app.py CHANGED
@@ -81,30 +81,26 @@ MODEL_DIR = os.path.join(BASE_DIR, "models")
81
  MODEL_PATH = os.path.join(MODEL_DIR, MODEL_NAME)
82
 
83
  def ensure_model():
 
84
  # Create models/ directory if missing
85
  os.makedirs(MODEL_DIR, exist_ok=True)
86
 
87
- # Download model if it's not already there
88
  model_path = os.path.join(MODEL_DIR, MODEL_NAME)
89
  if not os.path.isfile(model_path):
90
- print(f"Downloading model from {REPO_ID}...")
91
- # Download to a subdirectory to avoid file conflicts
92
- download_dir = os.path.join(MODEL_DIR, "download_cache")
93
- snapshot_download(
94
- repo_id=REPO_ID,
95
- repo_type="model",
96
- local_dir=download_dir,
97
- local_dir_use_symlinks=False
98
- )
99
 
100
- # Move the specific model file we want to models/
101
- src_path = os.path.join(download_dir, MODEL_NAME)
102
- if os.path.exists(src_path):
103
- import shutil
104
- shutil.move(src_path, model_path)
105
- print(f"Moved model to {model_path}")
106
- else:
107
- raise ValueError(f"Downloaded files but couldn't find {MODEL_NAME}")
 
 
 
 
108
  else:
109
  print(f"Model already exists at {model_path}")
110
 
 
81
  MODEL_PATH = os.path.join(MODEL_DIR, MODEL_NAME)
82
 
83
  def ensure_model():
84
+ """Download specific model file if not exists."""
85
  # Create models/ directory if missing
86
  os.makedirs(MODEL_DIR, exist_ok=True)
87
 
 
88
  model_path = os.path.join(MODEL_DIR, MODEL_NAME)
89
  if not os.path.isfile(model_path):
90
+ print(f"Downloading model {MODEL_NAME} from {REPO_ID}...")
 
 
 
 
 
 
 
 
91
 
92
+ # Download specific file instead of whole repo
93
+ from huggingface_hub import hf_hub_download
94
+ try:
95
+ hf_hub_download(
96
+ repo_id=REPO_ID,
97
+ filename=MODEL_NAME,
98
+ local_dir=MODEL_DIR,
99
+ local_dir_use_symlinks=False
100
+ )
101
+ print(f"Successfully downloaded model to {model_path}")
102
+ except Exception as e:
103
+ raise RuntimeError(f"Failed to download model: {str(e)}")
104
  else:
105
  print(f"Model already exists at {model_path}")
106