Add a filter (or setting?) to allow alt text generation when the site has basic authentication

Aperta
#895 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
32/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
php
Ambito
ai, backend

Direzione di ricerca

Inizia dal filtro classifai_openai_api_request_post_options e traccia il percorso della richiesta di testo alternativo per classifai_feature_descriptive_text_generator. Confronta le opzioni di richiesta esistenti con la soluzione alternativa basic-auth fornita; il lavoro dovrebbe considerarsi completato quando le immagini provenienti da siti protetti da basic-auth possono essere elaborate senza richiedere al provider di recuperarle direttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Is your enhancement related to a problem? Please describe.

Right now alt text generation fails when basic authentication is enabled because OpenAI (or whichever provider) can't access the image directly.

This code works specifically for my use-case, but I'm wondering if there is a way to more easily handle this while we're working on protected staging sites.

/**
 * Handle basic auth for Classifai alt text generation via OpenAI.
 *
 * @param array  $options The options.
 * @param string $url     The URL.
 * @param string $feature The feature.
 *
 * @return array
 */
add_filter( 'classifai_openai_api_request_post_options', function( array $options, string $url, string $feature ): array {
	// Bail if not our feature.
	if ( 'classifai_feature_descriptive_text_generator' !== $feature ) {
		return $options;
	}

	// Bail if no body.
	if ( ! isset( $options['body'] ) ) {
		return $options;
	}

	// Bail if no messages.
	$body = json_decode( $options['body'], true );
	if ( ! $body || empty( $body['messages'] ) ) {
		return $options;
	}

	// Set the username and password.
	$un = 'someun';
	$pw = 'somepw';

	// Encode the credentials.
	$creds = base64_encode( $un . ':' . $pw );

	// Loop through the messages.
	foreach ( $body['messages'] as &$message ) {
		// Bail if not an array.
		if ( ! is_array( $message['content'] ) ) {
			continue;
		}

		// Loop through the items.
		foreach ( $message['content'] as &$item ) {
			// Bail if not an image.
			if ( empty( $item['type'] ) || 'image_url' !== $item['type'] ) {
				continue;
			}

			// Fetch the image manually.
			$resp = wp_remote_get( $item['image_url']['url'], [
				'headers' => [ 'Authorization' => 'Basic ' . $creds ],
			] );

			// Bail if error.
			if ( is_wp_error( $resp ) ) {
				continue;
			}

			// Get the raw body.
			$raw  = wp_remote_retrieve_body( $resp );

			// Encode the body.
			$b64  = base64_encode( $raw );

			// Figure out the MIME type (fallback to jpeg).
			$info = @getimagesizefromstring( $raw );
			$mime = $info ? $info['mime'] : 'image/jpeg';

			// Replace with a Data URI.
			$item = [
				'type'      => 'image_url',
				'image_url' => [
					'url'    => "data:{$mime};base64,{$b64}",
					'detail' => $item['image_url']['detail'] ?? 'auto',
				],
			];
		}
	}

	// Encode the body.
	$options['body'] = wp_json_encode( $body );

	return $options;

}, 10, 3  );
Designs

No response

Describe alternatives you've considered

No response

Code of Conduct
  • I agree to follow this project's Code of Conduct
Lingua principale
PHP
Stelle
714
Fork
68
Merge medio
4h 32m
PR unite (30g)
23

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di 10up/classifai

Tutte le issue di 10up/classifai

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.