Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 1518f03

Browse files
committed
feat: add support for hugging face inference api
feat: claude rolling out more hugging face examples feat: claude refactoring examples feat: claude adding an example runner feat: make it work
1 parent 50b6321 commit 1518f03

Some content is hidden

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

45 files changed

+1530
-0
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ AZURE_OPENAI_KEY=
2525
AZURE_LLAMA_BASEURL=
2626
AZURE_LLAMA_KEY=
2727

28+
# Hugging Face Access Token
29+
HUGGINGFACE_KEY=
30+
2831
# For using OpenRouter
2932
OPENROUTER_KEY=
3033

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\ApiClient;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Output\ConsoleOutput;
9+
use Symfony\Component\Console\SingleCommandApplication;
10+
use Symfony\Component\Console\Style\SymfonyStyle;
11+
12+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
13+
14+
$app = (new SingleCommandApplication('HuggingFace Model Listing'))
15+
->setDescription('Lists all available models on HuggingFace')
16+
->addOption('provider', 'p', InputOption::VALUE_REQUIRED, 'Name of the inference provider to filter models by')
17+
->addOption('task', 't', InputOption::VALUE_REQUIRED, 'Name of the task to filter models by')
18+
->setCode(function (InputInterface $input, ConsoleOutput $output) {
19+
$io = new SymfonyStyle($input, $output);
20+
$io->title('HuggingFace Model Listing');
21+
22+
$provider = $input->getOption('provider');
23+
$task = $input->getOption('task');
24+
25+
$models = (new ApiClient())->models($provider, $task);
26+
27+
if (0 === count($models)) {
28+
$io->error('No models found for the given provider and task.');
29+
30+
return Command::FAILURE;
31+
}
32+
33+
$io->listing(
34+
array_map(fn (Model $model) => $model->getName(), $models)
35+
);
36+
37+
return Command::SUCCESS;
38+
})
39+
->run();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Message\Content\Audio;
7+
use Symfony\Component\Dotenv\Dotenv;
8+
9+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
10+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
11+
12+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
13+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
14+
exit(1);
15+
}
16+
17+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
18+
$model = new Model('MIT/ast-finetuned-audioset-10-10-0.4593');
19+
$audio = Audio::fromFile(dirname(__DIR__, 2).'/tests/Fixture/audio.mp3');
20+
21+
$response = $platform->request($model, $audio, [
22+
'task' => Task::AUDIO_CLASSIFICATION,
23+
]);
24+
25+
dump($response->getContent());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Message\Content\Audio;
7+
use Symfony\Component\Dotenv\Dotenv;
8+
9+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
10+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
11+
12+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
13+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
14+
exit(1);
15+
}
16+
17+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
18+
$model = new Model('openai/whisper-large-v3');
19+
$audio = Audio::fromFile(dirname(__DIR__, 2).'/tests/Fixture/audio.mp3');
20+
21+
$response = $platform->request($model, $audio, [
22+
'task' => Task::AUTOMATIC_SPEECH_RECOGNITION,
23+
]);
24+
25+
echo $response->getContent().PHP_EOL;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Message\Message;
7+
use PhpLlm\LlmChain\Model\Message\MessageBag;
8+
use Symfony\Component\Dotenv\Dotenv;
9+
10+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
11+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
12+
13+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
14+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
15+
exit(1);
16+
}
17+
18+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
19+
$model = new Model('HuggingFaceH4/zephyr-7b-beta');
20+
21+
$messages = new MessageBag(Message::ofUser('Hello, how are you doing today?'));
22+
$response = $platform->request($model, $messages, [
23+
'task' => Task::CHAT_COMPLETION,
24+
]);
25+
26+
echo $response->getContent().PHP_EOL;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Response\VectorResponse;
7+
use Symfony\Component\Dotenv\Dotenv;
8+
9+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
10+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
11+
12+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
13+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
14+
exit(1);
15+
}
16+
17+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
18+
$model = new Model('thenlper/gte-large');
19+
20+
$response = $platform->request($model, 'Today is a sunny day and I will get some ice cream.', [
21+
'task' => Task::FEATURE_EXTRACTION,
22+
]);
23+
24+
assert($response instanceof VectorResponse);
25+
26+
echo 'Dimensions: '.$response->getContent()[0]->getDimensions().PHP_EOL;

examples/huggingface/fill-mask.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use Symfony\Component\Dotenv\Dotenv;
7+
8+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
9+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
10+
11+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
12+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
13+
exit(1);
14+
}
15+
16+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
17+
$model = new Model('FacebookAI/xlm-roberta-base');
18+
19+
$response = $platform->request($model, 'Hello I\'m a <mask> model.', [
20+
'task' => Task::FILL_MASK,
21+
]);
22+
23+
dump($response->getContent());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Message\Content\Image;
7+
use Symfony\Component\Dotenv\Dotenv;
8+
9+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
10+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
11+
12+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
13+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
14+
exit(1);
15+
}
16+
17+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
18+
$model = new Model('google/vit-base-patch16-224');
19+
20+
$image = Image::fromFile(dirname(__DIR__, 2).'/tests/Fixture/image.jpg');
21+
$response = $platform->request($model, $image, [
22+
'task' => Task::IMAGE_CLASSIFICATION,
23+
]);
24+
25+
dump($response->getContent());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Message\Content\Image;
7+
use Symfony\Component\Dotenv\Dotenv;
8+
9+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
10+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
11+
12+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
13+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
14+
exit(1);
15+
}
16+
17+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
18+
$model = new Model('nvidia/segformer-b0-finetuned-ade-512-512');
19+
20+
$image = Image::fromFile(dirname(__DIR__, 2).'/tests/Fixture/image.jpg');
21+
$response = $platform->request($model, $image, [
22+
'task' => Task::IMAGE_SEGMENTATION,
23+
]);
24+
25+
dump($response->getContent());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
4+
use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
5+
use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
6+
use PhpLlm\LlmChain\Model\Message\Content\Image;
7+
use Symfony\Component\Dotenv\Dotenv;
8+
9+
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
10+
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
11+
12+
if (empty($_ENV['HUGGINGFACE_KEY'])) {
13+
echo 'Please set the HUGGINGFACE_KEY environment variable.'.PHP_EOL;
14+
exit(1);
15+
}
16+
17+
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
18+
$model = new Model('Salesforce/blip-image-captioning-base');
19+
20+
$image = Image::fromFile(dirname(__DIR__, 2).'/tests/Fixture/image.jpg');
21+
$response = $platform->request($model, $image, [
22+
'task' => Task::IMAGE_TO_TEXT,
23+
]);
24+
25+
echo $response->getContent().PHP_EOL;

0 commit comments

Comments
 (0)