Gemini Developer API

Gemini Developer API की मदद से, Google के Gemini मॉडल का ऐक्सेस मिलता है. इससे आपको अपने Android ऐप्लिकेशन में, जनरेटिव एआई की बेहतरीन सुविधाएँ बनाने का मौका मिलता है. इनमें बातचीत वाली चैट, इमेज जनरेट करने की सुविधा (Nano Banana की मदद से), और टेक्स्ट, इमेज, ऑडियो, और वीडियो इनपुट के आधार पर टेक्स्ट जनरेट करने की सुविधा शामिल है.

Gemini Pro और Flash मॉडल को ऐक्सेस करने के लिए, Firebase AI Logic के साथ Gemini Developer API का इस्तेमाल किया जा सकता है. इससे बिना पेमेंट के शुरुआत की जा सकती है. इसके लिए, पेमेंट का तरीका (जैसे कि क्रेडिट कार्ड) जोड़ना ज़रूरी नहीं है. साथ ही, इसमें मुफ़्त में इस्तेमाल करने की सुविधा भी मिलती है. जब आपको यह पता चल जाए कि कम संख्या में उपयोगकर्ताओं के साथ इंटिग्रेशन काम कर रहा है, तब पैसे चुकाकर सदस्यता लेने वाले टियर पर अपग्रेड करके, इसे बड़े पैमाने पर इस्तेमाल किया जा सकता है.

Android ऐप्लिकेशन, Firebase Android SDK का इस्तेमाल करके क्लाउड में मौजूद Firebase बैकएंड को अनुरोध भेज रहा है. इसे दिखाने वाली इमेज. अनुरोधों को Gemini Developer API पर रूट किया जाता है. यह Gemini Pro और Flash, दोनों मॉडल का इस्तेमाल कर सकता है.
पहली इमेज. Gemini Developer API को ऐक्सेस करने के लिए, Firebase AI Logic इंटिग्रेशन आर्किटेक्चर.

शुरू करना

अपने ऐप्लिकेशन से सीधे तौर पर Gemini API का इस्तेमाल करने से पहले, आपको कुछ काम करने होंगे. जैसे, प्रॉम्प्ट देने के बारे में जानना. साथ ही, एसडीके का इस्तेमाल करने के लिए Firebase और अपने ऐप्लिकेशन को सेट अप करना.

प्रॉम्प्ट के साथ एक्सपेरिमेंट करना

प्रॉम्प्ट आज़माने से, आपको अपने Android ऐप्लिकेशन के लिए सबसे सही फ़्रेज़, कॉन्टेंट, और फ़ॉर्मैट ढूंढने में मदद मिल सकती है. Google AI Studio एक इंटिग्रेटेड डेवलपमेंट एनवायरमेंट (आईडीई) है. इसका इस्तेमाल, अपने ऐप्लिकेशन के इस्तेमाल के उदाहरणों के लिए प्रॉम्प्ट के प्रोटोटाइप बनाने और उन्हें डिज़ाइन करने के लिए किया जा सकता है.

इस्तेमाल के उदाहरण के लिए असरदार प्रॉम्प्ट बनाने में, कई तरह के एक्सपेरिमेंट शामिल होते हैं. यह प्रोसेस का एक अहम हिस्सा है. Firebase के दस्तावेज़ में जाकर, प्रॉम्प्ट के बारे में ज़्यादा जानें.

जब आपको लगे कि आपका प्रॉम्प्ट पूरी तरह तैयार है, तो <> बटन पर क्लिक करें. इससे आपको कोड स्निपेट मिलेंगे, जिन्हें अपने कोड में जोड़ा जा सकता है.

Firebase प्रोजेक्ट सेट अप करना और अपने ऐप्लिकेशन को Firebase से कनेक्ट करना

जब आप अपने ऐप्लिकेशन से एपीआई को कॉल करने के लिए तैयार हों, तो Firebase सेट अप करने के लिए, Firebase AI Logic के शुरुआती निर्देश में दिए गए "पहला चरण" में दिए गए निर्देशों का पालन करें. साथ ही, ज़रूरी एपीआई और सेवाओं को चालू करें.

Gradle डिपेंडेंसी जोड़ना

अपने ऐप्लिकेशन मॉड्यूल में ये Gradle डिपेंडेंसी जोड़ें:

