huggingface / frp

FRP Fork
Apache License 2.0
138 stars 19 forks source link

Gradio Share Server (FRP)

This repo is a fork of Fast Reverse Proxy (FRP), which runs on Gradio's Share Server to enable Gradio's Share links. Instructions on how to set up your own Share Server to generate custom share links are below 🔥

Background

When you create a Gradio app and launch it with share=True like this:

import gradio as gr

app = gr.Interface(lambda x: x, "image", "image")
app.launch(share=True)

you get a share link like: https://07ff8706ab.gradio.live, and your Gradio app is now accessible to anyone through the Internet (for up to 72 hours).

How does this happen? Your Gradio app runs on a Python server locally, but we use FRP to expose the local server to the Internet. FRP consists of two parts:

The FRP Client establishes a connection from your local machine to the FRP Server, which is accessible on the internet. The FRP Server then provides a public URL that forwards incoming requests to the FRP Client, which in turn sends them to your locally running web app.

Why Run Your Own Share Server?

You might want to run your own server for several reasons:

It's also quite straightforward. In your Gradio app, the only change you'll make is passing in the IP address to your share server, as the share_server_address parameter in launch():

import gradio as gr

app = gr.Interface(lambda x: x, "image", "image")
app.launch(share=True, share_server_address="my-gpt-wrapper.com:7000")

And voila!

Running on public URL: http://07f56cd0f87061c8a1.my-gpt-wrapper.com

Setting Up A Share Server

Prerequisites

Important: not just the root domain, but all subdomains should route to the IP address of your server. This typically means that you should have a wildcard entry in your DNS records, like the bottom-most entry here:

image

1. Install Docker (v. 20.10 or higher)

You can do this by running these commands (for this guide, we assume you are running Ubuntu) sequentially in the terminal after you have SSH'd into the server:

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
apt-cache policy docker-ce
sudo apt install docker-ce

Confirm that you Docker has been installed and you are running version 20.10 or higher by running: docker version in the terminal. You should see something like:

Client: Docker Engine - Community
 Version:           24.0.6
...

2. Clone This Repo

git clone git@github.com:huggingface/frp.git
cd frp

3. Edit the FRP Server Configuration File

Open the scripts/frps.ini file, and edit the value of the subdomain_host property to reflect your domain (without any prefixes).

You can also set bind_port, which is the default port used to the FRP connections to something other than 7000, or customize the 404 page (custom_404_page) which is shown when there is no active Gradio app connected to the Share Server.

Here's a sample customized .ini file:

[common]
log_level = trace
tcp_mux = true
bind_port = 7000
dashboard_port = 7001
vhost_http_port = 80
subdomain_host = my-gpt-wrapper.com
detailed_errors_to_client = false
custom_404_page = /etc/frp/my-404.html

Note: If you would like to change the expiry time of share linksdefault is 72 hours, edit this line in server/control.go as well.

4. Launch the FRP Server Docker Container

Note: you may need sudo permissions for these commands:

docker build -f dockerfiles/Dockerfile-for-frps -t frps:0.2 .
docker run --log-opt max-size=100m --memory=1G --cpus=1 --name frps3 -d --restart unless-stopped --network host -v ~/frp/scripts:/etc/frp frps:0.2 -c /etc/frp/frps.ini

5. Allow Traffic to Your Server

In order to make sure that users can connect to your Share Server, you need to ensure that traffic is allowed at the correct ports. You will need to allow:

Note: If you'd like, you can restrict the IP addresses of the former rule to those people who should be able to create share links and you can restric the IP addresses of the latter two rules to those users who should be able to view share links.

If you are using AWS, here is what your security rules might look like:

image

And that's it! You now have your own little Share Server! As mentioned earlier, you can use it by passing the IP address and FRPS port as a single string to the share_server_address parameter in launch() like this:

import gradio as gr

app = gr.Interface(lambda x: x, "image", "image")
app.launch(share=True, share_server_address="my-gpt-wrapper.com:7000")

Note: If you have installed HTTPS certificates on your Share Server, and your share links are being served through HTTPS, then you should also set share_server_protocol="https" in launch().