Skip to content

Commit d023c00

Browse files
Reintroduce install command, new Library checker, and use curl direcly for downloads
1 parent 9aeae25 commit d023c00

68 files changed

Lines changed: 508 additions & 210 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/transformers

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $application = new Application();
1212
try {
1313
$application->setName('Transformers PHP CLI');
1414

15-
// $application->add(new Codewithkyrian\Transformers\Commands\InitCommand());
15+
$application->add(new Codewithkyrian\Transformers\Commands\InstallCommand());
1616
$application->add(new Codewithkyrian\Transformers\Commands\DownloadModelCommand());
1717

1818
$application->run();

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"codewithkyrian/jinja-php": "^1.0",
1919
"codewithkyrian/transformers-libraries-downloader": "^1.0@dev",
2020
"imagine/imagine": "^1.3",
21-
"guzzlehttp/guzzle": "^7.0",
2221
"symfony/console": "^6.4|^7.0",
2322
"rokka/imagine-vips": "^0.31.0",
2423
"rindow/rindow-math-matrix": "^2.0",
@@ -63,7 +62,9 @@
6362
],
6463
"scripts": {
6564
"test": "vendor/bin/pest",
66-
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
65+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage",
66+
"post-install-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check",
67+
"post-update-cmd": "Codewithkyrian\\Transformers\\Utils\\LibsChecker::check"
6768
},
6869
"archive": {
6970
"exclude": [

docs/utils/image.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ can leverage this interoperability to perform advanced image processing tasks wi
268268

269269
```php
270270
use Codewithkyrian\Transformers\Utils\Image;
271-
use Codewithkyrian\Transformers\Utils\Tensor;
272271

273272
// Read an image
274273
$image = Image::read('path/to/image.jpg');

docs/utils/tensor.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ fromArray method. Below are examples of how to create tensors:
4747
### Using the Constructor
4848

4949
```php
50-
use Codewithkyrian\Transformers\Utils\Tensor;
51-
use Interop\Polite\Math\Matrix\NDArray;
50+
use Codewithkyrian\Transformers\Tensor\Tensor;use Interop\Polite\Math\Matrix\NDArray;
5251

5352
$data = [1, 2, 3, 4, 5, 6];
5453
$shape = [2, 3];
@@ -59,7 +58,7 @@ $tensor = new Tensor($data, $dtype, $shape); // If dtype is not provided, it def
5958
### Using the fromArray Method
6059

6160
```php
62-
use Codewithkyrian\Transformers\Utils\Tensor;
61+
use Codewithkyrian\Transformers\Tensor\Tensor;
6362

6463
$data = [[1, 2, 3], [4, 5, 6]];
6564
$tensor = Tensor::fromArray($data);
@@ -68,7 +67,7 @@ $tensor = Tensor::fromArray($data);
6867
### Using zeros and ones methods
6968

7069
```php
71-
use Codewithkyrian\Transformers\Utils\Tensor;
70+
use Codewithkyrian\Transformers\Tensor\Tensor;
7271

7372
$shape = [2, 3];
7473
$tensor = Tensor::zeros($shape); // Creates a tensor of zeros with the specified shape
@@ -78,7 +77,7 @@ $tensor = Tensor::ones($shape); // Creates a tensor of ones with the specified s
7877
### Using zerosLike and onesLike methods
7978

8079
```php
81-
use Codewithkyrian\Transformers\Utils\Tensor;
80+
use Codewithkyrian\Transformers\Tensor\Tensor;
8281

8382
$data = [[1, 2, 3], [4, 5, 6]];
8483
$tensor = Tensor::fromArray($data);
@@ -114,7 +113,7 @@ and buffer.
114113
Returns the tensor's flat buffer as a regular PHP array.
115114

116115
```php
117-
use Codewithkyrian\Transformers\Utils\Tensor;
116+
use Codewithkyrian\Transformers\Tensor\Tensor;
118117

119118
$data = [[1, 2, 3], [4, 5, 6]];
120119

examples/image-test.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
declare(strict_types=1);
44

5+
use Codewithkyrian\Transformers\Tensor\Tensor;
56
use Codewithkyrian\Transformers\Transformers;
67
use Codewithkyrian\Transformers\Utils\Image;
78
use Codewithkyrian\Transformers\Utils\ImageDriver;
8-
use Codewithkyrian\Transformers\Utils\Tensor;
99
use function Codewithkyrian\Transformers\Utils\timeUsage;
1010

1111
require_once './bootstrap.php';
1212

13+
ini_set('memory_limit', '2048M');
14+
1315
function toTensorTest(ImageDriver $imageDriver): Tensor
1416
{
1517
timeUsage();
@@ -18,7 +20,7 @@ function toTensorTest(ImageDriver $imageDriver): Tensor
1820
->setImageDriver($imageDriver)
1921
->apply();
2022

21-
$image = Image::read('images/butterfly.jpg');
23+
$image = Image::read('images/kyrian-cartoon.jpeg');
2224

2325
$image->rgb();
2426

@@ -47,13 +49,13 @@ function fromTensorTest(ImageDriver $imageDriver, Tensor $tensor) : Image
4749
dump("------------ toTensor ------------");
4850
$tensor = toTensorTest(ImageDriver::IMAGICK);
4951
$tensor = toTensorTest(ImageDriver::GD);
50-
//$tensor = toTensorTest(ImageDriver::VIPS);
52+
$tensor = toTensorTest(ImageDriver::VIPS);
5153

5254

5355
dump("------------ fromTensor ------------");
5456
$image = fromTensorTest(ImageDriver::IMAGICK, $tensor);
5557
$image = fromTensorTest(ImageDriver::GD, $tensor);
56-
//$image = fromTensorTest(ImageDriver::VIPS, $tensor);
58+
$image = fromTensorTest(ImageDriver::VIPS, $tensor);
5759

5860
// Save the image
59-
$image->save('images/butterfly-converted.jpg');
61+
//$image->save('images/images/kyrian-cartoon-converted.jpeg');

examples/pipelines/token-classification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require_once './bootstrap.php';
66

7-
use Codewithkyrian\Transformers\Utils\AggregationStrategy;
7+
use Codewithkyrian\Transformers\Generation\AggregationStrategy;
88
use function Codewithkyrian\Transformers\Pipelines\pipeline;
99
use function Codewithkyrian\Transformers\Utils\memoryUsage;
1010
use function Codewithkyrian\Transformers\Utils\timeUsage;

src/Commands/DownloadModelCommand.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Exception;
1313
use Symfony\Component\Console\Attribute\AsCommand;
1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Helper\ProgressBar;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
@@ -26,6 +27,8 @@
2627
)]
2728
class DownloadModelCommand extends Command
2829
{
30+
protected array $progressBars = [];
31+
2932
protected function configure(): void
3033
{
3134
$this->setHelp('This command downloads a pre-trained model from Hugging Face.');
@@ -67,14 +70,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6770
try {
6871
$task = $task ? Task::tryFrom($task) : null;
6972

73+
$onProgress = function ($type, $filename, $downloadSize, $downloaded) use ($output) {
74+
if ($type === 'advance_download') {
75+
$progressBar = $this->getProgressBar($filename, $output);
76+
$percent = round(($downloaded / $downloadSize) * 100, 2);
77+
$progressBar->setProgress((int)$percent);
78+
}
79+
elseif ($type === 'complete_download') {
80+
$progressBar = $this->getProgressBar($filename, $output);
81+
$progressBar->finish();
82+
$output->writeln('');
83+
}
84+
};
85+
7086
if ($task != null) {
71-
pipeline($task, $model, quantized: $quantized, output: $output);
87+
pipeline($task, $model, quantized: $quantized, onProgress: $onProgress);
7288
} else {
73-
AutoTokenizer::fromPretrained($model, output: $output);
74-
AutoModel::fromPretrained($model, $quantized, output: $output);
89+
AutoTokenizer::fromPretrained($model, onProgress: $onProgress);
90+
AutoModel::fromPretrained($model, $quantized, onProgress: $onProgress);
7591
}
7692

77-
7893
$output->writeln('✔ Model files downloaded successfully.');
7994

8095
$this->askToStar($input, $output);
@@ -86,6 +101,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
86101
}
87102
}
88103

104+
protected function getProgressBar(string $filename, OutputInterface $output): ProgressBar
105+
{
106+
ProgressBar::setFormatDefinition('hub', '%filename% : [%bar%] %percent:3s%%');
107+
108+
if (!isset($this->progressBars[$filename])) {
109+
$progressBar = new ProgressBar($output, 100);
110+
$progressBar->setFormat('hub');
111+
$progressBar->setBarCharacter('<fg=green>•</>');
112+
$progressBar->setEmptyBarCharacter("<fg=red>⚬</>");
113+
$progressBar->setProgressCharacter('<fg=green>➤</>');
114+
$progressBar->setMessage("✔ Downloading $filename", 'filename');
115+
$this->progressBars[$filename] = $progressBar;
116+
}
117+
118+
return $this->progressBars[$filename];
119+
}
120+
89121
protected function askToStar(InputInterface $input, OutputInterface $output): void
90122
{
91123
if ($input->getOption('no-interaction')) {

src/Commands/InstallCommand.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codewithkyrian\Transformers\Commands;
6+
7+
use Codewithkyrian\Transformers\Transformers;
8+
use Codewithkyrian\Transformers\Utils\LibsChecker;
9+
use Exception;
10+
use Symfony\Component\Console\Attribute\AsCommand;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Console\Question\ConfirmationQuestion;
15+
16+
#[AsCommand(
17+
name: 'install',
18+
description: 'Initialize Transformers PHP and downloads the required shared libraries.',
19+
aliases: ['initialize', 'init']
20+
)]
21+
class InstallCommand extends Command
22+
{
23+
protected function configure(): void
24+
{
25+
$this->setHelp('This command initializes Transformers PHP and downloads the required shared libraries.');
26+
}
27+
28+
protected function execute(InputInterface $input, OutputInterface $output): int
29+
{
30+
try {
31+
LibsChecker::check();
32+
33+
$this->askToStar($input, $output);
34+
35+
return Command::SUCCESS;
36+
} catch (Exception $e) {
37+
$output->writeln('<error>' . $e->getMessage() . '</error>');
38+
39+
return Command::FAILURE;
40+
}
41+
}
42+
43+
44+
protected function askToStar(InputInterface $input, OutputInterface $output): void
45+
{
46+
if ($input->getOption('no-interaction')) {
47+
return;
48+
}
49+
50+
$helper = $this->getHelper('question');
51+
$question = new ConfirmationQuestion('? All done! Would you like to show some love by starring the Transformers repo on GitHub? ', true);
52+
53+
if ($helper->ask($input, $output, $question)) {
54+
if (PHP_OS_FAMILY === 'Darwin') {
55+
exec('open https://github.com/CodeWithKyrian/transformers-php');
56+
}
57+
if (PHP_OS_FAMILY === 'Linux') {
58+
exec('xdg-open https://github.com/CodeWithKyrian/transformers-php');
59+
}
60+
if (PHP_OS_FAMILY === 'Windows') {
61+
exec('start https://github.com/CodeWithKyrian/transformers-php');
62+
}
63+
64+
$output->writeln('✔ Thank you!');
65+
} else {
66+
$output->writeln('✔ That\'s okay. You can always star the repo later.');
67+
}
68+
}
69+
}

src/FeatureExtractors/DetrFeatureExtractor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
namespace Codewithkyrian\Transformers\FeatureExtractors;
77

88
use Codewithkyrian\Transformers\Models\Output\ObjectDetectionOutput;
9-
use Codewithkyrian\Transformers\Models\Output\ModelOutput;
109
use Codewithkyrian\Transformers\Processors\Processor;
10+
use Codewithkyrian\Transformers\Tensor\Tensor;
1111
use Codewithkyrian\Transformers\Utils\Image;
12-
use Codewithkyrian\Transformers\Utils\Tensor;
1312
use Interop\Polite\Math\Matrix\NDArray;
1413

1514
class DetrFeatureExtractor extends ImageFeatureExtractor

src/FeatureExtractors/ImageFeatureExtractor.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
namespace Codewithkyrian\Transformers\FeatureExtractors;
77

8+
use Codewithkyrian\Transformers\Tensor\Tensor;
89
use Codewithkyrian\Transformers\Utils\Image;
9-
use Codewithkyrian\Transformers\Utils\Tensor;
1010
use Imagine\Image\Point;
11-
use Interop\Polite\Math\Matrix\NDArray;
12-
use function Codewithkyrian\Transformers\Utils\timeUsage;
1311

1412
class ImageFeatureExtractor extends FeatureExtractor
1513
{
@@ -165,12 +163,17 @@ public function cropMargin(Image $image, int $grayThreshold = 200): static
165163
public function padImage(
166164
Tensor $imageTensor,
167165
int|array $padSize,
166+
string $tensorFormat = 'CHW', // 'HWC' or 'CHW
168167
string $mode = 'constant',
169168
bool $center = false,
170169
int $constantValues = 0
171170
): Tensor
172171
{
173-
[$imageHeight, $imageWidth, $imageChannels] = $imageTensor->shape();
172+
if ($tensorFormat === 'CHW') {
173+
[$imageChannels, $imageHeight, $imageWidth] = $imageTensor->shape();
174+
} else {
175+
[$imageHeight, $imageWidth, $imageChannels] = $imageTensor->shape();
176+
}
174177

175178
if (is_array($padSize)) {
176179
$paddedImageWidth = $padSize['width'];

0 commit comments

Comments
 (0)