Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): add AI-generated avatars #3126

Merged
merged 30 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9530d27
feat(image): Add AI-generated avatars
matthewmayer Sep 21, 2024
2bb43b9
fix tests
matthewmayer Sep 21, 2024
28e5cf7
Merge branch 'next' into avatar-ai
ST-DDT Oct 26, 2024
5312d51
Merge branch 'next' into avatar-ai
matthewmayer Feb 3, 2025
c359235
update to personPortrait and new URIs
matthewmayer Feb 3, 2025
d6be62b
Merge branch 'next' into avatar-ai
matthewmayer Feb 3, 2025
895d653
bump since value
matthewmayer Feb 4, 2025
acfd587
add this to the weekly url availability checks:
matthewmayer Feb 4, 2025
3438d59
new test
matthewmayer Feb 4, 2025
6b2b0b8
remove 16px size
matthewmayer Feb 4, 2025
6407fca
Merge branch 'next' into avatar-ai
matthewmayer Feb 4, 2025
b726bb8
update docs
matthewmayer Feb 4, 2025
e987dbc
Update index.ts
matthewmayer Feb 5, 2025
e963e43
Update index.ts
matthewmayer Feb 5, 2025
6638f8d
Update index.ts
matthewmayer Feb 5, 2025
93b964c
Update index.ts
matthewmayer Feb 5, 2025
1c32022
Update index.ts
matthewmayer Feb 5, 2025
f7882cb
Update index.ts
matthewmayer Feb 5, 2025
f8c699e
Update index.ts
matthewmayer Feb 5, 2025
7ab7522
Update index.ts
matthewmayer Feb 5, 2025
425c2cf
Update index.ts
matthewmayer Feb 5, 2025
5e8dabb
fix URL
matthewmayer Feb 5, 2025
949ccec
tweak docs
matthewmayer Feb 5, 2025
557b25e
Update index.ts
matthewmayer Feb 5, 2025
e23fd66
final urls
matthewmayer Feb 7, 2025
8ed7ef7
new tests
matthewmayer Feb 7, 2025
5c248a3
final urls
matthewmayer Feb 7, 2025
a35394b
chore: fix import scan
ST-DDT Feb 7, 2025
242609e
chore: format
ST-DDT Feb 7, 2025
41b893c
Merge branch 'next' into avatar-ai
ST-DDT Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/modules/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';
import type { SexType } from '../person';

/**
* Module to generate images.
Expand All @@ -26,7 +27,11 @@ export class ImageModule extends ModuleBase {
*/
avatar(): string {
// Add new avatar providers here, when adding a new one.
return this.avatarGitHub();
const avatarMethod = this.faker.helpers.arrayElement([
this.avatarAI,
this.avatarGitHub,
]);
return avatarMethod();
}

/**
Expand All @@ -44,6 +49,35 @@ export class ImageModule extends ModuleBase {
)}`;
}

/**
* Generates a random avatar generated by AI. These are currently images of people which were generated by Stable Diffusion 3. The image URLs are served via the JSDelivr CDN and subject to their [terms of use](https://www.jsdelivr.com/terms).
*
* @param options Options for generating an AI avatar.
* @param options.sex The sex of the person for the avatar. Can be 'female' or 'male'. If not provided, defaults to a random selection.
*
* @example
* faker.image.avatarAI()
* // 'https://cdn.jsdelivr.net/.../female/32.jpg'
* faker.image.avatarAI({ sex: 'male' })
* // 'https://cdn.jsdelivr.net/.../male/32.jpg'
*
* @since 9.1.0
*/
avatarAI(options?: {
/**
* The sex of the person for the avatar.
* Can be 'female' or 'male'.
* If not provided, defaults to a random selection.
*/
sex?: SexType;
}): string {
const type = options?.sex ?? this.faker.person.sexType();
// FIXME: This should be replaced by the final CDN url, presumably something like: 'https://cdn.jsdelivr.net/gh/faker-js/faker-media/avatars_ai'
const baseURL =
'https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt';
return `${baseURL}/${type}/${this.faker.number.int({ min: 1, max: 50 })}.jpg`;
}

/**
* Generates a random avatar from `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar`.
*
Expand Down
18 changes: 15 additions & 3 deletions test/modules/__snapshots__/image.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`image > 42 > avatar 1`] = `"https://avatars.githubusercontent.com/u/37454012"`;
exports[`image > 42 > avatar 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/male/37.jpg"`;

exports[`image > 42 > avatarAI > noArgs 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/female/48.jpg"`;

exports[`image > 42 > avatarAI > with sex 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/female/19.jpg"`;

exports[`image > 42 > avatarGitHub 1`] = `"https://avatars.githubusercontent.com/u/37454012"`;

Expand Down Expand Up @@ -76,7 +80,11 @@ exports[`image > 42 > urlPlaceholder > with width 1`] = `"https://via.placeholde

exports[`image > 42 > urlPlaceholder > with width and height 1`] = `"https://via.placeholder.com/128x128/8ead33/1ddf0f.webp?text=benevolentia%20attonbitus%20auctus"`;

exports[`image > 1211 > avatar 1`] = `"https://avatars.githubusercontent.com/u/92852016"`;
exports[`image > 1211 > avatar 1`] = `"https://avatars.githubusercontent.com/u/89347165"`;

exports[`image > 1211 > avatarAI > noArgs 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/male/45.jpg"`;

exports[`image > 1211 > avatarAI > with sex 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/female/47.jpg"`;

exports[`image > 1211 > avatarGitHub 1`] = `"https://avatars.githubusercontent.com/u/92852016"`;

Expand Down Expand Up @@ -152,7 +160,11 @@ exports[`image > 1211 > urlPlaceholder > with width 1`] = `"https://via.placehol

exports[`image > 1211 > urlPlaceholder > with width and height 1`] = `"https://via.placeholder.com/128x128/ed4fef/a7fbae.webp?text=dapifer%20usque%20unde"`;

exports[`image > 1337 > avatar 1`] = `"https://avatars.githubusercontent.com/u/26202467"`;
exports[`image > 1337 > avatar 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/female/14.jpg"`;

exports[`image > 1337 > avatarAI > noArgs 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/female/8.jpg"`;

exports[`image > 1337 > avatarAI > with sex 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/sd3-avatars@country-prompt/female/14.jpg"`;

exports[`image > 1337 > avatarGitHub 1`] = `"https://avatars.githubusercontent.com/u/26202467"`;

Expand Down
14 changes: 14 additions & 0 deletions test/modules/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ describe('image', () => {
type: 'svg-uri',
});
});

t.describe('avatarAI', (t) => {
t.it('noArgs').it('with sex', { sex: 'female' });
});
});

describe('avatar', () => {
Expand Down Expand Up @@ -124,6 +128,16 @@ describe('image', () => {
});
});

describe('avatarAI', () => {
it('should return a random avatar url from AI', () => {
const avatarUrl = faker.image.avatarAI();

expect(avatarUrl).toBeTypeOf('string');
expect(avatarUrl).toMatch(/^https:\/\/cdn\.jsdelivr\.net\/.*\.jpg$/);
expect(() => new URL(avatarUrl)).not.toThrow();
});
});

describe('url', () => {
it('should return a random image url', () => {
const imageUrl = faker.image.url();
Expand Down