Kotlin

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:34.19.0"))

  // Add the dependencies for the Firebase AI Logic and App Check libraries
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
  implementation("com.google.firebase:firebase-appcheck-debug")
}

Java

dependencies {
  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:34.19.0"))

  // Add the dependencies for the Firebase AI Logic and App Check libraries
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-ai")
  implementation("com.google.firebase:firebase-appcheck-debug")

  // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
  implementation("com.google.guava:guava:31.0.1-android")

  // Required for streaming operations (to use `Publisher` from Reactive Streams)
  implementation("org.reactivestreams:reactive-streams:1.0.4")
}

लोकल डेवलपमेंट के लिए, App Check के डीबग प्रोवाइडर को कॉन्फ़िगर करना

जुलाई 2026 की शुरुआत से, Firebase console में एआई लॉजिक के लिए, सेटअप करने के तरीके के बारे में जानकारी देने वाले वर्कफ़्लो के तहत, Gemini API को सुरक्षित रखने के लिए Firebase App Check की सुविधा अपने-आप लागू हो जाएगी. लोकल डेवलपमेंट के लिए, आपको App Check के डीबग प्रोवाइडर को कॉन्फ़िगर करना होगा. इससे, पुष्टि को बायपास किया जा सकेगा. हालांकि, App Check लागू रहेगा.

  1. अपनी डीबग बिल्ड में, App Check को कॉन्फ़िगर करें, ताकि वह डीबग प्रोवाइडर फ़ैक्ट्री का इस्तेमाल कर सके:

    Kotlin

    Firebase.initialize(context = this)
    Firebase.appCheck.installAppCheckProviderFactory(
        DebugAppCheckProviderFactory.getInstance(),
    )
    

    Java

    FirebaseApp.initializeApp(/*context=*/ this);
    FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
    firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance());
    
  2. अपना डीबग टोकन पाएं:

    1. अपने ऐप्लिकेशन को एम्युलेटर में या टेस्ट के लिए डिवाइस पर चलाएं.

    2. अपने लॉग में App Check डीबग टोकन ढूंढें. उदाहरण के लिए:

      D DebugAppCheckProvider: Enter this debug secret into the allow list
      in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
      
    3. टोकन को कॉपी करें. उदाहरण के लिए, 123a4567-b89c-12d3-e456-789012345678.

  3. App Check में अपना डीबग टोकन रजिस्टर करें:

    1. Firebase कंसोल में, सुरक्षा > App Check > ऐप्लिकेशन टैब पर जाएं.

    2. अपना ऐप्लिकेशन ढूंढें. इसके बाद, ओवरफ़्लो मेन्यू () पर क्लिक करें. इसके बाद, डीबग टोकन मैनेज करें को चुनें.

    3. अपने डीबग टोकन को रजिस्टर करने के लिए, स्क्रीन पर दिए गए निर्देशों का पालन करें.

डीबग प्रोवाइडर के बारे में ज़्यादा जानकारी (इसमें नया डीबग टोकन पाने का तरीका भी शामिल है) के लिए, App Check के आधिकारिक दस्तावेज़ देखें.

जनरेटिव मॉडल को शुरू करना

GenerativeModel को इंस्टैंटिएट करके और मॉडल का नाम तय करके शुरू करें:

Kotlin

// Start by instantiating a GenerativeModel and specifying the model name:
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
    .generativeModel("gemini-3.5-flash")

Java

GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-3.5-flash");

GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);

Gemini Developer API के साथ इस्तेमाल किए जा सकने वाले उपलब्ध मॉडल के बारे में ज़्यादा जानें. मॉडल पैरामीटर कॉन्फ़िगर करने के बारे में भी ज़्यादा जानें.

अपने ऐप्लिकेशन से Gemini Developer API के साथ इंटरैक्ट करना

SDK टूल का इस्तेमाल करने के लिए, Firebase और ऐप्लिकेशन सेट अप करने के बाद, अब आपके पास अपने ऐप्लिकेशन से Gemini Developer API के साथ इंटरैक्ट करने का विकल्प है.

टेक्स्ट जनरेट करना

टेक्स्ट वाला जवाब जनरेट करने के लिए, अपने प्रॉम्प्ट के साथ generateContent() को कॉल करें.

Kotlin

