@@ -120,29 +120,35 @@ def __map_app_static_folders(fast_api_app: FastAPI):
120
120
fast_api_app .mount (f"/{ mnt_dir } " , staticfiles .StaticFiles (directory = mnt_dir_path ), name = mnt_dir )
121
121
122
122
123
- def fetch_models_task (nc : NextcloudApp , models : dict [str , dict ], progress_init_start_value : int ) -> None :
123
+ def fetch_models_task (
124
+ nc : NextcloudApp , models : dict [str , dict ], progress_init_start_value : int
125
+ ) -> dict [str , str | None ]:
124
126
"""Use for cases when you want to define custom `/init` but still need to easy download models."""
127
+ model_paths = {}
125
128
if models :
126
129
current_progress = progress_init_start_value
127
130
percent_for_each = min (int ((100 - progress_init_start_value ) / len (models )), 99 )
128
131
for model in models :
129
132
if model .startswith (("http://" , "https://" )):
130
- __fetch_model_as_file (current_progress , percent_for_each , nc , model , models [model ])
133
+ model_paths [ model ] = __fetch_model_as_file (current_progress , percent_for_each , nc , model , models [model ])
131
134
else :
132
- __fetch_model_as_snapshot (current_progress , percent_for_each , nc , model , models [model ])
135
+ model_paths [model ] = __fetch_model_as_snapshot (
136
+ current_progress , percent_for_each , nc , model , models [model ]
137
+ )
133
138
current_progress += percent_for_each
134
139
nc .set_init_status (100 )
140
+ return model_paths
135
141
136
142
137
143
def __fetch_model_as_file (
138
144
current_progress : int , progress_for_task : int , nc : NextcloudApp , model_path : str , download_options : dict
139
- ) -> None :
145
+ ) -> str | None :
140
146
result_path = download_options .pop ("save_path" , urlparse (model_path ).path .split ("/" )[- 1 ])
141
147
try :
142
148
with httpx .stream ("GET" , model_path , follow_redirects = True ) as response :
143
149
if not response .is_success :
144
150
nc .log (LogLvl .ERROR , f"Downloading of '{ model_path } ' returned { response .status_code } status." )
145
- return
151
+ return None
146
152
downloaded_size = 0
147
153
linked_etag = ""
148
154
for each_history in response .history :
@@ -163,7 +169,7 @@ def __fetch_model_as_file(
163
169
sha256_hash .update (byte_block )
164
170
if f'"{ sha256_hash .hexdigest ()} "' == linked_etag :
165
171
nc .set_init_status (min (current_progress + progress_for_task , 99 ))
166
- return
172
+ return None
167
173
168
174
with builtins .open (result_path , "wb" ) as file :
169
175
last_progress = current_progress
@@ -174,13 +180,17 @@ def __fetch_model_as_file(
174
180
if new_progress != last_progress :
175
181
nc .set_init_status (new_progress )
176
182
last_progress = new_progress
183
+
184
+ return result_path
177
185
except Exception as e : # noqa pylint: disable=broad-exception-caught
178
186
nc .log (LogLvl .ERROR , f"Downloading of '{ model_path } ' raised an exception: { e } " )
179
187
188
+ return None
189
+
180
190
181
191
def __fetch_model_as_snapshot (
182
192
current_progress : int , progress_for_task , nc : NextcloudApp , mode_name : str , download_options : dict
183
- ) -> None :
193
+ ) -> str :
184
194
from huggingface_hub import snapshot_download # noqa isort:skip pylint: disable=C0415 disable=E0401
185
195
from tqdm import tqdm # noqa isort:skip pylint: disable=C0415 disable=E0401
186
196
@@ -191,7 +201,9 @@ def display(self, msg=None, pos=None):
191
201
192
202
workers = download_options .pop ("max_workers" , 2 )
193
203
cache = download_options .pop ("cache_dir" , persistent_storage ())
194
- snapshot_download (mode_name , tqdm_class = TqdmProgress , ** download_options , max_workers = workers , cache_dir = cache )
204
+ return snapshot_download (
205
+ mode_name , tqdm_class = TqdmProgress , ** download_options , max_workers = workers , cache_dir = cache
206
+ )
195
207
196
208
197
209
def __nc_app (request : HTTPConnection ) -> dict :
0 commit comments