google-gemini / cookbook

Examples and guides for using the Gemini API
https://ai.google.dev/gemini-api/docs
Apache License 2.0
5.46k stars 834 forks source link

AttributeError: 'File' object has no attribute 'state' #174

Closed akshit0201 closed 6 months ago

akshit0201 commented 6 months ago

Description of the bug:

I'm following this cookbook: https://github.com/google-gemini/cookbook/blob/54fb45145f19fc918c978c736067489e37df2657/quickstarts/Video.ipynb#L224

Getting the above mentioned attributeError when accessing state of the uploaded file.

Actual vs expected behavior:

Here is my code:

import google.generativeai as genai

def upload_file(video_file):
        try:
            print("Inside upload file")
            genai.configure(api_key=userdata.get("gemini"))
            uploaded_file = genai.upload_file(video_file)
            while uploaded_file.state.name == "PROCESSING":
                print('Waiting for video to be processed.')
                time.sleep(10)
                uploaded_file = genai.get_file(uploaded_file.name)

            if uploaded_file.state.name == "FAILED":
                raise ValueError(uploaded_file.state.name)
            print(f'Video processing complete: ' + uploaded_file.uri)
            return uploaded_file
        except Exception as e:
            print(f"Error occurred when uploading file {e}")
            return None

Actual behaviour: Error occurred when uploading file AttributeError: 'File' object has no attribute 'state'.

Expected: Video processing complete: uploaded_file.uri

Any other information you'd like to share?

I executed print(dir(uploaded_file))

And received the following result: ['class', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', '_proto', 'create_time', 'delete', 'display_name', 'expiration_time', 'mime_type', 'name', 'sha256_hash', 'size_bytes', 'to_proto', 'update_time', 'uri']

As you can see state is not in the attribute list.

My genai.__version__ is 0.5.2

akshit0201 commented 6 months ago

Updating to 0.5.4 solved this issue. Should have read the changelog before coming here, extremely sorry.