Skip to content

Commit

Permalink
use openai for iamges
Browse files Browse the repository at this point in the history
  • Loading branch information
elitan committed Nov 6, 2023
1 parent f1240bb commit d699787
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 11 deletions.
3 changes: 2 additions & 1 deletion worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"transcribe": "ts-node src/2-transcribe.ts",
"openai": "ts-node src/3-openai.ts",
"su": "ts-node src/4-stable-diffusion.ts",
"images": "ts-node src/4-openai-images.ts",
"elevenlabs": "ts-node src/5-eleven-labs.ts",
"piper": "ts-node src/5-piper.ts",
"codegen": "kysely-codegen --dialect postgres --camel-case --out-file ./src/utils/kysely-types.d.ts",
Expand All @@ -34,7 +35,7 @@
"kysely": "^0.26.1",
"kysely-codegen": "^0.10.1",
"node-fetch": "^2.4.0",
"openai": "^4.11.1",
"openai": "^4.16.0",
"pg": "^8.11.1",
"slugify": "^1.6.6",
"ts-node": "^10.9.1",
Expand Down
14 changes: 10 additions & 4 deletions worker/pnpm-lock.yaml

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

86 changes: 86 additions & 0 deletions worker/src/4-openai-images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dotenv/config';
import { db } from './utils/db';
import { S3, PutObjectCommand } from '@aws-sdk/client-s3';
import { Transformer } from '@napi-rs/image';
import { openai } from './utils/openai';

const s3Client = new S3({
endpoint: 'https://ams3.digitaloceanspaces.com',
region: 'us-east-1',
credentials: {
accessKeyId: process.env.SPACES_KEY as string,
secretAccessKey: process.env.SPACES_SECRET as string,
},
});

async function main() {
const articlesToRefine = await db
.selectFrom('articles')
.select(['id', 'title', 'slug', 'imagePrompt'])
.where('imagePrompt', 'is not', null)
.where('articleImageId', 'is', null)
.orderBy('id', 'desc')
.execute();

for (const article of articlesToRefine) {
if (!article.imagePrompt) {
console.log('No image prompt found for article', article.id);
continue;
}

console.log('Generating image for article', article.id);

const image = await openai.images.generate({
model: 'dall-e-3',
prompt: article.imagePrompt,
response_format: 'b64_json',
n: 1,
quality: 'standard',
size: '1792x1024',
style: 'vivid',
});

if (!image.data[0].b64_json) {
console.log('No image found for article', article.id);
continue;
}

// generate unique filename
const fileName = `images/${article.id}.webp`;

const rawImage = Buffer.from(image.data[0].b64_json, 'base64');
const imageBinary = await new Transformer(rawImage).webp(75);

console.log('Uploading image to spaces');

// upload image to spaces
const params = {
Bucket: 'nyheter',
Key: fileName,
Body: imageBinary,
ContentType: 'image/webp',
ACL: 'public-read',
};

await s3Client.send(new PutObjectCommand(params));

const insertedArticleImage = await db
.insertInto('articleImages')
.values({
articleId: article.id,
imageUrl: `https://nyheter.ams3.cdn.digitaloceanspaces.com/${fileName}`,
})
.returning(['id'])
.executeTakeFirstOrThrow();

await db
.updateTable('articles')
.set({
articleImageId: insertedArticleImage.id,
})
.where('id', '=', article.id)
.execute();
}
}

main();
41 changes: 36 additions & 5 deletions worker/src/playground.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import 'dotenv/config';
import { openai } from './utils/openai';
import { Transformer } from '@napi-rs/image';
import { S3, PutObjectCommand } from '@aws-sdk/client-s3';

import { translate } from './utils/helpers';
import slugify from 'slugify';
const s3Client = new S3({
endpoint: 'https://ams3.digitaloceanspaces.com',
region: 'us-east-1',
credentials: {
accessKeyId: process.env.SPACES_KEY as string,
secretAccessKey: process.env.SPACES_SECRET as string,
},
});

async function main() {
const headline = 'ارتفاع الكرونا السويدية: التأثير على التضخم في المستقبل؟';
const prompt =
'A Swedish flag fluttering against a backdrop of a city skyline, symbolizing the Swedish labor market. In the foreground, a contract is laid out on a table, an ink pen resting on top of it. The contract represents the collective agreement signed by Klarna.';

const slug = slugify(headline, { strict: false, lower: true });
const image = await openai.images.generate({
model: 'dall-e-3',
prompt,
response_format: 'b64_json',
n: 1,
quality: 'standard',
size: '1792x1024',
style: 'vivid',
});

console.log({ slug });
console.log(image.data[0]);

const rawImage = Buffer.from(image.data[0]!.b64_json as string, 'base64');
const imageBinary = await new Transformer(rawImage).webp(75);

const params = {
Bucket: 'nyheter',
Key: 'test.webp',
Body: imageBinary,
ContentType: 'image/webp',
ACL: 'public-read',
};

await s3Client.send(new PutObjectCommand(params));
}

main();
2 changes: 1 addition & 1 deletion worker/src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
removeLastSentence,
} from './helpers';

const openai = new OpenAI({
export const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

Expand Down

0 comments on commit d699787

Please sign in to comment.