google-gemini / generative-ai-android

The official Android library for the Google Gemini API
https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=android
Apache License 2.0
640 stars 139 forks source link

Multiple Images #189

Open RobinKumar5986 opened 4 days ago

RobinKumar5986 commented 4 days ago

Description of the feature request:

I am developing an chat bot using the Gemini API but in this I wanted to Insert multiple images using some kind of loop.

But for the content builder I am not able to find any method in the Docs to do so. val inputContent = content { image(image1) image(image2) text("What's different between these pictures?") } If I wanted to add multiple images recursively of dynamically How can I do so ?

What problem are you trying to solve with this feature?

Trying to add multiple images dynamically.

Any other information you'd like to share?

No response

thatfiredev commented 3 days ago

@RobinKumar5986 have you tried looping inside of content?

val inputContent = content {
  for (img in images) { // <-- your loop
    image(img)
  }
  text("What's different between these pictures?")
}
RobinKumar5986 commented 2 days ago

@thatfiredev One mistake from my side for not specifying the programming language I am using.

Actually I am using java for this project so, I am not able to add multiple Images in the content :

The current structure of my code is. :

private void AskAi(String userMsg) { String apiKey = BuildConfig.API_KEY; GenerativeModel gm = new GenerativeModel("gemini-1.5-flash", apiKey); GenerativeModelFutures model = GenerativeModelFutures.from(gm);

    if(imageList.size()>2){
        Toast.makeText(this, "Currently we can take Only Two Images \uD83D\uDE14", Toast.LENGTH_SHORT).show();
    }
    else if(imageList.size()==1){
        content=new Content.Builder()
                .addImage(imageList.get(0))
                .addText(userMsg)
                .build();
    }else if(imageList.size()==2){
        content=new Content.Builder()
                .addImage(imageList.get(0))
                .addImage(imageList.get(1))
                .addText(userMsg)
                .build();
    }
    else {
        content = new Content.Builder()
                .addText(userMsg)
                .build();
    }

Please provide any details on How can I tackle this.

thatfiredev commented 1 day ago

@RobinKumar5986 That's ok.

You can do it like this:

    private void AskAi(String userMsg) {
        String apiKey = BuildConfig.API_KEY;
        GenerativeModel gm = new GenerativeModel("gemini-1.5-flash", apiKey);
        GenerativeModelFutures model = GenerativeModelFutures.from(gm);

        if(imageList.size()>2){
            Toast.makeText(this, "Currently we can take Only Two Images \uD83D\uDE14", Toast.LENGTH_SHORT).show();
        } else {
            Content.Builder builder = new Content.Builder();
            for (Bitmap image : imageList) {
                builder.addImage(image);
            }
            content = builder.addText(userMsg).build();
        }
        // ...
    }

I'm assuming that imageList is of type List<Bitmap>, but if it's not, you can change the type in the for loop.

RobinKumar5986 commented 1 day ago

@thatfiredev Issue : I am adding 5 images but and asking a simple question like how many images I have provide (not asking how many distinct image) but the gemini is not able to answer that. I have also, some more queries for that I am opening one more Issue Please look into that also.

Log : 2024-07-06 00:25:35.046 17469-17469 Number of Images : pid-17469 D 5 2024-07-06 00:26:04.683 17469-17469 Number of Images : pid-17469 D 5

code : int count=0; builder=new Content.Builder(); for(Bitmap img : imageList) { builder.addImage(img); } content=builder.addText(userMsg).build();

        Log.d("Number of Images :",imageList.size()+"");
image
RobinKumar5986 commented 1 day ago

@thatfiredev I want to thank you because the way you have suggested I am able to add multiple images but getting this issue now.