赞
踩
目录
序言:以用户界面为起点,探索底层大模型的学习之路
一、Gradio安装
二、Gradio 常用模块介绍
2.1 页面组件
2.2 功能组件
三、Hello World 认识Gradio程序
3.1 gr.Interface
3.2 gr.Blocks
四、Gradio的 gr.Blocks
4.1 Blocks的布局
五、Gadio 的AI demo案例
AGI程序开发及学习往往从枯燥无味的代码开始,很容易让人感到困倦,甚至放弃。但是,我们可以选择一条不同的路径。让我们从用户界面出发,直接进入AGI程序的学习之旅,逐步探索底层大模型的奥秘。
在这门课程中,我们将以用户视角为起点,聚焦于Gradio的应用。Gradio是一个强大的工具,可以帮助我们快速构建交互式应用程序。通过Gradio,我们可以直观地与底层的AGI模型进行交互,无需深入研究复杂的代码。
我们将引导学员逐步探索如何使用Gradio构建用户友好的界面,以及如何将这些界面与底层大模型进行集成。通过这一学习路线,学员将能够从用户的角度深入理解AGI程序的工作原理,并逐步掌握底层大模型的开发技巧。
让我们摒弃枯燥的代码,从用户界面出发,开启一段充满探索和创造的AGI程序学习之旅吧!
为什么选择gradio作为AGI的UI工具:
Gradio有什么案例吗?
看过我AI绘画教程的朋友知道AI绘画开源 stable diffusion 的UI 就是基于Gradio
相关教程:
楚门:mac mini 安装stable diffusion 保姆级教程1 赞同 · 6 评论文章
另外清华大学的ChatGLM2-6b的demo页面也是用的Gradio:
安装教程:
楚门:ChatGLM2-6B 阿里云部署保姆级教程5 赞同 · 3 评论文章
学习前置条件:
电脑一台(手动狗头)
开发环境:pycharm 或者jupter(建议新手)
python语言:也可以边学别看本教程,能看懂基本的语法就可以了
安装Gradio非常简单,您只需按照以下步骤进行操作:
pip install gradio
如果您在Mac系统上安装Gradio时遇到问题,请尝试使用pip3而不是pip。以下是针对Mac系统的Gradio安装命令:
pip3 install gradio
如果是jupter环境
等待片刻,pip会自动下载并安装Gradio及其所需的依赖项。
验证安装:安装完成后,您可以使用以下命令验证Gradio是否成功安装:
gradio --version
如果您看到Gradio的版本号信息,即表示安装成功。
至此,您已经成功安装了Gradio。现在您可以开始使用Gradio构建交互式应用程序了。
Gradio 提供两个类来构建应用程序UI:
Gradio的应用界面模块提供了不同的选择,根据开发者的需求和技术水平,可以选择使用gr.Interface进行简易场景的应用界面开发,或使用gr.Blocks进行更定制化的界面设计。
输入输出组件:
控制组件:
布局组件:
核心 Interface
类使用三个必需参数进行初始化:
fn
:将 UI 包裹起来的函数inputs
:用于输入的组件(例如 "text"
, "image"
或 "audio"
)outputs
:用于输出的组件(例如 "text"
, "image"
或 "label"
)下面是一个简单示例代码,演示如何使用Gradio创建一个基本的交互式应用程序:
- import gradio as gr
-
- # 定义一个问候函数,接收一个名字作为输入,返回一个带有问候语的文本输出
- def greet(name):
- return "你好," + name + "!"
-
- # 创建一个Gradio界面,将greet函数作为输入和输出函数传递给它
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
-
- # 启动Gradio应用程序
- iface.launch()
-
Blocks
使用with创建,在此 with
中创建的任何组件都会自动添加到应用程序中Button
,然后将 click
事件侦听器添加到此按钮与 Interface
一样,该方法 click
采用 Python 函数、输入组件和输出组件。以下是基于Blocks的示例:
- import gradio as gr
-
- def greet(name):
- return "你好 " + name + "!"
-
- with gr.Blocks() as demo:
- name = gr.Textbox(label="Name")
- output = gr.Textbox(label="Output Box")
- greet_btn = gr.Button("Greet")
- greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
-
-
- demo.launch()
从使用角度上我们主要介绍Blocks ,后续的AGI应用开发也主要使用Blocks
Rows行
with gr.Row
中的元素将全部水平显示。例如,并排显示两个按钮:
- with gr.Blocks() as demo:
- with gr.Row():
- btn1 = gr.Button("Button 1")
- btn2 = gr.Button("Button 2")
- demo.launch()
Columns列
with gr.Row 中的元素将全部垂直显示。例如:
with gr.Blocks() as demo2:
with gr.Column(scale=1, min_width=600):
text1 = gr.Textbox(label="prompt 1")
text2 = gr.Textbox(label="prompt 2")
inbtw = gr.Button("Between")
text4 = gr.Textbox(label="prompt 1")
text5 = gr.Textbox(label="prompt 2")
demo2.launch()
行列嵌套
- import gradio as gr
-
- with gr.Blocks() as demo:
- with gr.Row():
- text1 = gr.Textbox(label="t1")
- slider2 = gr.Textbox(label="s2")
- drop3 = gr.Dropdown(["a", "b", "c"], label="d3")
- with gr.Row():
- with gr.Column(scale=1, min_width=600):
- text1 = gr.Textbox(label="prompt 1")
- text2 = gr.Textbox(label="prompt 2")
- inbtw = gr.Button("Between")
- text4 = gr.Textbox(label="prompt 1")
- text5 = gr.Textbox(label="prompt 2")
- with gr.Column(scale=2, min_width=600):
- #img1 = gr.Image("images/cheetah.jpg")
- btn = gr.Button("Go").style(full_width=True)
-
- demo.launch()

多标签
- import numpy as np
- import gradio as gr
-
- # 导入必要的库
-
- def flip_text(x):
- return x[::-1]
-
- # 定义翻转文本函数
-
- def flip_image(x):
- return np.fliplr(x)
-
- # 定义翻转图片函数,使用了NumPy中的fliplr函数
-
- with gr.Blocks() as demo:
- gr.Markdown("Flip text or image files using this demo.")
-
- # 创建一个包含Markdown说明的示例块
-
- with gr.Tab("Flip Text"):
- text_input = gr.Textbox()
- text_output = gr.Textbox()
- text_button = gr.Button("Flip")
-
- # 创建一个用于翻转文本的选项卡,包含一个文本输入框、一个文本输出框和一个按钮
-
- with gr.Tab("Flip Image"):
- with gr.Row():
- image_input = gr.Image()
- image_output = gr.Image()
- image_button = gr.Button("Flip")
-
- # 创建一个用于翻转图片的选项卡,包含一个图片输入框、一个图片输出框和一个按钮
-
- with gr.Accordion("Open for More!"):
- gr.Markdown("Look at me...")
-
- # 创建一个手风琴部分,包含一个Markdown文本
-
- text_button.click(flip_text, inputs=text_input, outputs=text_output)
- image_button.click(flip_image, inputs=image_input, outputs=image_output)
-
- # 为按钮添加点击事件,点击时调用相应的翻转函数
-
- demo.launch()
-
- # 启动Gradio演示

所有的案例demo必须是智能的!呼应开头实操案例会直接结合 Langchain 进行AI demo的学习开发。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。