scope.launch {
    val response = model.generateContent("Write a story about a magic backpack.")
}

Java

Content prompt = new Content.Builder()
        .addText("Write a story about a magic backpack.")
        .build();

ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

इमेज और अन्य मीडिया से टेक्स्ट जनरेट करना

टेक्स्ट के साथ-साथ इमेज या अन्य मीडिया वाले प्रॉम्प्ट से भी टेक्स्ट जनरेट किया जा सकता है. generateContent() को कॉल करते समय, मीडिया को इनलाइन डेटा के तौर पर पास किया जा सकता है.

उदाहरण के लिए, बिटमैप का इस्तेमाल करने के लिए, image कॉन्टेंट टाइप का इस्तेमाल करें:

Kotlin

scope.launch {
    val response = model.generateContent(
        content {
            image(bitmap)
            text("what is the object in the picture?")
        }
    )
}

Java

Content content = new Content.Builder()
        .addImage(bitmap)
        .addText("what is the object in the picture?")
        .build();

ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

ऑडियो फ़ाइल पास करने के लिए, inlineData कॉन्टेंट टाइप का इस्तेमाल करें:

Kotlin

scope.launch {
    val contentResolver = applicationContext.contentResolver
    contentResolver.openInputStream(audioUri).use { stream ->
        stream?.let {
            val bytes = it.readBytes()

            val prompt = content {
                inlineData(bytes, "audio/mpeg") // Specify the appropriate audio MIME type
                text("Transcribe this audio recording.")
            }

            val response = model.generateContent(prompt)
        }
    }
}

Java

ContentResolver resolver = applicationContext.getContentResolver();

try (InputStream stream = resolver.openInputStream(audioUri)) {
    File audioFile = new File(new URI(audioUri.toString()));
    int audioSize = (int) audioFile.length();
    byte[] audioBytes = new byte[audioSize];
    if (stream != null) {
        stream.read(audioBytes, 0, audioBytes.length);
        stream.close();

        // Provide a prompt that includes audio specified earlier and text
        Content prompt = new Content.Builder()
                .addInlineData(audioBytes, "audio/mpeg")  // Specify the appropriate audio MIME type
                .addText("Transcribe what's said in this audio recording.")
                .build();

        // To generate text output, call `generateContent` with the prompt
        ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
        Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
            @Override
            public void onSuccess(GenerateContentResponse result) {
                String text = result.getText();
                Log.d(TAG, (text == null) ? "" : text);
            }
            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "Failed to generate a response", t);
            }
        }, executor);
    } else {
        Log.e(TAG, "Error getting input stream for file.");
        // Handle the error appropriately
    }
} catch (IOException e) {
    Log.e(TAG, "Failed to read the audio file", e);
} catch (URISyntaxException e) {
    Log.e(TAG, "Invalid audio file", e);
}

वीडियो फ़ाइल देने के लिए, inlineData कॉन्टेंट टाइप का इस्तेमाल जारी रखें:

Kotlin

scope.launch {
    val contentResolver = applicationContext.contentResolver
    contentResolver.openInputStream(videoUri).use { stream ->
        stream?.let {
            val bytes = it.readBytes()

            val prompt = content {
                inlineData(bytes, "video/mp4") // Specify the appropriate video MIME type
                text("Describe the content of this video")
            }

            val response = model.generateContent(prompt)
        }
    }
}

Java

ContentResolver resolver = applicationContext.getContentResolver();

try (InputStream stream = resolver.openInputStream(videoUri)) {
    File videoFile = new File(new URI(videoUri.toString()));
    int videoSize = (int) videoFile.length();
    byte[] videoBytes = new byte[videoSize];
    if (stream != null) {
        stream.read(videoBytes, 0, videoBytes.length);
        stream.close();

        // Provide a prompt that includes video specified earlier and text
        Content prompt = new Content.Builder()
                .addInlineData(videoBytes, "video/mp4")
                .addText("Describe the content of this video")
                .build();

        // To generate text output, call generateContent with the prompt
        ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
        Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
            @Override
            public void onSuccess(GenerateContentResponse result) {
                String resultText = result.getText();
                System.out.println(resultText);
            }

            @Override
            public void onFailure(Throwable t) {
                t.printStackTrace();
            }
        }, executor);
    }
} catch (IOException e) {
    e.printStackTrace();
} catch (URISyntaxException e) {
    e.printStackTrace();
}

