#🔒 Gemini file upload without local storage

232 messages · Page 1 of 1 (latest)

regal ivy
#

I run into a problem when i tried to upload user files to gemini in order to prompt them but i run the code in server less funcyion i tried multiple aproaches but it didn't work i have the user files saved into cloudinary so i have the link to the file but gemini don't trat links so idk what i can do plz help

dusky widgetBOT
#

@regal ivy

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

regal ivy
#

import os
import uuid
from django.core.files.storage import default_storage
from .converter import Convert_To_Test

def upload_file(file, quizz_id):
# Valid extensions and maximum file size in bytes
valid_extensions = ['doc', 'docx', 'pdf', 'png', 'jpg']
max_file_size = 10 * 1024 * 1024 # 10 MB

if not file:
    return {'error': 'No file provided.'}

# Validate file extension
file_extension = file.name.split('.')[-1].lower()
if file_extension not in valid_extensions:
    return {'error': 'Invalid file extension.'}

# Validate file size
if file.size > max_file_size:
    return {'error': 'File size exceeds the maximum limit of 10 MB.'}

# Generate a unique filename using UUID
unique_filename = f"{uuid.uuid4().hex}.{file_extension}"
folder_path = 'tmp'
file_path = default_storage.save(os.path.join(folder_path, unique_filename), file)

download_link = default_storage.url(file_path)

print(download_link)

Convert_To_Test(file_path,quizz_id)

return {'success': True, 'download_link': download_link}
rain trellis
#

What part is breaking down?

#

Do you have an error message that you can show us?

regal ivy
#

I had many

#

thsi one works

#

beacuse it installs the file in y local server

#

but if i send it in vercel it will display an error saying that u can't write

#

and that it read only

#

i need a way

#

to send the file to gemini api without having to save file in my local serv

regal ivy
#

no

#

the first one

rain trellis
#

what first one

regal ivy
#

def upload_to_gemini(path, mime_type=None):
file = genai.upload_file(path, mime_type=mime_type)
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
return file

dusky widgetBOT
#

Hey @regal ivy!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
rain trellis
#

Where in it?

regal ivy
#

here

#

the file upload

#

idk how to upload file without having to saveit in local serv

rain trellis
#

Uploading to Gemini causes Vercel to say that you can't write?

#

Didn't you already write when you uploaded the file?

regal ivy
#

No

#

cause i use cloudinary

#

it saves the file for u

#

it like aws S3 bucket

#

from my modest understanding

rain trellis
#

Makes sense

regal ivy
#

So when i didn't knew how to uplaod file to gimini from cloudinary i tried directly uplaoding it from user submittinghis file i get teh post variable and i send but it does not work cause gemini do not support it

#

idk how i can like transfer the file from cloudinary to gimini without having to save it in my serv

#

i tried tos tram it but gemini don't support it either

#

when i do they respond with a 500 error of their own

#

then i went to chat gbt and i tired to force the file to go threw with some weird code that i don't understand then it simply denied EOF occurred in violation of protocol (_ssl.c:2396)

regal ivy
#

what is this i'm not a programmer

rain trellis
#

What are you then?

regal ivy
#

i just read in google then i try to apply

rain trellis
#

yea

#

I was trying to track down what it was using, but I can't see anywhere it's actually writing to disk

#

What's the actual error you're getting?

#

!traceback

dusky widgetBOT
#
Traceback

Please provide the full traceback for your exception in order to help us identify your issue.
While the last line of the error message tells us what kind of error you got,
the full traceback will tell us which line, and other critical information to solve your problem.
Please avoid screenshots so we can copy and paste parts of the message.

A full traceback could look like:

Traceback (most recent call last):
  File "my_file.py", line 5, in <module>
    add_three("6")
  File "my_file.py", line 2, in add_three
    a = num + 3
        ~~~~^~~
TypeError: can only concatenate str (not "int") to str

If the traceback is long, use our pastebin.

regal ivy
#

No error cause with this code i save to local serv but when i deploy i can not write so my code is uselless

rain trellis
#

when i deploy i can not write

#

How do you know that?

#

Do you get an error telling you that you can't write?

regal ivy
#

cause it serverless function

regal ivy
rain trellis
#

wait what
You're using Django in a serverless function?

rain trellis
rain trellis
#

WTF

regal ivy
#

[ERROR] 2024-09-09T20:40:52.848Z 1dd03ac5-87e2-4282-8d4c-c81349b1ad58 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 20, in fetch_from_cloudinary
with open(temp_filename, 'wb') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: 'temp_732c698e24b1453aa309dd5e8f6d64f4.pdf'

regal ivy
#

i have problem with the convert tool

#

So ?

rain trellis
#

File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test

rain trellis
#

fetch_from_cloudinary doesn't exist in your paste

rain trellis
#

404

#

Is it a private repo?

regal ivy
#

yes let me make it public

#

it public now

dusky widgetBOT
#

quizzy_app/converter.py lines 61 to 64

local_file_path = fetch_from_cloudinary(cloudinary_url)

# Upload file and wait for it to be processed
files = [upload_to_gemini(local_file_path, mime_type="application/pdf")]```
regal ivy
rain trellis
#

That is the line where the error happens

#

So if you want to fix your problem

#

Then that is a very important line

regal ivy
#

really ?

rain trellis
regal ivy
#

ok le me remove it and try i'm pretty sure i tried without it and crashed anyways

rain trellis
#

Don't remove it

#

Then your files won't exist

regal ivy
#

[ERROR] 2024-09-09T20:53:27.504Z 63efb40c-e553-4813-816f-3459412462e3 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 20, in fetch_from_cloudinary
with open(temp_filename, 'wb') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: 'temp_e38caa575996402a8a6aaa897f6ef46c.pdf'

rain trellis
#

So this should be really simple

#

Wait maybe not

regal ivy
#

Clean up temporary file

storage = MediaCloudinaryStorage()
# Delete the file from Cloudinary
storage.delete(local_file_path)
#

u talking about that line right ?

rain trellis
#

no

#

I don't even know where that line is

regal ivy
#

the repo is public

#

this code basically tries to downoald the file locally from cloudinary but that not waht i need

#

i can't write

rain trellis
#
+ from io import BytesIO
  def fetch_from_cloudinary(cloudinary_url):
    response = requests.get(cloudinary_url)
    if response.status_code == 200:
-      # Create a temporary file
-      temp_filename = f"temp_{uuid.uuid4().hex}.pdf"
-      with open(temp_filename, 'wb') as f:
-        f.write(response.content)
-      return temp_filename
+      return BytesIO(response.content)
    else:
      raise Exception("Failed to fetch file from Cloudinary")
regal ivy
#

how to upload file to gimini then ?

rain trellis
#

If it works, it will work the same way

regal ivy
#

it needs to be uploaded directly

rain trellis
#

Try this real quick

#

See what error you get

rain trellis
#

yes

regal ivy
#

it will say Bytes not compatible either str butes or Fileobject

#

but i will try

rain trellis
#

Oh really?

#

Paste the EXACT error

#

This might actually work

regal ivy
#

it building

#

[ERROR] 2024-09-09T21:09:08.310Z 4ee02480-706d-48d0-a8c7-2819ad6cc957 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 20, in fetch_from_cloudinary
with open(temp_filename, 'wb') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 30] Read-only file system: 'temp_31d936da441044a48d707b468feee831.pdf'

rain trellis
#

You didn't change anything

regal ivy
#

id did

#

look repositry

rain trellis
regal ivy
#

what ??

#

let me see

regal ivy
#

i dump

#

sry

#

let me retry

#

def fetch_from_cloudinary(cloudinary_url):
response = requests.get(cloudinary_url)
if response.status_code == 200:
# Create a temporary file
temp_filename = f"temp_{uuid.uuid4().hex}.pdf"
with open(temp_filename, 'wb') as f:
f.write(response.content)
return temp_filename
return BytesIO(response.content)
else:
raise Exception("Failed to fetch file from Cloudinary")

#

it building

rain trellis
#

!code

dusky widgetBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

rain trellis
#

You need to remove the red parts

#

That’s what’s causing your errors

regal ivy
#

ah lol

#

okay i did it buuilding

#

[ERROR] 2024-09-09T21:20:02.052Z ae36dfc9-98b3-435d-bf76-ddee74ed90a1 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 61, in Convert_To_Test
files = [upload_to_gemini(local_file_path, mime_type="application/pdf")]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 24, in upload_to_gemini
file = genai.upload_file(path, mime_type=mime_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/google/generativeai/files.py", line 60, in upload_file
path = pathlib.Path(os.fspath(path))
^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not BytesIO

#

so ?

#

any idea sir ?

rain trellis
#

Google is forcing you to use a file on disk

#

See if there’s a different function on Google’s side that lets you upload a file from bytes

regal ivy
#

aparently there isn't

rain trellis
rain trellis
regal ivy
#

u can't uplaod anything in a serverlesss function

regal ivy
#

it uses ram instead of diak ?

rain trellis
rain trellis
regal ivy
regal ivy
regal ivy
rain trellis
rain trellis
regal ivy
rain trellis
#

I’m on mobile now, so I can’t type code very easily

regal ivy
#

ah okay thank u i will try it

#

i mean i'm trying it i just wainting the build to end

#

[ERROR] 2024-09-09T21:40:53.892Z 8505e479-1551-45b3-81e7-b6a9743ad24e Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 64, in Convert_To_Test
files = [upload_to_gemini(local_file_path, mime_type="application/pdf")]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/converter.py", line 27, in upload_to_gemini
file = genai.upload_file(path, mime_type=mime_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/google/generativeai/files.py", line 60, in upload_file
path = pathlib.Path(os.fspath(path))
^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not SpooledTemporaryFile ?

#

idk why it did this

rain trellis
regal ivy
#

gemini couldn't just take links 😮‍💨

rain trellis
#

Why are you trying to use serverless functions anyways?

regal ivy
#

cause it free it fast and it scalable and easy to use

#

so u don't have any idea ?

rain trellis
#

I do not

regal ivy
#

i think maybe i will try to upload to drive with bytes if they allow and then i think i can use file in drive to prompt it into ai

#

idk if it will work

#

if not i think i will do my own ocr

#

and txt extraction

rain trellis
#

#python-discussion message

#

Go back to the code you had at the start

#

And set location you’re saving files to to /tmp

humble sky
#

^ tempfile will do that for you

#

(it should at least)

regal ivy
humble sky
#

how big is that temp file?

regal ivy
humble sky
#

Because you're using SpooledTemporaryFile

regal ivy
#

sir

humble sky
#

use TemporaryDirectory instead

#

then use that to make a named file

regal ivy
#

what will that do

#

will it save file in disk ?

#

def fetch_from_cloudinary(cloudinary_url):
response = requests.get(cloudinary_url)
if response.status_code == 200:
file = SpooledTemporaryFile()
file.write(response.content)
file.seek(0) # Ensure the file pointer is at the start of the file
return file
else:
raise Exception("Failed to fetch file from Cloudinary")

#

can u show me what to modify sir ?

dusky widgetBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

regal ivy
humble sky
#

!d tempfile.mktemp is probably the best option

dusky widgetBOT
#

tempfile.mktemp(suffix='', prefix='tmp', dir=None)```
 Deprecated since version 2\.3: Use [`mkstemp()`](https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp) instead.

