PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.6
Kubio AI Page Builder v2.8.6
2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / AI / SDApi / KubioCloudSDApi.php
kubio / lib / AI / SDApi Last commit date
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