|
| 1 | +# Drupal AI Alt Text Generator Module |
| 2 | + |
| 3 | +Provides AI‑generated alt text suggestions for images without alt text in the media library, with a human review workflow. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Drupal 10 |
| 8 | +- PHP 7.4 or higher |
| 9 | +- PHP extensions: cURL, GD or Imagick |
| 10 | +- OpenAI API key |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +1. Copy the `alt_text_review` folder into your `modules/contrib` directory. |
| 15 | +2. Enable the module: |
| 16 | + ```bash |
| 17 | + drush en alt_text_review -y |
| 18 | + |
| 19 | +3. Assign the **Access Alt Text Review UI** permission to the roles that should be able to review and save alt text. |
| 20 | + |
| 21 | +## Configuration |
| 22 | + |
| 23 | +Visit **Configuration » Media » Alt Text Review Settings** (`/admin/config/media/alt-text-review`) and configure: |
| 24 | + |
| 25 | +* **OpenAI API key** |
| 26 | +* **AI Prompt** |
| 27 | + |
| 28 | + > A template for the AI, with the token `[max_length]` replaced by your max‑length setting. |
| 29 | +* **Alt text maximum character length** |
| 30 | + |
| 31 | + > Integer value (default 128) that replaces `[max_length]` in your prompt. |
| 32 | +* **Enable debug logging** |
| 33 | + |
| 34 | + > Logs full request/response payloads when checked. Disable in production. |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +Go to **Content » Media » Review Alt Text** (`/admin/content/media/alt-text-review`): |
| 39 | + |
| 40 | +1. The module queries for the first media item of type **Image** where `field_media_image.alt` is empty or NULL. |
| 41 | +2. It **automatically downscales** the image to fit within an 800 × 800 px box (preserving aspect ratio) to limit payload size. |
| 42 | +3. The downscaled image is base64‑encoded and sent, along with your prompt, to the AI. |
| 43 | +4. You see the image plus the AI’s suggested alt text. |
| 44 | +5. Edit or accept the suggestion and **Save alt text**. You’re redirected to the next image without alt text. |
| 45 | + |
| 46 | +## How It Works |
| 47 | + |
| 48 | +1. **Discovery** |
| 49 | + Runs an entity query for `media` bundle `image` where the alt field is empty. |
| 50 | +2. **Downscaling** |
| 51 | + Uses Drupal’s ImageFactory to load the file and resize it so neither width nor height exceeds 800 px. |
| 52 | +3. **Encoding** |
| 53 | + Reads the (downscaled) image file, base64‑encodes it, and wraps it in a `data:` URI. |
| 54 | +4. **Prompt Assembly** |
| 55 | + Injects your max‑length setting into the prompt template. |
| 56 | +5. **API Call** |
| 57 | + Sends a Chat Completion request to `https://api.openai.com/v1/chat/completions` with: |
| 58 | + |
| 59 | + * **model**: `gpt-4o-mini` |
| 60 | + * **messages**: `[ { role: "user", content: [ { type: "text", text: prompt }, { type: "image_url", image_url: { url: dataUri } } ] } ]` |
| 61 | + * **max\_tokens**: calculated from your max length. |
| 62 | +6. **Error Handling** |
| 63 | +
|
| 64 | + * Missing API key → early error message. |
| 65 | + * File not found → log and fallback message. |
| 66 | + * Request failure → log details if debug enabled, show generic error. |
| 67 | +7. **Review & Save** |
| 68 | + The form lets you tweak or accept the AI suggestion and saves it to the media entity’s `alt` attribute. |
| 69 | +
|
| 70 | +## Model and Pricing |
| 71 | +
|
| 72 | +This module uses **GPT‑4o mini**, priced at: |
| 73 | +
|
| 74 | +* **\$0.15 per 1 M input tokens** |
| 75 | +* **\$0.60 per 1 M output tokens** ([OpenAI][1], [Reuters][2]) |
| 76 | +
|
| 77 | +Because images are downscaled to 800×800 px, a typical JPEG (\~50 KB) yields about 68 000 base64 characters (≈17 000 tokens). With a 32‑token response, the cost per image is: |
| 78 | +
|
| 79 | +```text |
| 80 | +Input cost = (17 000 / 1 000 000) × $0.15 ≈ $0.00255 |
| 81 | +Output cost = ( 32 / 1 000 000) × $0.60 ≈ $0.00002 |
| 82 | +Total ≈ $0.00257 per image |
| 83 | +``` |
| 84 | +
|
| 85 | +> To further reduce cost, you can lower the downscale dimensions or adjust JPEG quality via your image styles. |
| 86 | +
|
| 87 | +[1]: https://openai.com/index/gpt-4o-mini-advancing-cost-efficient-intelligence/?utm_source=chatgpt.com "GPT-4o mini: advancing cost-efficient intelligence - OpenAI" |
| 88 | +[2]: https://www.reuters.com/technology/artificial-intelligence/openai-unveils-cheaper-small-ai-model-gpt-4o-mini-2024-07-18/?utm_source=chatgpt.com "OpenAI unveils cheaper small AI model GPT-4o mini" |
| 89 | +
|
0 commit comments