-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters