Closed LindaLawton closed 1 month 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
)
Oh nice, thanks for the tip @Hamza-nabil. We should do this.
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 ?
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}")
[ ] 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.
[x] Update Tests: Add test cases specifically for file stream scenarios.
Any additional improvements or steps needed for this feature?
Thank you!
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.
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
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