Skip to content

Commit

Permalink
Merge branch '12.4-outpaint-stabilityai' into '12.4'
Browse files Browse the repository at this point in the history
Extending image in StabilityAI, prompt field in extending

See merge request typo3-commons/mkcontentai!40
  • Loading branch information
hannesbochmann committed May 6, 2024
2 parents dfddd77 + a2bd5f8 commit 6941c06
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 91 deletions.
17 changes: 14 additions & 3 deletions Classes/Controller/AiImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,12 @@ public function upscaleAction(File $file)
/**
* @return ResponseInterface
*/
public function extendAction(string $direction, ?File $file = null, string $base64 = '')
public function extendAction(string $direction, ?File $file = null, string $base64 = '', ?string $promptText = '')
{
if (!isset($promptText) || '' === $promptText) {
$promptText = 'extend image content';
}

try {
$filePath = '';
if ($base64) {
Expand All @@ -217,7 +221,7 @@ public function extendAction(string $direction, ?File $file = null, string $base

throw new \Exception($translatedMessage, 1623345720);
}
$images = $this->client->extend($filePath, $direction);
$images = $this->client->extend($filePath, $direction, $promptText);
} catch (\Exception $e) {
$this->addFlashMessage($e->getMessage(), '', AbstractMessage::ERROR);
$this->redirect('filelist');
Expand All @@ -227,17 +231,24 @@ public function extendAction(string $direction, ?File $file = null, string $base
[
'images' => $images,
'originalFile' => $file,
'promptText' => $promptText,
]
);

return $this->handleResponse();
}

public function cropAndExtendAction(File $file): ResponseInterface
public function cropAndExtendAction(File $file, ?string $promptText = ''): ResponseInterface
{
if (!isset($promptText) || '' === $promptText) {
$promptText = 'extending content image';
}

$this->view->assignMultiple(
[
'file' => $file,
'promptText' => $promptText,
'clientApi' => substr(get_class($this->client), 28),
]
);

Expand Down
32 changes: 32 additions & 0 deletions Classes/Http/Client/Action/AltTextAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* Copyright notice
*
* (c) DMK E-BUSINESS GmbH <dev@dmk-ebusiness.de>
* All rights reserved
*
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

namespace DMK\MkContentAi\Http\Client\Action;

class AltTextAction extends BaseAction
{
public const API_LINK = 'https://alttext.ai/api/';

public function getActions(): array
{
return [
'altText' => 'v1/images',
'altTextByAssetId' => 'v1/images/%s',
'account' => 'v1/account',
];
}
}
48 changes: 48 additions & 0 deletions Classes/Http/Client/Action/BaseAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* Copyright notice
*
* (c) DMK E-BUSINESS GmbH <dev@dmk-ebusiness.de>
* All rights reserved
*
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

namespace DMK\MkContentAi\Http\Client\Action;

abstract class BaseAction
{
/**
* @return array<string,string>
*/
abstract public function getActions(): array;

/**
* @param string[] $params
*/
public function buildUrl(string $actionName, array $params = []): string
{
if (!array_key_exists($actionName, $this->getActions())) {
throw new \Exception(sprintf('Action with name %s not found', $actionName));
}

$url = $this->getActions()[$actionName];

return vsprintf($url, $params);
}

/**
* @param string[] $params
*/
public function buildFullUrl(string $apiLink, string $actionName, array $params = []): string
{
return $apiLink.$this->buildUrl($actionName, $params);
}
}
34 changes: 34 additions & 0 deletions Classes/Http/Client/Action/StabilityAiAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* Copyright notice
*
* (c) DMK E-BUSINESS GmbH <dev@dmk-ebusiness.de>
* All rights reserved
*
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

namespace DMK\MkContentAi\Http\Client\Action;

class StabilityAiAction extends BaseAction
{
public const API_LINK = 'https://api.stability.ai/';

public function getActions(): array
{
return [
'image' => 'v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image',
'imageVariation' => 'v1/generation/stable-diffusion-xl-1024-v1-0/image-to-image',
'account' => 'v1/user/account',
'upscale' => 'v1/generation/esrgan-v1-x2plus/image-to-image/upscale',
'extend' => 'v2beta/stable-image/edit/outpaint',
];
}
}
34 changes: 34 additions & 0 deletions Classes/Http/Client/Action/StableDiffusionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* Copyright notice
*
* (c) DMK E-BUSINESS GmbH <dev@dmk-ebusiness.de>
* All rights reserved
*
* This file is part of TYPO3 CMS-based extension "mkcontentai" by DMK E-BUSINESS GmbH.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

namespace DMK\MkContentAi\Http\Client\Action;

class StableDiffusionAction extends BaseAction
{
public const API_LINK = 'https://stablediffusionapi.com/api/v3/';
public const DREAMBOOTH_API_LINK = 'https://stablediffusionapi.com/api/v4/dreambooth/';

public function getActions(): array
{
return [
'system_load' => 'system_load',
'img2img' => 'img2img',
'text2img' => 'text2img',
'model_list' => 'model_list',
];
}
}
12 changes: 8 additions & 4 deletions Classes/Http/Client/AltTextClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace DMK\MkContentAi\Http\Client;

use DMK\MkContentAi\Http\Client\Action\AltTextAction;
use DMK\MkContentAi\Service\SiteLanguageService;
use DMK\MkContentAi\Utility\AiUtility;
use Symfony\Component\HttpClient\HttpClient;
Expand All @@ -30,11 +31,14 @@ class AltTextClient extends BaseClient implements ClientInterface
{
private SiteLanguageService $siteLanguageService;

private AltTextAction $altTextAction;

private HttpClientInterface $client;

public function __construct(SiteLanguageService $siteLanguageService)
public function __construct(SiteLanguageService $siteLanguageService, AltTextAction $altTextAction)
{
$this->siteLanguageService = $siteLanguageService;
$this->altTextAction = $altTextAction;
$this->client = HttpClient::create();
}

Expand Down Expand Up @@ -75,7 +79,7 @@ public function getAltTextForFile(File $file, ?string $languageIsoCode = null):

$headers = array_merge($this->getAuthorizationHeader(), $formData->getPreparedHeaders()->toArray());

$response = $this->client->request('POST', 'https://alttext.ai/api/v1/images', [
$response = $this->client->request('POST', $this->altTextAction->buildFullUrl($this->altTextAction::API_LINK, 'altText', []), [
'headers' => $headers,
'body' => $formData->bodyToIterable(),
]);
Expand Down Expand Up @@ -108,7 +112,7 @@ public function getByAssetId(int $assetId, ?string $languageIsoCode = null): str

$assetIdWithLangIsoCode = AiUtility::getAiAssetId($assetId, $languageIsoCode);

$response = $this->client->request('GET', 'https://alttext.ai/api/v1/images/'.$assetIdWithLangIsoCode, [
$response = $this->client->request('GET', $this->altTextAction->buildFullUrl($this->altTextAction::API_LINK, 'altTextByAssetId', [$assetIdWithLangIsoCode]), [
'headers' => $this->getAuthorizationHeader(),
]);

Expand All @@ -119,7 +123,7 @@ public function getByAssetId(int $assetId, ?string $languageIsoCode = null): str

public function getAccount(): \stdClass
{
$response = $this->client->request('GET', 'https://alttext.ai/api/v1/account', [
$response = $this->client->request('GET', $this->altTextAction->buildFullUrl($this->altTextAction::API_LINK, 'account', []), [
'headers' => $this->getAuthorizationHeader(),
]);

Expand Down
2 changes: 1 addition & 1 deletion Classes/Http/Client/ImageApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function upscale(File $file): Image;
/**
* @return array<Image>
*/
public function extend(string $sourceImagePath, string $direction): array;
public function extend(string $sourceImagePath, string $direction, ?string $promptText): array;

public function getFolderName(): string;

Expand Down
4 changes: 2 additions & 2 deletions Classes/Http/Client/OpenAiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function upscale(File $file): Image
/**
* @return array<Image>
*/
public function extend(string $sourceImagePath, string $direction = 'right'): array
public function extend(string $sourceImagePath, string $direction = 'right', ?string $promptText = ''): array
{
$extendService = GeneralUtility::makeInstance(ExtendService::class);

Expand All @@ -98,7 +98,7 @@ public function extend(string $sourceImagePath, string $direction = 'right'): ar
$array = [
'image' => curl_file_create($maskImage, 'r'),
'mask' => curl_file_create($maskImage, 'r'),
'prompt' => 'outpaint',
'prompt' => $promptText,
'n' => 1,
'size' => $resolutionForExtended['width'].'x'.$resolutionForExtended['height'],
];
Expand Down
Loading

0 comments on commit 6941c06

Please sign in to comment.