1. 附加功能
  2. 资源清理

资源清理

您的Gradio应用程序在其生命周期内可能会创建资源。 资源的例子包括gr.State变量、您创建并显式保存在内存中的任何变量,或者您保存到磁盘的文件。 随着时间的推移,这些资源可能会耗尽您服务器的所有RAM或磁盘空间,并导致您的应用程序崩溃。

Gradio 提供了一些工具,供您清理由您的应用程序创建的资源:

  1. 自动删除gr.State变量。
  2. 使用delete_cache参数自动清理缓存。
  3. Blocks.unload 事件。

让我们分别来看一下它们。

自动删除gr.State

当用户关闭浏览器标签时,Gradio 将在 60 分钟后自动删除与该用户会话关联的任何 gr.State 变量。如果用户在 60 分钟内重新连接,则不会删除任何状态。

您可以使用gr.State的以下两个参数进一步控制删除行为:

  1. delete_callback - 当变量被删除时调用的任意函数。此函数必须将状态值作为输入。此函数对于从GPU内存中删除变量非常有用。
  2. time_to_live - 状态在创建或更新后应存储的秒数。这将在会话关闭之前删除变量,因此对于清除可能长时间运行的会话的状态非常有用。

通过delete_cache自动清理缓存

您的Gradio应用程序将保存上传和生成的文件到一个特殊的目录,称为缓存目录。Gradio使用哈希方案来确保不会将重复的文件保存到缓存中,但随着时间的推移,缓存的大小将会增长(特别是如果您的应用程序变得非常流行😉)。

如果你指定了gr.Blocks()gr.Interface()gr.ChatInterface()delete_cache参数,Gradio可以定期为你清理缓存。 这个参数是一个形式为[frequency, age]的元组,两者都以秒为单位表示。 每frequency秒,如果文件创建时间超过age秒,这个Blocks实例创建的临时文件将被删除。 例如,将其设置为(86400, 86400)将每天删除超过一天的临时文件。 此外,当服务器重启时,缓存将被完全删除。

unload 事件

此外,Gradio 现在包含一个 Blocks.unload() 事件,允许你在用户断开连接时运行任意的清理函数(这不会有 60 分钟的延迟)。 与其他 gradio 事件不同,此事件不接受输入或输出。 你可以将 unload 事件视为 load 事件的反向操作。

将所有内容整合在一起

以下演示使用了所有这些功能。当用户访问页面时,会为该用户创建一个特殊的唯一目录。 随着用户与应用交互,图像会保存到该特殊目录中。 当用户关闭页面时,通过unload事件删除该会话中创建的图像。 缓存中的状态和文件也会自动清理。

from __future__ import annotations
import gradio as gr
import numpy as np
from PIL import Image
from pathlib import Path
import secrets
import shutil

current_dir = Path(__file__).parent

def generate_random_img(history: list[Image.Image], request: gr.Request):
    """Generate a random red, green, blue, orange, yellor or purple image."""
    colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 165, 0), (255, 255, 0), (128, 0, 128)]
    color = colors[np.random.randint(0, len(colors))]
    img = Image.new('RGB', (100, 100), color)

    user_dir: Path = current_dir / str(request.session_hash)
    user_dir.mkdir(exist_ok=True)
    path = user_dir / f"{secrets.token_urlsafe(8)}.webp"

    img.save(path)
    history.append(img)

    return img, history, history

def delete_directory(req: gr.Request):
    if not req.username:
        return
    user_dir: Path = current_dir / req.username
    shutil.rmtree(str(user_dir))

with gr.Blocks(delete_cache=(60, 3600)) as demo:
    gr.Markdown("""# State Cleanup Demo
                🖼️ Images are saved in a user-specific directory and deleted when the users closes the page via demo.unload.
                """)
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Row():
                img = gr.Image(label="Generated Image", height=300, width=300)
            with gr.Row():
                gen = gr.Button(value="Generate")
            with gr.Row():
                history = gr.Gallery(label="Previous Generations", height=500, columns=10)
                state = gr.State(value=[], delete_callback=lambda v: print("STATE DELETED"))

    demo.load(generate_random_img, [state], [img, state, history])
    gen.click(generate_random_img, [state], [img, state, history])
    demo.unload(delete_directory)

demo.launch()