本地使用秋葉整合包搭建了一個(gè)SD模型,對(duì)應(yīng)接口是 http://127.0.0.1:7860/sdapi/v1/txt2img
因?yàn)檎{(diào)用是json,返回也是json
文檔是:
在POST方法中請(qǐng)求返回
我看到方法似乎是用Stream實(shí)現(xiàn)的,這塊沒(méi)有太了解過(guò),看不太懂
protected function post($url, $data, $options, $headers = [])
{
// 如果url以/remove-background結(jié)尾
$removeButtons = [];
if (strpos($url, '/remove-background') !== false) {
$removeButtons = ['remove-background'];
}
$headers = array_merge([
// 'Authorization' => "Bearer " . $this->apikey ?: '',
// 'Accept' => 'image/*'
], $headers);
$options = $this->formatOptions($options);
$multipart = [];
foreach ($data as $field => $value) {
if (is_resource($value)) {
$multipart[] = [
'name' => $field,
'contents' => $value
];
} else {
$multipart[] = [
'name' => $field,
'contents' => $value,
];
}
}
$multipart = new \Workerman\Psr7\MultipartStream($multipart);
$boundary = $multipart->getBoundary();
$headers['Content-Type'] = "application/json";
$requestOptions = [
'method' => 'POST',
'data' => json_encode($data),
'headers' => $headers,
'success' => function (Response $response) use ($options, $removeButtons) {
$options['complete'](static::formatResponse((string)$response->getBody(), $removeButtons), $response);
},
'error' => function ($exception) use ($options) {
// var_dump($exception);
// var_dump($options);
$options['complete']([
'error' => [
'code' => 'exception',
'message' => $exception->getMessage(),
'detail' => (string) $exception
],
], new Response(0));
}
];
$http = new Client(['timeout' => 600]);
$http->request($url, $requestOptions);
}
我需要怎么調(diào)整才能保證和原有接口返回一致呢?
最新優(yōu)化方案:
plugin\sd\app\controller\ImageController 不做任何改變
修改:
plugin\sd\app\handler\driver\Sd
protected $api = 'http://127.0.0.1:7860';
create 方法
/**
* Speech
* @param $data
* @param $options
* @return void
* @throws BusinessException
* @throws Throwable
*/
public function create($data, $options)
{
$model = $data['model'] ?? 'core';
$action = ['sd-core' => 'core', 'sd3' => 'sd3', 'sd3-turbo' => 'sd3'][$model] ?? 'core';
$this->post($this->api . "/sdapi/v1/txt2img", $data, $options);
// $this->post($this->api . "/$this->version/stable-image/generate/$action", $data, $options);
}
post 方法:
/**
* @param $url
* @param $data
* @param $options
* @param array $headers
* @return void
* @throws Throwable
*/
protected function post($url, $data, $options, $headers = [])
{
// 如果url以/remove-background結(jié)尾
$removeButtons = [];
if (strpos($url, '/remove-background') !== false) {
$removeButtons = ['remove-background'];
}
$options = $this->formatOptions($options);
$headers['Content-Type'] = "application/json";
$requestOptions = [
'method' => 'POST',
'data' => json_encode($data),
'headers' => $headers,
'success' => function (Response $response) use ($options, $removeButtons) {
$options['complete'](static::formatResponse((string)$response->getBody(), $removeButtons), $response);
},
'error' => function ($exception) use ($options) {
$options['complete']([
'error' => [
'code' => 'exception',
'message' => $exception->getMessage(),
'detail' => (string) $exception
],
], new Response(0));
}
];
$http = new Client(['timeout' => 600]);
$http->request($url, $requestOptions);
}
formatResponse 方法
$json = json_decode($buffer, true);
if ($json) {
$data = [];
if (isset($json['errors'])) {
$data['error']['message'] = $json['errors'][0] ?? '未知錯(cuò)誤';
}
$data['detail'] = $json;
return $data;
}
改為:
$json = json_decode($buffer, true);
if ($json) {
if(!$json || !isset($json['images'][0])){
$data['error']['message'] = $json['errors'][0] ?? '未知錯(cuò)誤';
$data['detail'] = $json;
return $data;
}
}
$buffer = base64_decode($json['images'][0]);