-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHub.php
212 lines (172 loc) · 6.9 KB
/
Hub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
declare(strict_types=1);
namespace Codewithkyrian\Transformers\Utils;
use Codewithkyrian\Transformers\Exceptions\HubException;
use Codewithkyrian\Transformers\Transformers;
use Exception;
use Symfony\Component\Console\Helper\ProgressBar;
/**
* Utility class to download files from the Hugging Face Hub
*/
class Hub
{
private const ERROR_MAPPING = [
'400' => 'Bad request error occurred while trying to load file',
'401' => 'Unauthorized access to file',
'403' => 'Forbidden access to file',
'404' => 'File not found',
'408' => 'Timeout error occurred while trying to load file',
'500' => 'Internal Server Error occurred while trying to load file',
'502' => 'Bad Gateway error occurred while trying to load file',
'503' => 'Service Unavailable error occurred while trying to load file',
'504' => 'Gateway Timeout error occurred while trying to load file',
];
/**
* Tries to locate a file in a local folder and repo, downloads and cache it if necessary.
*
* @param string $pathOrRepoID This can be either a string, the "model id" of a model repo on huggingface.co,
* or a path to a local directory containing a model.
* @param string $fileName The name of the file to locate in $pathOrRepoID.
* @param string|null $cacheDir Path to a directory in which a downloaded pretrained model configuration should
* be cached if the standard cache should not be used.
* the cached versions if they exist. Defaults to false.
* @param string $revision The specific model version to use. It can be a branch name, a tag name,
* or a commit id. Defaults to 'main'.
* @param string $subFolder In case the relevant files are located inside a subfolder of the model repo or
* directory, indicate it here.
* @param bool $fatal Whether to raise an error if the file could not be loaded.
*
* @throws HubException
*/
public static function getFile(
string $pathOrRepoID,
string $fileName,
?string $cacheDir = null,
string $revision = 'main',
string $subFolder = '',
bool $fatal = true,
?callable $onProgress = null
): ?string
{
# Local cache and file paths
$cacheDir ??= Transformers::$cacheDir;
$filePath = joinPaths($cacheDir, $pathOrRepoID, $subFolder, $fileName);
# Check if file already exists
if (file_exists($filePath)) {
return $filePath;
}
$remoteURL = self::resolveRepositoryURL($pathOrRepoID, $revision, $fileName, $subFolder);
$partCounter = 1;
$partBasePath = "$filePath.part";
while (file_exists($partBasePath . $partCounter)) {
$partCounter++;
}
$partPath = $partBasePath . $partCounter;
# Resume download if partially downloaded
$downloadedBytes = 0;
if ($partCounter > 1) {
for ($i = 1; $i < $partCounter; $i++) {
$downloadedBytes += filesize($partBasePath . $i);
}
}
# Create directory structure if needed
ensureDirectory($filePath);
$options = [
'http' => [
'header' => [
'Range: bytes=' . $downloadedBytes . '-',
],
'User-Agent' => Transformers::$userAgent,
],
];
if (Transformers::$authToken) {
$options['http']['header'][] = 'Authorization: Bearer ' . Transformers::$authToken;
}
try {
if ($onProgress) {
$onProgress('begin_download', $fileName, 0, 0, 0, 0);
}
$progressCallback = function ($downloadSize, $downloaded, $uploadSize, $uploaded) use ($onProgress, $fileName) {
if ($onProgress) {
$onProgress('advance_download', $fileName, $downloadSize, $downloaded, $uploadSize, $uploaded);
}
};
Downloader::download($remoteURL, $partPath, $options, $progressCallback);
if ($onProgress) {
$onProgress('complete_download', $fileName, 0, 0, 0, 0);
}
# Combine part files if necessary
if ($partCounter > 1) {
self::combinePartFiles($filePath, $partBasePath, $partCounter);
} else {
rename($partPath, $filePath);
}
return $filePath;
} catch (Exception $e) {
self::handleException($e->getCode(), $remoteURL, $fatal);
}
return null;
}
/**
* @throws HubException
*/
public static function getJson(
string $pathOrRepoID,
string $fileName,
?string $cacheDir = null,
string $revision = 'main',
string $subFolder = '',
bool $fatal = true,
?callable $onProgress = null
): ?array
{
$file = self::getFile($pathOrRepoID, $fileName, $cacheDir, $revision, $subFolder, $fatal, $onProgress);
if ($file === null) {
return null;
}
return json_decode(file_get_contents($file), true);
}
private static function onProgress(ProgressBar $progressBar): callable
{
return function ($totalDownload, $downloadedBytes) use ($progressBar) {
if ($totalDownload == 0) return;
$percent = round(($downloadedBytes / $totalDownload) * 100, 2);
$progressBar->setProgress((int)$percent);
};
}
public static function combinePartFiles($filePath, $partBasePath, $partCount): void
{
$fileHandle = fopen($filePath, 'w');
for ($i = 1; $i <= $partCount; $i++) {
$partPath = $partBasePath . $i;
$partFileHandle = fopen($partPath, 'r');
stream_copy_to_stream($partFileHandle, $fileHandle);
fclose($partFileHandle);
unlink($partPath);
}
fclose($fileHandle);
}
/**
* @throws HubException
*/
private static function handleException(int $statusCode, string $remoteURL, bool $fatal = true): void
{
if (!$fatal) {
// File was not loaded correctly, but it is optional.
// TODO in future, cache the response?
return;
}
$message = self::ERROR_MAPPING[$statusCode] ?? "Error $statusCode occurred while trying to load file from $remoteURL";
throw new HubException($message, $statusCode);
}
private static function resolveRepositoryURL(string $pathOrRepoID, string $revision, string $fileName, string $subFolder): string
{
$remoteHost = Transformers::$remoteHost;
$remotePath = str_replace(
['{model}', '{revision}', '{file}'],
[$pathOrRepoID, $revision, $subFolder === '' ? $fileName : "$subFolder/$fileName"],
Transformers::$remotePathTemplate
);
return "$remoteHost/$remotePath";
}
}