इसी तरह, PDF (application/pdf) और सादा टेक्स्ट (text/plain) वाले दस्तावेज़ों को भी पास किया जा सकता है. इसके लिए, उनके MIME टाइप को पैरामीटर के तौर पर पास करें.

सिलसिलेवार बातचीत

आप सिलसिलेवार बातचीत की सुविधा भी इस्तेमाल कर सकते हैं. startChat() फ़ंक्शन का इस्तेमाल करके, चैट शुरू करें. विकल्प के तौर पर, मॉडल को मैसेज इतिहास दिया जा सकता है. इसके बाद, चैट मैसेज भेजने के लिए sendMessage() फ़ंक्शन को कॉल करें.

Kotlin

val chat = model.startChat(
    history = listOf(
        content(role = "user") { text("Hello, I have 2 dogs in my house.") },
        content(role = "model") { text("Great to meet you. What would you like to know?") }
    )
)

scope.launch {
    val response = chat.sendMessage("How many paws are in my house?")
}

Java

Content.Builder userContentBuilder = new Content.Builder();
userContentBuilder.setRole("user");
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
Content userContent = userContentBuilder.build();

Content.Builder modelContentBuilder = new Content.Builder();
modelContentBuilder.setRole("model");
modelContentBuilder.addText("Great to meet you. What would you like to know?");
Content modelContent = modelContentBuilder.build();

List<Content> history = Arrays.asList(userContent, modelContent);

// Initialize the chat
ChatFutures chat = model.startChat(history);

// Create a new user message
Content.Builder messageBuilder = new Content.Builder();
messageBuilder.setRole("user");
messageBuilder.addText("How many paws are in my house?");

Content message = messageBuilder.build();

// Send the message
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(message);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Android डिवाइस पर Nano Banana की मदद से इमेज जनरेट करना

की क्षमताओं के बारे में ज़्यादा जानें

Gemini Image मॉडल (इसे Nano Banana मॉडल) दुनिया भर के तथ्यों और बारीकियों की बेहतर समझ और तर्क करने की क्षमता का इस्तेमाल करके, इमेज जनरेट कर सकते हैं और उनमें बदलाव कर सकते हैं. ये मॉडल, कॉन्टेक्स्ट के हिसाब से इमेज जनरेट करते हैं. साथ ही, टेक्स्ट और इमेज के आउटपुट को बेहतरीन तरीके से ब्लेंड या इंटरलीव करते हैं. ये लंबे टेक्स्ट सीक्वेंस के साथ सटीक विज़ुअल भी जनरेट कर सकते हैं. साथ ही, ये कॉन्टेक्स्ट को बनाए रखते हुए, बातचीत के ज़रिए इमेज में बदलाव करने की सुविधा देते हैं.

इस गाइड में, Android के लिए Firebase AI Logic SDK टूल का इस्तेमाल करके, Gemini के इमेज मॉडल (Nano Banana मॉडल) इस्तेमाल करने का तरीका बताया गया है. Firebase के दस्तावेज़ में, Gemini की मदद से इमेज जनरेट करने के बारे में ज़्यादा जानकारी पाएं.

Google AI Studio का इंटरफ़ेस दिखाया गया है. इसमें टेक्स्ट इनपुट फ़ील्ड है. इसमें प्रॉम्प्ट के तौर पर लिखा है, 'एक टी-रेक्स की असली जैसी दिखने वाली तस्वीर, जिसमें उसने नीले रंग का बैग पैक पहना हो और वह प्रागैतिहासिक जंगल में घूम रहा हो.' साथ ही, जंगल में नीले रंग का बैग पैक पहने हुए टी-रेक्स की जनरेट की गई इमेज दिखाई गई है.
दूसरी इमेज. अपने Android ऐप्लिकेशन के लिए, Nano Banana की मदद से इमेज जनरेट करने के प्रॉम्प्ट को बेहतर बनाने के लिए, Google AI Studio का इस्तेमाल करें

जनरेटिव मॉडल को शुरू करना

