Ai Studio API Key and no access to the file upload as in cookbook

I get an error with AI Studio API key for upload a file that i want to reference in a message later on

The following line of code: text_file = genai.upload_file(path=file_path)
Gives this error (part): “…
raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, …”

I have been passing the content of the file as a message, but that does not scale well with rate limits etc.

Not sure what to do,

  • setup the ADC (Application Default Credentials)?
  • not possible in AI Studio API?
  • Different configuration.

Help much appreciated,

Thanks Rene

Hi @Rene_Luijk

Welcome to the dev forum.

Can you share the code that’s resulting in this error?

Thanks @sps

I will share the code below, but from my end, I am don’t need it to be resolved, I found a another way.

I moved into Colab Enterprise, and used the cookbook example from the Apollo Transript example with my API key generated in AI google Studio to upload and retrieve a file and give it to the gemini 1.5. My test code was giving errors there too (but different), but the cookbook example works.

As per your request, the code producing the reported error:

The posted error was/is in this small test script:

import google.generativeai as genai

def initialize_model():
    genai.configure(api_key="xxx")  
    return genai.GenerativeModel('gemini-1.5-pro-latest')


# Define the test function
def test_file_upload_and_question(file_path, prompt):
    # Upload a file and retrieve the URI
    text_file = genai.upload_file(path=file_path)
    file_uri = text_file.uri

    # Ask a question about the content of the file using the language model
    model = initialize_model()
    response = model.generate_content([prompt, text_file], request_options={"timeout": 600})

    # Print the response
    print(response.text)


# Define the main function
def main():
    # Set the static variables for the test
    file_path = "/..../Documents/GitHub/zk/0. Inbox/2024-05-29/BREAKING_ xAI Secures $6B for Supercomputer Gigafactory Compute.txt"
    prompt = "Find 1 moment in this text file that is the essence."

    test_file_upload_and_question(file_path, prompt)

# Call the main function
if __name__ == "__main__":
    main()