Skip to content

Commit 8ed5713

Browse files
committed
download to temp dir and then move to dst, to avoid interruptions when downloading
1 parent 6c6898b commit 8ed5713

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

paddlex/inference/utils/official_models.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
import shutil
17+
import tempfile
1518
from functools import lru_cache
1619
from pathlib import Path
1720

@@ -444,9 +447,17 @@ def _download_from_bos():
444447
def _download_from_hf():
445448
local_dir = self._save_dir / f"{key}"
446449
try:
447-
hf_hub.snapshot_download(
448-
repo_id=f"PaddlePaddle/{key}", local_dir=local_dir
449-
)
450+
if os.path.exists(local_dir):
451+
hf_hub.snapshot_download(
452+
repo_id=f"PaddlePaddle/{key}", local_dir=local_dir
453+
)
454+
else:
455+
with tempfile.TemporaryDirectory() as td:
456+
temp_dir = os.path.join(td, "temp_dir")
457+
hf_hub.snapshot_download(
458+
repo_id=f"PaddlePaddle/{key}", local_dir=temp_dir
459+
)
460+
shutil.move(temp_dir, local_dir)
450461
except Exception as e:
451462
logging.warning(
452463
f"Encounter exception when download model from huggingface: \n{e}.\nPaddleX would try to download from BOS."

0 commit comments

Comments
 (0)