Firebase AI Logic एसडीके टूल की मदद से, Imagen API के ज़रिए Imagen 3 मॉडल को ऐक्सेस किया जा सकता है. इससे, टेक्स्ट प्रॉम्प्ट से इमेज जनरेट की जा सकती हैं. इस सुविधा की मदद से, ये काम किए जा सकते हैं:
- आम भाषा में लिखे गए प्रॉम्प्ट से इमेज जनरेट करना
- अलग-अलग फ़ॉर्मैट और स्टाइल में इमेज जनरेट करना
- इमेज में मौजूद टेक्स्ट को रेंडर करना
ध्यान दें कि Firebase AI Logic पर, Imagen मॉडल के लिए उपलब्ध सभी सुविधाएं काम नहीं करती हैं. इस पेज पर, काम करने वाली सुविधाओं और सुविधाओं के बारे में ज़्यादा जानें.
सिर्फ़ टेक्स्ट वाले इनपुट के लिए कोड पर जाएं
शुरू करने से पहले
इस पेज पर, सेवा देने वाली कंपनी से जुड़ा कॉन्टेंट और कोड देखने के लिए, Gemini API पर क्लिक करें. |
अगर आपने अब तक ऐसा नहीं किया है, तो शुरू करने से जुड़ी गाइड पढ़ें. इसमें, Firebase प्रोजेक्ट सेट अप करने, अपने ऐप्लिकेशन को Firebase से कनेक्ट करने, SDK टूल जोड़ने, चुने गए एपीआई प्रोवाइडर के लिए बैकएंड सेवा को शुरू करने, और ImagenModel
इंस्टेंस बनाने का तरीका बताया गया है.
इस सुविधा के साथ काम करने वाले मॉडल
इमेज जनरेट करने की सुविधा, Imagen 3 मॉडल के साथ काम करती है.
ध्यान दें कि Firebase AI Logic, Gemini मॉडल की मदद से इमेज जनरेट करने की सुविधा भी देता है. इसकी जानकारी देने वाला दस्तावेज़ जल्द ही उपलब्ध होगा!
सिर्फ़ टेक्स्ट वाले इनपुट से इमेज जनरेट करना
टेक्स्ट के ज़रिए प्रॉम्प्ट देकर, 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
// Create an `ImagenModel` instance with an Imagen model that supports your use case
val imagenModel = Firebase.googleAI.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 = imagenModel.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 3 model that supports your use case
const imagenModel = 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 imagenModel.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
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final model =
FirebaseAI.googleAI().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
फ़िलहाल, Unity में Imagen का इस्तेमाल नहीं किया जा सकता. हालांकि, जल्द ही इसकी सुविधा उपलब्ध होगी!
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल चुनने का तरीका जानें.
सिर्फ़ टेक्स्ट वाले इनपुट से कई इमेज जनरेट करना
इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का शुरू करने से पहले सेक्शन पूरा करें. इस सेक्शन में, आपको अपनी पसंद के Gemini API सेवा देने वाली कंपनी के लिए बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी से जुड़ा कॉन्टेंट दिखे. |
डिफ़ॉल्ट रूप से, Imagen 3 मॉडल हर अनुरोध के लिए सिर्फ़ एक इमेज जनरेट करते हैं.
हालांकि, 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
// Create an `ImagenModel` instance with an Imagen model that supports your use case
val imagenModel = Firebase.googleAI.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 = imagenModel.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 3 model that supports your use case
const imagenModel = 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 imagenModel.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
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final model =
FirebaseAI.googleAI().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
फ़िलहाल, Unity में Imagen का इस्तेमाल नहीं किया जा सकता. हालांकि, जल्द ही इसकी सुविधा उपलब्ध होगी!
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल चुनने का तरीका जानें.
इस्तेमाल की जा सकने वाली सुविधाएं और ज़रूरी शर्तें
Imagen 3 मॉडल, इमेज जनरेट करने से जुड़ी कई सुविधाएं देते हैं. इस सेक्शन में बताया गया है कि Firebase AI Logic के साथ मॉडल इस्तेमाल करते समय, किन चीज़ों का इस्तेमाल किया जा सकता है.
इस्तेमाल की जा सकने वाली सुविधाएं और क्षमताएं
Firebase AI Logic, Imagen 3 मॉडल के साथ इन सुविधाओं के साथ काम करता है.
लोगों और चेहरों को जनरेट करना (यह ज़रूरी है कि आपके Firebase प्रोजेक्ट को Google Cloud से अनुमति मिली हो)
जनरेट की गई इमेज में टेक्स्ट जनरेट करना
जनरेट की गई इमेज में वॉटरमार्क जोड़ना
इमेज जनरेशन पैरामीटर कॉन्फ़िगर करना. जैसे, जनरेट की गई इमेज की संख्या, आसपेक्ट रेशियो, और वॉटरमार्क
सुरक्षा सेटिंग कॉन्फ़िगर करना
Firebase AI Logic पर Imagen 3 मॉडल की ये ऐडवांस सुविधाएं काम नहीं करतीं.
ध्यान दें कि इनमें से ज़्यादातर सुविधाओं के लिए, अनुमति पा चुके उपयोगकर्ताओं की सूची में शामिल होना ज़रूरी है. भले ही, आपने Imagen मॉडल को सर्वर साइड पर इस्तेमाल किया हो.
इमेज में बदलाव करने या उन्हें मैनिप्युलेट करने की सुविधाएं. इनमें इमेज को बड़ा करने की सुविधा भी शामिल है
मॉडल के अनुरोध में इमेज शामिल करना (जैसे, फ़्यू-शॉट लर्निंग के लिए)
SDK टूल का इस्तेमाल करके, डिजिटल वॉटरमार्क की पुष्टि करना
अगर आपको यह पुष्टि करनी है कि किसी इमेज में वॉटरमार्क है या नहीं, तो Vertex AI Studio के मीडिया टैब का इस्तेमाल करके, इमेज को अपलोड करें.टेक्स्ट से"लाइव इमेज" जनरेट करना (MP4 जनरेशन)
पहले से तय किए गए स्टाइल का इस्तेमाल करके इमेज जनरेट करना
includeSafetyAttributes
को चालू करना. इसका मतलब है किsafetyAttributes.categories
औरsafetyAttributes.scores
को वापस नहीं किया जा सकताप्रॉम्प्ट को बेहतर बनाने (
enhancePrompt
पैरामीटर) की सुविधा बंद करना. इसका मतलब है कि एलएलएम पर आधारित प्रॉम्प्ट को फिर से लिखने वाला टूल, दिए गए प्रॉम्प्ट में अपने-आप ज़्यादा जानकारी जोड़ देगा. इससे, बेहतर क्वालिटी की ऐसी इमेज मिलती हैं जो दिए गए प्रॉम्प्ट को बेहतर तरीके से दिखाती हैंमॉडल के जवाब (
storageUri
पैरामीटर) के हिस्से के तौर पर, जनरेट की गई इमेज को सीधे Google Cloud Storage में लिखना. इसके बजाय, इमेज को जवाब में हमेशा Base64 एन्कोड की गई इमेज के बाइट के तौर पर दिखाया जाता है.
अगर आपको जनरेट की गई इमेज को Cloud Storage में अपलोड करना है, तो Cloud Storage for Firebase का इस्तेमाल करें.
खास जानकारी और सीमाएं
सीमाएं (हर अनुरोध के लिए) | Imagen 3 | Imagen 3 Fast |
---|---|---|
इनपुट टोकन की ज़्यादा से ज़्यादा संख्या | 480 टोकन | 480 टोकन |
आउटपुट इमेज की ज़्यादा से ज़्यादा संख्या | चार इमेज | चार इमेज |
आउटपुट इमेज के लिए इस्तेमाल किए जा सकने वाले रिज़ॉल्यूशन (पिक्सल) |
|
|
तुम और क्या कर सकती हो?
-
प्रोडक्शन की तैयारी शुरू करें (प्रोडक्शन की चेकलिस्ट देखें). इसमें ये चीज़ें शामिल हैं:
- Gemini API को बिना अनुमति वाले क्लाइंट के गलत इस्तेमाल से बचाने के लिए, Firebase App Check सेट अप करना
- Firebase Remote Config को इंटिग्रेट करना ऐप्लिकेशन का नया वर्शन रिलीज़ किए बिना, अपने ऐप्लिकेशन में वैल्यू (जैसे, मॉडल का नाम) अपडेट करने के लिए.
कॉन्टेंट जनरेशन को कंट्रोल करने का तरीका जानें
- प्रॉम्प्ट के डिज़ाइन को समझना. इसमें सबसे सही तरीके, रणनीतियां, और प्रॉम्प्ट के उदाहरण शामिल हैं.
- Imagen मॉडल पैरामीटर कॉन्फ़िगर करें जैसे कि आसपेक्ट रेशियो, व्यक्ति जनरेट करना, और वॉटरमार्क करना.
- सुरक्षा सेटिंग का इस्तेमाल करें, ताकि आपको ऐसे जवाब न मिलें जो नुकसान पहुंचा सकते हैं.
इस्तेमाल किए जा सकने वाले मॉडल के बारे में ज़्यादा जानें
अलग-अलग कामों के लिए उपलब्ध मॉडल, उनके कोटे, और कीमत के बारे में जानें.Firebase AI Logic के साथ अपने अनुभव के बारे में सुझाव/राय दें या शिकायत करें