Return an absolute pathname of a file that did not exist at the time the call is made. The *prefix*, *suffix*, and *dir* arguments are similar to those of [`mkstemp()`](https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp), except that bytes file names, `suffix=None` and `prefix=None` are not supported.
humble sky
#

it returns a string which you can open()

#

wrap it in Path() if you like

rain trellis
regal ivy
rain trellis
#

Since 2.3!?

humble sky
#

if you use NamedTemporaryFile, you can get the file name via f.name

regal ivy
#

[ERROR] 2024-09-09T22:12:53.725Z aaf1ff84-f00f-429a-af2a-e17d088a3ce5 Internal Server Error: /editor
Traceback (most recent call last):
File "/var/task/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/var/task/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/task/quizzy_app/views.py", line 112, in editor
upload_file(file, Actual_Quizz_id)
File "/var/task/quizzy_app/uploader.py", line 30, in upload_file
Convert_To_Test(download_link,quizz_id)
File "/var/task/quizzy_app/converter.py", line 69, in Convert_To_Test
local_file_path = fetch_from_cloudinary(cloudinary_url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Convert_To_Test.<locals>.fetch_from_cloudinary() missing 1 required positional argument: 'filename'

humble sky
#
with tempfile.NamedTemporaryFile() as f:
  f.write("hahahahah")
  f.seek(0)

  upload_file(f.name, ...)
regal ivy
#

man please write the code for me i don't understand shit 🥺

#

that the gihub ripo

#

waittttt

#

it worked but why did it crash ?

#

504 gateway timeout what does this even mean ?

rain trellis
#

.httpstatus 504

fresh valleyBOT
#
**Status: 504**
regal ivy
#

So how to solve lol ?

rain trellis
#

go faster

humble sky
#

do less

regal ivy
#

hh what ?

rain trellis
#

Because you're using serverless functions, you only have a certain number of seconds to finish everything you're doing before the server gets shut down and you get kicked off

rain trellis
regal ivy
#

shoot

#

this is a serious problem they want my money

regal ivy
#

Can i like chunck my requests or something ?

dusky widgetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.