GenerativeModel को इंस्टैंशिएट करें और Gemini के इमेज मॉडल का नाम बताएं. जैसे, gemini-3.1-flash-image. इसके बाद, responseModalities को कॉन्फ़िगर करें, ताकि इंटरलीव किए गए टेक्स्ट और इमेज जनरेट करने के लिए TEXT और IMAGE दोनों शामिल किए जा सकें. अगर आपने TEXT को शामिल नहीं किया है, तो मॉडल सिर्फ़ इमेज दिखाएगा.

Kotlin

val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
    modelName = "gemini-3.1-flash-image",
    // Configure the model to respond with images (required) and text (optional)
    generationConfig = generationConfig {
        responseModalities = listOf(
            ResponseModality.TEXT,
            ResponseModality.IMAGE
        )
    }
)

Java

GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()).generativeModel(
        "gemini-3.1-flash-image",
        // Configure the model to respond with text and images (required)
        new GenerationConfig.Builder()
                .setResponseModalities(Arrays.asList(ResponseModality.TEXT, ResponseModality.IMAGE))
                .build()
);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

इमेज जनरेट करना (सिर्फ़ टेक्स्ट वाला इनपुट)

Gemini मॉडल को, सिर्फ़ टेक्स्ट वाला प्रॉम्प्ट देकर इमेज जनरेट करने के लिए कहा जा सकता है:

Kotlin

scope.launch {
    // Provide a text prompt instructing the model to generate an image
    val prompt =
        "A hyper realistic picture of a t-rex with a blue bag pack roaming a pre-historic forest."
    // To generate image output, call `generateContent` with the text input
    val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt)
        .candidates.first().content.parts.filterIsInstance<ImagePart>()
        .firstOrNull()?.image
}

Java

// Provide a text prompt instructing the model to generate an image
Content prompt = new Content.Builder()
        .addText("Generate an image of the Eiffel Tower with fireworks in the background.")
        .build();
// To generate an image, call `generateContent` with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        // iterate over all the parts in the first candidate in the result object
        for (Part part : result.getCandidates().get(0).getContent().getParts()) {
            if (part instanceof ImagePart) {
                ImagePart imagePart = (ImagePart) part;
                // The returned image as a bitmap
                Bitmap generatedImageAsBitmap = imagePart.getImage();
                break;
            }
        }
    }
    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

इमेज में बदलाव करना (टेक्स्ट और इमेज इनपुट)

Gemini मॉडल से, मौजूदा इमेज में बदलाव करने के लिए कहा जा सकता है. इसके लिए, अपने प्रॉम्प्ट में टेक्स्ट और एक या उससे ज़्यादा इमेज शामिल करें:

Kotlin

scope.launch {
    // Provide a text prompt instructing the model to edit the image
    val prompt = content {
        image(bitmap)
        text("Edit this image to make it look like a cartoon")
    }
    // To edit the image, call `generateContent` with the prompt (image and text input)
    val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt)
        .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
    // Handle the generated text and image
}

Java

// Provide an image for the model to edit
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.scones);
// Provide a text prompt instructing the model to edit the image
Content promptcontent = new Content.Builder()
        .addImage(bitmap)
        .addText("Edit this image to make it look like a cartoon")
        .build();
// To edit the image, call `generateContent` with the prompt (image and text input)
ListenableFuture<GenerateContentResponse> response = model.generateContent(promptcontent);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        // iterate over all the parts in the first candidate in the result object
        for (Part part : result.getCandidates().get(0).getContent().getParts()) {
            if (part instanceof ImagePart) {
                ImagePart imagePart = (ImagePart) part;
                Bitmap generatedImageAsBitmap = imagePart.getImage();
                break;
            }
        }
    }
    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

सिलसिलेवार बातचीत के ज़रिए, इमेज में बदलाव करना और उन्हें बेहतर बनाना

इमेज एडिटिंग के लिए बातचीत वाली अप्रोच का इस्तेमाल करने के लिए, सिलसिलेवार बातचीत का इस्तेमाल किया जा सकता है. इससे, ओरिजनल इमेज को फिर से भेजे बिना, बदलावों को बेहतर बनाने के लिए फ़ॉलो-अप अनुरोध किए जा सकते हैं.

सबसे पहले, startChat() के साथ चैट शुरू करें. इसके लिए, मैसेज का इतिहास भी दिया जा सकता है. इसके बाद, अगले मैसेज के लिए sendMessage() का इस्तेमाल करें:

Kotlin

