-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mslinnea/add-alt-text
Add alt text generation
- Loading branch information
Showing
9 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { getMimeType, getBase64Image } from '../../helpers/image-helpers'; | ||
|
||
const { enums, helpers } = window.aiServices.ai; | ||
const AI_CAPABILITIES = [enums.AiCapability.MULTIMODAL_INPUT, enums.AiCapability.TEXT_GENERATION]; | ||
|
||
export default async function generateAltText(props, setInProgress, service) { | ||
const { attributes, setAttributes } = props; | ||
|
||
// Check if AI service is available | ||
if (!service) { | ||
// eslint-disable-next-line no-console | ||
console.error('AI service is not available.'); | ||
return; | ||
} | ||
|
||
// Check if image URL is available | ||
if (!attributes.url) { | ||
// eslint-disable-next-line no-console | ||
console.error('Image URL is missing.'); | ||
return; | ||
} | ||
|
||
setInProgress(true); | ||
|
||
// Fetch the image data and prepare the request | ||
try { | ||
const mimeType = getMimeType(attributes.url); | ||
const base64Image = await getBase64Image(attributes.url); | ||
|
||
// Call the AI service to generate alt text | ||
const candidates = await service.generateText( | ||
{ | ||
role: enums.ContentRole.USER, | ||
parts: [ | ||
{ | ||
text: __( | ||
'Create a brief description of what the following image shows, suitable as alternative text for screen readers.', | ||
'ai-seo-tools', | ||
), | ||
}, | ||
{ | ||
inlineData: { | ||
mimeType, | ||
data: base64Image, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
feature: 'ai-seo-tools-alt-text', | ||
capabilities: AI_CAPABILITIES, | ||
}, | ||
); | ||
|
||
// Extract the generated alt text | ||
const alt = helpers | ||
.getTextFromContents(helpers.getCandidateContents(candidates)) | ||
.replaceAll('\n\n\n\n', '\n\n'); | ||
|
||
// Set the alt text attribute | ||
setAttributes({ alt }); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error generating alt text:', error); | ||
} finally { | ||
setInProgress(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import ImageControls from '../imageControls'; | ||
|
||
// eslint-disable-next-line func-names | ||
const addToolbar = (BlockEdit) => function (props) { | ||
// eslint-disable-next-line react/prop-types | ||
const { name } = props; | ||
if (name === 'core/image') { | ||
return ( | ||
<> | ||
<BlockEdit {...props} /> | ||
<ImageControls {...props} /> | ||
</> | ||
); | ||
} | ||
return <BlockEdit {...props} />; | ||
}; | ||
|
||
addFilter( | ||
'editor.BlockEdit', | ||
'ai-seo-tools/add-block-toolbar', | ||
addToolbar, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { ToolbarButton } from '@wordpress/components'; | ||
import { useState } from 'react'; | ||
import { BlockControls } from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
import generateAltText from '../altText/index'; | ||
|
||
const { enums, store: aiStore } = window.aiServices.ai; | ||
const AI_CAPABILITIES = [enums.AiCapability.MULTIMODAL_INPUT, enums.AiCapability.TEXT_GENERATION]; | ||
|
||
export default function ImageControls(props) { | ||
const [inProgress, setInProgress] = useState(false); | ||
const service = useSelect((select) => select(aiStore) | ||
.getAvailableService( | ||
{ capabilities: AI_CAPABILITIES }, | ||
)); | ||
|
||
const handleClick = async () => { | ||
setInProgress(true); | ||
await generateAltText(props, setInProgress, service); | ||
setInProgress(false); | ||
}; | ||
|
||
return ( | ||
<BlockControls group="inline"> | ||
<ToolbarButton | ||
label={__('Write Alt Text', 'ai-seo-tools')} | ||
icon="universal-access-alt" | ||
showTooltip | ||
disabled={inProgress} | ||
onClick={handleClick} | ||
/> | ||
</BlockControls> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export function getMimeType(url) { | ||
const extension = url.split('.').pop().toLowerCase(); | ||
switch (extension) { | ||
case 'png': | ||
return 'image/png'; | ||
case 'gif': | ||
return 'image/gif'; | ||
case 'avif': | ||
return 'image/avif'; | ||
case 'webp': | ||
return 'image/webp'; | ||
case 'jpg': | ||
case 'jpeg': | ||
default: | ||
return 'image/jpeg'; | ||
} | ||
} | ||
|
||
export async function getBase64Image(url) { | ||
const data = await fetch(url); | ||
const blob = await data.blob(); | ||
return new Promise((resolve) => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(blob); | ||
reader.onloadend = () => { | ||
const base64data = reader.result; | ||
resolve(base64data); | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './components/settingsScreen'; | ||
import './components/settingsPanel'; | ||
import './components/blockControls'; |