google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.62k stars 322 forks source link

Upload file via stream #537

Closed LindaLawton closed 1 month ago

LindaLawton commented 2 months ago

Description of the feature request:

Currently the only way to upload a file is to send the path of the file

It looks like client.py#L73

Is there anyway to get this to take a stream

media = googleapiclient.http.MediaFileUpload(
        filename=path, mimetype=mime_type, resumable=resumable
    )

What problem are you trying to solve with this feature?

I am downloading a file from another site and would rather not have to save it to disk only to send it to Gemini.

Any other information you'd like to share?

please

Hamza-nabil commented 2 months ago

Yes, you can use the MediaIoBaseUpload class to upload a stream instead of a file path.

fh = BytesIO('...Some data to upload...')

media = googleapiclient.http.MediaIoBaseUpload(
     fh, mimetype=mime_type, resumable=resumable
)
MarkDaoust commented 2 months ago

Oh nice, thanks for the tip @Hamza-nabil. We should do this.

Hamza-nabil commented 2 months ago

I have created a draft PR #556 to add support for streaming file uploads in the upload_file function.

@MarkDaoust Can you please review it and provide feedback ?

Usage Example

import os
import requests
from io import BytesIO

import google.generativeai as genai

genai.configure(api_key=os.environ['GOOGLE_API_KEY'])

# Define the URL
url = "https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf"

# Download the file using requests and create a BytesIO object
response = requests.get(url)
if response.status_code == 200:
    data = BytesIO(response.content)

    # Upload the file and print a confirmation
    sample_file = genai.upload_file(path=data, display_name="Test stream upload")
    print(f"File uploaded successfully: {sample_file}")

Next Steps:

Any additional improvements or steps needed for this feature?

Thank you!

MarkDaoust commented 1 month ago

Fixed by: https://github.com/google-gemini/generative-ai-python/pull/556

Update Tests: Add test cases specifically for file stream scenarios.

I included one in the PR.

Argument Name Change: Renaming the path argument to something more descriptive like file_source. This change is not a priority as it may impact existing code.

Yeah, backwards compatibility is always important.