Introducing Gradio 5.0
Read MoreIntroducing Gradio 5.0
Read MoreSharing your Gradio app with others (by hosting it on Spaces, on your own server, or through temporary share links) exposes certain files on your machine to the internet.
This guide explains which files are exposed as well as some best practices for making sure the files on your machine are secure.
gr.set_static_paths
function. This parameter allows you to pass in a list of directories or filenames that will be considered static. This means that they will not be copied to the cache and will be served directly from your computer. This can help save disk space and reduce the time your app takes to launch but be mindful of possible security implications. (By default, this parameter is an empty list).Files in the allowed_paths
parameter in launch()
. This parameter allows you to pass in a list of additional directories or exact filepaths you'd like to allow users to have access to. (By default, this parameter is an empty list).
Files in Gradio's cache. After you launch your Gradio app, Gradio copies certain files into a temporary cache and makes these files accessible to users. Let's unpack this in more detail below.
First, it's important to understand why Gradio has a cache at all. Gradio copies files to a cache directory before returning them to the frontend. This prevents files from being overwritten by one user while they are still needed by another user of your application. For example, if your prediction function returns a video file, then Gradio will move that video to the cache after your prediction function runs and returns a URL the frontend can use to show the video. Any file in the cache is available via URL to all users of your running application.
Tip: You can customize the location of the cache by setting the `GRADIO_TEMP_DIR` environment variable to an absolute path, such as `/home/usr/scripts/project/temp/`.
Gradio moves three kinds of files into the cache
avatar_images
of gr.Chatbot
allowed_paths
parameter of the Blocks.launch
method.tempfile.gettempdir()
.Note: files in the current working directory whose name starts with a period (.
) will not be moved to the cache, even if they are returned from a prediction function, since they often contain sensitive information.
If none of these criteria are met, the prediction function that is returning that file will raise an exception instead of moving the file to cache. Gradio performs this check so that arbitrary files on your machine cannot be accessed.
Files uploaded by a user to your Gradio app (e.g. through the File
or Image
input components).
Tip: If at any time Gradio blocks a file that you would like it to process, add its path to the `allowed_paths` parameter.
While running, Gradio apps will NOT ALLOW users to access:
Files that you explicitly block via the blocked_paths
parameter in launch()
. You can pass in a list of additional directories or exact filepaths to the blocked_paths
parameter in launch()
. This parameter takes precedence over the files that Gradio exposes by default, or by the allowed_paths
parameter or the gr.set_static_paths
function.
Any other paths on the host machine. Users should NOT be able to access other arbitrary paths on the host.
Sharing your Gradio application will also allow users to upload files to your computer or server. You can set a maximum file size for uploads to prevent abuse and to preserve disk space. You can do this with the max_file_size
parameter of .launch
. For example, the following two code snippets limit file uploads to 5 megabytes per file.
import gradio as gr
demo = gr.Interface(lambda x: x, "image", "image")
demo.launch(max_file_size="5mb")
# or
demo.launch(max_file_size=5 * gr.FileSize.MB)
max_file_size
for your application.gr.Image
, gr.File
, etc.). For example, the following interface would allow anyone to move an arbitrary file in your local directory to the cache: gr.Interface(lambda s: s, "text", "file")
. This is because the user input is treated as an arbitrary file path. allowed_paths
as small as possible. If a path in allowed_paths
is a directory, any file within that directory can be accessed. Make sure the entires of allowed_paths
only contains files related to your application.python app.py
to python Users/sources/project/app.py
.