Not sure if you're still looking for an answer, but for anyone else who is dealing with file uploads when using Gradio I have found the following function (and variations of it) that makes use of the shutil
library infinitely useful!
def process_file(fileobj):
path = "/home/ubuntu/temps/" + os.path.basename(fileobj)
shutil.copyfile(fileobj.name, path)
# now you can process the file at path as needed, e.g:
# do_something_to_file(path)
Without doing this, I found I had a lot of issues treating the file initially uploaded to Gradio as a tempfile._TemporaryFileWrapper
object. I found I was encountering a lot of permission issues and it was hard to do everything I wanted with the object and the tempfile
library.
This new method with shutil
gives you complete control over a permanent file object, and if you need to delete it afterwards just add in the code to do so when you're done.
In the context of using this with Gradio, it would work in a simple example as follows:
import gradio as gr
import os
import shutil
def process_file(fileobj):
path = "/home/ubuntu/temps/" + os.path.basename(fileobj) #NB*
shutil.copyfile(fileobj.name, path)
return do_something_to_file(path)
demo = gr.Interface(
fn=process_file,
inputs=[
"file",
],
outputs="text"
)
demo.launch(server_name='0.0.0.0')
NB: I am doing this on an Ubuntu instance, obviously please modify path name accordingly for your operating system and needs. If you have issues with this method check that you and your python script have permission to write to whichever directory you specify in your path.