Firebase AI Logic एसडीके की मदद से, आपको Imagen मॉडल (Imagen API के ज़रिए) का ऐक्सेस मिलता है. इससे टेक्स्ट प्रॉम्प्ट से इमेज जनरेट की जा सकती हैं. इस सुविधा की मदद से, ये काम किए जा सकते हैं:
- आम भाषा में लिखे गए प्रॉम्प्ट की मदद से इमेज जनरेट करना
- अलग-अलग फ़ॉर्मैट और स्टाइल में इमेज जनरेट करना
- इमेज में टेक्स्ट रेंडर करना
ध्यान दें कि Firebase AI Logic में, फ़िलहाल Imagen मॉडल के लिए उपलब्ध सभी सुविधाएँ काम नहीं करती हैं. इस पेज पर बाद में, समर्थित सुविधाएं और क्षमताएं सेक्शन में जाकर इसके बारे में ज़्यादा जानें.
सिर्फ़ टेक्स्ट वाले इनपुट के कोड पर जाएं
Gemini और Imagen मॉडल में से किसी एक को चुनना
Firebase AI Logic एसडीके, इमेज जनरेट करने की सुविधा देते हैं. इसके लिए, Gemini मॉडल या Imagen मॉडल का इस्तेमाल किया जा सकता है. ज़्यादातर मामलों में, Gemini से शुरुआत करें. इसके बाद, खास कामों के लिए Imagen चुनें. इन कामों में इमेज की क्वालिटी बहुत ज़रूरी होती है.
ध्यान दें कि Firebase AI Logic SDK टूल, Imagen मॉडल के साथ इमेज इनपुट (जैसे, बदलाव करने के लिए) की सुविधा अभी तक नहीं देते हैं. इसलिए, अगर आपको इनपुट इमेज के साथ काम करना है, तो Gemini मॉडल का इस्तेमाल करें.
इनमें से कोई विकल्प चुनें Gemini:
- कॉन्टेक्स्ट के हिसाब से काम की इमेज जनरेट करने के लिए, दुनिया भर की जानकारी और तर्क का इस्तेमाल करना.
- टेक्स्ट और इमेज को एक साथ बेहतर तरीके से दिखाने के लिए.
- लंबे टेक्स्ट सीक्वेंस में सटीक विज़ुअल एम्बेड करने के लिए.
- कॉन्टेक्स्ट को बनाए रखते हुए, बातचीत के दौरान इमेज में बदलाव करने के लिए.
इनमें से कोई विकल्प चुनें Imagen:
- इमेज की क्वालिटी, फ़ोटो जैसा असली लुक, आर्टिस्टिक डिटेल या खास स्टाइल (उदाहरण के लिए, इंप्रेशनिज़्म या ऐनिमे) को प्राथमिकता देने के लिए.
- जनरेट की गई इमेज का आसपेक्ट रेशियो (लंबाई-चौड़ाई का अनुपात) या फ़ॉर्मैट साफ़ तौर पर बताने के लिए.
शुरू करने से पहले
इस पेज पर, Gemini API उपलब्ध कराने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, उस कंपनी पर क्लिक करें. |
अगर आपने अब तक शुरुआती गाइड नहीं पढ़ी है, तो इसे पढ़ें. इसमें बताया गया है कि Firebase प्रोजेक्ट कैसे सेट अप करें, अपने ऐप्लिकेशन को Firebase से कैसे कनेक्ट करें, एसडीके कैसे जोड़ें, चुने गए एपीआई उपलब्ध कराने वाली कंपनी के लिए बैकएंड सेवा को कैसे शुरू करें, और ImagenModel
इंस्टेंस कैसे बनाएं.
इस सुविधा के साथ काम करने वाले मॉडल
Gemini Developer API सिर्फ़ Imagen 3 के नए स्टेबल मॉडल से इमेज जनरेट करने की सुविधा देता है. यह Imagen के अन्य मॉडल के साथ काम नहीं करता. Imagen मॉडल की ये सीमाएं, Gemini Developer API को ऐक्सेस करने के तरीके से जुड़ी नहीं हैं.
imagen-3.0-generate-002
imagen-4.0-generate-preview-06-06
imagen-4.0-ultra-generate-preview-06-06
सिर्फ़ टेक्स्ट वाले इनपुट से इमेज जनरेट करना
Imagen मॉडल से, टेक्स्ट प्रॉम्प्ट देकर इमेज जनरेट करने के लिए कहा जा सकता है. एक इमेज या एक से ज़्यादा इमेज जनरेट की जा सकती हैं.
सिर्फ़ टेक्स्ट वाले इनपुट से एक इमेज जनरेट करना
इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें. उस सेक्शन में, आपको Gemini API सेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे. |
Imagen मॉडल से, टेक्स्ट प्रॉम्प्ट देकर एक इमेज जनरेट करने के लिए कहा जा सकता है.
पक्का करें कि आपने ImagenModel
इंस्टेंस बनाया हो और generateImages
को कॉल किया हो.
Swift
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create an `ImagenModel` instance with a model that supports your use case
let model = ai.imagenModel(modelName: "imagen-3.0-generate-002")
// Provide an image generation prompt
let prompt = "An astronaut riding a horse"
// To generate an image, call `generateImages` with the text prompt
let response = try await model.generateImages(prompt: prompt)
// Handle the generated image
guard let image = response.images.first else {
fatalError("No image in the response.")
}
let uiImage = UIImage(data: image.data)
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun generateImage() {
// Initialize the Gemini Developer API backend service
val ai = Firebase.ai(backend = GenerativeBackend.googleAI())
// Create an `ImagenModel` instance with an Imagen model that supports your use case
val model = ai.imagenModel("imagen-3.0-generate-002")
// Provide an image generation prompt
val prompt = "An astronaut riding a horse"
// To generate an image, call `generateImages` with the text prompt
val imageResponse = model.generateImages(prompt)
// Handle the generated image
val image = imageResponse.images.first()
val bitmapImage = image.asBitmap()
}
Java
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModel imagenModel = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.imagenModel(
/* modelName */ "imagen-3.0-generate-002");
ImagenModelFutures model = ImagenModelFutures.from(imagenModel);
// Provide an image generation prompt
String prompt = "An astronaut riding a horse";
// To generate an image, call `generateImages` with the text prompt
Futures.addCallback(model.generateImages(prompt), new FutureCallback<ImagenGenerationResponse<ImagenInlineImage>>() {
@Override
public void onSuccess(ImagenGenerationResponse<ImagenInlineImage> result) {
if (result.getImages().isEmpty()) {
Log.d("TAG", "No images generated");
}
Bitmap bitmap = result.getImages().get(0).asBitmap();
// Use the bitmap to display the image in your UI
}
@Override
public void onFailure(Throwable t) {
// ...
}
}, Executors.newSingleThreadExecutor());
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create an `ImagenModel` instance with an Imagen model that supports your use case
const model = getImagenModel(ai, { model: "imagen-3.0-generate-002" });
// Provide an image generation prompt
const prompt = "An astronaut riding a horse.";
// To generate an image, call `generateImages` with the text prompt
const response = await model.generateImages(prompt)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (response.filteredReason) {
console.log(response.filteredReason);
}
if (response.images.length == 0) {
throw new Error("No images in the response.")
}
const image = response.images[0];
Dart
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
final model = FirebaseAI.googleAI();
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final model = ai.imagenModel(model: 'imagen-3.0-generate-002');
// Provide an image generation prompt
const prompt = 'An astronaut riding a horse.';
// To generate an image, call `generateImages` with the text prompt
final response = await model.generateImages(prompt);
if (response.images.isNotEmpty) {
final image = response.images[0];
// Process the image
} else {
// Handle the case where no images were generated
print('Error: No images were generated.');
}
Unity
using Firebase.AI;
// Initialize the Gemini Developer API backend service
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create an `ImagenModel` instance with a model that supports your use case
var model = ai.GetImagenModel(modelName: "imagen-3.0-generate-002");
// Provide an image generation prompt
var prompt = "An astronaut riding a horse";
// To generate an image, call `generateImages` with the text prompt
var response = await model.GenerateImagesAsync(prompt: prompt);
// Handle the generated image
if (response.Images.Count == 0) {
throw new Exception("No image in the response.");
}
var image = response.Images[0].AsTexture2D();
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के लिए, सही मॉडल चुनने का तरीका जानें.
सिर्फ़ टेक्स्ट वाले इनपुट से कई इमेज जनरेट करना
इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें. उस सेक्शन में, आपको Gemini API सेवा देने वाली कंपनी के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे. |
डिफ़ॉल्ट रूप से, Imagen मॉडल हर अनुरोध के लिए सिर्फ़ एक इमेज जनरेट करते हैं.
हालांकि, ImagenModel
इंस्टेंस बनाते समय, ImagenGenerationConfig
देकर, Imagen मॉडल से हर अनुरोध के लिए कई इमेज जनरेट करने के लिए कहा जा सकता है.
पक्का करें कि आपने ImagenModel
इंस्टेंस बनाया हो और generateImages
को कॉल किया हो.
Swift
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create an `ImagenModel` instance with a model that supports your use case
let model = ai.imagenModel(
modelName: "imagen-3.0-generate-002",
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig: ImagenGenerationConfig(numberOfImages: 4)
)
// Provide an image generation prompt
let prompt = "An astronaut riding a horse"
// To generate images, call `generateImages` with the text prompt
let response = try await model.generateImages(prompt: prompt)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if let filteredReason = response.filteredReason {
print(filteredReason)
}
// Handle the generated images
let uiImages = response.images.compactMap { UIImage(data: $0.data) }
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun generateImage() {
// Initialize the Gemini Developer API backend service
val ai = Firebase.ai(backend = GenerativeBackend.googleAI())
// Create an `ImagenModel` instance with an Imagen model that supports your use case
val model = ai.imagenModel(
modelName = "imagen-3.0-generate-002",
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig = ImagenGenerationConfig(numberOfImages = 4)
)
// Provide an image generation prompt
val prompt = "An astronaut riding a horse"
// To generate images, call `generateImages` with the text prompt
val imageResponse = model.generateImages(prompt)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (imageResponse.filteredReason != null) {
Log.d(TAG, "FilteredReason: ${imageResponse.filteredReason}")
}
for (image in imageResponse.images) {
val bitmap = image.asBitmap()
// Use the bitmap to display the image in your UI
}
}
Java
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
ImagenGenerationConfig imagenGenerationConfig = new ImagenGenerationConfig.Builder()
.setNumberOfImages(4)
.build();
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModel imagenModel = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.imagenModel(
/* modelName */ "imagen-3.0-generate-002",
/* imageGenerationConfig */ imagenGenerationConfig);
ImagenModelFutures model = ImagenModelFutures.from(imagenModel);
// Provide an image generation prompt
String prompt = "An astronaut riding a horse";
// To generate images, call `generateImages` with the text prompt
Futures.addCallback(model.generateImages(prompt), new FutureCallback<ImagenGenerationResponse<ImagenInlineImage>>() {
@Override
public void onSuccess(ImagenGenerationResponse<ImagenInlineImage> result) {
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (result.getFilteredReason() != null){
Log.d("TAG", "FilteredReason: " + result.getFilteredReason());
}
// Handle the generated images
List<ImagenInlineImage> images = result.getImages();
for (ImagenInlineImage image : images) {
Bitmap bitmap = image.asBitmap();
// Use the bitmap to display the image in your UI
}
}
@Override
public void onFailure(Throwable t) {
// ...
}
}, Executors.newSingleThreadExecutor());
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create an `ImagenModel` instance with an Imagen model that supports your use case
const model = getImagenModel(
ai,
{
model: "imagen-3.0-generate-002",
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig: {
numberOfImages: 4
}
}
);
// Provide an image generation prompt
const prompt = "An astronaut riding a horse.";
// To generate images, call `generateImages` with the text prompt
const response = await model.generateImages(prompt)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (response.filteredReason) {
console.log(response.filteredReason);
}
if (response.images.length == 0) {
throw new Error("No images in the response.")
}
const images = response.images[0];
Dart
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
final ai = FirebaseAI.googleAI();
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final model = ai.imagenModel(
model: 'imagen-3.0-generate-002',
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig: ImagenGenerationConfig(numberOfImages: 4),
);
// Provide an image generation prompt
const prompt = 'An astronaut riding a horse.';
// To generate images, call `generateImages` with the text prompt
final response = await model.generateImages(prompt);
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (response.filteredReason != null) {
print(response.filteredReason);
}
if (response.images.isNotEmpty) {
final images = response.images;
for(var image in images) {
// Process the image
}
} else {
// Handle the case where no images were generated
print('Error: No images were generated.');
}
Unity
using Firebase.AI;
// Initialize the Gemini Developer API backend service
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create an `ImagenModel` instance with a model that supports your use case
var model = ai.GetImagenModel(
modelName: "imagen-3.0-generate-002",
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig: new ImagenGenerationConfig(numberOfImages: 4)
);
// Provide an image generation prompt
var prompt = "An astronaut riding a horse";
// To generate an image, call `generateImages` with the text prompt
var response = await model.GenerateImagesAsync(prompt: prompt);
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (!string.IsNullOrEmpty(response.FilteredReason)) {
UnityEngine.Debug.Log("Filtered reason: " + response.FilteredReason);
}
// Handle the generated images
var images = response.Images.Select(image => image.AsTexture2D());
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के लिए, सही मॉडल चुनने का तरीका जानें.
इस्तेमाल की जा सकने वाली सुविधाएं और ज़रूरी शर्तें
Imagen मॉडल, इमेज जनरेट करने से जुड़ी कई सुविधाएं देते हैं. इस सेक्शन में बताया गया है कि Firebase AI Logic के साथ मॉडल इस्तेमाल करते समय, किन सुविधाओं का इस्तेमाल किया जा सकता है.
इस्तेमाल की जा सकने वाली सुविधाएं और क्षमताएं
Firebase AI Logic, Imagen मॉडल की इन सुविधाओं के साथ काम करता है.
लोगों और चेहरों को जनरेट करना. इसके लिए, यह ज़रूरी है कि आपके Firebase प्रोजेक्ट को Google Cloud से अनुमति मिली हो
जनरेट की गई इमेज में टेक्स्ट जनरेट करना
जनरेट की गई इमेज में वॉटरमार्क जोड़ना
इमेज जनरेट करने के पैरामीटर कॉन्फ़िगर करना. जैसे, जनरेट की गई इमेज की संख्या, आसपेक्ट रेशियो (लंबाई-चौड़ाई का अनुपात), और वॉटरमार्किंग
सुरक्षा सेटिंग कॉन्फ़िगर करना
Firebase AI Logic, Imagen मॉडल की इन ऐडवांस सुविधाओं के साथ काम नहीं करता.
ध्यान दें कि इनमें से ज़्यादातर सुविधाओं के लिए, उपयोगकर्ताओं की मंज़ूरी वाली सूची में शामिल होना ज़रूरी है. भले ही, सर्वर-साइड पर Imagen मॉडल का इस्तेमाल किया जा रहा हो.
इमेज में बदलाव करने या उसे बेहतर बनाने की सुविधाएं. इनमें इमेज को बड़ा करना भी शामिल है
मॉडल को किए गए अनुरोध में इमेज शामिल करना (जैसे, कुछ उदाहरणों के साथ सीखना)
SDK का इस्तेमाल करके, डिजिटल वॉटरमार्क की पुष्टि करना
अगर आपको यह पुष्टि करनी है कि किसी इमेज में वॉटरमार्क है, तो मीडिया टैब का इस्तेमाल करके, उस इमेज को Vertex AI Studio में अपलोड करें.टेक्स्ट से"लाइव इमेज" जनरेट करना (MP4 जनरेट करना)
पहले से तय की गई स्टाइल का इस्तेमाल करके इमेज जनरेट करना
includeSafetyAttributes
को चालू करने का मतलब है किsafetyAttributes.categories
औरsafetyAttributes.scores
को वापस नहीं किया जा सकताप्रॉम्प्ट को बेहतर बनाने की सुविधा (
enhancePrompt
पैरामीटर) बंद करना. इसका मतलब है कि एलएलएम पर आधारित प्रॉम्प्ट को फिर से लिखने वाला टूल, दिए गए प्रॉम्प्ट में हमेशा ज़्यादा जानकारी अपने-आप जोड़ देगा, ताकि बेहतर क्वालिटी वाली ऐसी इमेज जनरेट की जा सकें जो दिए गए प्रॉम्प्ट को बेहतर तरीके से दिखाती होंजनरेट की गई इमेज को सीधे तौर पर Google Cloud Storage में लिखना, मॉडल से मिले जवाब (
storageUri
पैरामीटर) का हिस्सा होता है. इसके बजाय, इमेज को हमेशा जवाब में base64-encoded इमेज बाइट के तौर पर दिखाया जाता है.
अगर आपको जनरेट की गई इमेज को Cloud Storage पर अपलोड करना है, तो Cloud Storage for Firebase का इस्तेमाल करें.
खासियतें और सीमाएं
प्रॉपर्टी (हर अनुरोध के लिए) | मान |
---|---|
इनपुट टोकन की ज़्यादा से ज़्यादा संख्या | 480 टोकन |
आउटपुट इमेज की ज़्यादा से ज़्यादा संख्या | 4 इमेज |
आउटपुट इमेज के लिए काम करने वाले रिज़ॉल्यूशन (पिक्सल) |
|
तुम और क्या कर सकती हो?
-
प्रोडक्शन के लिए तैयारी शुरू करें. इसके लिए, प्रोडक्शन की चेकलिस्ट देखें. इसमें ये चीज़ें शामिल हैं:
- Firebase App Check सेट अप करना ताकि बिना अनुमति वाले क्लाइंट, Gemini API का गलत इस्तेमाल न कर पाएं.
- Firebase Remote Config को इंटिग्रेट करना ताकि ऐप्लिकेशन का नया वर्शन रिलीज़ किए बिना, ऐप्लिकेशन में वैल्यू अपडेट की जा सकें. जैसे, मॉडल का नाम.
कॉन्टेंट जनरेट करने की सुविधा को कंट्रोल करने का तरीका जानें
- प्रॉम्प्ट डिज़ाइन को समझें. इसमें सबसे सही तरीके, रणनीतियां, और उदाहरण के तौर पर दिए गए प्रॉम्प्ट शामिल हैं.
- आस्पेक्ट रेशियो, व्यक्ति जनरेशन, और वॉटरमार्किंग जैसे Imagen मॉडल पैरामीटर कॉन्फ़िगर करें.
- सुरक्षा सेटिंग का इस्तेमाल करें, ताकि आपको ऐसे जवाब मिलने की संभावना को कम किया जा सके जिन्हें नुकसान पहुंचाने वाला माना जा सकता है.
साथ काम करने वाले मॉडल के बारे में ज़्यादा जानें
अलग-अलग कामों के लिए उपलब्ध मॉडल और उनके कोटे और कीमत के बारे में जानें.Firebase AI Logic के साथ अपने अनुभव के बारे में सुझाव/राय दें या शिकायत करें