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

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 9.1.0
* @since 9.2.0

*/
avatarAI(options?: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name doesnt really tell what this method returns, only how the result was generated.
As discussed elsewhere I would like to call these portraits, but I'm open to other suggestions.

https://en.wikipedia.org/wiki/Portrait

/**
* 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use our normal destructuring syntax for this.

// 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