缘起:MindSearch
python frontend/mindsearch_gradio.py
https://github.com/InternLM/MindSearch
https://github.com/InternLM/MindSearch/tree/main/frontend
https://github.com/InternLM/MindSearch/blob/main/frontend/mindsearch_gradio.py
概述 : Gradio
安装 Gradio
- 前提条件
- Gradio 需要 Python 3.8 或更高版本
我们建议使用
pip
安装Gradio
,它默认包含在 Python 中。在终端或命令提示符下运行它:
pip install gradio
提示:最好在虚拟环境中安装 Gradio。
这里提供了所有常见操作系统的详细安装说明。https://www.gradio.app/main/guides/installing-gradio-in-a-virtual-environment
运行日志
(base) C:\Users\xxx>pip install gradio
Defaulting to user installation because normal site-packages is not writeable
Collecting gradioUsing cached gradio-5.9.1-py3-none-any.whl.metadata (16 kB)
Collecting aiofiles<24.0,>=22.0 (from gradio)Using cached aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)
Requirement already satisfied: anyio<5.0,>=3.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (4.7.0)
Collecting fastapi<1.0,>=0.115.2 (from gradio)Using cached fastapi-0.115.6-py3-none-any.whl.metadata (27 kB)
Collecting ffmpy (from gradio)Using cached ffmpy-0.5.0-py3-none-any.whl.metadata (3.0 kB)
Collecting gradio-client==1.5.2 (from gradio)Using cached gradio_client-1.5.2-py3-none-any.whl.metadata (7.1 kB)
Requirement already satisfied: httpx>=0.24.1 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (0.28.1)
Collecting huggingface-hub>=0.25.1 (from gradio)Using cached huggingface_hub-0.27.0-py3-none-any.whl.metadata (13 kB)
Collecting jinja2<4.0 (from gradio)Using cached jinja2-3.1.5-py3-none-any.whl.metadata (2.6 kB)
Collecting markupsafe~=2.0 (from gradio)Using cached MarkupSafe-2.1.5-cp312-cp312-win_amd64.whl.metadata (3.1 kB)
Requirement already satisfied: numpy<3.0,>=1.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (2.2.1)
Requirement already satisfied: orjson~=3.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (3.10.12)
Requirement already satisfied: packaging in d:\program\miniforge3\lib\site-packages (from gradio) (24.2)
Collecting pandas<3.0,>=1.0 (from gradio)Using cached pandas-2.2.3-cp312-cp312-win_amd64.whl.metadata (19 kB)
Collecting pillow<12.0,>=8.0 (from gradio)Using cached pillow-11.1.0-cp312-cp312-win_amd64.whl.metadata (9.3 kB)
Requirement already satisfied: pydantic>=2.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (2.10.4)
Collecting pydub (from gradio)Using cached pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)
Collecting python-multipart>=0.0.18 (from gradio)Using cached python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)
Requirement already satisfied: pyyaml<7.0,>=5.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (6.0.2)
Collecting ruff>=0.2.2 (from gradio)Using cached ruff-0.8.5-py3-none-win_amd64.whl.metadata (25 kB)
Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)Using cached safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)
Collecting semantic-version~=2.0 (from gradio)Using cached semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)
Collecting starlette<1.0,>=0.40.0 (from gradio)Using cached starlette-0.45.1-py3-none-any.whl.metadata (6.3 kB)
Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)Using cached tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)
Collecting typer<1.0,>=0.12 (from gradio)Using cached typer-0.15.1-py3-none-any.whl.metadata (15 kB)
Requirement already satisfied: typing-extensions~=4.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from gradio) (4.12.2)
Collecting uvicorn>=0.14.0 (from gradio)Using cached uvicorn-0.34.0-py3-none-any.whl.metadata (6.5 kB)
Collecting fsspec (from gradio-client==1.5.2->gradio)Using cached fsspec-2024.12.0-py3-none-any.whl.metadata (11 kB)
Collecting websockets<15.0,>=10.0 (from gradio-client==1.5.2->gradio)Using cached websockets-14.1-cp312-cp312-win_amd64.whl.metadata (6.9 kB)
Requirement already satisfied: idna>=2.8 in d:\program\miniforge3\lib\site-packages (from anyio<5.0,>=3.0->gradio) (3.10)
Requirement already satisfied: sniffio>=1.1 in c:\users\edy\appdata\roaming\python\python312\site-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)
Collecting starlette<1.0,>=0.40.0 (from gradio)Using cached starlette-0.41.3-py3-none-any.whl.metadata (6.0 kB)
Requirement already satisfied: certifi in d:\program\miniforge3\lib\site-packages (from httpx>=0.24.1->gradio) (2024.8.30)
Requirement already satisfied: httpcore==1.* in c:\users\edy\appdata\roaming\python\python312\site-packages (from httpx>=0.24.1->gradio) (1.0.7)
Requirement already satisfied: h11<0.15,>=0.13 in c:\users\edy\appdata\roaming\python\python312\site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)
Collecting filelock (from huggingface-hub>=0.25.1->gradio)Using cached filelock-3.16.1-py3-none-any.whl.metadata (2.9 kB)
Requirement already satisfied: requests in d:\program\miniforge3\lib\site-packages (from huggingface-hub>=0.25.1->gradio) (2.32.3)
Requirement already satisfied: tqdm>=4.42.1 in d:\program\miniforge3\lib\site-packages (from huggingface-hub>=0.25.1->gradio) (4.67.1)
Collecting python-dateutil>=2.8.2 (from pandas<3.0,>=1.0->gradio)Using cached python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB)
Collecting pytz>=2020.1 (from pandas<3.0,>=1.0->gradio)Using cached pytz-2024.2-py2.py3-none-any.whl.metadata (22 kB)
Collecting tzdata>=2022.7 (from pandas<3.0,>=1.0->gradio)Using cached tzdata-2024.2-py2.py3-none-any.whl.metadata (1.4 kB)
Requirement already satisfied: annotated-types>=0.6.0 in c:\users\edy\appdata\roaming\python\python312\site-packages (from pydantic>=2.0->gradio) (0.7.0)
Requirement already satisfied: pydantic-core==2.27.2 in c:\users\edy\appdata\roaming\python\python312\site-packages (from pydantic>=2.0->gradio) (2.27.2)
Collecting click>=8.0.0 (from typer<1.0,>=0.12->gradio)Using cached click-8.1.8-py3-none-any.whl.metadata (2.3 kB)
Collecting shellingham>=1.3.0 (from typer<1.0,>=0.12->gradio)Using cached shellingham-1.5.4-py2.py3-none-any.whl.metadata (3.5 kB)
Collecting rich>=10.11.0 (from typer<1.0,>=0.12->gradio)Using cached rich-13.9.4-py3-none-any.whl.metadata (18 kB)
Requirement already satisfied: colorama in d:\program\miniforge3\lib\site-packages (from click>=8.0.0->typer<1.0,>=0.12->gradio) (0.4.6)
Collecting six>=1.5 (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio)Using cached six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Collecting markdown-it-py>=2.2.0 (from rich>=10.11.0->typer<1.0,>=0.12->gradio)Using cached markdown_it_py-3.0.0-py3-none-any.whl.metadata (6.9 kB)
Collecting pygments<3.0.0,>=2.13.0 (from rich>=10.11.0->typer<1.0,>=0.12->gradio)Using cached pygments-2.18.0-py3-none-any.whl.metadata (2.5 kB)
Requirement already satisfied: charset_normalizer<4,>=2 in d:\program\miniforge3\lib\site-packages (from requests->huggingface-hub>=0.25.1->gradio) (3.4.0)
Requirement already satisfied: urllib3<3,>=1.21.1 in d:\program\miniforge3\lib\site-packages (from requests->huggingface-hub>=0.25.1->gradio) (2.2.3)
Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio)Using cached mdurl-0.1.2-py3-none-any.whl.metadata (1.6 kB)
Downloading gradio-5.9.1-py3-none-any.whl (57.2 MB)---------------------------------------- 57.2/57.2 MB 110.6 kB/s eta 0:00:00
Downloading gradio_client-1.5.2-py3-none-any.whl (320 kB)
Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)
Downloading fastapi-0.115.6-py3-none-any.whl (94 kB)
Downloading huggingface_hub-0.27.0-py3-none-any.whl (450 kB)
Downloading jinja2-3.1.5-py3-none-any.whl (134 kB)
Downloading MarkupSafe-2.1.5-cp312-cp312-win_amd64.whl (17 kB)
Downloading pandas-2.2.3-cp312-cp312-win_amd64.whl (11.5 MB)---------------------------------------- 11.5/11.5 MB 246.0 kB/s eta 0:00:00
Downloading pillow-11.1.0-cp312-cp312-win_amd64.whl (2.6 MB)---------------------------------------- 2.6/2.6 MB 87.8 kB/s eta 0:00:00
Downloading python_multipart-0.0.20-py3-none-any.whl (24 kB)
Downloading ruff-0.8.5-py3-none-win_amd64.whl (9.7 MB)---------------------------------------- 9.7/9.7 MB 131.8 kB/s eta 0:00:00
Downloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)
Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)
Downloading starlette-0.41.3-py3-none-any.whl (73 kB)
Downloading tomlkit-0.13.2-py3-none-any.whl (37 kB)
Downloading typer-0.15.1-py3-none-any.whl (44 kB)
Downloading uvicorn-0.34.0-py3-none-any.whl (62 kB)
Downloading ffmpy-0.5.0-py3-none-any.whl (6.0 kB)
Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)
Downloading click-8.1.8-py3-none-any.whl (98 kB)
Downloading fsspec-2024.12.0-py3-none-any.whl (183 kB)
Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
Downloading pytz-2024.2-py2.py3-none-any.whl (508 kB)
Downloading rich-13.9.4-py3-none-any.whl (242 kB)
Downloading shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB)
Downloading tzdata-2024.2-py2.py3-none-any.whl (346 kB)
Downloading websockets-14.1-cp312-cp312-win_amd64.whl (163 kB)
Downloading filelock-3.16.1-py3-none-any.whl (16 kB)
Downloading markdown_it_py-3.0.0-py3-none-any.whl (87 kB)
Downloading pygments-2.18.0-py3-none-any.whl (1.2 MB)---------------------------------------- 1.2/1.2 MB 106.9 kB/s eta 0:00:00
Downloading six-1.17.0-py2.py3-none-any.whl (11 kB)
Downloading mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Installing collected packages: pytz, pydub, websockets, tzdata, tomlkit, six, shellingham, semantic-version, ruff, python-multipart, pygments, pillow, mdurl, markupsafe, fsspec, filelock, ffmpy, click, aiofiles, uvicorn, starlette, python-dateutil, markdown-it-py, jinja2, huggingface-hub, safehttpx, rich, pandas, gradio-client, fastapi, typer, gradioWARNING: The script pygmentize.exe is installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.WARNING: The script uvicorn.exe is installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.WARNING: The script markdown-it.exe is installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.WARNING: The script huggingface-cli.exe is installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.WARNING: The script fastapi.exe is installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.WARNING: The script typer.exe is installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.WARNING: The scripts gradio.exe and upload_theme.exe are installed in 'C:\Users\EDY\AppData\Roaming\Python\Python312\Scripts' which is not on PATH.Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed aiofiles-23.2.1 click-8.1.8 fastapi-0.115.6 ffmpy-0.5.0 filelock-3.16.1 fsspec-2024.12.0 gradio-5.9.1 gradio-client-1.5.2 huggingface-hub-0.27.0 jinja2-3.1.5 markdown-it-py-3.0.0 markupsafe-2.1.5 mdurl-0.1.2 pandas-2.2.3 pillow-11.1.0 pydub-0.25.1 pygments-2.18.0 python-dateutil-2.9.0.post0 python-multipart-0.0.20 pytz-2024.2 rich-13.9.4 ruff-0.8.5 safehttpx-0.1.6 semantic-version-2.10.0 shellingham-1.5.4 six-1.17.0 starlette-0.41.3 tomlkit-0.13.2 typer-0.15.1 tzdata-2024.2 uvicorn-0.34.0 websockets-14.1(base) C:\Users\xxx>
第1个Gradio应用程序
- 你可以在自己喜欢的代码编辑器、Jupyter 笔记本、Google Colab 或其他任何编写 Python 的地方运行 Gradio。让我们编写第一个 Gradio 应用程序吧:
app.py
import gradio as gr # 导入gradio库,gradio用于快速创建机器学习模型的web界面# 定义一个名为greet的函数,接收两个参数:name 和 intensity
def greet(name, intensity):# 函数返回一个字符串,包含问候语和根据intensity重复的名字return "Hello, " + name + "!" * int(intensity) # 使用gr.Interface创建一个UI界面,fn指定了接口的函数,inputs定义了输入类型,outputs定义了输出类型
demo = gr.Interface(fn=greet, # 指定greet函数作为接口的回调函数inputs=["text", "slider"], # 设置两个输入,一个文本输入框和一个滑块outputs=["text"], # 设置一个文本输出,用于显示greet函数的结果
)demo.launch() # 启动界面,这将会在本地服务器上运行web应用程序
- 该代码的功能是使用Gradio库创建一个简单的web界面,该界面通过一个「text」框输入名字和一个「slider」滑块输入亲密度(intensity),然后点击提交会调用greet函数生成含有问候语的字符串。根据滑块的值,名字会被重复相应的次数,并将结果显示在界面上。
✍️ 提示:为了提高代码的可读性,我们将导入名称从 gradio 简化为 gr 。这是一个被广泛采用的约定,您应该遵守,这样任何与您的代码打交道的人都能很容易地理解它。
现在,运行您的代码。如果您将 Python 代码写入一个名为 app.py 的文件,那么您就可以在终端运行 python app.py 。
如果从文件运行,下面的演示将在 http://localhost:7860 的浏览器中打开。如果在笔记本中运行,演示将嵌入笔记本中显示。
(base) D:\Workspace\CodeRepositories\gradio-app> python app.py
信息: 用提供的模式无法找到文件。
* Running on local URL: http://127.0.0.1:7860To create a public link, set `share=True` in `launch()`.
在左边的文本框中输入您的姓名,拖动滑块,然后按提交按钮。您应该会在右侧看到一个友好的问候语。
✍️ 提示:在本地开发时,你可以在热重载模式下运行
Gradio
应用程序,每当你对文件进行修改时,Gradio 应用程序就会自动重载。
要做到这一点,只需在文件名前输入gradio
而不是python
即可。
在上面的例子中,你需要在终端中输入gradio app.py
。
有关热重载的更多信息,请参阅《热重载指南》https://www.gradio.app/guides/developing-faster-with-reload-mode。
理解 Interface 类
你会注意到,为了制作第一个演示,你创建了一个
gr.Interface
类的实例。
Interface
类旨在为机器学习模型创建演示,这些模型接受一个或多个输入,并返回一个或多个输出。
Interface
类有三个核心参数:
- fn: the function to wrap a user interface (UI) around
fn :用于封装用户界面(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:输入要使用的 Gradio 组件。组件数量应与函数参数数量一致。
- 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:输出时使用的 Gradio 组件。组件的数量应与函数返回值的数量一致。
- fn 参数:非常灵活--你可以传递任何你想用 UI 封装的 Python 函数。
在上面的示例中,我们看到的是一个相对简单的函数,但这个函数可以是任何东西,从音乐生成器到税务计算器,再到预训练机器学习模型的预测函数。
- inputs 和 outputs 参数:包含一个或多个 Gradio 组件。
正如我们将看到的,Gradio 包含 30 多个内置组件(如 gr.Textbox() 、 gr.Image() 和 gr.HTML() 组件),专为机器学习应用而设计。
- 如果函数接受多个参数(如上文所述),则向
inputs
传递一个输入组件列表,每个输入组件依次对应函数的一个参数。 - 如果函数返回一个以上的值,也可以这样做:只需向 outputs 传递一个组件列表即可。
这种灵活性使 Interface 类成为创建演示的一种非常强大的方式。
我们将在 "构建界面 " https://www.gradio.app/main/guides/the-interface-class 系列中深入探讨 gr.Interface 。
分享您的演示
- 如果不能分享,再漂亮的演示又有什么用呢?Gradio 可以让你轻松分享机器学习演示,而不必担心托管到网络服务器上的麻烦。
- 只需在
launch()
中设置share=True
,就能为你的演示创建一个可公开访问的 URL。让我们重温一下演示示例,将最后一行修改如下:
import gradio as gr # 导入gradio库,用于快速创建交互式的机器学习应用程序的Web界面# 定义一个名为greet的函数,接收一个参数:name
def greet(name):# 函数返回一个字符串拼接,内容为向用户打招呼return "Hello " + name + "!"# 创建一个Gradio界面,该界面将包括用于输入和显示结果的文本框
demo = gr.Interface(fn=greet, # 指定greet函数作为用户输入的处理函数inputs="textbox", # 定义用户输入界面为一个文本框outputs="textbox" # 定义输出界面也为一个文本框
)# 启动Gradio界面,并开启共享功能
demo.launch(share=True) # 通过添加参数share=True来共享应用程序,可以通过互联网访问
- 运行这段代码后,几秒钟内就会为您的演示生成一个公共 URL,类似于这样:
https://a23dsf231adb.gradio.live
-
现在,世界各地的任何人都可以通过浏览器试用您的 Gradio 演示,而机器学习模型和所有计算仍在您的计算机上本地运行。
-
要了解更多关于分享演示的信息,请阅读我们的 Gradio 应用程序分享指南https://www.gradio.app/guides/sharing-your-app 。
Core Gradio Classes/Gradio 的核心模块
到目前为止,我们已经讨论了 Interface 类,它是一个高级类,可以让我们用 Gradio 快速制作演示。但 Gradio 还包括什么呢?
带有 gr.ChatInterface 的聊天机器人
- Gradio 还包含另一个高级类 gr.ChatInterface ,专门用于创建聊天机器人用户界面。与 Interface 类似,你只需提供一个函数,Gradio 就会创建一个完整的聊天机器人用户界面。如果你对创建聊天机器人感兴趣,可以直接跳转到我们专门的 gr.ChatInterface 指南https://www.gradio.app/guides/creating-a-chatbot-fast。
使用 gr.Blocks 的自定义演示
-
Gradio 还提供了一种底层方法,利用 gr.Blocks 类设计具有更灵活布局和数据流的网络应用程序。通过块,你可以控制组件在页面上的位置,处理复杂的数据流(例如,输出可以作为其他函数的输入),并根据用户交互更新组件的属性/可见性。
-
您可以使用 gr.Blocks() 构建非常定制和复杂的应用程序。例如,广受欢迎的图像生成器 Automatic1111 Web UI (https://github.com/AUTOMATIC1111/stable-diffusion-webui)就是使用 Gradio 块构建的。我们将在 "使用积木构建 "系列中深入探讨 gr.Blocks(https://www.gradio.app/guides/blocks-and-event-listeners) 。
Gradio Python 和 JavaScript 生态系统
-
这就是 gradio Python 核心库的要点,但 Gradio 实际上远不止这些!它是一个由 Python 和 JavaScript 库组成的完整生态系统,让您可以用 Python 或 JavaScript 构建机器学习应用程序,或以编程方式进行查询。以下是 Gradio 生态系统的其他相关部分:
-
Gradio Python Client
(gradio_client): query any Gradio app programmatically in Python.
Gradio Python Client ( gradio_client ): 查询任何 用 Python 编程的Gradio 应用程序。
https://www.gradio.app/guides/getting-started-with-the-python-client
Gradio JavaScript Client
(@gradio/client): query any Gradio app programmatically in JavaScript.
Gradio JavaScript Client ( @gradio/client ):使用 JavaScript 程式查詢任何 Gradio 應用程式。
https://www.gradio.app/guides/getting-started-with-the-js-client
Gradio-Lite
(@gradio/lite): write Gradio apps in Python that run entirely in the browser (no server needed!), thanks to Pyodide.
Gradio-Lite ( @gradio/lite ): 多亏了 Pyodide,用 Python 编写的 Gradio 应用程序可以完全在浏览器中运行(无需服务器!)。
https://www.gradio.app/guides/gradio-lite
- Hugging Face Spaces: the most popular place to host Gradio applications — for free!
Hugging Face Spaces:免费托管 Gradio 应用程序的最受欢迎的地方!
https://huggingface.co/spaces
下一步是什么?
- 继续使用 Gradio 指南学习 Gradio,其中包括解释、示例代码和嵌入式交互演示。下一步:让我们深入了解 Interface 类
https://www.gradio.app/guides/the-interface-class 。
- 或者,如果您已经掌握了基础知识,但还在寻找特定的内容,您可以搜索技术性更强的 API 文档 https://www.gradio.app/docs 。
X 参考文献
- Gradio
- https://www.gradio.app/docs
- https://github.com/gradio-app/gradio
- 【Gradio】快速入门 - CSDN