Skip to content

Commit

Permalink
Change configuration methods to be more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithKyrian committed Mar 15, 2024
1 parent a1f3f87 commit 3343faa
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 18 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ Next, you must run the installation/initialize command to download the shared li
./vendor/bin/transformers install
```

> [!CAUTION]
> These shared libraries to be downloaded are platform-specific, so it's important to run this command on the target
> platform where the code will be executed. For example, if you're using a Docker container, run the `install` command
> inside that container.
## PHP FFI Extension

Transformers PHP uses the PHP FFI extension to interact with the ONNX runtime. The FFI extension is included by default
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
uncommenting(remove the `;` from the beginning of the line) the
following line in your `php.ini` file:

```ini
extension = ffi
```

Also, you need to set the `ffi.enable` directive to `true` in your `php.ini` file:

```ini
ffi.enable = true
```

After making these changes, restart your web server or PHP-FPM service, and you should be good to go.

## Documentation

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

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

You can call the `set` methods in any order, or leave any out entirely, in which case, it uses the default values. For
Expand Down
2 changes: 1 addition & 1 deletion examples/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

require_once './vendor/autoload.php';

Transformers::configure();
Transformers::setup()->apply();

10 changes: 6 additions & 4 deletions src/Commands/DownloadModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Codewithkyrian\Transformers\Models\Auto\AutoModelForSeq2SeqLM;
use Codewithkyrian\Transformers\Models\Auto\AutoModelForSequenceClassification;
use Codewithkyrian\Transformers\Pipelines\Task;
use Codewithkyrian\Transformers\PretrainedTokenizers\AutoTokenizer;
use Codewithkyrian\Transformers\Transformers;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -53,24 +54,25 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
Transformers::configure();

$output->writeln('✔ Downloading model...');

$model = $input->getArgument('model');
$cacheDir = $input->getOption('cache-dir');
$quantized = $input->getOption('quantized');
$task = $input->getArgument('task');

Transformers::setup()
->setCacheDir($cacheDir)
->apply();

// Download the model
try {
$task = $task ? Task::tryFrom($task) : null;

if ($task != null) {
pipeline($task, $model);
} else {
AutoModel::fromPretrained($model, $quantized, cacheDir: $cacheDir);
AutoTokenizer::fromPretrained($model, $quantized);
AutoModel::fromPretrained($model, $quantized);
}


Expand Down
14 changes: 14 additions & 0 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use function Codewithkyrian\Transformers\Utils\ensureDirectory;
Expand All @@ -25,10 +26,23 @@ class InitCommand extends Command
protected function configure(): void
{
$this->setHelp('This command initializes Transformers PHP and downloads the required shared libraries.');

$this->addOption(
'cache-dir',
'c',
InputOption::VALUE_OPTIONAL,
'The directory to cache the libraries in.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$cacheDir = $input->getOption('cache-dir');

if ($cacheDir != null) {
Transformers::$cacheDir = $cacheDir;
}

try {
if (file_exists(Transformers::libFile())) {
$output->writeln("✔ Transformer has been formerly initialized");
Expand Down
13 changes: 8 additions & 5 deletions src/Transformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ class Transformers

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

public static function configure(): static
public static function setup(): static
{
FFI::$lib = self::libFile();

return new static;
}

public function apply(): void
{
FFI::$lib = self::libFile();
}

public static function libFile(): string
{
$template = joinPaths(Transformers::$cacheDir, self::platform('file'), 'lib', self::platform('lib'));
Expand All @@ -42,9 +45,9 @@ public static function libFile(): string
* @param string $cacheDir
* @return $this
*/
public function setCacheDir(string $cacheDir): static
public function setCacheDir(?string $cacheDir): static
{
self::$cacheDir = $cacheDir;
if ($cacheDir != null) self::$cacheDir = $cacheDir;

return $this;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use function Codewithkyrian\Transformers\Pipelines\pipeline;

beforeAll(function () {
Transformers::configure()
->setCacheDir('tests/models');
Transformers::setup()
->setCacheDir('tests/models')
->apply();
});

it('can create a pipeline for a task', function () {
Expand Down
5 changes: 3 additions & 2 deletions tests/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use Codewithkyrian\Transformers\Transformers;

beforeAll(function () {
Transformers::configure()
->setCacheDir('tests/models');
Transformers::setup()
->setCacheDir('tests/models')
->apply();
});

it('can tokenize a text', function ($textToTokenize, $expectedTokens) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Utils/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use function Codewithkyrian\Transformers\Utils\joinPaths;

beforeEach(function () {
Transformers::configure()
->setCacheDir('tests/models');
Transformers::setup()
->setCacheDir('tests/models')
->apply();
});

it('joins paths correctly', function () {
Expand Down

0 comments on commit 3343faa

Please sign in to comment.