Skip to content
/CreatorBox/images/jms_logo.png

交互式应用 📤

字数
959 字
阅读时间
5 分钟

Gradio 是一个用于快速搭建机器学习模型、脚本或工具交互界面的开源库,支持网页展示、分享链接、组件拖拽等功能,非常适合用来做可视化演示或验证。

在这里,您可以自由定制开发与集成,灵活适配自身需求场景,打造个性化应用;

应用列表

gradio_apps

提示

  • 按需使用,开启成功后,需要重启

插件开发

1. 定义应用

python
...
from src.app.prefs import INSTANCES as prefs
from src.gradio.tools import helper
from src.gradio.tools.toast import Toast

def gradio(i18n=None, **kwargs):

    # 配置
    cfg = prefs.get()

    # 界面
    with gr.Blocks(**kwargs) as app:

        helper.header_description("YouTube 在线视频下载")

        input_url = gr.Textbox(label="链接", placeholder="粘贴链接")

        with gr.Accordion("资源", open=True):
            title_txt = gr.Textbox(label="标题", lines=1, interactive=False)
            with gr.Tabs() as tabs:
                with gr.TabItem("封面"):
                    thumb_image = gr.Image(label="预览", height=300)
                with gr.TabItem("视频"):
                    video_table = gr.Dataframe(label="列表", headers=["ID", "格式", "分辨率", "大小", "音频"], interactive=True, elem_id="output-table")
                    video_fmt_input = gr.Textbox(label="ID")
                    btn_v_download = gr.Button("下载")
                    video_file = gr.Video(label="预览", height=300)
                with gr.TabItem("音频"):
                    audio_table = gr.Dataframe(label="列表", headers=["ID", "格式", "码率", "大小"], interactive=True, elem_id="output-table")
                    audio_fmt_input = gr.Textbox(label="ID")
                    btn_a_download = gr.Button("下载")
                    audio_file = gr.Audio(label="预览", type="filepath")

        with gr.Row():
            parse_btn = gr.Button("解析", variant="primary")

        with gr.Accordion("示例", open=True):
            gr.Examples(
                label="链接",
                examples=[
                    ["https://www.youtube.com/watch?v=SMWdVHNByhk"],
                ],
                inputs=[input_url],
                cache_examples=False,
            )

        # 解析
        @gr.on(parse_btn.click, inputs=input_url, outputs=[thumb_image, title_txt, video_table, audio_table])
        def on_parse(url):
            try:
                if not url:
                    raise Toast.warn("请输入链接")
                thumb, title, v_formats, a_formats = parse_video(url)
                return thumb, title, v_formats, a_formats
            except Exception as e:
                return None, None, [], []

        # 切换标签页
        @gr.on(tabs.change, outputs=[video_fmt_input, audio_fmt_input])
        def on_tab_change(active_tab):
            return "", ""

        # 表格行选择
        # 视频
        @gr.on(video_table.select, outputs=video_fmt_input)
        def on_video_select(evt: gr.SelectData):
            selected_row = evt.row_value
            return selected_row[0]

        # 音频
        @gr.on(audio_table.select, outputs=audio_fmt_input)
        def on_audio_select(evt: gr.SelectData):
            selected_row = evt.row_value
            return selected_row[0]

        # 视频下载
        # 视频
        @gr.on(btn_v_download.click, inputs=[input_url, video_fmt_input], outputs=video_file)
        def on_download_video(url, fmt_id):
            if not url:
                return Toast.warn("请输入链接")
            if not fmt_id:
                return Toast.warn("请选择ID")
            # 输出目录
            output_dir = os.path.join(cfg.gradio.output_dir, DLP_DIR)
            cbfile.mk_folder(output_dir, is_clean=cfg.gradio.debug)
            # 下载
            filepath = download_by_format(url, output_dir, fmt_id)
            return cbfile.move_to_temp(filepath) if filepath else None

        # 音频
        @gr.on(btn_a_download.click, inputs=[input_url, audio_fmt_input], outputs=audio_file)
        def on_download_audio(url, fmt_id):
            if not url:
                return Toast.warn("请输入链接")
            if not fmt_id:
                return Toast.warn("请选择ID")
            # 输出目录
            output_dir = os.path.join(cfg.gradio.output_dir, DLP_DIR)
            cbfile.mk_folder(output_dir, is_clean=cfg.gradio.debug)
            # 下载
            filepath = download_by_format(url, output_dir, fmt_id)
            return cbfile.move_to_temp(filepath) if filepath else None

    return app

if __name__ == "__main__":
    gradio().launch(server_port=8001)
python
import gradio as gr

###################### 主题自定义 ######################
theme = gr.themes.Ocean(
    text_size="sm",
)
###################### 主题自定义 ######################

def _default_theme():
    return theme

if __name__ == "__main__":
    # 1、run `python src\gradio\theme.py` to open the theme builder
    # 2、or see https://huggingface.co/spaces/gradio/theme_builder
    from gradio import themes as t
    t.builder()

2. 注册应用

bash
curl -X 'POST' \
  'http://172.18.0.1:8000/gr/mounts' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
      "name": "dlp",                                      # 名称
      "description": "YouTube Downloader Plus (yt-dlp)",  # 描述
      "module": "src.gradio.pages.dlp_",                  # 路径
      "attr": "gradio",                                   # 方法
      "path": "/dlp",                                     # 请求
      "status": 1                                         # 状态
  }'

3. 打开应用

封面视频音频
gradio_settings_basegradio_settings_capcutgradio_settings_masker

更多场景

1. 批量运行

批量化去操作?

待补充

2. 智能体嵌入

如何对接第三方的AI工作流

待补充

持续更新

专属插件

设计、开发和集成基于 CreatorBox 的专用插件,以扩展现有软件功能

如果你需要插件定制,请在 插件开发&集成 留言