Gradio迈向每月百万用户的旅程!

阅读更多
Gradio logo
  1. 助手
  2. set_static_paths

Gradio新手?从这里开始: 开始入门

查看发布历史

set_static_paths

gradio.set_static_paths(···)

描述

设置gradio应用要服务的静态路径。
静态文件直接从文件系统提供,而不是被复制。当向用户发送这些文件时,它们会通过设置为“inline”的Content-Disposition HTTP标头提供给用户。这表明如果可能,文件应直接在浏览器窗口中显示。当您想要提供已知在 gradio 应用程序的生命周期内不会被修改的文件(例如 gr.Examples 中使用的文件)时,此功能非常有用。通过设置静态路径,您的应用程序将启动得更快,并且将消耗更少的磁盘空间。调用此函数将为在同一解释器会话中定义的所有 gradio 应用程序设置静态路径,直到再次调用或会话结束。

使用示例

import gradio as gr

# Paths can be a list of strings or pathlib.Path objects
# corresponding to filenames or directories.
gr.set_static_paths(paths=["test/test_files/"])

# The example files and the default value of the input
# will not be copied to the gradio cache and will be served directly.
demo = gr.Interface(
    lambda s: s.rotate(45),
    gr.Image(value="test/test_files/cheetah1.jpg", type="pil"),
    gr.Image(),
    examples=["test/test_files/bus.png"],
)

demo.launch()

初始化

参数
🔗
paths: str | Path | list[str | Path]

gradio 应用要服务的 文件路径 或 文件路径列表 或 目录名称。 如果是目录名称,则位于该目录中的所有文件都将被视为静态文件,而不会移动到 gradio 缓存。 这也意味着该目录中的所有文件都可以通过网络访问。