Skip to content

Commit a7dbad6

Browse files
refactor: Simplify library components and improve error handling
- Updated Samplerate FFI method to handle errors more robustly - Modified Audio and Image utility classes to use new constructor patterns
1 parent 3f3db2b commit a7dbad6

8 files changed

Lines changed: 45 additions & 17 deletions

File tree

examples/pipelines/text-generation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
//$generator = pipeline('text-generation', 'Xenova/gpt2');
1616
//$generator = pipeline('text-generation', 'Xenova/Qwen1.5-0.5B-Chat');
17-
//$generator = pipeline('text-generation', 'Xenova/TinyLlama-1.1B-Chat-v1.0');
18-
$generator = pipeline('text-generation', 'onnx-community/Llama-3.2-1B-Instruct', modelFilename: 'model_q4');
17+
$generator = pipeline('text-generation', 'Xenova/TinyLlama-1.1B-Chat-v1.0');
18+
// $generator = pipeline('text-generation', 'onnx-community/Llama-3.2-1B-Instruct', modelFilename: 'model_q4');
1919

2020
$streamer = TextStreamer::make()->shouldSkipPrompt();
2121

src/FFI/Libvips.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Codewithkyrian\Transformers\FFI;
4+
5+
class Libvips extends NativeLibrary
6+
{
7+
public function __construct()
8+
{
9+
parent::__construct(false);
10+
}
11+
12+
protected function getHeaderName(): string
13+
{
14+
return 'vips';
15+
}
16+
17+
protected function getLibraryName(): string
18+
{
19+
return 'libvips';
20+
}
21+
}

src/FFI/NativeLibrary.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,19 @@ public function getLibraryPath(): string
103103
return joinPaths($this->getLibDirectory(), "{$this->getLibraryName()}.{$this->getLibraryExtension()}");
104104
}
105105

106+
public function getPlatformPath(): string
107+
{
108+
return joinPaths(dirname(__DIR__, 2), 'shared', $this->platformConfig['directory']);
109+
}
110+
106111
public function getIncludeDirectory(): string
107112
{
108-
return joinPaths(dirname(__DIR__, 2), 'shared', $this->platformConfig['directory'], 'include');
113+
return joinPaths($this->getPlatformPath(), 'include');
109114
}
110115

111116
public function getLibDirectory(): string
112117
{
113-
return joinPaths(dirname(__DIR__, 2), 'shared', $this->platformConfig['directory'], 'lib');
118+
return joinPaths($this->getPlatformPath(), 'lib');
114119
}
115120

116121
public function getLibraryExtension(): string

src/FFI/Samplerate.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,21 @@ protected function getLibraryName(): string
3838
*
3939
* @param int $converter_type The type of converter to create.
4040
* @param int $channels The number of channels.
41-
* @param int $error A pointer to an error code.
4241
*
4342
* @return CData|null The sample rate converter, or null if creation failed.
4443
* @throws Exception
4544
*/
46-
public function src_new(int $converter_type, int $channels, int &$error): ?CData
45+
public function src_new(int $converter_type, int $channels): ?CData
4746
{
48-
$errorPtr = $this->new('int');
49-
$errorPtr->cdata = $error;
47+
$error = $this->new('int32_t');
5048

51-
$src = $this->ffi->{'src_new'}($converter_type, $channels, $errorPtr);
52-
$error = $errorPtr->cdata;
49+
$state = $this->ffi->{'src_new'}($converter_type, $channels, \FFI::addr($error));
50+
51+
if ($error->cdata !== 0) {
52+
throw new RuntimeException($this->strerror($error->cdata));
53+
}
5354

54-
return $src;
55+
return $state;
5556
}
5657

5758
/**

src/Pipelines/AudioClassificationPipeline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __invoke(array|string $inputs, ...$args): array
5454
$toReturn = [];
5555

5656
foreach ($inputs as $input) {
57-
$audio = Audio::read($input);
57+
$audio = new Audio($input);
5858
$audioTensor = $audio->toTensor(samplerate: $sampleRate);
5959

6060
$processedInputs = ($this->processor)($audioTensor);

src/Pipelines/AutomaticSpeechRecognitionPipeline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private function __invokeWhisper(array|string $inputs, ...$args): array|Tensor|I
150150
?->setTimestampBegin($timestampBegin);
151151

152152
foreach ($inputs as $input) {
153-
$audio = Audio::read($input);
153+
$audio = new Audio($input);
154154
$audioTensor = $audio->toTensor(samplerate: $samplingRate);
155155

156156
$chunks = [];

src/Utils/Audio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function toTensor(int $samplerate = 41000, int $chunkSize = 2048): Tensor
5656
$tensorData = '';
5757
$totalOutputFrames = 0;
5858

59-
$state = $this->src->src_new($this->src->enum('SRC_SINC_FASTEST'), $this->channels(), $error);
59+
$state = $this->src->src_new($this->src->enum('SRC_SINC_FASTEST'), $this->channels());
6060

6161
$inputSize = $chunkSize * $this->channels();
6262
$inputData = $this->src->new("float[$inputSize]");

src/Utils/Image.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
r<?php
1+
<?php
22

33
declare(strict_types=1);
44

55
namespace Codewithkyrian\Transformers\Utils;
66

7+
use Codewithkyrian\Transformers\FFI\Libvips;
78
use Codewithkyrian\Transformers\Tensor\Tensor;
89
use Codewithkyrian\Transformers\Transformers;
910
use Exception;
@@ -96,8 +97,8 @@ public static function setDriver(ImageDriver $imageDriver): void
9697
};
9798

9899
if ($imageDriver === ImageDriver::VIPS) {
99-
$libsDir = basePath('libs');
100-
putenv("VIPSHOME=$libsDir");
100+
$libvips = new Libvips();
101+
putenv("VIPSHOME={$libvips->getPlatformPath()}");
101102
}
102103
}
103104

0 commit comments

Comments
 (0)