Skip to content

Commit

Permalink
refactor: refactor connectors a bit so that the settings values becom…
Browse files Browse the repository at this point in the history
…e simpler
  • Loading branch information
Joachim Schole committed Apr 25, 2024
1 parent 8c146cd commit 2888a2a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 26 deletions.
15 changes: 14 additions & 1 deletion src/connectors/AbstractHuggingFaceConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@
use fork\altify\Plugin;
use GuzzleHttp\Client;

class AbstractHuggingFaceConnector implements ConnectorInterface
abstract class AbstractHuggingFaceConnector implements ConnectorInterface
{
protected string $name = '';
protected string $handle = '';

public function getName(): string
{
return $this->name;
}

public function getHandle(): string
{
return $this->handle;
}

protected function getClient(): Client
{
return new Client([
Expand Down
5 changes: 4 additions & 1 deletion src/connectors/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

namespace fork\altify\connectors;

interface ConnectorInterface {}
interface ConnectorInterface {
public function getName(): string;
public function getHandle(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

class HuggingFaceBlipBaseAltTextGenerator extends AbstractHuggingFaceAltTextGenerator
{
protected string $name = 'BLIP base model (Hugging Face)';
protected string $handle = 'hfBlipBase';
protected string $modelPath = '/models/Salesforce/blip-image-captioning-base';
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

class HuggingFaceBlipLargeAltTextGenerator extends AbstractHuggingFaceAltTextGenerator
{
protected string $name = 'BLIP large model (Hugging Face)';
protected string $handle = 'hfBlipLarge';
protected string $modelPath = '/models/Salesforce/blip-image-captioning-large';
}
14 changes: 14 additions & 0 deletions src/connectors/translation/DeeplTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@

class DeeplTranslator implements TranslatorInterface
{
protected string $name = 'DeepL';
protected string $handle = 'deepl';

public function getName(): string
{
return $this->name;
}

public function getHandle(): string
{
return $this->handle;
}

/**
* @param Asset $image
* @return string|null
Expand All @@ -26,4 +39,5 @@ public function translateAltTextForImage(Asset $image): ?string

return $altText;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

class HuggingFaceOpusMtEnDeTranslator extends AbstractHuggingFaceTranslator
{
protected string $name = 'OPUS MT En -> De';
protected string $handle = 'opusMtEnDe';
protected string $modelPath = '/models/Helsinki-NLP/opus-mt-en-de';
}
2 changes: 2 additions & 0 deletions src/connectors/translation/HuggingFaceT5SmallTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

class HuggingFaceT5SmallTranslator extends AbstractHuggingFaceTranslator
{
protected string $name = 'T5 small En -> De';
protected string $handle = 't5SmallEnDe';
protected string $modelPath = '/models/google-t5/t5-small';
}
87 changes: 63 additions & 24 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use fork\altify\connectors\alttextgeneration\AltTextGeneratorInterface;
use fork\altify\connectors\alttextgeneration\HuggingFaceBlipBaseAltTextGenerator;
use fork\altify\connectors\alttextgeneration\HuggingFaceBlipLargeAltTextGenerator;
use fork\altify\connectors\ConnectorInterface;
use fork\altify\connectors\translation\DeeplTranslator;
use fork\altify\connectors\translation\HuggingFaceOpusMtEnDeTranslator;
use fork\altify\connectors\translation\HuggingFaceT5SmallTranslator;
Expand All @@ -23,19 +24,15 @@
*/
class Settings extends Model
{
public const GENERATOR_HUGGING_FACE_BLIP_LARGE = 'BLIP large model (Hugging Face)';
public const GENERATOR_HUGGING_FACE_BLIP_BASE = 'BLIP base model (Hugging Face)';
private const GENERATOR_MAPPING = [
self::GENERATOR_HUGGING_FACE_BLIP_LARGE => HuggingFaceBlipLargeAltTextGenerator::class,
self::GENERATOR_HUGGING_FACE_BLIP_BASE => HuggingFaceBlipBaseAltTextGenerator::class
private const GENERATORS = [
HuggingFaceBlipLargeAltTextGenerator::class,
HuggingFaceBlipBaseAltTextGenerator::class
];
public const TRANSLATOR_DEEPL = 'DeepL';
public const TRANSLATOR_HUGGING_FACE_OPUS_MT = 'OPUS MT En -> De';
public const TRANSLATOR_HUGGING_FACE_T5_SMALL = 'T5 small En -> De';
private const TRANSLATOR_MAPPING = [
self::TRANSLATOR_DEEPL => DeeplTranslator::class,
self::TRANSLATOR_HUGGING_FACE_OPUS_MT => HuggingFaceOpusMtEnDeTranslator::class,
self::TRANSLATOR_HUGGING_FACE_T5_SMALL => HuggingFaceT5SmallTranslator::class,

private const TRANSLATORS = [
DeeplTranslator::class,
HuggingFaceOpusMtEnDeTranslator::class,
HuggingFaceT5SmallTranslator::class,
];

public ?string $altTextGenerator = null;
Expand All @@ -59,14 +56,21 @@ public function getDeeplApiKey(): ?string
return App::parseEnv($this->deeplApiKey);
}

/**
* @return array[]
* @noinspection PhpUnused
*/
public function getGeneratorSuggestions(): array
{
$data = [];

foreach (self::GENERATOR_MAPPING as $name => $hint) {
foreach (self::getAvailableGenerators() as $handle => $classname) {
/** @var AltTextGeneratorInterface $obj */
$obj = new $classname();

$data[] = [
'name' => $name,
'hint' => $hint
'name' => $obj->getName(),
'hint' => $handle
];
}

Expand All @@ -76,14 +80,21 @@ public function getGeneratorSuggestions(): array
]];
}

/**
* @return array[]
* @noinspection PhpUnused
*/
public function getTranslatorSuggestions(): array
{
$data = [];

foreach (self::TRANSLATOR_MAPPING as $name => $hint) {
foreach (self::getAvailableTranslators() as $handle => $classname) {
/** @var TranslatorInterface $obj */
$obj = new $classname();

$data[] = [
'name' => $name,
'hint' => $hint
'name' => $obj->getName(),
'hint' => $handle
];
}

Expand All @@ -99,10 +110,12 @@ public function getTranslatorSuggestions(): array
public function getAltTextGenerator(): AltTextGeneratorInterface
{
$altTextGenerator = App::parseEnv($this->altTextGenerator);
if (class_exists($altTextGenerator)) {
$className = $altTextGenerator;
$generators = $this->getAvailableGenerators();

if (key_exists($altTextGenerator, $generators)) {
$className = $generators[$altTextGenerator];
} else {
$className = self::GENERATOR_MAPPING[$this->altTextGenerator ?? self::GENERATOR_HUGGING_FACE_BLIP_LARGE];
$className = HuggingFaceBlipLargeAltTextGenerator::class;
}
if (!is_a($className, AltTextGeneratorInterface::class, true)) {
throw new InvalidConfigException(Craft::t(
Expand All @@ -124,10 +137,12 @@ public function getAltTextGenerator(): AltTextGeneratorInterface
public function getAltTextTranslator(): TranslatorInterface
{
$altTextTranslator = App::parseEnv($this->altTextTranslator);
if (class_exists($altTextTranslator)) {
$className = $altTextTranslator;
$translators = self::getAvailableTranslators();

if (key_exists($altTextTranslator, $translators)) {
$className = $translators[$altTextTranslator];
} else {
$className = self::TRANSLATOR_MAPPING[$this->altTextTranslator ?? self::TRANSLATOR_HUGGING_FACE_T5_SMALL];
$className = HuggingFaceT5SmallTranslator::class;
}
if (!is_a($className, TranslatorInterface::class, true)) {
throw new InvalidConfigException(Craft::t(
Expand All @@ -142,4 +157,28 @@ public function getAltTextTranslator(): TranslatorInterface

return new $className;
}

private static function getAvailableGenerators(): array
{
return self::buildConnectorArray(self::GENERATORS);
}

private static function getAvailableTranslators(): array
{
return self::buildConnectorArray(self::TRANSLATORS);
}

private static function buildConnectorArray(array $classnames): array
{
$data = [];

foreach ($classnames as $classname) {
/** @var ConnectorInterface $obj */
$obj = new $classname();

$data[$obj->getHandle()] = $classname;
}

return $data;
}
}

0 comments on commit 2888a2a

Please sign in to comment.