官網(wǎng)
Gradio 是一個(gè)開(kāi)源 Python 包,可讓您快速為機(jī)器學(xué)習(xí)模型、API 或任何任意 Python 函數(shù)構(gòu)建演示或 Web 應(yīng)用程序。然后,您可以使用 Gradio 的內(nèi)置共享功能在幾秒鐘內(nèi)共享演示或 Web 應(yīng)用程序的鏈接。無(wú)需 JavaScript、CSS 或網(wǎng)絡(luò)托管經(jīng)驗(yàn)!
只需幾行 Python 代碼就可以創(chuàng)建一個(gè)像上面這樣的漂亮演示,所以讓我們開(kāi)始吧 ??
Installation 安裝
先決條件:Gradio 需要 Python 3.8 or higher
我們建議使用 pip
安裝 Gradio,它默認(rèn)包含在 Python 中。在終端或命令提示符中運(yùn)行此命令:
??提示:最好在虛擬環(huán)境中安裝Gradio。此處提供了所有常見(jiàn)操作系統(tǒng)的詳細(xì)安裝說(shuō)明。
Building Your First Demo
構(gòu)建您的第一個(gè)演示
您可以在您最喜歡的代碼編輯器、Jupyter 筆記本、Google Colab 或您編寫(xiě) Python 的任何其他地方運(yùn)行 Gradio。讓我們編寫(xiě)您的第一個(gè) Gradio 應(yīng)用程序:
# 導(dǎo)入 Gradio 庫(kù),它是一個(gè)用于構(gòu)建機(jī)器學(xué)習(xí)模型演示的開(kāi)源庫(kù)。
import gradio as gr
# 定義一個(gè)函數(shù) greet,它接受兩個(gè)參數(shù):name(名字)和 intensity(強(qiáng)度)。
# 這個(gè)函數(shù)返回一個(gè)字符串,內(nèi)容是重復(fù)“Hello, ”和name的“!”,重復(fù)次數(shù)由intensity決定。
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
# 創(chuàng)建一個(gè) Gradio 接口對(duì)象,它將使用 greet 函數(shù)。
# 接受兩個(gè)輸入:一個(gè)文本輸入(名字)和一個(gè)滑動(dòng)條輸入(強(qiáng)度)。
# 輸出是一個(gè)文本。
demo = gr.Interface(
fn=greet, # 指定要使用的函數(shù)
inputs=["text", "slider"], # 輸入類(lèi)型:文本和滑動(dòng)條
outputs=["text"], # 輸出類(lèi)型:文本
)
# 啟動(dòng) Gradio 接口,允許用戶通過(guò)網(wǎng)頁(yè)界面與 greet 函數(shù)交互。
demo.launch()
??提示:我們將導(dǎo)入的名稱(chēng)從 gradio
縮短為 gr
,以提高代碼的可讀性。這是一個(gè)廣泛采用的約定,您應(yīng)該遵循它,以便使用您的代碼的任何人都可以輕松理解它。
??提示:如果第一次生成本地網(wǎng)址后打開(kāi)顯示空白,可以嘗試一下科學(xué)上網(wǎng)
現(xiàn)在,運(yùn)行您的代碼。例如,如果您將 Python 代碼編寫(xiě)在名為 app.py
的文件中,那么您可以從終端運(yùn)行 python app.py
。
如果從文件運(yùn)行,下面的演示將在瀏覽器中打開(kāi) http://localhost:7860。如果您在筆記本中運(yùn)行,演示將嵌入筆記本中。
在左側(cè)的文本框中輸入您的姓名,拖動(dòng)滑塊,然后按“提交”按鈕。您應(yīng)該在右側(cè)看到一條友好的問(wèn)候語(yǔ)。
?? 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 .
??提示:在本地開(kāi)發(fā)時(shí),您可以在熱重載模式下運(yùn)行 Gradio 應(yīng)用程序,只要您對(duì)文件進(jìn)行更改,該模式就會(huì)自動(dòng)重新加載 Gradio 應(yīng)用程序。為此,只需在文件名前輸入 gradio
而不是 python
。在上面的示例中,您可以在終端中輸入: gradio app.py
。在Hot Reloading Guide中了解更多信息。
Understanding the Interface
Class
了解 Interface
類(lèi)
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.
您會(huì)注意到,為了制作第一個(gè)演示,您創(chuàng)建了 gr.Interface
類(lèi)的實(shí)例。 Interface
類(lèi)旨在為機(jī)器學(xué)習(xí)模型創(chuàng)建演示,該模型接受一個(gè)或多個(gè)輸入并返回一個(gè)或多個(gè)輸出。
The Interface
class has three core arguments:Interface
類(lèi)具有三個(gè)核心參數(shù):
-
fn
: the function to wrap a user interface (UI) aroundfn
:包裝用戶界面(UI)的函數(shù) -
inputs
: the Gradio component(s) to use for the input. The number of components should match the number of arguments in your function.inputs
:用于輸入的漸變組件。組件的數(shù)量應(yīng)與函數(shù)中參數(shù)的數(shù)量相匹配。 -
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
:用于輸出的漸變組件。組件的數(shù)量應(yīng)與函數(shù)返回值的數(shù)量相匹配。
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
參數(shù)非常靈活——您可以傳遞任何您想要用 UI 包裝的 Python 函數(shù)。在上面的示例中,我們看到了一個(gè)相對(duì)簡(jiǎn)單的函數(shù),但該函數(shù)可以是從音樂(lè)生成器到稅收計(jì)算器再到預(yù)訓(xùn)練機(jī)器學(xué)習(xí)模型的預(yù)測(cè)函數(shù)的任何函數(shù)。
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
參數(shù)采用一個(gè)或多個(gè) Gradio 組件。正如我們將看到的,Gradio 包含 30 多個(gè)專(zhuān)為機(jī)器設(shè)計(jì)的內(nèi)置組件(例如 gr.Textbox()
、 gr.Image()
和 gr.HTML()
組件)學(xué)習(xí)應(yīng)用程序。
?? 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()
).
??提示:對(duì)于 inputs
和 outputs
參數(shù),您可以將這些組件的名稱(chēng)作為字符串 ( "textbox"
) 或類(lèi)的實(shí)例 ( 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.
如果您的函數(shù)接受多個(gè)參數(shù)(如上面的情況),請(qǐng)將輸入組件列表傳遞給 inputs
,每個(gè)輸入組件按順序?qū)?yīng)于函數(shù)的參數(shù)之一。如果您的函數(shù)返回多個(gè)值,同樣如此:只需將組件列表傳遞給 outputs
即可。這種靈活性使得 Interface
類(lèi)成為創(chuàng)建演示的非常強(qiáng)大的方式。
We’ll dive deeper into the gr.Interface
on our series on building Interfaces.
我們將在構(gòu)建界面系列中更深入地探討 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 可讓您輕松共享機(jī)器學(xué)習(xí)演示,而無(wú)需擔(dān)心托管在 Web 服務(wù)器上的麻煩。只需在 launch()
中設(shè)置 share=True
,將為您的演示創(chuàng)建一個(gè)可公開(kāi)訪問(wèn)的 URL。讓我們重新審視我們的示例演示,但將最后一行更改如下:
# 導(dǎo)入 Gradio 庫(kù),它是一個(gè)用于構(gòu)建機(jī)器學(xué)習(xí)模型演示的開(kāi)源庫(kù)。
import gradio as gr
# 定義一個(gè)函數(shù) greet,它接受一個(gè)參數(shù):name(名字)。
# 這個(gè)函數(shù)返回一個(gè)字符串,內(nèi)容是“Hello”加上傳入的名字。
def greet(name):
return "Hello " + name + "!"
# 創(chuàng)建一個(gè) Gradio 接口對(duì)象,它將使用 greet 函數(shù)。
# 接受一個(gè)文本輸入(名字)。
# 輸出是一個(gè)文本。
demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
# 啟動(dòng) Gradio 接口,允許用戶通過(guò)網(wǎng)頁(yè)界面與 greet 函數(shù)交互。
# share=True 參數(shù)允許生成的鏈接被其他人訪問(wèn)。
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:
當(dāng)您運(yùn)行此代碼時(shí),將在幾秒鐘內(nèi)為您的演示生成一個(gè)公共 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.
現(xiàn)在,世界各地的任何人都可以通過(guò)瀏覽器嘗試您的 Gradio 演示,而機(jī)器學(xué)習(xí)模型和所有計(jì)算將繼續(xù)在您的計(jì)算機(jī)上本地運(yùn)行。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-855908.html
To learn more about sharing your demo, read our dedicated guide on sharing your Gradio application.
要了解有關(guān)共享演示的更多信息,請(qǐng)閱讀我們有關(guān)共享 Gradio 應(yīng)用程序的專(zhuān)用指南。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-855908.html
到了這里,關(guān)于Gradio快速搭建機(jī)器學(xué)習(xí)模型的wedui展示用戶界面/深度學(xué)習(xí)網(wǎng)頁(yè)模型部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!