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
KubioCloudSDApi.php
113 lines
| 1 | <?php |
| 2 | namespace Kubio\Ai; |
| 3 | |
| 4 | use IlluminateAgnostic\Arr\Support\Arr; |
| 5 | use Kubio\Core\Importer; |
| 6 | |
| 7 | class KubioCloudSDApi { |
| 8 | private $api_key = false; |
| 9 | private $api_endpoint = ''; |
| 10 | private $api_accept_type = 'application/json'; |
| 11 | private $api_content_type = 'application/json'; |
| 12 | private $api_dummy_response = false; |
| 13 | |
| 14 | const __DEFAULTS__ = array( |
| 15 | 'steps' => 40, |
| 16 | 'width' => 512, |
| 17 | 'height' => 512, |
| 18 | 'seed' => 0, |
| 19 | 'cfg_scale' => 5, |
| 20 | 'samples' => 1, |
| 21 | 'style_preset' => 'photographic', |
| 22 | ); |
| 23 | |
| 24 | function __construct( $api_key = false, $api_model = false, $api_dummy_response = false ) { |
| 25 | $this->api_dummy_response = $api_dummy_response; |
| 26 | |
| 27 | // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged |
| 28 | @ini_set( 'max_execution_time', 70 ); |
| 29 | |
| 30 | // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged |
| 31 | set_time_limit( 70 ); |
| 32 | } |
| 33 | |
| 34 | public function post( $data, $files = array() ) { |
| 35 | if ( $this->api_dummy_response === true ) { |
| 36 | return $data; |
| 37 | } |
| 38 | |
| 39 | $response = kubio_ai_call_api( |
| 40 | $this->get_api_url(), |
| 41 | $data |
| 42 | ); |
| 43 | |
| 44 | return $this->response( $response ); |
| 45 | } |
| 46 | |
| 47 | public function set_api_endpoint( $api_endpoint ) { |
| 48 | if ( $api_endpoint !== false ) { |
| 49 | $this->api_endpoint = $api_endpoint; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private function get_api_url() { |
| 54 | return $this->api_endpoint; |
| 55 | } |
| 56 | |
| 57 | public function set_api_accept_type( $api_accept_type ) { |
| 58 | $this->api_accept_type = $api_accept_type; |
| 59 | } |
| 60 | |
| 61 | public function set_api_content_type( $api_content_type ) { |
| 62 | $this->api_content_type = $api_content_type; |
| 63 | } |
| 64 | |
| 65 | public function response( $response ) { |
| 66 | if ( is_wp_error( $response ) || array_key_exists( 'kai', $response ) ) { |
| 67 | return $response; |
| 68 | } |
| 69 | |
| 70 | $artifacts = Arr::get( $response, 'content.artifacts', array() ); |
| 71 | |
| 72 | if ( ! count( $artifacts ) ) { |
| 73 | return new \WP_Error( |
| 74 | 'error_no_image_generate', |
| 75 | __( 'No image was generated', 'kubio' ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | $images = array(); |
| 80 | $errors = array(); |
| 81 | foreach ( $artifacts as $image ) { |
| 82 | $filename = wp_generate_uuid4() . '.png'; |
| 83 | $upload = Importer::base64ToImage( $filename, $image['base64'] ); |
| 84 | if ( is_wp_error( $upload ) ) { |
| 85 | $errors[] = $upload; |
| 86 | } else { |
| 87 | $images[] = $upload['url']; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if ( ! empty( $errors ) ) { |
| 92 | return $errors[0]; |
| 93 | } |
| 94 | |
| 95 | return array_merge( |
| 96 | $response, |
| 97 | array( |
| 98 | 'content' => $images, |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | function maybe_log_response( $resp, $section = 'text_to_image_sd', $level = 'info' ) { |
| 104 | kubio_ai_log( |
| 105 | $level, |
| 106 | $section, |
| 107 | array(), |
| 108 | $resp, |
| 109 | array() |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 |