generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

Readme for new Gemini Code Samples on Recipie to be updated to accept Image

Open AndrewVoirol opened this issue 1 year ago • 7 comments

Description of the bug:

The current example produces a base 64 response instead of the actual recipe of the item.

Actual vs expected behavior:

image

Any other information you'd like to share?

This code works correctly

`import google.generativeai as genai from PIL import Image

genai.configure(api_key="[API_KEY")

model = genai.GenerativeModel('gemini-pro-vision') cookie_picture = Image.open('image.png') prompt = "Give me a recipe for this:"

response = model.generate_content( contents=[prompt, cookie_picture] )

`print(response.text)``

AndrewVoirol avatar Dec 13 '23 15:12 AndrewVoirol

import google.generativeai as genai
from pathlib import Path

genai.configure(api_key="YOUR_ACTUAL_API_KEY")

# Ensure that the model ID is correct and that you have access to it
model = genai.GenerativeModel('gemini-pro-vision')

# Read the image as binary data
image_path = Path('image.png')
image_data = image_path.read_bytes()

prompt = "Give me a recipe for this:"
contents = {
    'parts': [
        {'mime_type': 'image/png', 'data': image_data},
        {'text': prompt}
    ]
}

# Send the prompt and image data to the model
response = model.generate_content(contents=contents)

# Print the result
print(response.text)

AndrewVoirol avatar Dec 13 '23 15:12 AndrewVoirol

Might be better to use the code formatting with three backticks (```) as your comments are getting read as headers in markdown

bedros-p avatar Dec 13 '23 15:12 bedros-p

import google.generativeai as genai
from pathlib import Path

genai.configure(api_key="YOUR_ACTUAL_API_KEY")

# Ensure that the model ID is correct and that you have access to it
model = genai.GenerativeModel('gemini-pro-vision')

# Read the image as binary data
image_path = Path('image.png')
image_data = image_path.read_bytes()

prompt = "Give me a recipe for this:"
contents = {
    'parts': [
        {'mime_type': 'image/png', 'data': image_data},
        {'text': prompt}
    ]
}

# Send the prompt and image data to the model
response = model.generate_content(contents=contents)

# Print the result
print(response.text)

AndrewVoirol avatar Dec 13 '23 16:12 AndrewVoirol

thank you @bedros-p for the help and guidance.

AndrewVoirol avatar Dec 13 '23 16:12 AndrewVoirol

Either of those solutions work. But it makes no sense for that to print a bunch of binary.

I should fix whatever's going wrong in the first case.

That might just the the error trying to give you more information and inlining the object that it failed to convert. I should tri that if it's too long.

MarkDaoust avatar Dec 14 '23 00:12 MarkDaoust

Agree, I was trying this for a bit when the code was there, and I had the key. It wasn't working at all. Then I suppose as the model became available (to me) later in the morning, when I ran the code it streamed that binary out. For what it's worth to the next step in this solution or what was causing it to spit out the response that wasn't initially helpful? Perhaps, it could, down the road, if there is a circumstance of token and context, and knowing what the output would have been. Fun that it's working and things will continue to get even better!

AndrewVoirol avatar Dec 14 '23 01:12 AndrewVoirol

I got the same thing, but it wasn't a bunch of binary. It was raising an error that contained request information. Since the image is part of the request, it gets printed as well, but it takes up too much space to see the original error. You can capture the output to a file and then read the error like that.

#160 points to another doc where the example included an extra set of [ ]. I've sent a fix.

MarkDaoust avatar May 17 '24 22:05 MarkDaoust