-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathAddMissingAltTextCommand.php
More file actions
138 lines (115 loc) · 5.56 KB
/
Copy pathAddMissingAltTextCommand.php
File metadata and controls
138 lines (115 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\ConnectorAi\Action\ActionContext;
use Ibexa\Contracts\ConnectorAi\Action\DataType\Image;
use Ibexa\Contracts\ConnectorAi\Action\DataType\Text;
use Ibexa\Contracts\ConnectorAi\Action\GenerateAltTextAction;
use Ibexa\Contracts\ConnectorAi\Action\RuntimeContext;
use Ibexa\Contracts\ConnectorAi\ActionConfiguration\ActionConfigurationOptions;
use Ibexa\Contracts\ConnectorAi\ActionServiceInterface;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\FieldTypeService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentList;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\DateMetadata;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
use Ibexa\Core\FieldType\Image\Value;
use Ibexa\Core\IO\IOBinarydataHandler;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'app:add-alt-text',
)]
final readonly class AddMissingAltTextCommand
{
private const string IMAGE_FIELD_IDENTIFIER = 'image';
public function __construct(
private ContentService $contentService,
private PermissionResolver $permissionResolver,
private UserService $userService,
private FieldTypeService $fieldTypeService,
private ActionServiceInterface $actionService,
private IOBinarydataHandler $binaryDataHandler
) {
}
public function __invoke(
#[Argument(name: 'user', description: 'Login of the user executing the actions')] string $user,
OutputInterface $output
): int {
$this->setUser($user);
$modifiedImages = $this->getModifiedImages();
$output->writeln(sprintf('Found %d modified image in the last 24h', $modifiedImages->getTotalCount()));
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
foreach ($modifiedImages as $content) {
/** @var \Ibexa\Core\FieldType\Image\Value $value */
$value = $content->getFieldValue(self::IMAGE_FIELD_IDENTIFIER);
if ($value === null || !$this->shouldGenerateAltText($value)) {
$output->writeln(sprintf('Image %s has the image field empty, the file cannot be accessed, or the alternative text is already specified. Skipping.', $content->getName()));
continue;
}
$contentUpdateStruct = $this->contentService->newContentUpdateStruct();
$value->alternativeText = $this->getSuggestedAltText($this->convertImageToBase64($value->uri), $content->getDefaultLanguageCode());
$contentUpdateStruct->setField(self::IMAGE_FIELD_IDENTIFIER, $value);
$updatedContent = $this->contentService->updateContent(
$this->contentService->createContentDraft($content->getContentInfo())->getVersionInfo(),
$contentUpdateStruct
);
$this->contentService->publishVersion($updatedContent->getVersionInfo());
}
return Command::SUCCESS;
}
private function getSuggestedAltText(string $imageEncodedInBase64, string $languageCode): string
{
$action = new GenerateAltTextAction(new Image([$imageEncodedInBase64]));
$action->setRuntimeContext(new RuntimeContext(['languageCode' => $languageCode]));
$action->setActionContext(
new ActionContext(
new ActionConfigurationOptions(['default_locale_fallback' => 'en']), // System context
new ActionConfigurationOptions(['max_lenght' => 100]), // Action Type options
new ActionConfigurationOptions( // Action Handler options
[
'prompt' => 'Generate the alt text for this image in less than 100 characters.',
'temperature' => 0.7,
'max_tokens' => 4096,
'model' => 'gpt-4o-mini',
]
)
)
);
$output = $this->actionService->execute($action)->getOutput();
assert($output instanceof Text);
return $output->getText();
}
private function convertImageToBase64(string $uri): string
{
$id = $this->binaryDataHandler->getIdFromUri($uri);
$file = $this->binaryDataHandler->getContents($id);
return 'data:image/jpeg;base64,' . base64_encode($file);
}
private function getModifiedImages(): ContentList
{
$filter = (new Filter())
->withCriterion(
new DateMetadata(DateMetadata::MODIFIED, Operator::GTE, strtotime('-1 day'))
)
->andWithCriterion(new ContentTypeIdentifier('image'));
return $this->contentService->find($filter);
}
/** @phpstan-assert-if-true string $value->uri */
private function shouldGenerateAltText(Value $value): bool
{
return $this->fieldTypeService->getFieldType('ibexa_image')->isEmptyValue($value) === false &&
$value->isAlternativeTextEmpty() &&
$value->uri !== null;
}
private function setUser(string $userLogin): void
{
$this->permissionResolver->setCurrentUserReference($this->userService->loadUserByLogin($userLogin));
}
}