Problem with temporary files

Hi,

I have a problem using the tempfile library on Spaces. This problem occurs sometimes.

I have this space that generates an output file that is a disk write and the main function returns the path to this file.

I know that if I do not use a tempfile, this works correctly, but I’d like to use tempfile to prevent generations of outputs in a local execution.

This is the function that generates the problem (temporary file lines are commented out to avoid errors).

And the logs when the execution fails are the following:

Traceback (most recent call last):
File “/home/user/.local/lib/python3.8/site-packages/gradio/routes.py”, line 259, in run_predict
output = await app.blocks.process_api(
File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 691, in process_api
predictions = self.postprocess_data(fn_index, predictions, state)
File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 657, in postprocess_data
block.postprocess(prediction_value)
File “/home/user/.local/lib/python3.8/site-packages/gradio/components.py”, line 1676, in postprocess
y = processing_utils.create_tmp_copy_of_file(y, dir=self.temp_dir)
File “/home/user/.local/lib/python3.8/site-packages/gradio/processing_utils.py”, line 296, in create_tmp_copy_of_file
shutil.copy2(file_path, file_obj.name)
File “/usr/local/lib/python3.8/shutil.py”, line 435, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File “/usr/local/lib/python3.8/shutil.py”, line 264, in copyfile
with open(src, ‘rb’) as fsrc, open(dst, ‘wb’) as fdst:
FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/tmpsp16mbfp/soccer532nbnksvotwvex0_out.mp4’

As I mentioned earlier, this code runs perfectly in many situations, but sometimes finish with the above error.

Any suggestions to use the tempfile library capabilities properly on Spaces?

Thank you,
Diego.

1 Like

Hi @diegofernandezc !

You may have to copy your file into your temporary directory so that it doesn’t get deleted?

I think it’s fine if your demo doesn’t create a temporary file. Gradio will do that under the hood for you when your output component is File.

I looked for this on various websites including youtube and solved it using to verify the temp folder path.