assist-feedback.php
11 months ago
assistant.php
11 months ago
base.php
11 months ago
dropped-file.php
11 months ago
edit-image.php
11 months ago
embed.php
11 months ago
feedback.php
11 months ago
function.php
11 months ago
image.php
11 months ago
parameter.php
11 months ago
text.php
11 months ago
transcribe.php
11 months ago
image.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Image extends Meow_MWAI_Query_Base { |
| 4 | public ?string $resolution = null; |
| 5 | public ?string $style = null; |
| 6 | public ?string $localDownload = 'uploads'; |
| 7 | public ?string $localDownloadExpiry = 'uploads'; |
| 8 | |
| 9 | #region Constructors, Serialization |
| 10 | |
| 11 | public function __construct( ?string $message = '', ?string $model = null ) { |
| 12 | parent::__construct( $message ); |
| 13 | $this->model = $model; |
| 14 | $this->feature = 'text-to-image'; // image-to-image, inpainting, etc |
| 15 | global $mwai_core; |
| 16 | $this->localDownload = $mwai_core->get_option( 'image_local_download' ); |
| 17 | $this->localDownloadExpiry = $mwai_core->get_option( 'image_expires_download' ); |
| 18 | } |
| 19 | |
| 20 | #[\ReturnTypeWillChange] |
| 21 | public function jsonSerialize(): array { |
| 22 | $json = [ |
| 23 | 'message' => $this->message, |
| 24 | |
| 25 | 'ai' => [ |
| 26 | 'model' => $this->model, |
| 27 | 'feature' => $this->feature, |
| 28 | 'resolution' => $this->resolution |
| 29 | ], |
| 30 | |
| 31 | 'system' => [ |
| 32 | 'class' => get_class( $this ), |
| 33 | 'envId' => $this->envId, |
| 34 | 'scope' => $this->scope, |
| 35 | 'session' => $this->session |
| 36 | ] |
| 37 | ]; |
| 38 | |
| 39 | if ( !empty( $this->context ) ) { |
| 40 | $json['context']['content'] = $this->context; |
| 41 | } |
| 42 | |
| 43 | return $json; |
| 44 | } |
| 45 | |
| 46 | #endregion |
| 47 | |
| 48 | #region Parameters |
| 49 | |
| 50 | public function set_resolution( string $resolution ) { |
| 51 | $this->resolution = $resolution; |
| 52 | } |
| 53 | |
| 54 | public function set_style( string $style ) { |
| 55 | $this->style = $style; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set how the image will be treated locally, if it will be downloaded or not, etc. |
| 60 | * @param string $localDownload The local download method. Could be 'uploads', 'library' or null. |
| 61 | */ |
| 62 | public function set_local_download( ?string $localDownload ) { |
| 63 | $this->localDownload = $localDownload; |
| 64 | } |
| 65 | |
| 66 | // Based on the params of the query, update the attributes |
| 67 | public function inject_params( array $params ): void { |
| 68 | parent::inject_params( $params ); |
| 69 | $params = $this->convert_keys( $params ); |
| 70 | |
| 71 | if ( !empty( $params['resolution'] ) ) { |
| 72 | $this->set_resolution( $params['resolution'] ); |
| 73 | } |
| 74 | if ( !empty( $params['style'] ) ) { |
| 75 | $this->set_style( $params['style'] ); |
| 76 | } |
| 77 | // Check both camelCase and snake_case versions for compatibility |
| 78 | if ( array_key_exists( 'localDownload', $params ) ) { |
| 79 | $this->set_local_download( $params['localDownload'] ); |
| 80 | } elseif ( array_key_exists( 'local_download', $params ) ) { |
| 81 | $this->set_local_download( $params['local_download'] ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #endregion |
| 86 | |
| 87 | #region Final Checks |
| 88 | |
| 89 | public function final_checks() { |
| 90 | parent::final_checks(); |
| 91 | |
| 92 | // Since DALL-E 3 only supports 1 image, we force it. |
| 93 | // (Likely the same limitation for other models.) |
| 94 | $this->maxResults = 1; |
| 95 | |
| 96 | global $mwai_core; |
| 97 | $engine = Meow_MWAI_Engines_Factory::get( $mwai_core, $this->envId ); |
| 98 | $modelInfo = $engine->retrieve_model_info( $this->model ); |
| 99 | if ( empty( $modelInfo ) ) { |
| 100 | Meow_MWAI_Logging::error( 'No model info found for model: ' . $this->model, '🖼️' ); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Let's check for resolutions. |
| 105 | if ( !isset( $modelInfo['resolutions'] ) || empty( $modelInfo['resolutions'] ) ) { |
| 106 | Meow_MWAI_Logging::error( 'No resolutions defined for model: ' . $this->model, '🖼️' ); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // If we have no resolution set, we will use the first one |
| 111 | if ( empty( $this->resolution ) ) { |
| 112 | $this->resolution = $modelInfo['resolutions'][0]['name']; |
| 113 | } |
| 114 | |
| 115 | // If we have a resolutions array ([ name, label ]), let’s ensure our current resolution (name) is supported |
| 116 | $resolutions = $modelInfo['resolutions']; |
| 117 | $found = false; |
| 118 | foreach ( $resolutions as $resolution ) { |
| 119 | if ( $resolution['name'] === $this->resolution ) { |
| 120 | $found = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // If we don't find the resolution, we will set it to the first one. |
| 126 | if ( !$found ) { |
| 127 | $supportedResolutions = []; |
| 128 | foreach ( $resolutions as $resolution ) { |
| 129 | $supportedResolutions[] = $resolution['name']; |
| 130 | } |
| 131 | $supportedResolutions = implode( ', ', $supportedResolutions ); |
| 132 | $error = sprintf( 'The model %s does not support the resolution %s (using %s instead). Supported resolutions are: %s.', $this->model, $this->resolution, $resolutions[0]['name'], $supportedResolutions ); |
| 133 | $this->resolution = $resolutions[0]['name']; |
| 134 | Meow_MWAI_Logging::error( $error, '🖼️' ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #endregion |
| 139 | } |
| 140 |