Download app/Services/StableDiffusion.php from pnacorp/magicai: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/pnacorp/magicai/resolve/main/app/Services/StableDiffusion.php
- Command line
-
hf download hf://pnacorp/magicai/app/Services/StableDiffusion.php
-
curl -L -o StableDiffusion.php https://huggingface.co/pnacorp/magicai/resolve/main/app/Services/StableDiffusion.php
1.76 kB
| namespace App\Services; | |
| use GuzzleHttp\Client; | |
| use GuzzleHttp\Psr7\Response; | |
| class StableDiffusion | |
| { | |
| protected Client $client; | |
| protected Response $response; | |
| protected string $accept = 'application/json'; | |
| public function __construct( | |
| private string $apiKey, | |
| private ?string $organization = null, | |
| private ?string $stabilityClientId = null, | |
| private string $stabilityClientVersion = '1.2.1' | |
| ) { | |
| $this->client = new Client([ | |
| 'base_uri' => 'https://api.stability.ai/v1/', | |
| 'headers' => [ | |
| 'Authorization' => 'Bearer ' . $this->apiKey, | |
| 'Accept' => $this->accept, | |
| ], | |
| ]); | |
| } | |
| public function generate( | |
| string $text, | |
| float $weight = 0.5, | |
| ?string $engine = null, | |
| ) { | |
| // engine: stable-diffusion-v1-6 | |
| $response = $this->client->post('generation/stable-diffusion-v1-6/text-to-image', [ | |
| 'json' => [ | |
| 'text_prompts' => [[ | |
| 'text' => $text, | |
| 'weight' => $weight, | |
| ]], | |
| ], | |
| ]); | |
| $this->response = $response; | |
| return match ($this->accept) { | |
| 'application/json' => json_decode($response->getBody()->getContents(), true), | |
| 'image/png' => $response->getBody()->getContents(), | |
| }; | |
| } | |
| public function image() | |
| { | |
| $this->accept = 'image/png'; | |
| return $this; | |
| } | |
| public function json() | |
| { | |
| return $this; | |
| } | |
| public function engines() | |
| { | |
| $response = $this->client->get('engines/list'); | |
| return json_decode($response->getBody()->getContents(), true); | |
| } | |
| } | |