app.post(
'/get-alt',
zValidator('form', getAltSchema),
async (c) => {
const { image } = c.req.valid('form')
try {
let imageData: ArrayBuffer;
if (image instanceof File) {
imageData = await image.arrayBuffer();
} else {
const response = await fetch(image.toString());
if (!response.ok) {
return c.json({ error: 'Failed to fetch image from URL' }, 400);
}
imageData = await response.arrayBuffer();
}
const imageBytes = new Uint8Array(imageData);
try {
const response = await c.env.AI.run('@cf/meta/llama-3.2-11b-vision-instruct', {
prompt: 'Create a concise alt text for this image. Use simple language and keep it under 160 characters.',
image: imageBytes
});
return c.json(response);
} catch (error) {
console.error('AI processing error:', error);
return c.json({ error: 'Failed to process image with AI model' }, 500);
}
} catch (error) {
console.error('Image processing error:', error);
return c.json({ error: 'Failed to process image data' }, 500);
}
}
)
app.post(
'/get-alt',
zValidator('form', getAltSchema),
async (c) => {
const { image } = c.req.valid('form')
try {
let imageData: ArrayBuffer;
if (image instanceof File) {
imageData = await image.arrayBuffer();
} else {
const response = await fetch(image.toString());
if (!response.ok) {
return c.json({ error: 'Failed to fetch image from URL' }, 400);
}
imageData = await response.arrayBuffer();
}
const imageBytes = new Uint8Array(imageData);
try {
const response = await c.env.AI.run('@cf/meta/llama-3.2-11b-vision-instruct', {
prompt: 'Create a concise alt text for this image. Use simple language and keep it under 160 characters.',
image: imageBytes
});
return c.json(response);
} catch (error) {
console.error('AI processing error:', error);
return c.json({ error: 'Failed to process image with AI model' }, 500);
}
} catch (error) {
console.error('Image processing error:', error);
return c.json({ error: 'Failed to process image data' }, 500);
}
}
)