Gradio 非侵入式修改的离线使用方案

网上很多方案都是直接更换采用gradio-offline的包。但这个包很久不更新了并且是直接修改源码,所以自己打包也比较麻烦。因此这里提供了一种非侵入式的方法,动态替换部分资源的使用并重定向到本地静态资源,根据需要自己下载资源到本地就可以了。

本方案在gradio 5.34.0上测试通过。理论上兼容一个大版本应该没问题,如果有其他需求可以自己改一下。

使用方法

  1. gr_offline.py 放到你的项目中
  2. 导入补丁并在使用gradio之前调用 patch 函数
  3. 下载离线的静态资源到 ./static

使用样例

python 复制代码
import gr_offline
import gradio as gr

# 必须在使用gradio前调用一次
gr_offline.patch()

# 这个自己根据需要修改
gr.set_static_paths("./static")

def greet(name, intensity):
    return "Hello, " + name + "!" * int(intensity)

demo = gr.Interface(
    fn=greet,
    inputs=["text", "slider"],
    outputs=["text"],
)

demo.launch()

资源替换

  • CDN

    旧: https://cdnjs.cloudflare.com/{}

    新: ./static/{}

  • Google Font

    旧: https://fonts.googleapis.com/css2?family={file_name}:wght@{weight}&display=swap

    新: ./static/fonts/{file_name}/{file_name}-{weight}.woff2

原始资源下载链接

  • https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js
  • https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap

gr_offline.py

python 复制代码
def _patch_env() -> None:
    import os

    os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"


def _patch_font() -> None:
    from gradio.themes.utils.fonts import GoogleFont, LocalFont

    GoogleFont.__init__ = LocalFont.__init__  # type: ignore
    GoogleFont.stylesheet = LocalFont.stylesheet  # type: ignore


def _patch_templates() -> None:
    import re
    import types
    import jinja2
    from gradio import routes
    from fastapi.templating import Jinja2Templates

    remove_regex: list[re.Pattern] = [
        re.compile(
            r'<meta\b[^>]*?property=["']og:[^"'>]*["'][^>]*?>',
            flags=re.IGNORECASE | re.DOTALL,
        ),
        re.compile(
            r'<meta\b[^>]*?name=["']twitter:[^"'>]*["'][^>]*?>',
            flags=re.IGNORECASE | re.DOTALL,
        ),
        re.compile(
            r'<link\b[^>]*?href=["']?[^"'>]*fonts.googleapis.com[^"'>]*["']?[^>]*?>',
            flags=re.IGNORECASE | re.DOTALL,
        ),
        re.compile(
            r'<link\b[^>]*?href=["']?[^"'>]*fonts.gstatic.com[^"'>]*["']?[^>]*?>',
            flags=re.IGNORECASE | re.DOTALL,
        ),
    ]
    cdn_regex: list[re.Pattern] = [re.compile(r"https?://cdnjs.cloudflare.com/.*?")]

    def _do_patch(html: str) -> str:
        for pattern in remove_regex:
            html = re.sub(pattern, "", html)
        for pattern in cdn_regex:
            html = re.sub(pattern, "static/", html)
        return html

    def _patched_render(self: jinja2.Template, *args, **kwargs) -> str:
        html = jinja2.Template.render(self, *args, **kwargs)
        return _do_patch(html)

    async def _patched_render_async(self: jinja2.Template, *args, **kwargs) -> str:
        html = await jinja2.Template.render_async(self, *args, **kwargs)
        return _do_patch(html)

    class PatchedJinja2Templates(Jinja2Templates):
        def get_template(self, name: str) -> jinja2.Template:
            template: jinja2.Template = super().get_template(name)
            template.render = types.MethodType(_patched_render, template)
            template.render_async = types.MethodType(_patched_render_async, template)
            return template

    routes.templates = PatchedJinja2Templates(directory=routes.STATIC_TEMPLATE_LIB)
    routes.templates.env.filters["toorjson"] = routes.toorjson


def patch() -> None:
    _patch_env()
    _patch_font()
    _patch_templates()

Github:Gist

相关推荐
luckys.one4 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
大翻哥哥5 小时前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融
zhousenshan7 小时前
Python爬虫常用框架
开发语言·爬虫·python
IMER SIMPLE7 小时前
人工智能-python-深度学习-经典神经网络AlexNet
人工智能·python·深度学习
CodeCraft Studio7 小时前
国产化Word处理组件Spire.DOC教程:使用 Python 将 Markdown 转换为 HTML 的详细教程
python·html·word·markdown·国产化·spire.doc·文档格式转换
aaaweiaaaaaa8 小时前
HTML和CSS学习
前端·css·学习·html
xcnn_8 小时前
前端入门——案例一:登录界面设计(html+css+js)
前端·css·html
专注API从业者8 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
java1234_小锋8 小时前
[免费]基于Python的协同过滤电影推荐系统(Django+Vue+sqlite+爬虫)【论文+源码+SQL脚本】
python·django·电影推荐系统·协同过滤
看海天一色听风起雨落9 小时前
Python学习之装饰器
开发语言·python·学习