0


Gradio快速搭建机器学习模型的wedui展示用户界面/深度学习网页模型部署

Gradio 快速开始

官网

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

在这里插入图片描述
只需几行 Python 代码就可以创建一个像上面这样的漂亮演示,所以让我们开始吧 💫

Installation 安装

先决条件:Gradio 需要 Python 3.8 or higher

我们建议使用

pip

安装 Gradio,它默认包含在 Python 中。在终端或命令提示符中运行此命令:

✍️提示:最好在虚拟环境中安装Gradio。此处提供了所有常见操作系统的详细安装说明。

Building Your First Demo

构建您的第一个演示

您可以在您最喜欢的代码编辑器、Jupyter 笔记本、Google Colab 或您编写 Python 的任何其他地方运行 Gradio。让我们编写您的第一个 Gradio 应用程序:

# 导入 Gradio 库,它是一个用于构建机器学习模型演示的开源库。
import gradio as gr

# 定义一个函数 greet,它接受两个参数:name(名字)和 intensity(强度)。
# 这个函数返回一个字符串,内容是重复“Hello, ”和name的“!”,重复次数由intensity决定。
def greet(name, intensity):
    return "Hello, " + name + "!" * int(intensity)

# 创建一个 Gradio 接口对象,它将使用 greet 函数。
# 接受两个输入:一个文本输入(名字)和一个滑动条输入(强度)。
# 输出是一个文本。
demo = gr.Interface(
    fn=greet,  # 指定要使用的函数
    inputs=["text", "slider"],  # 输入类型:文本和滑动条
    outputs=["text"],  # 输出类型:文本
)

# 启动 Gradio 接口,允许用户通过网页界面与 greet 函数交互。
demo.launch()

✍️提示:我们将导入的名称从

gradio

缩短为

gr

,以提高代码的可读性。这是一个广泛采用的约定,您应该遵循它,以便使用您的代码的任何人都可以轻松理解它。

✍️提示:如果第一次生成本地网址后打开显示空白,可以尝试一下科学上网

现在,运行您的代码。例如,如果您将 Python 代码编写在名为

app.py

的文件中,那么您可以从终端运行

python app.py

如果从文件运行,下面的演示将在浏览器中打开 http://localhost:7860。如果您在笔记本中运行,演示将嵌入笔记本中。
在这里插入图片描述

在左侧的文本框中输入您的姓名,拖动滑块,然后按“提交”按钮。您应该在右侧看到一条友好的问候语。

✍️ Tip: When developing locally, you can run your Gradio app in hot reload mode, which automatically reloads the Gradio app whenever you make changes to the file. To do this, simply type in

gradio

before the name of the file instead of

python

. In the example above, you would type:

gradio app.py

in your terminal. Learn more about hot reloading in the .
✍️提示:在本地开发时,您可以在热重载模式下运行 Gradio 应用程序,只要您对文件进行更改,该模式就会自动重新加载 Gradio 应用程序。为此,只需在文件名前输入

gradio

而不是

python

。在上面的示例中,您可以在终端中输入:

gradio app.py

。在Hot Reloading Guide中了解更多信息。

**Understanding the

Interface

Class
了解

Interface

类**

You’ll notice that in order to make your first demo, you created an instance of the

gr.Interface

class. The

Interface

class is designed to create demos for machine learning models which accept one or more inputs, and return one or more outputs.
您会注意到,为了制作第一个演示,您创建了

gr.Interface

类的实例。

Interface

类旨在为机器学习模型创建演示,该模型接受一个或多个输入并返回一个或多个输出。

The

Interface

class has three core arguments:

Interface

类具有三个核心参数:

  • fn: the function to wrap a user interface (UI) aroundfn :包装用户界面(UI)的函数
  • inputs: the Gradio component(s) to use for the input. The number of components should match the number of arguments in your function.inputs :用于输入的渐变组件。组件的数量应与函数中参数的数量相匹配。
  • outputs: the Gradio component(s) to use for the output. The number of components should match the number of return values from your function.outputs :用于输出的渐变组件。组件的数量应与函数返回值的数量相匹配。

The

fn

