Skip to content

Commit 6406602

Browse files
committed
update
1 parent 0438b57 commit 6406602

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

src/Prompt.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function toString($inputParams = []): string
6666
if (array_key_exists($unsetKey, $inputParams)) {
6767
unset($inputParams[$unsetKey]);
6868
}
69+
if (array_key_exists('prompt', $inputParams)) {
70+
unset($inputParams['prompt']);
71+
}
6972
$prompt .= ', ' . implode(', ', array_values(array_unique($inputParams)));
7073
}
7174

src/Replicate.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Replicate
3030

3131
private string $version = '';
3232

33+
private string $webhook = '';
34+
3335
private function __construct(
3436
public ?Prompt $prompt = null,
3537
private int $width = 512,
@@ -158,6 +160,12 @@ public function inputParams(string $key, mixed $value)
158160
return $this;
159161
}
160162

163+
public function setWebHook(string $url)
164+
{
165+
$this->webhook = $url;
166+
return $this;
167+
}
168+
161169
public function width(int $width)
162170
{
163171
assert($width > 0, 'Width must be greater than 0');
@@ -199,13 +207,19 @@ public function generate()
199207
}
200208
$input = array_merge($input, $this->inputParams);
201209

210+
$json = [
211+
'version' => $this->getVersion(),
212+
'input' => $input,
213+
];
214+
215+
if (! empty($this->webhook)) {
216+
$json['webhook'] = $this->webhook;
217+
}
218+
202219
$response = $this->client()->post(
203220
$this->getBaseUrl(),
204221
[
205-
'json' => [
206-
'version' => $this->getVersion(),
207-
'input' => $input,
208-
],
222+
'json' => $json,
209223
]
210224
);
211225

src/StableDiffusion.php

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

4849
if ($api) {
@@ -206,20 +207,39 @@ public function superResolution()
206207
return json_decode($response->getBody()->getContents(), true);
207208
}
208209

210+
public function controlnet()
211+
{
212+
if (empty($this->payload)) {
213+
throw new Exception('Invalid payload. @see https://stablediffusionapi.com/docs/controlnet-main/');
214+
}
215+
216+
$response = $this->client()->post(
217+
'https://stablediffusionapi.com/api/v5/controlnet',
218+
[
219+
'json' => $this->payload,
220+
]
221+
);
222+
223+
return json_decode($response->getBody()->getContents(), true);
224+
// $this->saveResult($result, $this->apiBase->text2imgUrl());
225+
}
226+
209227
private function saveResult($result, $url)
210228
{
211-
$data = [
212-
'replicate_id' => Arr::has($result, 'id') ? Arr::get($result, 'id') : 0,
213-
'platform' => self::$platform,
214-
'user_prompt' => isset($this->payload['prompt']) ? $this->payload['prompt'] : '',
215-
'full_prompt' => $this->payloadArr2String(),
216-
'url' => $url,
217-
'status' => Arr::has($result, 'status') ? Arr::get($result, 'status') : '',
218-
'output' => Arr::has($result, 'output') ? Arr::get($result, 'output') : '',
219-
'error' => Arr::has($result, 'error') ? Arr::get($result, 'error') : null,
220-
'predict_time' => Arr::has($result, 'generationTime') ? Arr::get($result, 'generationTime') : null,
221-
];
222-
StableDiffusionResult::create($data);
229+
if (Arr::get($result, 'status') !== 'error') {
230+
$data = [
231+
'replicate_id' => Arr::has($result, 'id') ? Arr::get($result, 'id') : 0,
232+
'platform' => self::$platform,
233+
'user_prompt' => isset($this->payload['prompt']) ? $this->payload['prompt'] : '',
234+
'full_prompt' => $this->payloadArr2String(),
235+
'url' => $url,
236+
'status' => Arr::has($result, 'status') ? Arr::get($result, 'status') : '',
237+
'output' => Arr::has($result, 'output') ? Arr::get($result, 'output') : '',
238+
'error' => Arr::has($result, 'error') ? Arr::get($result, 'error') : null,
239+
'predict_time' => Arr::has($result, 'generationTime') ? Arr::get($result, 'generationTime') : null,
240+
];
241+
StableDiffusionResult::create($data);
242+
}
223243
}
224244

225245
private function client(): ClientInterface

src/Uri/EnterpriseApiV1.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of the imactool/hyperf-stable-diffusion.
6+
*
7+
* (c) imactool <chinauser1208@gmail.come>
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
namespace Imactool\HyperfStableDiffusion\Uri;
12+
13+
class EnterpriseApiV1
14+
{
15+
public const ORIGIN = 'https://stablediffusionapi.com/api';
16+
17+
public const API_VERSION = 'v1';
18+
19+
public const OPEN_AI_URL = self::ORIGIN . '/' . self::API_VERSION;
20+
21+
public function text2imgUrl(): string
22+
{
23+
return self::OPEN_AI_URL . '/enterprise/text2img';
24+
}
25+
26+
public function img2imgUrl(): string
27+
{
28+
return self::OPEN_AI_URL . '/enterprise/img2img';
29+
}
30+
31+
public function inpaintUrl(): string
32+
{
33+
return self::OPEN_AI_URL . '/enterprise/inpaint';
34+
}
35+
}

0 commit comments

Comments
 (0)