Advertisement

Responsive Advertisement

Cloudflare Worker AI text to image example

Prompt Input

Generate Image from Prompt

After clicking submit, please wait as it takes some time to generate.

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const prompt = url.searchParams.get('prompt') || 'cyberpunk cat';

    const inputs = {
      prompt: prompt,
    };

    const response = await env.AI.run(
      '@cf/stabilityai/stable-diffusion-xl-base-1.0',
      inputs,
    );

    return new Response(response, {
      headers: {
        'Access-Control-Allow-Origin': '*',  // Allow requests from any origin
        'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type',
        'content-type': 'image/png',
      },
    });
  },
};

Another example on GitHub.

Post a Comment

0 Comments