99import httpx
1010from huggingface_hub import HfApi
1111
12+ from pyrit .common .deprecation import print_deprecation_message
13+
1214logger = logging .getLogger (__name__ )
1315
1416
@@ -37,7 +39,9 @@ def get_available_files(model_id: str, token: str) -> list[str]:
3739 return []
3840
3941
40- async def download_specific_files (model_id : str , file_patterns : list [str ] | None , token : str , cache_dir : Path ) -> None :
42+ async def download_specific_files_async (
43+ model_id : str , file_patterns : list [str ] | None , token : str , cache_dir : Path
44+ ) -> None :
4145 """
4246 Download specific files from a Hugging Face model repository.
4347 If file_patterns is None, downloads all files.
@@ -61,10 +65,12 @@ async def download_specific_files(model_id: str, file_patterns: list[str] | None
6165 urls = [base_url + file for file in files_to_download ]
6266
6367 # Download the files
64- await download_files (urls , token , cache_dir )
68+ await download_files_async (urls , token , cache_dir )
6569
6670
67- async def download_chunk (url : str , headers : dict [str , str ], start : int , end : int , client : httpx .AsyncClient ) -> bytes :
71+ async def download_chunk_async (
72+ url : str , headers : dict [str , str ], start : int , end : int , client : httpx .AsyncClient
73+ ) -> bytes :
6874 """
6975 Download a chunk of the file with a specified byte range.
7076
@@ -77,7 +83,7 @@ async def download_chunk(url: str, headers: dict[str, str], start: int, end: int
7783 return response .content
7884
7985
80- async def download_file (url : str , token : str , download_dir : Path , num_splits : int ) -> None :
86+ async def download_file_async (url : str , token : str , download_dir : Path , num_splits : int ) -> None :
8187 """Download a file in multiple segments (splits) using byte-range requests."""
8288 headers = {"Authorization" : f"Bearer { token } " }
8389 async with httpx .AsyncClient (follow_redirects = True ) as client :
@@ -95,7 +101,7 @@ async def download_file(url: str, token: str, download_dir: Path, num_splits: in
95101 for i in range (num_splits ):
96102 start = i * chunk_size
97103 end = start + chunk_size - 1 if i < num_splits - 1 else file_size - 1
98- tasks .append (download_chunk (url , headers , start , end , client ))
104+ tasks .append (download_chunk_async (url , headers , start , end , client ))
99105
100106 # Download all chunks concurrently
101107 chunks = await asyncio .gather (* tasks )
@@ -107,16 +113,63 @@ async def download_file(url: str, token: str, download_dir: Path, num_splits: in
107113 logger .info (f"Downloaded { file_name } to { file_path } " )
108114
109115
110- async def download_files (
116+ async def download_files_async (
111117 urls : list [str ], token : str , download_dir : Path , num_splits : int = 3 , parallel_downloads : int = 4
112118) -> None :
113119 """Download multiple files with parallel downloads and segmented downloading."""
114120 # Limit the number of parallel downloads
115121 semaphore = asyncio .Semaphore (parallel_downloads )
116122
117- async def download_with_limit (url : str ) -> None :
123+ async def download_with_limit_async (url : str ) -> None :
118124 async with semaphore :
119- await download_file (url , token , download_dir , num_splits )
125+ await download_file_async (url , token , download_dir , num_splits )
120126
121127 # Run downloads concurrently, but limit to parallel_downloads at a time
122- await asyncio .gather (* (download_with_limit (url ) for url in urls ))
128+ await asyncio .gather (* (download_with_limit_async (url ) for url in urls ))
129+
130+
131+ async def download_specific_files (model_id : str , file_patterns : list [str ] | None , token : str , cache_dir : Path ) -> None :
132+ """Delegate to :func:`download_specific_files_async` (deprecated alias)."""
133+ print_deprecation_message (
134+ old_item = "pyrit.common.download_hf_model.download_specific_files" ,
135+ new_item = "pyrit.common.download_hf_model.download_specific_files_async" ,
136+ removed_in = "0.16.0" ,
137+ )
138+ await download_specific_files_async (model_id , file_patterns , token , cache_dir )
139+
140+
141+ async def download_chunk (url : str , headers : dict [str , str ], start : int , end : int , client : httpx .AsyncClient ) -> bytes :
142+ """
143+ Delegate to :func:`download_chunk_async` (deprecated alias).
144+
145+ Returns:
146+ The content of the downloaded chunk.
147+ """
148+ print_deprecation_message (
149+ old_item = "pyrit.common.download_hf_model.download_chunk" ,
150+ new_item = "pyrit.common.download_hf_model.download_chunk_async" ,
151+ removed_in = "0.16.0" ,
152+ )
153+ return await download_chunk_async (url , headers , start , end , client )
154+
155+
156+ async def download_file (url : str , token : str , download_dir : Path , num_splits : int ) -> None :
157+ """Delegate to :func:`download_file_async` (deprecated alias)."""
158+ print_deprecation_message (
159+ old_item = "pyrit.common.download_hf_model.download_file" ,
160+ new_item = "pyrit.common.download_hf_model.download_file_async" ,
161+ removed_in = "0.16.0" ,
162+ )
163+ await download_file_async (url , token , download_dir , num_splits )
164+
165+
166+ async def download_files (
167+ urls : list [str ], token : str , download_dir : Path , num_splits : int = 3 , parallel_downloads : int = 4
168+ ) -> None :
169+ """Delegate to :func:`download_files_async` (deprecated alias)."""
170+ print_deprecation_message (
171+ old_item = "pyrit.common.download_hf_model.download_files" ,
172+ new_item = "pyrit.common.download_hf_model.download_files_async" ,
173+ removed_in = "0.16.0" ,
174+ )
175+ await download_files_async (urls , token , download_dir , num_splits , parallel_downloads )
0 commit comments