Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iMactool committed Jun 7, 2023
1 parent 0438b57 commit 6406602
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public function toString($inputParams = []): string
if (array_key_exists($unsetKey, $inputParams)) {
unset($inputParams[$unsetKey]);
}
if (array_key_exists('prompt', $inputParams)) {
unset($inputParams['prompt']);
}
$prompt .= ', ' . implode(', ', array_values(array_unique($inputParams)));
}

Expand Down
22 changes: 18 additions & 4 deletions src/Replicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Replicate

private string $version = '';

private string $webhook = '';

private function __construct(
public ?Prompt $prompt = null,
private int $width = 512,
Expand Down Expand Up @@ -158,6 +160,12 @@ public function inputParams(string $key, mixed $value)
return $this;
}

public function setWebHook(string $url)
{
$this->webhook = $url;
return $this;
}

public function width(int $width)
{
assert($width > 0, 'Width must be greater than 0');
Expand Down Expand Up @@ -199,13 +207,19 @@ public function generate()
}
$input = array_merge($input, $this->inputParams);

$json = [
'version' => $this->getVersion(),
'input' => $input,
];

if (! empty($this->webhook)) {
$json['webhook'] = $this->webhook;
}

$response = $this->client()->post(
$this->getBaseUrl(),
[
'json' => [
'version' => $this->getVersion(),
'input' => $input,
],
'json' => $json,
]
);

Expand Down
44 changes: 32 additions & 12 deletions src/StableDiffusion.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function supportApi(string $api = ''): array|string
$aiApis = [
'StableDiffusionApiV3', // Stable Diffusion V3 APIs comes with below features https://documenter.getpostman.com/view/18679074/2s83zdwReZ#c7e3c6a0-b57d-4d17-ad5a-c4eb8571021f
'DreamboothApiV4', // [Beta] DreamBooth API https://documenter.getpostman.com/view/18679074/2s83zdwReZ#27db9713-6068-41c2-8431-ada0d08d3cd5
'EnterpriseApiV1', // [企业] Enterprise API https://stablediffusionapi.com/docs/enterprise-plan/overview
];

if ($api) {
Expand Down Expand Up @@ -206,20 +207,39 @@ public function superResolution()
return json_decode($response->getBody()->getContents(), true);
}

public function controlnet()
{
if (empty($this->payload)) {
throw new Exception('Invalid payload. @see https://stablediffusionapi.com/docs/controlnet-main/');
}

$response = $this->client()->post(
'https://stablediffusionapi.com/api/v5/controlnet',
[
'json' => $this->payload,
]
);

return json_decode($response->getBody()->getContents(), true);
// $this->saveResult($result, $this->apiBase->text2imgUrl());
}

private function saveResult($result, $url)
{
$data = [
'replicate_id' => Arr::has($result, 'id') ? Arr::get($result, 'id') : 0,
'platform' => self::$platform,
'user_prompt' => isset($this->payload['prompt']) ? $this->payload['prompt'] : '',
'full_prompt' => $this->payloadArr2String(),
'url' => $url,
'status' => Arr::has($result, 'status') ? Arr::get($result, 'status') : '',
'output' => Arr::has($result, 'output') ? Arr::get($result, 'output') : '',
'error' => Arr::has($result, 'error') ? Arr::get($result, 'error') : null,
'predict_time' => Arr::has($result, 'generationTime') ? Arr::get($result, 'generationTime') : null,
];
StableDiffusionResult::create($data);
if (Arr::get($result, 'status') !== 'error') {
$data = [
'replicate_id' => Arr::has($result, 'id') ? Arr::get($result, 'id') : 0,
'platform' => self::$platform,
'user_prompt' => isset($this->payload['prompt']) ? $this->payload['prompt'] : '',
'full_prompt' => $this->payloadArr2String(),
'url' => $url,
'status' => Arr::has($result, 'status') ? Arr::get($result, 'status') : '',
'output' => Arr::has($result, 'output') ? Arr::get($result, 'output') : '',
'error' => Arr::has($result, 'error') ? Arr::get($result, 'error') : null,
'predict_time' => Arr::has($result, 'generationTime') ? Arr::get($result, 'generationTime') : null,
];
StableDiffusionResult::create($data);
}
}

private function client(): ClientInterface
Expand Down
35 changes: 35 additions & 0 deletions src/Uri/EnterpriseApiV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* This file is part of the imactool/hyperf-stable-diffusion.
*
* (c) imactool <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Imactool\HyperfStableDiffusion\Uri;

class EnterpriseApiV1
{
public const ORIGIN = 'https://stablediffusionapi.com/api';

public const API_VERSION = 'v1';

public const OPEN_AI_URL = self::ORIGIN . '/' . self::API_VERSION;

public function text2imgUrl(): string
{
return self::OPEN_AI_URL . '/enterprise/text2img';
}

public function img2imgUrl(): string
{
return self::OPEN_AI_URL . '/enterprise/img2img';
}

public function inpaintUrl(): string
{
return self::OPEN_AI_URL . '/enterprise/inpaint';
}
}

0 comments on commit 6406602

Please sign in to comment.