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) 将每天删除创建时间超过一天(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()

gradio