Skip to content

Commit e89b340

Browse files
feat: code style improvements across board
1 parent 38b8c5f commit e89b340

11 files changed

Lines changed: 26 additions & 245 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
"php": "^8.1",
1717
"ext-ffi": "*",
1818
"codewithkyrian/jinja-php": "^1.0",
19+
"codewithkyrian/platform-package-installer": "^1.0",
1920
"imagine/imagine": "^1.3",
2021
"rindow/rindow-math-matrix": "^2.1",
2122
"rindow/rindow-matlib-ffi": "^1.1",
2223
"rindow/rindow-openblas-ffi": "^1.0",
2324
"rokka/imagine-vips": "^0.40",
24-
"symfony/console": "^6.4|^7.0",
25-
"codewithkyrian/platform-package-installer": "^1.0"
25+
"symfony/console": "^6.4|^7.0"
2626
},
2727
"require-dev": {
2828
"ffi/var-dumper": "^1.0",

examples/bootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
require_once './vendor/autoload.php';
99

10+
$cacheDir = '/Volumes/KYRIAN SSD/Transformers';
11+
1012
Transformers::setup()
11-
->setCacheDir('/Users/Kyrian/.transformers')
13+
->setCacheDir($cacheDir)
1214
->setImageDriver(ImageDriver::VIPS);

examples/nextTokenScores.json

Whitespace-only changes.

index.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/DataStructures/CharTrie.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ public function __construct()
2727
*/
2828
public function extend(array $texts): void
2929
{
30-
// foreach ($texts as $text) {
31-
// $this->push($text);
32-
// }
3330
array_map([$this, 'push'], $texts);
3431
}
3532

@@ -61,7 +58,7 @@ public function commonPrefixSearch(string $text): Generator
6158
$node = $this->root;
6259
$prefix = "";
6360
$length = mb_strlen($text);
64-
61+
6562
for ($i = 0; $i < $length && $node != null; $i++) {
6663
$ch = mb_substr($text, $i, 1);
6764
$prefix .= $ch;
@@ -72,4 +69,4 @@ public function commonPrefixSearch(string $text): Generator
7269
}
7370
}
7471
}
75-
}
72+
}

src/Exceptions/ModelExecutionException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class ModelExecutionException extends \Exception implements TransformersException
99
{
10-
public static function make(string $message): self
10+
public static function make(string $message, ?\Throwable $previous = null): self
1111
{
12-
return new self("An error occurred during model execution: $message");
12+
return new self("An error occurred during model execution: $message", 0, $previous);
1313
}
14-
}
14+
}

src/FeatureExtractors/FeatureExtractor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
class FeatureExtractor
1212
{
13-
public function __construct(public array $config)
13+
public function __construct(public array $config) {}
14+
15+
public function __invoke(mixed $input, ...$args)
1416
{
17+
return $input;
1518
}
16-
}
19+
}

src/Utils/Downloader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Downloader
3535
* @param callable|null $onProgress Callback to notify about the download progress
3636
* @return false|string
3737
*/
38-
public static function download(string $url, string $to, array $options = [], callable $onProgress = null): bool|string
38+
public static function download(string $url, string $to, array $options = [], ?callable $onProgress = null): bool|string
3939
{
4040
$curlHandle = curl_init();
4141

@@ -137,4 +137,4 @@ public static function download(string $url, string $to, array $options = [], ca
137137

138138
return $headers;
139139
}
140-
}
140+
}

src/Utils/Hub.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Codewithkyrian\Transformers\Transformers;
99
use Exception;
1010
use RuntimeException;
11-
use Symfony\Component\Console\Helper\ProgressBar;
1211

1312
/**
1413
* Utility class to download files from the Hugging Face Hub
@@ -55,8 +54,7 @@ public static function getFile(
5554
string $subFolder = '',
5655
bool $fatal = true,
5756
?callable $onProgress = null
58-
): ?string
59-
{
57+
): ?string {
6058
# Local cache and file paths
6159
$cacheDir ??= Transformers::getCacheDir();
6260

@@ -72,17 +70,17 @@ public static function getFile(
7270
$partCounter = 1;
7371
$partBasePath = "$filePath.part";
7472

75-
while (file_exists($partBasePath.$partCounter)) {
73+
while (file_exists($partBasePath . $partCounter)) {
7674
$partCounter++;
7775
}
7876

79-
$partPath = $partBasePath.$partCounter;
77+
$partPath = $partBasePath . $partCounter;
8078

8179
# Resume download if partially downloaded
8280
$downloadedBytes = 0;
8381
if ($partCounter > 1) {
8482
for ($i = 1; $i < $partCounter; $i++) {
85-
$downloadedBytes += filesize($partBasePath.$i);
83+
$downloadedBytes += filesize($partBasePath . $i);
8684
}
8785
}
8886

@@ -92,14 +90,14 @@ public static function getFile(
9290
$options = [
9391
'http' => [
9492
'header' => [
95-
'Range: bytes='.$downloadedBytes.'-',
93+
'Range: bytes=' . $downloadedBytes . '-',
9694
],
9795
'User-Agent' => Transformers::getUserAgent(),
9896
],
9997
];
10098

10199
if (Transformers::getAuthToken()) {
102-
$options['http']['header'][] = 'Authorization: Bearer '.Transformers::getAuthToken();
100+
$options['http']['header'][] = 'Authorization: Bearer ' . Transformers::getAuthToken();
103101
}
104102

105103
try {
@@ -146,8 +144,7 @@ public static function getJson(
146144
string $subFolder = '',
147145
bool $fatal = true,
148146
?callable $onProgress = null
149-
): ?array
150-
{
147+
): ?array {
151148
$file = self::getFile($pathOrRepoID, $fileName, $cacheDir, $revision, $subFolder, $fatal, $onProgress);
152149

153150
if ($file === null) {
@@ -166,23 +163,11 @@ public static function getJson(
166163
return $data;
167164
}
168165

169-
170-
private static function onProgress(ProgressBar $progressBar): callable
171-
{
172-
return function ($totalDownload, $downloadedBytes) use ($progressBar) {
173-
if ($totalDownload == 0) return;
174-
175-
$percent = round(($downloadedBytes / $totalDownload) * 100, 2);
176-
$progressBar->setProgress((int)$percent);
177-
};
178-
}
179-
180-
181166
public static function combinePartFiles($filePath, $partBasePath, $partCount): void
182167
{
183168
$fileHandle = fopen($filePath, 'w');
184169
for ($i = 1; $i <= $partCount; $i++) {
185-
$partPath = $partBasePath.$i;
170+
$partPath = $partBasePath . $i;
186171
$partFileHandle = fopen($partPath, 'r');
187172
stream_copy_to_stream($partFileHandle, $fileHandle);
188173
fclose($partFileHandle);

src/Utils/LibsChecker.php

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)