Gradio搭建人工智能界面

发布于 2023-12-26  634 次阅读



Gradio好啊,好啊,好啊.Hugging Face好啊,好啊.

Gradio是一个开源Python包,允许您为机器学习模型、API或任何任意Python函数快速构建演示或web应用程序。然后,您可以使用Gradio的内置共享功能,在几秒钟内共享演示或web应用程序的链接。无需JavaScript、CSS或网络托管经验!

欢迎体验Grearn - a Hugging Face Space by proanimer

Hot reload

Developing Faster With Reload Mode (gradio.app)

gradio run.py

在使用重载模式时,Gradio专门在代码中寻找一个名为demo的Gradio Blocks/Interface演示。如果您将您的demo命名为其他名称,则需要将演示的名称作为代码中的第二个参数传入。所以,如果你的run.py文件是这样的:

import gradio as gr

with gr.Blocks() as my_demo:
    gr.Markdown("# Greetings from Gradio!")
    inp = gr.Textbox(placeholder="What is your name?")
    out = gr.Textbox()

    inp.change(fn=lambda x: f"Welcome, {x}!",
               inputs=inp,
               outputs=out)

if __name__ == "__main__":
    my_demo.launch()import gradio as gr

with gr.Blocks() as my_demo:
    gr.Markdown("# Greetings from Gradio!")
    inp = gr.Textbox(placeholder="What is your name?")
    out = gr.Textbox()

    inp.change(fn=lambda x: f"Welcome, {x}!",
               inputs=inp,
               outputs=out)

if __name__ == "__main__":
    my_demo.launch()

使用下面命令启动reload模式

 gradio run.py my_demo.

我发现开发时使用reload最好把launch放在name == "main"下Unable to launch with reload mode with default port · Issue #4755 · gradio-app/gradio (github.com)

launch参数

在reload模式下没用,开发完毕后可以使用,用于改变端口、获得公网地址用于分享等.

Interface Class

Interface类旨在为机器学习模型创建演示,这些模型接受一个或多个输入,并返回一个或更多输出.

Interface类有三个核心参数:

  • fn:包装用户界面(UI)的函数
  • inputs:用于输入的Gradio组件。组件的数量应与函数中的参数数量相匹配。
  • outputs:用于输出的Gradio组件。组件的数量应该与函数返回值的数量相匹配。
demo = gr.Interface(
    fn=greet,
    inputs=[gr.components.Textbox(placeholder="input your words"), gr.Textbox(placeholder=""),gr.components.Slider()],
    outputs=["text",gr.Checkbox(label="选择")],
)

可以多个输入,多个输出,输出由fn计算得到,但是貌似这只能构建一个单组件.

Blocks

Blocks是Gradio的低级API,它允许您创建比Interfaces更多的自定义web应用程序和演示(但仍然完全使用Python)。

与Interface类相比,Blocks提供了更多的灵活性和控制:

(1)组件的布局(

2)触发功能执行的事件

(3)数据流(例如,输入可以触发输出,这可以触发下一级的输出)

Blocks还提供了将相关演示分组在一起的方法,例如使用选项卡。块的基本用法如下:创建一个块对象,然后将其用作上下文(使用“with”语句),然后在块上下文中定义布局、组件或事件。最后,调用launch()方法来启动演示。

def update(name):
    return "Hello" + name + "!"

with gr.Blocks() as demo:
    gr.Markdown("## Hello World")
    with gr.Row():
        textbox = gr.Textbox(placeholder="input your words")
        slider = gr.components.Slider()
    btn = gr.Button("Run")
    btn.click(fn=update,input=textbox,output=slider)
demo.launch()
import gradio as gr

def increase(num):
    return num + 1

with gr.Blocks() as demo:
    a = gr.Number(label="a")
    b = gr.Number(label="b")
    atob = gr.Button("a > b")
    btoa = gr.Button("b > a")
    atob.click(increase, a, b)
    btoa.click(increase, b, a)

demo.launch()

TabbedInterface

TabbedInterface是通过提供一个接口列表来创建的,每个接口都在一个单独的选项卡中呈现。

def update(name):
    return "Hello" + name + "!"


with gr.Blocks(theme=gr.themes.Glass()) as test:
    gr.Markdown("## Hello World")
    with gr.Row():
        textbox = gr.Textbox(placeholder="input your words",label="name")
        slider = gr.components.Slider(label="Greet",interactive=True)
    btn = gr.Button("Run")
    btn.click(fn=update, inputs=textbox, outputs=slider)

stt_demo = gr.load(
    "huggingface/facebook/wav2vec2-base-960h",
    title=None,
    inputs="mic",
    description="Let me try to guess what you're saying!",
)

demo = gr.TabbedInterface([stt_demo,test],["STT","Hello World"])

if __name__ == '__main__':
    demo.launch()

ChatInterface

聊天机器人是大型语言模型的一个流行应用程序。使用gradio,您可以轻松地构建聊天机器人模型的演示并与用户共享,或者使用直观的聊天机器人UI自己尝试。

import random
import gradio as gr
def random_response(message, history):
     return random.choice(["Yes", "No"])

gr.ChatInterface(random_response).launch()

streaming

如果应用程序预计流量会很大,请使用queue()方法来控制处理速率。

可以搭配Openai或者Hugging Face上的大语言模型使用.同时搭配LangChain使用.

上面就是基本的四个大模块

资料

  1. Gradio Guides
届ける言葉を今は育ててる
最后更新于 2023-12-26