Skip to content

Commit

Permalink
feat(slb-489): image ai modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Oct 30, 2024
1 parent 55343e5 commit 4288843
Show file tree
Hide file tree
Showing 42 changed files with 2,865 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ dependencies:
- image.style.large
- media.type.image
module:
- focal_point
- path
- silverback_image_ai
id: media.image.default
targetEntityType: media
bundle: image
Expand All @@ -21,7 +21,7 @@ content:
settings: { }
third_party_settings: { }
field_media_image:
type: image_focal_point
type: image_focal_point_ai
weight: 0
region: content
settings:
Expand Down
2 changes: 2 additions & 0 deletions apps/cms/config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module:
role_delegation: 0
serialization: 0
shortcut: 0
silverback_ai: 0
silverback_autosave: 0
silverback_campaign_urls: 0
silverback_cloudinary: 0
Expand All @@ -74,6 +75,7 @@ module:
silverback_graphql_persisted: 0
silverback_gutenberg: 0
silverback_iframe: 0
silverback_image_ai: 0
silverback_preview_link: 0
silverback_publisher_monitor: 0
silverback_translations: 0
Expand Down
2 changes: 2 additions & 0 deletions apps/cms/config/sync/silverback_ai.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
open_ai_base_uri: 'https://api.openai.com/v1/'
open_ai_key: ''
8 changes: 8 additions & 0 deletions apps/cms/config/sync/silverback_image_ai.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
open_ai_base_uri: 'https://api.openai.com/v1/'
open_ai_key: ''
ai_model: ''
words_length: 40
alt_prefix: Silverback
alt_suffix: ''
ai_context: ''
debug_mode: 1
11 changes: 11 additions & 0 deletions apps/cms/config/sync/system.action.media_alt_ai_update_action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
uuid: 5fc8a9eb-135d-4fec-a94a-b90808339bd5
langcode: en
status: true
dependencies:
module:
- media
id: media_alt_ai_update_action
label: 'Alt text update (images only)'
type: media
plugin: 'entity:alt_ai_update_action:media'
configuration: { }
3 changes: 3 additions & 0 deletions apps/cms/scaffold/settings.php.append.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ if (file_exists(__DIR__ . '/settings.local.php')) {
if (file_exists(__DIR__ . '/services.local.yml')) {
$settings['container_yamls'][] = __DIR__ . '/services.local.yml';
}

// @todo: On final deploy add this only for PROD
$config['silverback_ai.settings']['open_ai_api_key'] = getenv('OPEN_AI_API_KEY');
14 changes: 14 additions & 0 deletions packages/drupal/silverback_ai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## INTRODUCTION
[TDB]

## REQUIREMENTS
[TBD]

## INSTALLATION

Install as you would normally install a contributed Drupal module.
See: https://www.drupal.org/node/895232 for further information.

## CONFIGURATION
[TBD]

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
open_ai_base_uri: 'https://api.openai.com/v1/'
open_ai_key: ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## INTRODUCTION

The Silveback Alt AI module is a DESCRIBE_THE_MODULE_HERE.

The primary use case for this module is:

- Use case #1
- Use case #2
- Use case #3

## REQUIREMENTS

DESCRIBE_MODULE_DEPENDENCIES_HERE

## INSTALLATION

Install as you would normally install a contributed Drupal module.
See: https://www.drupal.org/node/895232 for further information.

## CONFIGURATION
- Configuration step #1
- Configuration step #2
- Configuration step #3

## MAINTAINERS

Current maintainers for Drupal 10:

- FIRST_NAME LAST_NAME (NICKNAME) - https://www.drupal.org/u/NICKNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ai_model: 'gpt-4o-mini'
debug_mode: false
words_length: '40'
alt_prefix: ''
alt_suffix: ''
ai_context: ''
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
langcode: en
status: true
dependencies:
module:
- media
id: media_alt_ai_update_action
label: 'Alt text update (imaged only)'
type: media
plugin: entity:alt_ai_update_action:media
configuration: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
silverback_image_ai.commands:
class: \Drupal\silverback_image_ai\Drush\Commands\SilverbackImageAiCommands
arguments: ['@entity_type.manager', '@silverback_image_ai.batch.updater', '@silverback_image_ai.utilities']
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'Silverback Alt AI'
type: module
description: 'Silverback AI utilities for images'
package: Silverback
core_version_requirement: ^10
dependencies:
- silverback_ai:silverback_ai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

/**
* @file
* Install, update and uninstall functions for the Silveback Alt AI module.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
media.alt_update:
route_name: silverback_image_ai.image_ai_batch_update
title: 'Alt text update (AI)'
weight: 12
appears_on:
- entity.media.collection
- view.media_library.page
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
silverback_image_ai.alt_ai_settings:
title: Silverback Image AI settings
description: Settings for the Silverback image AI utilities.
parent: silverback_ai.admin_config_ai
route_name: silverback_image_ai.settings
weight: 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* @file
* Primary module hooks for Silveback Alt AI module.
*/

/**
*
*/
function silverback_image_ai_form_alter(&$form, &$form_state, $form_id) {
$service = \Drupal::service('silverback_image_ai.utilities');

if ($form_id == 'views_exposed_form') {
// ..
$route_name = \Drupal::routeMatch()->getRouteName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
silverback_image_ai.settings:
path: '/admin/config/system/silverback/image-ai-settings'
defaults:
_title: 'Silverback Alt AI Settings'
_form: 'Drupal\silverback_image_ai\Form\ImageAiSettingsForm'
requirements:
_permission: 'administer site configuration'

silverback_image_ai.image_ai_batch_update:
path: '/admin/silverback-ai/update/image'
defaults:
_title: 'Image Ai Batch Update'
_form: 'Drupal\silverback_image_ai\Form\ImageAiBatchUpdateForm'
requirements:
_permission: 'access content'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
silverback_image_ai.utilities:
class: Drupal\silverback_image_ai\ImageAiUtilities
arguments: ['@logger.factory','@config.factory', '@http_client', '@silverback_ai.token.usage', '@silverback_ai.openai_http_client', '@entity_type.manager']
silverback_image_ai.batch.updater:
class: 'Drupal\silverback_image_ai\MediaUpdaterBatch'
arguments:
- '@logger.factory'
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Drupal\silverback_image_ai\Drush\Commands;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\silverback_image_ai\ImageAiUtilities;
use Drupal\silverback_image_ai\MediaUpdaterBatch;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* A Drush commandfile.
*/
final class SilverbackImageAiCommands extends DrushCommands {

/**
* Constructs a SilverbackImageAiCommands object.
*/
public function __construct(
private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly MediaUpdaterBatch $batch,
private readonly ImageAiUtilities $service
) {
parent::__construct();
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('silverback_image_ai.batch.updater'),
$container->get('silverback_image_ai.utilities'),
);
}

/**
* Command description here.
*
* @param false[] $options
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drush\Exceptions\UserAbortException
*/
#[CLI\Command(name: 'silverback-image-ai:alt:generate', aliases: ['slb:alt:g'])]
#[CLI\Option(name: 'update-all', description: 'Update all image alt texts. ATTENTION: This will overwrite existing alt texts.')]
#[CLI\Usage(name: 'silverback-image-ai:alt:generate', description: 'Generate alt text for media images.')]
public function commandName(array $options = [
'update-all' => FALSE,
]) {
$media_entities = [];
if ($options['update-all']) {
$this->io()->warning(dt('ATTENTION: This action will overwrite all existing media image alt texts.'));
if ($this->io()->confirm(dt('Are you sure you want to update all existing alt texts?'), FALSE)) {
$media_entities = $this->service->getMediaEntitiesToUpdateAll();
$this->batch->create($media_entities);
}
else {
throw new UserAbortException();
}
}
else {
try {
$media_entities = $this->service->getMediaEntitiesToUpdateWithAlt();
$this->batch->create($media_entities);
} catch (InvalidPluginDefinitionException|PluginNotFoundException $e) {
// @todo
}
}

// Temp.
// $media_entities = array_slice($this->getMediaEntities(), 0, 2);.
$this->logger()->success(dt('@count media images updated.', [
'@count' => count($media_entities),
]));
}

}
Loading

0 comments on commit 4288843

Please sign in to comment.