Files
archived-ai/examples/huggingface/object-detection.php
2025-06-07 15:28:50 +02:00

35 lines
1007 B
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\AI\Platform\Bridge\HuggingFace\PlatformFactory;
use Symfony\AI\Platform\Bridge\HuggingFace\Task;
use Symfony\AI\Platform\Message\Content\Image;
use Symfony\AI\Platform\Model;
use Symfony\Component\Dotenv\Dotenv;
require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
if (empty($_ENV['HUGGINGFACE_KEY'])) {
echo 'Please set the HUGGINGFACE_KEY environment variable.'.\PHP_EOL;
exit(1);
}
$platform = PlatformFactory::create($_ENV['HUGGINGFACE_KEY']);
$model = new Model('facebook/detr-resnet-50');
$image = Image::fromFile(dirname(__DIR__, 2).'/fixtures/image.jpg');
$response = $platform->request($model, $image, [
'task' => Task::OBJECT_DETECTION,
]);
dump($response->getContent());