argument is very flexible — you can pass any Python function that you want to wrap with a UI. In the example above, we saw a relatively simple function, but the function could be anything from a music generator to a tax calculator to the prediction function of a pretrained machine learning model.

fn

参数非常灵活——您可以传递任何您想要用 UI 包装的 Python 函数。在上面的示例中,我们看到了一个相对简单的函数,但该函数可以是从音乐生成器到税收计算器再到预训练机器学习模型的预测函数的任何函数。

The

input

and

output

arguments take one or more Gradio components. As we’ll see, Gradio includes more than 30 built-in components (such as the

gr.Textbox()

,

gr.Image()

, and

gr.HTML()

components) that are designed for machine learning applications.

input

output

参数采用一个或多个 Gradio 组件。正如我们将看到的,Gradio 包含 30 多个专为机器设计的内置组件(例如

gr.Textbox()

gr.Image()

gr.HTML()

组件)学习应用程序。

✍️ Tip: For the

inputs

and

outputs

arguments, you can pass in the name of these components as a string (

"textbox"

) or an instance of the class (

gr.Textbox()

).
✍️提示:对于

inputs

outputs

参数,您可以将这些组件的名称作为字符串 (

"textbox"

) 或类的实例 (

gr.Textbox()

)。

If your function accepts more than one argument, as is the case above, pass a list of input components to

inputs

, with each input component corresponding to one of the arguments of the function, in order. The same holds true if your function returns more than one value: simply pass in a list of components to

outputs

. This flexibility makes the

Interface

class a very powerful way to create demos.
如果您的函数接受多个参数(如上面的情况),请将输入组件列表传递给

inputs

,每个输入组件按顺序对应于函数的参数之一。如果您的函数返回多个值,同样如此:只需将组件列表传递给

outputs

即可。这种灵活性使得

Interface

类成为创建演示的非常强大的方式。

We’ll dive deeper into the

gr.Interface

on our series on building Interfaces.
我们将在构建界面系列中更深入地探讨

gr.Interface

Sharing Your Demo 分享您的演示

What good is a beautiful demo if you can’t share it? Gradio lets you easily share a machine learning demo without having to worry about the hassle of hosting on a web server. Simply set

share=True

in

launch()

, and a publicly accessible URL will be created for your demo. Let’s revisit our example demo, but change the last line as follows:
如果不能分享,再漂亮的演示又有什么用呢? Gradio 可让您轻松共享机器学习演示,而无需担心托管在 Web 服务器上的麻烦。只需在

launch()

中设置

share=True

,将为您的演示创建一个可公开访问的 URL。让我们重新审视我们的示例演示,但将最后一行更改如下:

# 导入 Gradio 库,它是一个用于构建机器学习模型演示的开源库。
import gradio as gr

# 定义一个函数 greet,它接受一个参数:name(名字)。
# 这个函数返回一个字符串,内容是“Hello”加上传入的名字。
def greet(name):
    return "Hello " + name + "!"

# 创建一个 Gradio 接口对象,它将使用 greet 函数。
# 接受一个文本输入(名字)。
# 输出是一个文本。
demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")

# 启动 Gradio 接口,允许用户通过网页界面与 greet 函数交互。
# share=True 参数允许生成的链接被其他人访问。
demo.launch(share=True)  # Share your demo with just 1 extra parameter 🚀

When you run this code, a public URL will be generated for your demo in a matter of seconds, something like:
当您运行此代码时,将在几秒钟内为您的演示生成一个公共 URL,如下所示:

👉

https://a23dsf231adb.gradio.live

Now, anyone around the world can try your Gradio demo from their browser, while the machine learning model and all computation continues to run locally on your computer.
现在,世界各地的任何人都可以通过浏览器尝试您的 Gradio 演示,而机器学习模型和所有计算将继续在您的计算机上本地运行。

To learn more about sharing your demo, read our dedicated guide on sharing your Gradio application.
要了解有关共享演示的更多信息,请阅读我们有关共享 Gradio 应用程序的专用指南。


本文转载自: https://blog.csdn.net/m0_52987303/article/details/136506340
版权归原作者 天天写点代码 所有, 如有侵权,请联系我们删除。

“Gradio快速搭建机器学习模型的wedui展示用户界面/深度学习网页模型部署”的评论:

还没有评论