Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
generate images with replicate
  • Loading branch information
zeke committed Dec 17, 2024
commit 921a88a53d6007307d7a711913c898cb6ba8930f
1 change: 1 addition & 0 deletions .dev.vars.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
OPENAI_API_KEY="YOUR-API-KEY"
REPLICATE_API_TOKEN="YOUR-API-TOKEN"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ npm run dev

## Deploy

Upload your secret
Upload your secrets

```bash
npx wrangler secret put OPENAI_API_KEY
npx wrangler secret put REPLICATE_API_TOKEN
```

```bash
Expand Down
176 changes: 175 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"wrangler": "^3.60.3"
},
"dependencies": {
"hono": "^4.6.13"
"hono": "^4.6.13",
"replicate": "^1.0.1"
}
}
5 changes: 3 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
<body>
<div class="content">
<h1>This is a plain old website</h1>
<p>This is just a plain website that is using plain old JavaScript</p>
<button onclick="talkToTheHand()">Talk to the hand</button>
<p>I can make images with AI. Tell me what you want to see.</p>

<div id="image-container"></div>
</div>
<footer>
<p>Built with 🧡 on <a href="https://developers.cloudflare.com">Cloudflare Workers</a> and the <a href="https://platform.openai.com/docs/api-reference/realtime">OpenAI Realtime API</a></p>
Expand Down
29 changes: 29 additions & 0 deletions public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ const fns = {
await hand.sendCommand(numberOfFingers);
return { success: true, numberOfFingers };
},
generateImage: async ({ prompt }) => {
console.log('generateImage', prompt);
const imageUrl = await fetch('/generate-image', {
method: 'POST',
body: prompt,
}).then((r) => r.text());

console.log('imageUrl', imageUrl);

// append the image to the page
const img = document.createElement('img');
img.src = imageUrl;
img.style.maxWidth = '100%';
const container = document.getElementById('image-container');
container.prepend(img);

return { success: true, imageUrl };
}
};

// Create a WebRTC Agent
Expand Down Expand Up @@ -84,6 +102,17 @@ function configureData() {
name: 'getPageHTML',
description: 'Gets the HTML for the current page',
},
{
type: 'function',
name: 'generateImage',
description: 'Generates an image using AI and displays it on the page',
parameters: {
type: 'object',
properties: {
prompt: { type: 'string', description: 'Text description of the image to generate' }
}
}
}
],
},
};
Expand Down
25 changes: 25 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hono } from 'hono';
import Replicate from 'replicate';

const app = new Hono<{ Bindings: Env }>();

Expand Down Expand Up @@ -34,4 +35,28 @@ app.post('/rtc-connect', async (c) => {
});
});


app.post('/generate-image', async (c) => {
const replicate = new Replicate({auth: c.env.REPLICATE_API_TOKEN})
const model = 'black-forest-labs/flux-schnell'
const prompt = await c.req.text()
const output = await replicate.run(model, {
input: {
prompt,
image_format: 'webp',
}
})

// Some image models return an array of output files, others just a single file.
const imageUrl = Array.isArray(output) ? output[0].url() : output.url()

console.log({imageUrl})

return c.body(imageUrl, {
headers: {
'Content-Type': 'image/webp',
},
});
});

export default app;
1 change: 1 addition & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

interface Env {
OPENAI_API_KEY: string;
REPLICATE_API_TOKEN: string;
}