Skip to content

Commit 3343faa

Browse files
Change configuration methods to be more intuitive
1 parent a1f3f87 commit 3343faa

8 files changed

Lines changed: 65 additions & 18 deletions

File tree

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ Next, you must run the installation/initialize command to download the shared li
102102
./vendor/bin/transformers install
103103
```
104104

105+
> [!CAUTION]
106+
> These shared libraries to be downloaded are platform-specific, so it's important to run this command on the target
107+
> platform where the code will be executed. For example, if you're using a Docker container, run the `install` command
108+
> inside that container.
109+
110+
## PHP FFI Extension
111+
112+
Transformers PHP uses the PHP FFI extension to interact with the ONNX runtime. The FFI extension is included by default
113+
in PHP 7.4 and later, but it may not be enabled by default. If the FFI extension is not enabled, you can enable it by
114+
uncommenting(remove the `;` from the beginning of the line) the
115+
following line in your `php.ini` file:
116+
117+
```ini
118+
extension = ffi
119+
```
120+
121+
Also, you need to set the `ffi.enable` directive to `true` in your `php.ini` file:
122+
123+
```ini
124+
ffi.enable = true
125+
```
126+
127+
After making these changes, restart your web server or PHP-FPM service, and you should be good to go.
128+
105129
## Documentation
106130

107131
For more detailed information on how to use the library, check out the
@@ -120,12 +144,13 @@ You can configure the behaviour of the Transformers PHP library as follows:
120144
```php
121145
use Codewithkyrian\Transformers\Transformers;
122146

123-
Transformers::configure()
147+
Transformers::setup()
124148
->setCacheDir('...') // Set the default cache directory for transformers models. Defaults to `.transformers-cache/models`
125149
->setRemoteHost('...') // Set the remote host for downloading models. Defaults to `https://huggingface.co`
126150
->setRemotePathTemplate('...') // Set the remote path template for downloading models. Defaults to `{model}/resolve/{revision}/{file}`
127151
->setAuthToken('...') // Set the auth token for downloading models. Defaults to `null`
128-
->setUserAgent('...'); // Set the user agent for downloading models. Defaults to `transformers-php/{version}`
152+
->setUserAgent('...') // Set the user agent for downloading models. Defaults to `transformers-php/{version}`
153+
->apply(); // Apply the configuration
129154
```
130155

131156
You can call the `set` methods in any order, or leave any out entirely, in which case, it uses the default values. For

examples/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

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

9-
Transformers::configure();
9+
Transformers::setup()->apply();
1010

src/Commands/DownloadModelCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Codewithkyrian\Transformers\Models\Auto\AutoModelForSeq2SeqLM;
1111
use Codewithkyrian\Transformers\Models\Auto\AutoModelForSequenceClassification;
1212
use Codewithkyrian\Transformers\Pipelines\Task;
13+
use Codewithkyrian\Transformers\PretrainedTokenizers\AutoTokenizer;
1314
use Codewithkyrian\Transformers\Transformers;
1415
use Symfony\Component\Console\Attribute\AsCommand;
1516
use Symfony\Component\Console\Command\Command;
@@ -53,24 +54,25 @@ protected function configure(): void
5354

5455
protected function execute(InputInterface $input, OutputInterface $output): int
5556
{
56-
Transformers::configure();
57-
5857
$output->writeln('✔ Downloading model...');
5958

6059
$model = $input->getArgument('model');
6160
$cacheDir = $input->getOption('cache-dir');
6261
$quantized = $input->getOption('quantized');
6362
$task = $input->getArgument('task');
6463

64+
Transformers::setup()
65+
->setCacheDir($cacheDir)
66+
->apply();
6567

66-
// Download the model
6768
try {
6869
$task = $task ? Task::tryFrom($task) : null;
6970

7071
if ($task != null) {
7172
pipeline($task, $model);
7273
} else {
73-
AutoModel::fromPretrained($model, $quantized, cacheDir: $cacheDir);
74+
AutoTokenizer::fromPretrained($model, $quantized);
75+
AutoModel::fromPretrained($model, $quantized);
7476
}
7577

7678

src/Commands/InitCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Console\Attribute\AsCommand;
1111
use Symfony\Component\Console\Command\Command;
1212
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
1314
use Symfony\Component\Console\Output\OutputInterface;
1415
use Symfony\Component\Console\Question\ConfirmationQuestion;
1516
use function Codewithkyrian\Transformers\Utils\ensureDirectory;
@@ -25,10 +26,23 @@ class InitCommand extends Command
2526
protected function configure(): void
2627
{
2728
$this->setHelp('This command initializes Transformers PHP and downloads the required shared libraries.');
29+
30+
$this->addOption(
31+
'cache-dir',
32+
'c',
33+
InputOption::VALUE_OPTIONAL,
34+
'The directory to cache the libraries in.'
35+
);
2836
}
2937

3038
protected function execute(InputInterface $input, OutputInterface $output): int
3139
{
40+
$cacheDir = $input->getOption('cache-dir');
41+
42+
if ($cacheDir != null) {
43+
Transformers::$cacheDir = $cacheDir;
44+
}
45+
3246
try {
3347
if (file_exists(Transformers::libFile())) {
3448
$output->writeln("✔ Transformer has been formerly initialized");

src/Transformers.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ class Transformers
2323

2424
public static ?string $userAgent = 'transformers-php/0.1.0';
2525

26-
public static function configure(): static
26+
public static function setup(): static
2727
{
28-
FFI::$lib = self::libFile();
29-
3028
return new static;
3129
}
3230

31+
public function apply(): void
32+
{
33+
FFI::$lib = self::libFile();
34+
}
35+
3336
public static function libFile(): string
3437
{
3538
$template = joinPaths(Transformers::$cacheDir, self::platform('file'), 'lib', self::platform('lib'));
@@ -42,9 +45,9 @@ public static function libFile(): string
4245
* @param string $cacheDir
4346
* @return $this
4447
*/
45-
public function setCacheDir(string $cacheDir): static
48+
public function setCacheDir(?string $cacheDir): static
4649
{
47-
self::$cacheDir = $cacheDir;
50+
if ($cacheDir != null) self::$cacheDir = $cacheDir;
4851

4952
return $this;
5053
}

tests/PipelineTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use function Codewithkyrian\Transformers\Pipelines\pipeline;
99

1010
beforeAll(function () {
11-
Transformers::configure()
12-
->setCacheDir('tests/models');
11+
Transformers::setup()
12+
->setCacheDir('tests/models')
13+
->apply();
1314
});
1415

1516
it('can create a pipeline for a task', function () {

tests/TokenizerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use Codewithkyrian\Transformers\Transformers;
99

1010
beforeAll(function () {
11-
Transformers::configure()
12-
->setCacheDir('tests/models');
11+
Transformers::setup()
12+
->setCacheDir('tests/models')
13+
->apply();
1314
});
1415

1516
it('can tokenize a text', function ($textToTokenize, $expectedTokens) {

tests/Utils/HubTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use function Codewithkyrian\Transformers\Utils\joinPaths;
1414

1515
beforeEach(function () {
16-
Transformers::configure()
17-
->setCacheDir('tests/models');
16+
Transformers::setup()
17+
->setCacheDir('tests/models')
18+
->apply();
1819
});
1920

2021
it('joins paths correctly', function () {

0 commit comments

Comments
 (0)