|
| 1 | +'use server'; |
| 2 | +/** |
| 3 | + * @fileOverview Generates three distinct image variations from a user-provided photo. |
| 4 | + * |
| 5 | + * - generateVariationsFromPhoto - A function that takes a photo and generates three image variations. |
| 6 | + * - GenerateVariationsFromPhotoInput - The input type for the generateVariationsFromPhoto function. |
| 7 | + * - GenerateVariationsFromPhotoOutput - The return type for the generateVariationsFromPhoto function. |
| 8 | + */ |
| 9 | + |
| 10 | +import {ai} from '@/ai/genkit'; |
| 11 | +import {z} from 'genkit'; |
| 12 | + |
| 13 | +const GenerateVariationsFromPhotoInputSchema = z.object({ |
| 14 | + photoDataUri: z |
| 15 | + .string() |
| 16 | + .describe( |
| 17 | + 'A photo to generate variations from, as a data URI that must include a MIME type and use Base64 encoding. Expected format: \'data:<mimetype>;base64,<encoded_data>\'.' |
| 18 | + ), |
| 19 | +}); |
| 20 | +export type GenerateVariationsFromPhotoInput = z.infer<typeof GenerateVariationsFromPhotoInputSchema>; |
| 21 | + |
| 22 | +const GenerateVariationsFromPhotoOutputSchema = z.object({ |
| 23 | + image1DataUri: z.string().describe('The first generated image variation as a data URI.'), |
| 24 | + image2DataUri: z.string().describe('The second generated image variation as a data URI.'), |
| 25 | + image3DataUri: z.string().describe('The third generated image variation as a data URI.'), |
| 26 | +}); |
| 27 | +export type GenerateVariationsFromPhotoOutput = z.infer<typeof GenerateVariationsFromPhotoOutputSchema>; |
| 28 | + |
| 29 | +export async function generateVariationsFromPhoto( |
| 30 | + input: GenerateVariationsFromPhotoInput |
| 31 | +): Promise<GenerateVariationsFromPhotoOutput> { |
| 32 | + return generateVariationsFromPhotoFlow(input); |
| 33 | +} |
| 34 | + |
| 35 | +const generateVariationsFromPhotoFlow = ai.defineFlow( |
| 36 | + { |
| 37 | + name: 'generateVariationsFromPhotoFlow', |
| 38 | + inputSchema: GenerateVariationsFromPhotoInputSchema, |
| 39 | + outputSchema: GenerateVariationsFromPhotoOutputSchema, |
| 40 | + }, |
| 41 | + async input => { |
| 42 | + const {photoDataUri} = input; |
| 43 | + |
| 44 | + // Generate three image variations using the Gemini 2.5 Flash Image model |
| 45 | + const [image1, image2, image3] = await Promise.all([ |
| 46 | + ai.generate({ |
| 47 | + model: 'googleai/gemini-2.5-flash-image-preview', |
| 48 | + prompt: [ |
| 49 | + {media: {url: photoDataUri}}, |
| 50 | + {text: 'Generate a similar image with a slightly different style.'}, |
| 51 | + ], |
| 52 | + config: { |
| 53 | + responseModalities: ['TEXT', 'IMAGE'], |
| 54 | + }, |
| 55 | + }), |
| 56 | + ai.generate({ |
| 57 | + model: 'googleai/gemini-2.5-flash-image-preview', |
| 58 | + prompt: [ |
| 59 | + {media: {url: photoDataUri}}, |
| 60 | + {text: 'Generate a similar image with a different color palette.'}, |
| 61 | + ], |
| 62 | + config: { |
| 63 | + responseModalities: ['TEXT', 'IMAGE'], |
| 64 | + }, |
| 65 | + }), |
| 66 | + ai.generate({ |
| 67 | + model: 'googleai/gemini-2.5-flash-image-preview', |
| 68 | + prompt: [ |
| 69 | + {media: {url: photoDataUri}}, |
| 70 | + {text: 'Generate a similar image with a different background.'}, |
| 71 | + ], |
| 72 | + config: { |
| 73 | + responseModalities: ['TEXT', 'IMAGE'], |
| 74 | + }, |
| 75 | + }), |
| 76 | + ]); |
| 77 | + |
| 78 | + return { |
| 79 | + image1DataUri: image1.media!.url, |
| 80 | + image2DataUri: image2.media!.url, |
| 81 | + image3DataUri: image3.media!.url, |
| 82 | + }; |
| 83 | + } |
| 84 | +); |
0 commit comments