scope.launch {
    // Create the initial prompt instructing the model to edit the image
    val prompt = content {
        image(bitmap)
        text("Edit this image to make it look like a cartoon")
    }
    // Initialize the chat
    val chat = model.startChat()
    // To generate an initial response, send a user message with the image and text prompt
    var response = chat.sendMessage(prompt)
    // Inspect the returned image
    var generatedImageAsBitmap: Bitmap? = response
        .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
    // Follow up requests do not need to specify the image again
    response = chat.sendMessage("But make it old-school line drawing style")
    generatedImageAsBitmap = response
        .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
}

Java

// Provide an image for the model to edit
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.scones);
// Initialize the chat
ChatFutures chat = model.startChat();
// Create the initial prompt instructing the model to edit the image
Content prompt = new Content.Builder()
        .setRole("user")
        .addImage(bitmap)
        .addText("Edit this image to make it look like a cartoon")
        .build();
// To generate an initial response, send a user message with the image and text prompt
ListenableFuture<GenerateContentResponse> response = chat.sendMessage(prompt);
// Extract the image from the initial response
ListenableFuture<Bitmap> initialRequest = Futures.transform(response,
        result -> {
            for (Part part : result.getCandidates().get(0).getContent().getParts()) {
                if (part instanceof ImagePart) {
                    ImagePart imagePart = (ImagePart) part;
                    return imagePart.getImage();
                }
            }
            return null;
        }, executor);
// Follow up requests do not need to specify the image again
ListenableFuture<GenerateContentResponse> modelResponseFuture = Futures.transformAsync(
        initialRequest,
        generatedImage -> {
            Content followUpPrompt = new Content.Builder()
                    .addText("But make it old-school line drawing style")
                    .build();
            return chat.sendMessage(followUpPrompt);
        }, executor);
// Add a final callback to check the reworked image
Futures.addCallback(modelResponseFuture, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        for (Part part : result.getCandidates().get(0).getContent().getParts()) {
            if (part instanceof ImagePart) {
                ImagePart imagePart = (ImagePart) part;
                Bitmap generatedImageAsBitmap = imagePart.getImage();
                break;
            }
        }
    }
    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

ध्यान रखने वाली बातें और सीमाएं

इन बातों और सीमाओं का ध्यान रखें:

  • आउटपुट फ़ॉर्मैट: इमेज, PNG फ़ॉर्मैट में जनरेट होती हैं. इनका डाइमेंशन ज़्यादा से ज़्यादा 1024 पिक्सल होता है.
  • इनपुट के टाइप: यह मॉडल, इमेज जनरेट करने के लिए ऑडियो या वीडियो इनपुट के साथ काम नहीं करता.
  • भाषा से जुड़ी सहायता: बेहतर परफ़ॉर्मेंस के लिए, इन भाषाओं का इस्तेमाल करें: अंग्रेज़ी (en), मेक्सिकन स्पैनिश (es-mx), जापानी (ja-jp), सरलीकृत चीनी (zh-cn), और हिन्दी (hi-in).
  • जनरेट करने से जुड़ी समस्याएं:
    • ऐसा हो सकता है कि इमेज जनरेट करने की सुविधा हमेशा काम न करे. इस वजह से, कभी-कभी सिर्फ़ टेक्स्ट वाला आउटपुट मिलता है. इमेज के तौर पर जवाब पाने के लिए, साफ़ तौर पर निर्देश दें. उदाहरण के लिए, "एक इमेज जनरेट करो", "जवाब के साथ-साथ इमेज भी दिखाओ", "इमेज अपडेट करो".
    • ऐसा हो सकता है कि मॉडल, जवाब जनरेट करने की प्रोसेस को बीच में ही रोक दे. फिर से कोशिश करें या कोई दूसरा प्रॉम्प्ट आज़माएं.
    • मॉडल, टेक्स्ट को इमेज के तौर पर जनरेट कर सकता है. टेक्स्ट के तौर पर जवाब देने के लिए, साफ़ तौर पर निर्देश दें. उदाहरण के लिए, "इमेज के साथ-साथ टेक्स्ट भी जनरेट करो".

ज़्यादा जानकारी के लिए, Firebase का दस्तावेज़ देखें.

अगले चरण

ऐप्लिकेशन सेट अप करने के बाद, इन बातों का ध्यान रखें: