mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-23 11:39:17 +08:00
Adds some missing details to guides (#8676)
* more docs * changes * chatbot fast * add environment variables guide * add environment variables guide * Update guides/09_other-tutorials/running-gradio-on-your-web-server-with-nginx.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> --------- Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
This commit is contained in:
parent
3b8238c2e2
commit
8bc78c447c
99
guides/03_additional-features/08_environment-variables.md
Normal file
99
guides/03_additional-features/08_environment-variables.md
Normal file
@ -0,0 +1,99 @@
|
||||
# Environment Variables
|
||||
|
||||
Environment variables in Gradio provide a way to customize your applications and launch settings without changing the codebase. In this guide, we'll explore the key environment variables supported in Gradio and how to set them.
|
||||
|
||||
## Key Environment Variables
|
||||
|
||||
### 1. `GRADIO_SERVER_PORT`
|
||||
|
||||
- **Description**: Specifies the port on which the Gradio app will run.
|
||||
- **Default**: `7860`
|
||||
- **Example**:
|
||||
```bash
|
||||
export GRADIO_SERVER_PORT=8000
|
||||
```
|
||||
|
||||
### 2. `GRADIO_SERVER_NAME`
|
||||
|
||||
- **Description**: Defines the host name for the Gradio server. To make Gradio accessible from any IP address, set this to `"0.0.0.0"`
|
||||
- **Default**: `"127.0.0.1"`
|
||||
- **Example**:
|
||||
```bash
|
||||
export GRADIO_SERVER_NAME="0.0.0.0"
|
||||
```
|
||||
|
||||
### 3. `GRADIO_ANALYTICS_ENABLED`
|
||||
|
||||
- **Description**: Whether Gradio should provide
|
||||
- **Default**: `"True"`
|
||||
- **Options**: `"True"`, `"False"`
|
||||
- **Example**:
|
||||
```sh
|
||||
export GRADIO_ANALYTICS_ENABLED="True"
|
||||
```
|
||||
|
||||
### 4. `GRADIO_DEBUG`
|
||||
|
||||
- **Description**: Enables or disables debug mode in Gradio. If debug mode is enabled, the main thread does not terminate allowing error messages to be printed in environments such as Google Colab.
|
||||
- **Default**: `0`
|
||||
- **Example**:
|
||||
```sh
|
||||
export GRADIO_DEBUG=1
|
||||
```
|
||||
|
||||
### 5. `GRADIO_ALLOW_FLAGGING`
|
||||
|
||||
- **Description**: Controls whether users can flag inputs/outputs in the Gradio interface. See [the Guide on flagging](/guides/using-flagging) for more details.
|
||||
- **Default**: `"manual"`
|
||||
- **Options**: `"never"`, `"manual"`, `"auto"`
|
||||
- **Example**:
|
||||
```sh
|
||||
export GRADIO_ALLOW_FLAGGING="never"
|
||||
```
|
||||
|
||||
### 6. `GRADIO_TEMP_DIR`
|
||||
|
||||
- **Description**: Specifies the directory where temporary files created by Gradio are stored.
|
||||
- **Default**: System default temporary directory
|
||||
- **Example**:
|
||||
```sh
|
||||
export GRADIO_TEMP_DIR="/path/to/temp"
|
||||
```
|
||||
|
||||
### 7. `GRADIO_ROOT_PATH`
|
||||
|
||||
- **Description**: Sets the root path for the Gradio application. Useful if running Gradio [behind a reverse proxy](/guides/running-gradio-on-your-web-server-with-nginx).
|
||||
- **Default**: `""`
|
||||
- **Example**:
|
||||
```sh
|
||||
export GRADIO_ROOT_PATH="/myapp"
|
||||
```
|
||||
|
||||
### 8. `GRADIO_SHARE`
|
||||
|
||||
- **Description**: Enables or disables sharing the Gradio app.
|
||||
- **Default**: `"False"`
|
||||
- **Options**: `"True"`, `"False"`
|
||||
- **Example**:
|
||||
```sh
|
||||
export GRADIO_SHARE="True"
|
||||
```
|
||||
|
||||
|
||||
## How to Set Environment Variables
|
||||
|
||||
To set environment variables in your terminal, use the `export` command followed by the variable name and its value. For example:
|
||||
|
||||
```sh
|
||||
export GRADIO_SERVER_PORT=8000
|
||||
```
|
||||
|
||||
If you're using a `.env` file to manage your environment variables, you can add them like this:
|
||||
|
||||
```sh
|
||||
GRADIO_SERVER_PORT=8000
|
||||
GRADIO_SERVER_NAME="localhost"
|
||||
```
|
||||
|
||||
Then, use a tool like `dotenv` to load these variables when running your application.
|
||||
|
@ -279,6 +279,35 @@ def predict(message, history):
|
||||
gr.ChatInterface(predict).launch()
|
||||
```
|
||||
|
||||
**Handling Concurrent Users with Threads**
|
||||
|
||||
The example above works if you have a single user — or if you have multiple users, since it passes the entire history of the conversation each time there is a new message from a user.
|
||||
|
||||
However, the `openai` library also provides higher-level abstractions that manage conversation history for you, e.g. the [Threads abstraction](https://platform.openai.com/docs/assistants/how-it-works/managing-threads-and-messages). If you use these abstractions, you will need to create a separate thread for each user session. Here's a partial example of how you can do that, by accessing the `session_hash` within your `predict()` function:
|
||||
|
||||
```py
|
||||
import openai
|
||||
import gradio as gr
|
||||
|
||||
client = openai.OpenAI(api_key = os.getenv("OPENAI_API_KEY"))
|
||||
threads = {}
|
||||
|
||||
def predict(message, history, request: gr.Request):
|
||||
if request.session_hash in threads:
|
||||
thread = threads[request.session_hash]
|
||||
else:
|
||||
threads[request.session_hash] = client.beta.threads.create()
|
||||
|
||||
message = client.beta.threads.messages.create(
|
||||
thread_id=thread.id,
|
||||
role="user",
|
||||
content=message)
|
||||
|
||||
...
|
||||
|
||||
gr.ChatInterface(predict).launch()
|
||||
```
|
||||
|
||||
## Example using a local, open-source LLM with Hugging Face
|
||||
|
||||
Of course, in many cases you want to run a chatbot locally. Here's the equivalent example using Together's RedePajama model, from Hugging Face (this requires you to have a GPU with CUDA).
|
||||
|
@ -1,8 +1,8 @@
|
||||
# Building a FastAPI App with the Gradio Python Client
|
||||
# Building a Web App with the Gradio Python Client
|
||||
|
||||
Tags: CLIENT, API, WEB APP
|
||||
|
||||
In this blog post, we will demonstrate how to use the `gradio_client` [Python library](getting-started-with-the-python-client/), which enables developers to make requests to a Gradio app programmatically, by creating an example FastAPI web app. The web app we will be building is called "Acapellify," and it will allow users to upload video files as input and return a version of that video without instrumental music. It will also display a gallery of generated videos.
|
||||
In this blog post, we will demonstrate how to use the `gradio_client` [Python library](getting-started-with-the-python-client/), which enables developers to make requests to a Gradio app programmatically, by creating an end-to-end example web app using FastAPI. The web app we will be building is called "Acapellify," and it will allow users to upload video files as input and return a version of that video without instrumental music. It will also display a gallery of generated videos.
|
||||
|
||||
**Prerequisites**
|
||||
|
||||
@ -111,6 +111,10 @@ async def home(request: Request):
|
||||
|
||||
@app.post("/uploadvideo/")
|
||||
async def upload_video(video: UploadFile = File(...)):
|
||||
video_path = video.filename
|
||||
with open(video_path, "wb+") as fp:
|
||||
fp.write(video.file.read())
|
||||
|
||||
new_video = process_video(video.filename)
|
||||
videos.append(new_video)
|
||||
return RedirectResponse(url='/', status_code=303)
|
||||
|
@ -51,11 +51,18 @@ server {
|
||||
|
||||
Tip: Setting the `X-Forwarded-Host` and `X-Forwarded-Proto` headers is important as Gradio uses these, in conjunction with the `root_path` parameter discussed below, to construct the public URL that your app is being served on. Gradio uses the public URL to fetch various static assets. If these headers are not set, your Gradio app may load in a broken state.
|
||||
|
||||
*Note:* The `$host` variable does not include the host port. If you are serving your Gradio application on a raw IP address and port, you should use the `$http_host` variable instead, in these lines:
|
||||
|
||||
```bash
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
```
|
||||
|
||||
## Run your Gradio app on your web server
|
||||
|
||||
1. 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.
|
||||
|
||||
Tip: You can also provide a complete URL for `root_path` (beginning with `http` or `https`) in which case the `root_path` is treated as an absolute URL instead of a URL suffix (but in this case, you'll need to update the `root_path` if the domain changes).
|
||||
*Note:* Instead of a subpath, you can also provide a complete URL for `root_path` (beginning with `http` or `https`) in which case the `root_path` is treated as an absolute URL instead of a URL suffix (but in this case, you'll need to update the `root_path` if the domain changes).
|
||||
|
||||
Here's a simple example of a Gradio app with a custom `root_path` corresponding to the Nginx configuration above.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user