-
-
Notifications
You must be signed in to change notification settings - Fork 929
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
base: next
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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?: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
/** | ||
* 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.