使用 Nginx 在 Web 服务器上运行 Gradio 应用程序

标签:部署,网络服务器,NGINX

介绍

Gradio 是一个 Python 库,可让你为机器学习模型和数据处理管道快速创建可自定义的 Web 应用程序。 Gradio 应用程序可以免费部署在Hugging Face Spaces上。

Gradio is a Python library that allows you to quickly create customizable web apps for your machine learning models and data processing pipelines. Gradio apps can be deployed on Hugging Face Spaces for free.

但在某些情况下,你可能希望在自己的 Web 服务器上部署 Gradio 应用程序。 你可能已经在使用Nginx ,一个高性能的网络服务器,来为你的网站提供服务(例如 https://www.example.com ),并且你希望将 Gradio 附加到你网站上的特定子路径(例如 https://www.example.com/gradio-demo )。

In some cases though, you might want to deploy a Gradio app on your own web server. You might already be using Nginx, a highly performant web server, to serve your website (say https://www.example.com), and you want to attach Gradio to a specific subpath on your website (e.g. https://www.example.com/gradio-demo).

在本指南中,我们将指导你完成在你自己的 Web 服务器上运行 Nginx 背后的 Gradio 应用程序以实现此目的的过程。

In this Guide, we will guide you through the process of running a Gradio app behind Nginx on your own web server to achieve this.

先决条件

Prerequisites

  1. 安装了 NginxGradio的 Linux Web 服务器

    A Linux web server with Nginx installed and Gradio installed

  2. 在你的网络服务器上保存为 python 文件的工作 Gradio 应用程序

    A working Gradio app saved as a python file on your web server

编辑 Nginx 配置文件

  1. 首先编辑 Web 服务器上的 Nginx 配置文件。 默认情况下,它位于: /etc/nginx/nginx.conf

    Start by editing the Nginx configuration file on your web server. By default, this is located at: /etc/nginx/nginx.conf

http 块中,添加以下行以包含来自单独文件的服务器块配置:

In the http block, add the following line to include server block configurations from a separate file:

include /etc/nginx/sites-enabled/*;
  1. /etc/nginx/sites-available 目录中创建一个新文件(如果该目录尚不存在,则创建该目录),使用代表你的应用程序的文件名,例如: sudo nano /etc/nginx/sites-available/my_gradio_app

    Create a new file in the /etc/nginx/sites-available directory (create the directory if it does not already exist), using a filename that represents your app, for example: sudo nano /etc/nginx/sites-available/my_gradio_app

  2. 将以下内容粘贴到你的文件编辑器中:

    Paste the following into your file editor:

server {
    listen 80;
    server_name example.com www.example.com;  # Change this to your domain name 

    location /gradio-demo/ {  # Change this if you'd like to server your Gradio app on a different path
        proxy_pass http://127.0.0.1:7860/; # Change this if your Gradio app will be running on a different port
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

在你的网络服务器上运行你的 Gradio 应用程序

  1. 在启动 Gradio 应用程序之前,你需要将 root_path 设置为与你在 nginx 配置中指定的子路径相同。 这对于 Gradio 在除域根之外的任何子路径上运行是必需的。

    Before you launch your Gradio app, you'll need to set the root_path to be the same as the subpath that you specified in your nginx configuration. This is necessary for Gradio to run on any subpath besides the root of the domain.

这是一个带有自定义 root_path 的 Gradio 应用程序的简单示例:

Here's a simple example of a Gradio app with a custom root_path:

import gradio as gr
import time

def test(x):
time.sleep(4)
return x

gr.Interface(test, "textbox", "textbox").queue().launch(root_path="/gradio-demo")
  1. 通过键入 tmux 并按回车键启动 tmux 会话(可选)

    Start a tmux session by typing tmux and pressing enter (optional)

建议你在 tmux 会话中运行你的 Gradio 应用程序,以便你可以轻松地使其在后台运行

It's recommended that you run your Gradio app in a tmux session so that you can keep it running in the background easily

  1. 然后,启动你的 Gradio 应用程序。 只需输入 python ,然后输入 Gradio python 文件的名称。 默认情况下,你的应用程序将在 localhost:7860 上运行,但如果它在不同的端口上启动,你将需要更新上面的 nginx 配置文件。

    Then, start your Gradio app. Simply type in python followed by the name of your Gradio python file. By default, your app will run on localhost:7860, but if it starts on a different port, you will need to update the nginx configuration file above.

重启 Nginx

  1. 如果你处于 tmux 会话中,请按 CTRL+B(或 CMD+B)退出,然后按“D”键。

    If you are in a tmux session, exit by typing CTRL+B (or CMD+B), followed by the "D" key.

  2. 最后,通过运行 sudo systemctl restart nginx 重新启动 nginx。

    Finally, restart nginx by running sudo systemctl restart nginx.

就是这样! 如果你在浏览器上访问 https://example.com/gradio-demo ,你应该会看到你的 Gradio 应用程序正在运行

And that's it! If you visit https://example.com/gradio-demo on your browser, you should see your Gradio app running there