Stable Diffusion如何本地化调用?

sora

本地使用秋叶整合包搭建了一个SD模型,对应接口是 http://127.0.0.1:7860/sdapi/v1/txt2img
因为调用是json,返回也是json
文档是:

在POST方法中请求返回

我看到方法似乎是用Stream实现的,这块没有太了解过,看不太懂

    protected function post($url, $data, $options, $headers = [])
    {
        // 如果url以/remove-background结尾
        $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);
    }

我需要怎么调整才能保证和原有接口返回一致呢?

129 2 2
2个回答

wocall

牛人

  • 暂无评论
sora

最新优化方案:
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结尾
        $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] ?? '未知错误';
            }
            $data['detail'] = $json;
            return $data;
        }

改为:


        $json = json_decode($buffer, true);
        if ($json) {
            if(!$json || !isset($json['images'][0])){
                $data['error']['message'] = $json['errors'][0] ?? '未知错误';
                $data['detail'] = $json;
                return $data;
            }
        }
        $buffer = base64_decode($json['images'][0]);
  • 暂无评论
×
🔝