KubioCloudSDApi.php
1 year ago
KubioImageGenerate.php
1 year ago
KubioImageManager.php
1 year ago
KubioImagePrompts.php
2 years ago
KubioImageSearch.php
1 year ago
sd-endpoints.php
1 year ago
KubioImageGenerate.php
101 lines
| 1 | <?php |
| 2 | namespace Kubio\Ai; |
| 3 | class KubioImageGenerate { |
| 4 | private $api = null; |
| 5 | private $negative_prompt = 'worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, camera'; |
| 6 | |
| 7 | function __construct( $api_key = false, $api_model = false, $api_dummy_response = false ) { |
| 8 | $this->api = new KubioCloudSDApi( $api_key, $api_model, $api_dummy_response ); |
| 9 | } |
| 10 | |
| 11 | public function mask( array $data, $image_prompt, $settings = array() ) { |
| 12 | $this->api->set_api_endpoint( 'v1/image-generation/mask' ); |
| 13 | $data = $this->prepare_data( $data, $image_prompt ); |
| 14 | |
| 15 | $response = $this->post( |
| 16 | $data, |
| 17 | $settings |
| 18 | ); |
| 19 | |
| 20 | return $response; |
| 21 | } |
| 22 | |
| 23 | public function variations( array $data, $image_prompt, $settings = array() ) { |
| 24 | $this->api->set_api_endpoint( 'v1/image-generation/image-to-image' ); |
| 25 | |
| 26 | $data = $this->prepare_data( $data, $image_prompt ); |
| 27 | |
| 28 | $response = $this->post( |
| 29 | $data, |
| 30 | $settings |
| 31 | ); |
| 32 | |
| 33 | return $response; |
| 34 | } |
| 35 | |
| 36 | public function samples( array $data, $image_prompt, $settings = array() ) { |
| 37 | $this->api->set_api_endpoint( 'v1/image-generation/text-to-image' ); |
| 38 | |
| 39 | $data = $this->prepare_data( $data, $image_prompt ); |
| 40 | |
| 41 | $response = $this->post( |
| 42 | $data, |
| 43 | $settings |
| 44 | ); |
| 45 | |
| 46 | return $response; |
| 47 | } |
| 48 | |
| 49 | private function prepare_data( $data, $image_prompt ) { |
| 50 | if ( is_array( $image_prompt ) ) { |
| 51 | foreach ( $image_prompt as $prompt ) { |
| 52 | if ( $prompt !== '' ) { |
| 53 | $data['text_prompts'][] = array( |
| 54 | 'text' => $prompt, |
| 55 | 'weight' => 1, |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $data['text_prompts'][] = array( |
| 61 | 'text' => $this->negative_prompt, |
| 62 | 'weight' => -1, |
| 63 | ); |
| 64 | |
| 65 | } else { |
| 66 | if ( $image_prompt !== '' ) { |
| 67 | $data['text_prompts'] = $this->build_text_prompts( $image_prompt ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | public function build_text_prompts( $image_prompt ) { |
| 75 | return array( |
| 76 | array( |
| 77 | 'text' => $image_prompt, |
| 78 | 'weight' => 1, |
| 79 | ), |
| 80 | array( |
| 81 | 'text' => $this->negative_prompt, |
| 82 | 'weight' => -1, |
| 83 | ), |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | public function set_negative_prompt( $negative_prompt ) { |
| 88 | $this->negative_prompt = $negative_prompt; |
| 89 | } |
| 90 | |
| 91 | private function post( $data, $files = array(), $settings = array() ) { |
| 92 | $response = $this->api->post( |
| 93 | $data, |
| 94 | $files, |
| 95 | $settings |
| 96 | ); |
| 97 | |
| 98 | return $response; |
| 99 | } |
| 100 | } |
| 101 |