alukach
alukach
CDCloudflare Developers
Created by frolic on 2/12/2024 in #pages-help
bindings deployed with wrangler.toml
@Cyb3r-Jok3 To expand on this, the bindings configured in wrangler.toml are purely for local development?
4 replies
CDCloudflare Developers
Created by alukach on 3/17/2024 in #workers-ai
Have others been able to run text-to-
For anyone reading this in the future, what worked for me was to push all of the AI operations I was running into a separate worker that I then added to my NextJS app as a Service Binding (https://developers.cloudflare.com/workers/configuration/bindings/about-service-bindings/). This works well for my needs.
import { Ai } from '@cloudflare/ai';
import { AiTextToImageInput, AiTextToImageOutput } from '@cloudflare/ai/dist/ai/tasks/text-to-image';

export interface Env {
AI: Ai;
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (request.method !== 'POST') {
return new Response('Invalid method', { status: 405 });
}

const url = new URL(request.url);
const model = url.pathname.slice(1);

const body = await request.json<AiTextToImageInput>();

const ai = new Ai(env.AI);
const startTime = Date.now();
const image = (await ai.run(model as any, body)) as AiTextToImageOutput;
const duration = (Date.now() - startTime) / 1000;
return new Response(image, { headers: { 'x-duration': `${duration}` } });
},
};
import { Ai } from '@cloudflare/ai';
import { AiTextToImageInput, AiTextToImageOutput } from '@cloudflare/ai/dist/ai/tasks/text-to-image';

export interface Env {
AI: Ai;
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (request.method !== 'POST') {
return new Response('Invalid method', { status: 405 });
}

const url = new URL(request.url);
const model = url.pathname.slice(1);

const body = await request.json<AiTextToImageInput>();

const ai = new Ai(env.AI);
const startTime = Date.now();
const image = (await ai.run(model as any, body)) as AiTextToImageOutput;
const duration = (Date.now() - startTime) / 1000;
return new Response(image, { headers: { 'x-duration': `${duration}` } });
},
};
1 replies