Nymbo commited on
Commit
b0c01e0
·
verified ·
1 Parent(s): 4569c57

Upload 23 files

Browse files
sources/00_the-interface-class.md ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # The `Interface` class
3
+
4
+ As mentioned in the [Quickstart](/main/guides/quickstart), the `gr.Interface` class is a high-level abstraction in Gradio that allows you to quickly create a demo for any Python function simply by specifying the input types and the output types. Revisiting our first demo:
5
+
6
+ $code_hello_world_4
7
+
8
+
9
+ We see that the `Interface` class is initialized with three required parameters:
10
+
11
+ - `fn`: the function to wrap a user interface (UI) around
12
+ - `inputs`: which Gradio component(s) to use for the input. The number of components should match the number of arguments in your function.
13
+ - `outputs`: which Gradio component(s) to use for the output. The number of components should match the number of return values from your function.
14
+
15
+ In this Guide, we'll dive into `gr.Interface` and the various ways it can be customized, but before we do that, let's get a better understanding of Gradio components.
16
+
17
+ ## Gradio Components
18
+
19
+ Gradio includes more than 30 pre-built components (as well as many [community-built _custom components_](https://www.gradio.app/custom-components/gallery)) that can be used as inputs or outputs in your demo. These components correspond to common data types in machine learning and data science, e.g. the `gr.Image` component is designed to handle input or output images, the `gr.Label` component displays classification labels and probabilities, the `gr.Plot` component displays various kinds of plots, and so on.
20
+
21
+ **Static and Interactive Components**
22
+
23
+ Every component has a _static_ version that is designed to *display* data, and most components also have an _interactive_ version designed to let users input or modify the data. Typically, you don't need to think about this distinction, because when you build a Gradio demo, Gradio automatically figures out whether the component should be static or interactive based on whether it is being used as an input or output. However, you can set this manually using the `interactive` argument that every component supports.
24
+
25
+ **Preprocessing and Postprocessing**
26
+
27
+ When a component is used as an input, Gradio automatically handles the _preprocessing_ needed to convert the data from a type sent by the user's browser (such as an uploaded image) to a form that can be accepted by your function (such as a `numpy` array).
28
+
29
+
30
+ Similarly, when a component is used as an output, Gradio automatically handles the _postprocessing_ needed to convert the data from what is returned by your function (such as a list of image paths) to a form that can be displayed in the user's browser (a gallery of images).
31
+
32
+ ## Components Attributes
33
+
34
+ We used the default versions of the `gr.Textbox` and `gr.Slider`, but what if you want to change how the UI components look or behave?
35
+
36
+ Let's say you want to customize the slider to have values from 1 to 10, with a default of 2. And you wanted to customize the output text field — you want it to be larger and have a label.
37
+
38
+ If you use the actual classes for `gr.Textbox` and `gr.Slider` instead of the string shortcuts, you have access to much more customizability through component attributes.
39
+
40
+ $code_hello_world_2
41
+ $demo_hello_world_2
42
+
43
+ ## Multiple Input and Output Components
44
+
45
+ Suppose you had a more complex function, with multiple outputs as well. In the example below, we define a function that takes a string, boolean, and number, and returns a string and number.
46
+
47
+ $code_hello_world_3
48
+ $demo_hello_world_3
49
+
50
+ Just as each component in the `inputs` list corresponds to one of the parameters of the function, in order, each component in the `outputs` list corresponds to one of the values returned by the function, in order.
51
+
52
+ ## An Image Example
53
+
54
+ Gradio supports many types of components, such as `Image`, `DataFrame`, `Video`, or `Label`. Let's try an image-to-image function to get a feel for these!
55
+
56
+ $code_sepia_filter
57
+ $demo_sepia_filter
58
+
59
+ When using the `Image` component as input, your function will receive a NumPy array with the shape `(height, width, 3)`, where the last dimension represents the RGB values. We'll return an image as well in the form of a NumPy array.
60
+
61
+ As mentioned above, Gradio handles the preprocessing and postprocessing to convert images to NumPy arrays and vice versa. You can also control the preprocessing performed with the `type=` keyword argument. For example, if you wanted your function to take a file path to an image instead of a NumPy array, the input `Image` component could be written as:
62
+
63
+ ```python
64
+ gr.Image(type="filepath", shape=...)
65
+ ```
66
+
67
+ You can read more about the built-in Gradio components and how to customize them in the [Gradio docs](https://gradio.app/docs).
68
+
69
+ ## Example Inputs
70
+
71
+ You can provide example data that a user can easily load into `Interface`. This can be helpful to demonstrate the types of inputs the model expects, as well as to provide a way to explore your dataset in conjunction with your model. To load example data, you can provide a **nested list** to the `examples=` keyword argument of the Interface constructor. Each sublist within the outer list represents a data sample, and each element within the sublist represents an input for each input component. The format of example data for each component is specified in the [Docs](https://gradio.app/docs#components).
72
+
73
+ $code_calculator
74
+ $demo_calculator
75
+
76
+ You can load a large dataset into the examples to browse and interact with the dataset through Gradio. The examples will be automatically paginated (you can configure this through the `examples_per_page` argument of `Interface`).
77
+
78
+ Continue learning about examples in the [More On Examples](https://gradio.app/guides/more-on-examples) guide.
79
+
80
+ ## Descriptive Content
81
+
82
+ In the previous example, you may have noticed the `title=` and `description=` keyword arguments in the `Interface` constructor that helps users understand your app.
83
+
84
+ There are three arguments in the `Interface` constructor to specify where this content should go:
85
+
86
+ - `title`: which accepts text and can display it at the very top of interface, and also becomes the page title.
87
+ - `description`: which accepts text, markdown or HTML and places it right under the title.
88
+ - `article`: which also accepts text, markdown or HTML and places it below the interface.
89
+
90
+ ![annotated](https://github.com/gradio-app/gradio/blob/main/guides/assets/annotated.png?raw=true)
91
+
92
+ Note: if you're using the `Blocks` class, you can insert text, markdown, or HTML anywhere in your application using the `gr.Markdown(...)` or `gr.HTML(...)` components.
93
+
94
+ Another useful keyword argument is `label=`, which is present in every `Component`. This modifies the label text at the top of each `Component`. You can also add the `info=` keyword argument to form elements like `Textbox` or `Radio` to provide further information on their usage.
95
+
96
+ ```python
97
+ gr.Number(label='Age', info='In years, must be greater than 0')
98
+ ```
99
+
100
+ ## Additional Inputs within an Accordion
101
+
102
+ If your prediction function takes many inputs, you may want to hide some of them within a collapsed accordion to avoid cluttering the UI. The `Interface` class takes an `additional_inputs` argument which is similar to `inputs` but any input components included here are not visible by default. The user must click on the accordion to show these components. The additional inputs are passed into the prediction function, in order, after the standard inputs.
103
+
104
+ You can customize the appearance of the accordion by using the optional `additional_inputs_accordion` argument, which accepts a string (in which case, it becomes the label of the accordion), or an instance of the `gr.Accordion()` class (e.g. this lets you control whether the accordion is open or closed by default).
105
+
106
+ Here's an example:
107
+
108
+ $code_interface_with_additional_inputs
109
+ $demo_interface_with_additional_inputs
110
+
sources/01_blocks-and-event-listeners.md ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Blocks and Event Listeners
3
+
4
+ We briefly descirbed the Blocks class in the [Quickstart](/main/guides/quickstart#custom-demos-with-gr-blocks) as a way to build custom demos. Let's dive deeper.
5
+
6
+
7
+ ## Blocks Structure
8
+
9
+ Take a look at the demo below.
10
+
11
+ $code_hello_blocks
12
+ $demo_hello_blocks
13
+
14
+ - First, note the `with gr.Blocks() as demo:` clause. The Blocks app code will be contained within this clause.
15
+ - Next come the Components. These are the same Components used in `Interface`. However, instead of being passed to some constructor, Components are automatically added to the Blocks as they are created within the `with` clause.
16
+ - Finally, the `click()` event listener. Event listeners define the data flow within the app. In the example above, the listener ties the two Textboxes together. The Textbox `name` acts as the input and Textbox `output` acts as the output to the `greet` method. This dataflow is triggered when the Button `greet_btn` is clicked. Like an Interface, an event listener can take multiple inputs or outputs.
17
+
18
+ You can also attach event listeners using decorators - skip the `fn` argument and assign `inputs` and `outputs` directly:
19
+
20
+ $code_hello_blocks_decorator
21
+
22
+ ## Event Listeners and Interactivity
23
+
24
+ In the example above, you'll notice that you are able to edit Textbox `name`, but not Textbox `output`. This is because any Component that acts as an input to an event listener is made interactive. However, since Textbox `output` acts only as an output, Gradio determines that it should not be made interactive. You can override the default behavior and directly configure the interactivity of a Component with the boolean `interactive` keyword argument.
25
+
26
+ ```python
27
+ output = gr.Textbox(label="Output", interactive=True)
28
+ ```
29
+
30
+ _Note_: What happens if a Gradio component is neither an input nor an output? If a component is constructed with a default value, then it is presumed to be displaying content and is rendered non-interactive. Otherwise, it is rendered interactive. Again, this behavior can be overridden by specifying a value for the `interactive` argument.
31
+
32
+ ## Types of Event Listeners
33
+
34
+ Take a look at the demo below:
35
+
36
+ $code_blocks_hello
37
+ $demo_blocks_hello
38
+
39
+ Instead of being triggered by a click, the `welcome` function is triggered by typing in the Textbox `inp`. This is due to the `change()` event listener. Different Components support different event listeners. For example, the `Video` Component supports a `play()` event listener, triggered when a user presses play. See the [Docs](http://gradio.app/docs#components) for the event listeners for each Component.
40
+
41
+ ## Multiple Data Flows
42
+
43
+ A Blocks app is not limited to a single data flow the way Interfaces are. Take a look at the demo below:
44
+
45
+ $code_reversible_flow
46
+ $demo_reversible_flow
47
+
48
+ Note that `num1` can act as input to `num2`, and also vice-versa! As your apps get more complex, you will have many data flows connecting various Components.
49
+
50
+ Here's an example of a "multi-step" demo, where the output of one model (a speech-to-text model) gets fed into the next model (a sentiment classifier).
51
+
52
+ $code_blocks_speech_text_sentiment
53
+ $demo_blocks_speech_text_sentiment
54
+
55
+ ## Function Input List vs Dict
56
+
57
+ The event listeners you've seen so far have a single input component. If you'd like to have multiple input components pass data to the function, you have two options on how the function can accept input component values:
58
+
59
+ 1. as a list of arguments, or
60
+ 2. as a single dictionary of values, keyed by the component
61
+
62
+ Let's see an example of each:
63
+ $code_calculator_list_and_dict
64
+
65
+ Both `add()` and `sub()` take `a` and `b` as inputs. However, the syntax is different between these listeners.
66
+
67
+ 1. To the `add_btn` listener, we pass the inputs as a list. The function `add()` takes each of these inputs as arguments. The value of `a` maps to the argument `num1`, and the value of `b` maps to the argument `num2`.
68
+ 2. To the `sub_btn` listener, we pass the inputs as a set (note the curly brackets!). The function `sub()` takes a single dictionary argument `data`, where the keys are the input components, and the values are the values of those components.
69
+
70
+ It is a matter of preference which syntax you prefer! For functions with many input components, option 2 may be easier to manage.
71
+
72
+ $demo_calculator_list_and_dict
73
+
74
+ ## Function Return List vs Dict
75
+
76
+ Similarly, you may return values for multiple output components either as:
77
+
78
+ 1. a list of values, or
79
+ 2. a dictionary keyed by the component
80
+
81
+ Let's first see an example of (1), where we set the values of two output components by returning two values:
82
+
83
+ ```python
84
+ with gr.Blocks() as demo:
85
+ food_box = gr.Number(value=10, label="Food Count")
86
+ status_box = gr.Textbox()
87
+ def eat(food):
88
+ if food > 0:
89
+ return food - 1, "full"
90
+ else:
91
+ return 0, "hungry"
92
+ gr.Button("EAT").click(
93
+ fn=eat,
94
+ inputs=food_box,
95
+ outputs=[food_box, status_box]
96
+ )
97
+ ```
98
+
99
+ Above, each return statement returns two values corresponding to `food_box` and `status_box`, respectively.
100
+
101
+ Instead of returning a list of values corresponding to each output component in order, you can also return a dictionary, with the key corresponding to the output component and the value as the new value. This also allows you to skip updating some output components.
102
+
103
+ ```python
104
+ with gr.Blocks() as demo:
105
+ food_box = gr.Number(value=10, label="Food Count")
106
+ status_box = gr.Textbox()
107
+ def eat(food):
108
+ if food > 0:
109
+ return {food_box: food - 1, status_box: "full"}
110
+ else:
111
+ return {status_box: "hungry"}
112
+ gr.Button("EAT").click(
113
+ fn=eat,
114
+ inputs=food_box,
115
+ outputs=[food_box, status_box]
116
+ )
117
+ ```
118
+
119
+ Notice how when there is no food, we only update the `status_box` element. We skipped updating the `food_box` component.
120
+
121
+ Dictionary returns are helpful when an event listener affects many components on return, or conditionally affects outputs and not others.
122
+
123
+ Keep in mind that with dictionary returns, we still need to specify the possible outputs in the event listener.
124
+
125
+ ## Updating Component Configurations
126
+
127
+ The return value of an event listener function is usually the updated value of the corresponding output Component. Sometimes we want to update the configuration of the Component as well, such as the visibility. In this case, we return a new Component, setting the properties we want to change.
128
+
129
+ $code_blocks_essay_simple
130
+ $demo_blocks_essay_simple
131
+
132
+ See how we can configure the Textbox itself through a new `gr.Textbox()` method. The `value=` argument can still be used to update the value along with Component configuration. Any arguments we do not set will use their previous values.
133
+
134
+ ## Examples
135
+
136
+ Just like with `gr.Interface`, you can also add examples for your functions when you are working with `gr.Blocks`. In this case, instantiate a `gr.Examples` similar to how you would instantiate any other component. The constructor of `gr.Examples` takes two required arguments:
137
+
138
+ * `examples`: a nested list of examples, in which the outer list consists of examples and each inner list consists of an input corresponding to each input component
139
+ * `inputs`: the component or list of components that should be populated when the examples are clicked
140
+
141
+ You can also set `cache_examples=True` similar to `gr.Interface`, in which case two additional arguments must be provided:
142
+
143
+ * `outputs`: the component or list of components corresponding to the output of the examples
144
+ * `fn`: the function to run to generate the outputs corresponding to the examples
145
+
146
+ Here's an example showing how to use `gr.Examples` in a `gr.Blocks` app:
147
+
148
+ $code_calculator_blocks
149
+
150
+ **Note**: In Gradio 4.0 or later, when you click on examples, not only does the value of the input component update to the example value, but the component's configuration also reverts to the properties with which you constructed the component. This ensures that the examples are compatible with the component even if its configuration has been changed.
151
+
152
+
153
+
154
+ ## Running Events Consecutively
155
+
156
+ You can also run events consecutively by using the `then` method of an event listener. This will run an event after the previous event has finished running. This is useful for running events that update components in multiple steps.
157
+
158
+ For example, in the chatbot example below, we first update the chatbot with the user message immediately, and then update the chatbot with the computer response after a simulated delay.
159
+
160
+ $code_chatbot_consecutive
161
+ $demo_chatbot_consecutive
162
+
163
+ The `.then()` method of an event listener executes the subsequent event regardless of whether the previous event raised any errors. If you'd like to only run subsequent events if the previous event executed successfully, use the `.success()` method, which takes the same arguments as `.then()`.
164
+
165
+ ## Running Events Continuously
166
+
167
+ You can run events on a fixed schedule using `gr.Timer()` object. This will run the event when the timer's `tick` event fires. See the code below:
168
+
169
+ ```python
170
+ with gr.Blocks as demo:
171
+ timer = gr.Timer(5)
172
+ textbox = gr.Textbox()
173
+ textbox2 = gr.Textbox()
174
+ timer.tick(set_textbox_fn, textbox, textbox2)
175
+ ```
176
+
177
+ This can also be used directly with a Component's `every=` parameter as such:
178
+
179
+ ```python
180
+ with gr.Blocks as demo:
181
+ timer = gr.Timer(5)
182
+ textbox = gr.Textbox()
183
+ textbox2 = gr.Textbox(set_textbox_fn, inputs=[textbox], every=timer)
184
+ ```
185
+
186
+ Here is an example of a demo that print the current timestamp, and also prints random numbers regularly!
187
+
188
+ $code_timer
189
+ $demo_timer
190
+
191
+ ## Gathering Event Data
192
+
193
+ You can gather specific data about an event by adding the associated event data class as a type hint to an argument in the event listener function.
194
+
195
+ For example, event data for `.select()` can be type hinted by a `gradio.SelectData` argument. This event is triggered when a user selects some part of the triggering component, and the event data includes information about what the user specifically selected. If a user selected a specific word in a `Textbox`, a specific image in a `Gallery`, or a specific cell in a `DataFrame`, the event data argument would contain information about the specific selection.
196
+
197
+ In the 2 player tic-tac-toe demo below, a user can select a cell in the `DataFrame` to make a move. The event data argument contains information about the specific cell that was selected. We can first check to see if the cell is empty, and then update the cell with the user's move.
198
+
199
+ $code_tictactoe
200
+ $demo_tictactoe
201
+
202
+ ## Binding Multiple Triggers to a Function
203
+
204
+ Often times, you may want to bind multiple triggers to the same function. For example, you may want to allow a user to click a submit button, or press enter to submit a form. You can do this using the `gr.on` method and passing a list of triggers to the `trigger`.
205
+
206
+ $code_on_listener_basic
207
+ $demo_on_listener_basic
208
+
209
+ You can use decorator syntax as well:
210
+
211
+ $code_on_listener_decorator
212
+
213
+ You can use `gr.on` to create "live" events by binding to the `change` event of components that implement it. If you do not specify any triggers, the function will automatically bind to all `change` event of all input components that include a `change` event (for example `gr.Textbox` has a `change` event whereas `gr.Button` does not).
214
+
215
+ $code_on_listener_live
216
+ $demo_on_listener_live
217
+
218
+ You can follow `gr.on` with `.then`, just like any regular event listener. This handy method should save you from having to write a lot of repetitive code!
219
+
220
+ ## Binding a Component Value Directly to a Function of Other Components
221
+
222
+ If you want to set a Component's value to always be a function of the value of other Components, you can use the following shorthand:
223
+
224
+ ```python
225
+ with gr.Blocks() as demo:
226
+ num1 = gr.Number()
227
+ num2 = gr.Number()
228
+ product = gr.Number(lambda a, b: a * b, inputs=[num1, num2])
229
+ ```
230
+
231
+ This functionally the same as:
232
+ ```python
233
+ with gr.Blocks() as demo:
234
+ num1 = gr.Number()
235
+ num2 = gr.Number()
236
+ product = gr.Number()
237
+
238
+ gr.on(
239
+ [num1.change, num2.change, demo.load],
240
+ lambda a, b: a * b,
241
+ inputs=[num1, num2],
242
+ outputs=product
243
+ )
244
+ ```
sources/01_more-on-examples.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # More on Examples
3
+
4
+ In the [previous Guide](/main/guides/the-interface-class), we discussed how to provide example inputs for your demo to make it easier for users to try it out. Here, we dive into more details.
5
+
6
+ ## Providing Examples
7
+
8
+ Adding examples to an Interface is as easy as providing a list of lists to the `examples`
9
+ keyword argument.
10
+ Each sublist is a data sample, where each element corresponds to an input of the prediction function.
11
+ The inputs must be ordered in the same order as the prediction function expects them.
12
+
13
+ If your interface only has one input component, then you can provide your examples as a regular list instead of a list of lists.
14
+
15
+ ### Loading Examples from a Directory
16
+
17
+ You can also specify a path to a directory containing your examples. If your Interface takes only a single file-type input, e.g. an image classifier, you can simply pass a directory filepath to the `examples=` argument, and the `Interface` will load the images in the directory as examples.
18
+ In the case of multiple inputs, this directory must
19
+ contain a log.csv file with the example values.
20
+ In the context of the calculator demo, we can set `examples='/demo/calculator/examples'` and in that directory we include the following `log.csv` file:
21
+
22
+ ```csv
23
+ num,operation,num2
24
+ 5,"add",3
25
+ 4,"divide",2
26
+ 5,"multiply",3
27
+ ```
28
+
29
+ This can be helpful when browsing flagged data. Simply point to the flagged directory and the `Interface` will load the examples from the flagged data.
30
+
31
+ ### Providing Partial Examples
32
+
33
+ Sometimes your app has many input components, but you would only like to provide examples for a subset of them. In order to exclude some inputs from the examples, pass `None` for all data samples corresponding to those particular components.
34
+
35
+ ## Caching examples
36
+
37
+ You may wish to provide some cached examples of your model for users to quickly try out, in case your model takes a while to run normally.
38
+ If `cache_examples=True`, your Gradio app will run all of the examples and save the outputs when you call the `launch()` method. This data will be saved in a directory called `gradio_cached_examples` in your working directory by default. You can also set this directory with the `GRADIO_EXAMPLES_CACHE` environment variable, which can be either an absolute path or a relative path to your working directory.
39
+
40
+ Whenever a user clicks on an example, the output will automatically be populated in the app now, using data from this cached directory instead of actually running the function. This is useful so users can quickly try out your model without adding any load!
41
+
42
+ Alternatively, you can set `cache_examples="lazy"`. This means that each particular example will only get cached after it is first used (by any user) in the Gradio app. This is helpful if your prediction function is long-running and you do not want to wait a long time for your Gradio app to start.
43
+
44
+ Keep in mind once the cache is generated, it will not be updated automatically in future launches. If the examples or function logic change, delete the cache folder to clear the cache and rebuild it with another `launch()`.
sources/01_queuing.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Queuing
3
+
4
+ Every Gradio app comes with a built-in queuing system that can scale to thousands of concurrent users. You can configure the queue by using `queue()` method which is supported by the `gr.Interface`, `gr.Blocks`, and `gr.ChatInterface` classes.
5
+
6
+ For example, you can control the number of requests processed at a single time by setting the `default_concurrency_limit` parameter of `queue()`, e.g.
7
+
8
+ ```python
9
+ demo = gr.Interface(...).queue(default_concurrency_limit=5)
10
+ demo.launch()
11
+ ```
12
+
13
+ This limits the number of requests processed for this event listener at a single time to 5. By default, the `default_concurrency_limit` is actually set to `1`, which means that when many users are using your app, only a single user's request will be processed at a time. This is because many machine learning functions consume a significant amount of memory and so it is only suitable to have a single user using the demo at a time. However, you can change this parameter in your demo easily.
14
+
15
+ See the [docs on queueing](/docs/gradio/interface#interface-queue) for more details on configuring the queuing parameters.
16
+
17
+ You can see analytics on the number and status of all requests processed by the queue by visiting the `/monitoring` endpoint of your app. This endpoint will print a secret URL to your console that links to the full analytics dashboard.
sources/01_quickstart.md ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Quickstart
3
+
4
+ Gradio is an open-source Python package that allows you to quickly **build** a demo or web application for your machine learning model, API, or any arbitary Python function. You can then **share** a link to your demo or web application in just a few seconds using Gradio's built-in sharing features. *No JavaScript, CSS, or web hosting experience needed!*
5
+
6
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/lcm-screenshot-3.gif" style="padding-bottom: 10px">
7
+
8
+ It just takes a few lines of Python to create a demo like the one above, so let's get started 💫
9
+
10
+ ## Installation
11
+
12
+ **Prerequisite**: Gradio requires [Python 3.8 or higher](https://www.python.org/downloads/)
13
+
14
+
15
+ We recommend installing Gradio using `pip`, which is included by default in Python. Run this in your terminal or command prompt:
16
+
17
+ ```bash
18
+ pip install gradio
19
+ ```
20
+
21
+
22
+ Tip: it is best to install Gradio in a virtual environment. Detailed installation instructions for all common operating systems <a href="https://www.gradio.app/main/guides/installing-gradio-in-a-virtual-environment">are provided here</a>.
23
+
24
+ ## Building Your First Demo
25
+
26
+ You can run Gradio in your favorite code editor, Jupyter notebook, Google Colab, or anywhere else you write Python. Let's write your first Gradio app:
27
+
28
+
29
+ $code_hello_world_4
30
+
31
+
32
+ Tip: We shorten the imported name from <code>gradio</code> to <code>gr</code> for better readability of code. This is a widely adopted convention that you should follow so that anyone working with your code can easily understand it.
33
+
34
+ Now, run your code. If you've written the Python code in a file named, for example, `app.py`, then you would run `python app.py` from the terminal.
35
+
36
+ The demo below will open in a browser on [http://localhost:7860](http://localhost:7860) if running from a file. If you are running within a notebook, the demo will appear embedded within the notebook.
37
+
38
+ $demo_hello_world_4
39
+
40
+ Type your name in the textbox on the left, drag the slider, and then press the Submit button. You should see a friendly greeting on the right.
41
+
42
+ Tip: When developing locally, you can run your Gradio app in <strong>hot reload mode</strong>, which automatically reloads the Gradio app whenever you make changes to the file. To do this, simply type in <code>gradio</code> before the name of the file instead of <code>python</code>. In the example above, you would type: `gradio app.py` in your terminal. Learn more about hot reloading in the <a href="https://www.gradio.app/guides/developing-faster-with-reload-mode">Hot Reloading Guide</a>.
43
+
44
+
45
+ **Understanding the `Interface` Class**
46
+
47
+ You'll notice that in order to make your first demo, you created an instance of the `gr.Interface` class. The `Interface` class is designed to create demos for machine learning models which accept one or more inputs, and return one or more outputs.
48
+
49
+ The `Interface` class has three core arguments:
50
+
51
+ - `fn`: the function to wrap a user interface (UI) around
52
+ - `inputs`: the Gradio component(s) to use for the input. The number of components should match the number of arguments in your function.
53
+ - `outputs`: the Gradio component(s) to use for the output. The number of components should match the number of return values from your function.
54
+
55
+ The `fn` argument is very flexible -- you can pass *any* Python function that you want to wrap with a UI. In the example above, we saw a relatively simple function, but the function could be anything from a music generator to a tax calculator to the prediction function of a pretrained machine learning model.
56
+
57
+ The `inputs` and `outputs` arguments take one or more Gradio components. As we'll see, Gradio includes more than [30 built-in components](https://www.gradio.app/docs/gradio/components) (such as the `gr.Textbox()`, `gr.Image()`, and `gr.HTML()` components) that are designed for machine learning applications.
58
+
59
+ Tip: For the `inputs` and `outputs` arguments, you can pass in the name of these components as a string (`"textbox"`) or an instance of the class (`gr.Textbox()`).
60
+
61
+ If your function accepts more than one argument, as is the case above, pass a list of input components to `inputs`, with each input component corresponding to one of the arguments of the function, in order. The same holds true if your function returns more than one value: simply pass in a list of components to `outputs`. This flexibility makes the `Interface` class a very powerful way to create demos.
62
+
63
+ We'll dive deeper into the `gr.Interface` on our series on [building Interfaces](https://www.gradio.app/main/guides/the-interface-class).
64
+
65
+ ## Sharing Your Demo
66
+
67
+ What good is a beautiful demo if you can't share it? Gradio lets you easily share a machine learning demo without having to worry about the hassle of hosting on a web server. Simply set `share=True` in `launch()`, and a publicly accessible URL will be created for your demo. Let's revisit our example demo, but change the last line as follows:
68
+
69
+ ```python
70
+ import gradio as gr
71
+
72
+ def greet(name):
73
+ return "Hello " + name + "!"
74
+
75
+ demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
76
+
77
+ demo.launch(share=True) # Share your demo with just 1 extra parameter 🚀
78
+ ```
79
+
80
+ When you run this code, a public URL will be generated for your demo in a matter of seconds, something like:
81
+
82
+ 👉 &nbsp; `https://a23dsf231adb.gradio.live`
83
+
84
+ Now, anyone around the world can try your Gradio demo from their browser, while the machine learning model and all computation continues to run locally on your computer.
85
+
86
+ To learn more about sharing your demo, read our dedicated guide on [sharing your Gradio application](https://www.gradio.app/guides/sharing-your-app).
87
+
88
+
89
+ ## Core Gradio Classes
90
+
91
+ So far, we've been discussing the `Interface` class, which is a high-level class that lets to build demos quickly with Gradio. But what else does Gradio include?aaa
92
+
93
+ ### Chatbots with `gr.ChatInterface`
94
+
95
+ Gradio includes another high-level class, `gr.ChatInterface`, which is specifically designed to create Chatbot UIs. Similar to `Interface`, you supply a function and Gradio creates a fully working Chatbot UI. If you're interested in creating a chatbot, you can jump straight to [our dedicated guide on `gr.ChatInterface`](https://www.gradio.app/guides/creating-a-chatbot-fast).
96
+
97
+ ### Custom Demos with `gr.Blocks`
98
+
99
+ Gradio also offers a low-level approach for designing web apps with more flexible layouts and data flows with the `gr.Blocks` class. Blocks allows you to do things like control where components appear on the page, handle complex data flows (e.g. outputs can serve as inputs to other functions), and update properties/visibility of components based on user interaction — still all in Python.
100
+
101
+ You can build very custom and complex applications using `gr.Blocks()`. For example, the popular image generation [Automatic1111 Web UI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) is built using Gradio Blocks. We dive deeper into the `gr.Blocks` on our series on [building with Blocks](https://www.gradio.app/guides/blocks-and-event-listeners).
102
+
103
+
104
+ ### The Gradio Python & JavaScript Ecosystem
105
+
106
+ That's the gist of the core `gradio` Python library, but Gradio is actually so much more! Its an entire ecosystem of Python and JavaScript libraries that let you build machine learning applications, or query them programmatically, in Python or JavaScript. Here are other related parts of the Gradio ecosystem:
107
+
108
+ * [Gradio Python Client](https://www.gradio.app/guides/getting-started-with-the-python-client) (`gradio_client`): query any Gradio app programmatically in Python.
109
+ * [Gradio JavaScript Client](https://www.gradio.app/guides/getting-started-with-the-js-client) (`@gradio/client`): query any Gradio app programmatically in JavaScript.
110
+ * [Gradio-Lite](https://www.gradio.app/guides/gradio-lite) (`@gradio/lite`): write Gradio apps in Python that run entirely in the browser (no server needed!), thanks to Pyodide.
111
+ * [Hugging Face Spaces](https://huggingface.co/spaces): the most popular place to host Gradio applications — for free!
112
+
113
+ ## What's Next?
114
+
115
+ Keep learning about Gradio sequentially using the Gradio Guides, which include explanations as well as example code and embedded interactive demos. Next up: [let's dive deeper into the Interface class](https://www.gradio.app/guides/the-interface-class).
116
+
117
+ Or, if you already know the basics and are looking for something specific, you can search the more [technical API documentation](https://www.gradio.app/docs/).
118
+
119
+
sources/02_controlling-layout.md ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Controlling Layout
3
+
4
+ By default, Components in Blocks are arranged vertically. Let's take a look at how we can rearrange Components. Under the hood, this layout structure uses the [flexbox model of web development](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox).
5
+
6
+ ## Rows
7
+
8
+ Elements within a `with gr.Row` clause will all be displayed horizontally. For example, to display two Buttons side by side:
9
+
10
+ ```python
11
+ with gr.Blocks() as demo:
12
+ with gr.Row():
13
+ btn1 = gr.Button("Button 1")
14
+ btn2 = gr.Button("Button 2")
15
+ ```
16
+
17
+ To make every element in a Row have the same height, use the `equal_height` argument of the `style` method.
18
+
19
+ ```python
20
+ with gr.Blocks() as demo:
21
+ with gr.Row(equal_height=True):
22
+ textbox = gr.Textbox()
23
+ btn2 = gr.Button("Button 2")
24
+ ```
25
+
26
+ The widths of elements in a Row can be controlled via a combination of `scale` and `min_width` arguments that are present in every Component.
27
+
28
+ - `scale` is an integer that defines how an element will take up space in a Row. If scale is set to `0`, the element will not expand to take up space. If scale is set to `1` or greater, the element will expand. Multiple elements in a row will expand proportional to their scale. Below, `btn2` will expand twice as much as `btn1`, while `btn0` will not expand at all:
29
+
30
+ ```python
31
+ with gr.Blocks() as demo:
32
+ with gr.Row():
33
+ btn0 = gr.Button("Button 0", scale=0)
34
+ btn1 = gr.Button("Button 1", scale=1)
35
+ btn2 = gr.Button("Button 2", scale=2)
36
+ ```
37
+
38
+ - `min_width` will set the minimum width the element will take. The Row will wrap if there isn't sufficient space to satisfy all `min_width` values.
39
+
40
+ Learn more about Rows in the [docs](https://gradio.app/docs/row).
41
+
42
+ ## Columns and Nesting
43
+
44
+ Components within a Column will be placed vertically atop each other. Since the vertical layout is the default layout for Blocks apps anyway, to be useful, Columns are usually nested within Rows. For example:
45
+
46
+ $code_rows_and_columns
47
+ $demo_rows_and_columns
48
+
49
+ See how the first column has two Textboxes arranged vertically. The second column has an Image and Button arranged vertically. Notice how the relative widths of the two columns is set by the `scale` parameter. The column with twice the `scale` value takes up twice the width.
50
+
51
+ Learn more about Columns in the [docs](https://gradio.app/docs/column).
52
+
53
+ # Dimensions
54
+
55
+ You can control the height and width of various components, where the parameters are available. These parameters accept either a number (interpreted as pixels) or a string. Using a string allows the direct application of any CSS unit to the encapsulating Block element, catering to more specific design requirements. When omitted, Gradio uses default dimensions suited for most use cases.
56
+
57
+ Below is an example illustrating the use of viewport width (vw):
58
+
59
+ ```python
60
+ import gradio as gr
61
+
62
+ with gr.Blocks() as demo:
63
+ im = gr.ImageEditor(
64
+ width="50vw",
65
+ )
66
+
67
+ demo.launch()
68
+ ```
69
+
70
+ When using percentage values for dimensions, you may want to define a parent component with an absolute unit (e.g. `px` or `vw`). This approach ensures that child components with relative dimensions are sized appropriately:
71
+
72
+
73
+ ```python
74
+ import gradio as gr
75
+
76
+ css = """
77
+ .container {
78
+ height: 100vh;
79
+ }
80
+ """
81
+
82
+ with gr.Blocks(css=css) as demo:
83
+ with gr.Column(elem_classes=["container"]):
84
+ name = gr.Chatbot(value=[["1", "2"]], height="70%")
85
+
86
+ demo.launch()
87
+ ```
88
+
89
+ In this example, the Column layout component is given a height of 100% of the viewport height (100vh), and the Chatbot component inside it takes up 70% of the Column's height.
90
+
91
+ You can apply any valid CSS unit for these parameters. For a comprehensive list of CSS units, refer to [this guide](https://www.w3schools.com/cssref/css_units.php). We recommend you always consider responsiveness and test your interfaces on various screen sizes to ensure a consistent user experience.
92
+
93
+
94
+
95
+ ## Tabs and Accordions
96
+
97
+ You can also create Tabs using the `with gr.Tab('tab_name'):` clause. Any component created inside of a `with gr.Tab('tab_name'):` context appears in that tab. Consecutive Tab clauses are grouped together so that a single tab can be selected at one time, and only the components within that Tab's context are shown.
98
+
99
+ For example:
100
+
101
+ $code_blocks_flipper
102
+ $demo_blocks_flipper
103
+
104
+ Also note the `gr.Accordion('label')` in this example. The Accordion is a layout that can be toggled open or closed. Like `Tabs`, it is a layout element that can selectively hide or show content. Any components that are defined inside of a `with gr.Accordion('label'):` will be hidden or shown when the accordion's toggle icon is clicked.
105
+
106
+ Learn more about [Tabs](https://gradio.app/docs/tab) and [Accordions](https://gradio.app/docs/accordion) in the docs.
107
+
108
+ ## Visibility
109
+
110
+ Both Components and Layout elements have a `visible` argument that can set initially and also updated. Setting `gr.Column(visible=...)` on a Column can be used to show or hide a set of Components.
111
+
112
+ $code_blocks_form
113
+ $demo_blocks_form
114
+
115
+ ## Variable Number of Outputs
116
+
117
+ By adjusting the visibility of components in a dynamic way, it is possible to create
118
+ demos with Gradio that support a _variable numbers of outputs_. Here's a very simple example
119
+ where the number of output textboxes is controlled by an input slider:
120
+
121
+ $code_variable_outputs
122
+ $demo_variable_outputs
123
+
124
+ ## Defining and Rendering Components Separately
125
+
126
+ In some cases, you might want to define components before you actually render them in your UI. For instance, you might want to show an examples section using `gr.Examples` above the corresponding `gr.Textbox` input. Since `gr.Examples` requires as a parameter the input component object, you will need to first define the input component, but then render it later, after you have defined the `gr.Examples` object.
127
+
128
+ The solution to this is to define the `gr.Textbox` outside of the `gr.Blocks()` scope and use the component's `.render()` method wherever you'd like it placed in the UI.
129
+
130
+ Here's a full code example:
131
+
132
+ ```python
133
+ input_textbox = gr.Textbox()
134
+
135
+ with gr.Blocks() as demo:
136
+ gr.Examples(["hello", "bonjour", "merhaba"], input_textbox)
137
+ input_textbox.render()
138
+ ```
sources/02_flagging.md ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Flagging
3
+
4
+ You may have noticed the "Flag" button that appears by default in your `Interface`. When a user using your demo sees input with interesting output, such as erroneous or unexpected model behaviour, they can flag the input for you to review. Within the directory provided by the `flagging_dir=` argument to the `Interface` constructor, a CSV file will log the flagged inputs. If the interface involves file data, such as for Image and Audio components, folders will be created to store those flagged data as well.
5
+
6
+ For example, with the calculator interface shown above, we would have the flagged data stored in the flagged directory shown below:
7
+
8
+ ```directory
9
+ +-- calculator.py
10
+ +-- flagged/
11
+ | +-- logs.csv
12
+ ```
13
+
14
+ _flagged/logs.csv_
15
+
16
+ ```csv
17
+ num1,operation,num2,Output
18
+ 5,add,7,12
19
+ 6,subtract,1.5,4.5
20
+ ```
21
+
22
+ With the sepia interface shown earlier, we would have the flagged data stored in the flagged directory shown below:
23
+
24
+ ```directory
25
+ +-- sepia.py
26
+ +-- flagged/
27
+ | +-- logs.csv
28
+ | +-- im/
29
+ | | +-- 0.png
30
+ | | +-- 1.png
31
+ | +-- Output/
32
+ | | +-- 0.png
33
+ | | +-- 1.png
34
+ ```
35
+
36
+ _flagged/logs.csv_
37
+
38
+ ```csv
39
+ im,Output
40
+ im/0.png,Output/0.png
41
+ im/1.png,Output/1.png
42
+ ```
43
+
44
+ If you wish for the user to provide a reason for flagging, you can pass a list of strings to the `flagging_options` argument of Interface. Users will have to select one of the strings when flagging, which will be saved as an additional column to the CSV.
45
+
46
+
sources/02_key-features.md ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Key Features
3
+
4
+ Let's go through some of the key features of Gradio. This guide is intended to be a high-level overview of various things that you should be aware of as you build your demo. Where appropriate, we link to more detailed guides on specific topics.
5
+
6
+ 1. [Components](#components)
7
+ 2. [Queuing](#queuing)
8
+ 3. [Streaming outputs](#streaming-outputs)
9
+ 4. [Streaming inputs](#streaming-inputs)
10
+ 5. [Alert modals](#alert-modals)
11
+ 6. [Styling](#styling)
12
+ 7. [Progress bars](#progress-bars)
13
+ 8. [Batch functions](#batch-functions)
14
+
15
+ ## Components
16
+
17
+ Gradio includes more than 30 pre-built components (as well as many user-built _custom components_) that can be used as inputs or outputs in your demo with a single line of code. These components correspond to common data types in machine learning and data science, e.g. the `gr.Image` component is designed to handle input or output images, the `gr.Label` component displays classification labels and probabilities, the `gr.Plot` component displays various kinds of plots, and so on.
18
+
19
+ Each component includes various constructor attributes that control the properties of the component. For example, you can control the number of lines in a `gr.Textbox` using the `lines` argument (which takes a positive integer) in its constructor. Or you can control the way that a user can provide an image in the `gr.Image` component using the `sources` parameter (which takes a list like `["webcam", "upload"]`).
20
+
21
+ **Static and Interactive Components**
22
+
23
+ Every component has a _static_ version that is designed to *display* data, and most components also have an _interactive_ version designed to let users input or modify the data. Typically, you don't need to think about this distinction, because when you build a Gradio demo, Gradio automatically figures out whether the component should be static or interactive based on whether it is being used as an input or output. However, you can set this manually using the `interactive` argument that every component supports.
24
+
25
+ **Preprocessing and Postprocessing**
26
+
27
+ When a component is used as an input, Gradio automatically handles the _preprocessing_ needed to convert the data from a type sent by the user's browser (such as an uploaded image) to a form that can be accepted by your function (such as a `numpy` array).
28
+
29
+
30
+ Similarly, when a component is used as an output, Gradio automatically handles the _postprocessing_ needed to convert the data from what is returned by your function (such as a list of image paths) to a form that can be displayed in the user's browser (a gallery of images).
31
+
32
+ Consider an example demo with three input components (`gr.Textbox`, `gr.Number`, and `gr.Image`) and two outputs (`gr.Number` and `gr.Gallery`) that serve as a UI for your image-to-image generation model. Below is a diagram of what our preprocessing will send to the model and what our postprocessing will require from it.
33
+
34
+ ![](https://github.com/gradio-app/gradio/blob/main/guides/assets/dataflow.svg?raw=true)
35
+
36
+ In this image, the following preprocessing steps happen to send the data from the browser to your function:
37
+
38
+ * The text in the textbox is converted to a Python `str` (essentially no preprocessing)
39
+ * The number in the number input is converted to a Python `int` (essentially no preprocessing)
40
+ * Most importantly, ihe image supplied by the user is converted to a `numpy.array` representation of the RGB values in the image
41
+
42
+ Images are converted to NumPy arrays because they are a common format for machine learning workflows. You can control the _preprocessing_ using the component's parameters when constructing the component. For example, if you instantiate the `Image` component with the following parameters, it will preprocess the image to the `PIL` format instead:
43
+
44
+ ```py
45
+ img = gr.Image(type="pil")
46
+ ```
47
+
48
+ Postprocessing is even simpler! Gradio automatically recognizes the format of the returned data (e.g. does the user's function return a `numpy` array or a `str` filepath for the `gr.Image` component?) and postprocesses it appropriately into a format that can be displayed by the browser.
49
+
50
+ So in the image above, the following postprocessing steps happen to send the data returned from a user's function to the browser:
51
+
52
+ * The `float` is displayed as a number and displayed directly to the user
53
+ * The list of string filepaths (`list[str]`) is interpreted as a list of image filepaths and displayed as a gallery in the browser
54
+
55
+ Take a look at the [Docs](https://gradio.app/docs) to see all the parameters for each Gradio component.
56
+
57
+ ## Queuing
58
+
59
+ Every Gradio app comes with a built-in queuing system that can scale to thousands of concurrent users. You can configure the queue by using `queue()` method which is supported by the `gr.Interface`, `gr.Blocks`, and `gr.ChatInterface` classes.
60
+
61
+ For example, you can control the number of requests processed at a single time by setting the `default_concurrency_limit` parameter of `queue()`, e.g.
62
+
63
+ ```python
64
+ demo = gr.Interface(...).queue(default_concurrency_limit=5)
65
+ demo.launch()
66
+ ```
67
+
68
+ This limits the number of requests processed for this event listener at a single time to 5. By default, the `default_concurrency_limit` is actually set to `1`, which means that when many users are using your app, only a single user's request will be processed at a time. This is because many machine learning functions consume a significant amount of memory and so it is only suitable to have a single user using the demo at a time. However, you can change this parameter in your demo easily.
69
+
70
+ See the [docs on queueing](https://gradio.app/docs/gradio/interface#interface-queue) for more details on configuring the queuing parameters.
71
+
72
+ ## Streaming outputs
73
+
74
+ In some cases, you may want to stream a sequence of outputs rather than show a single output at once. For example, you might have an image generation model and you want to show the image that is generated at each step, leading up to the final image. Or you might have a chatbot which streams its response one token at a time instead of returning it all at once.
75
+
76
+ In such cases, you can supply a **generator** function into Gradio instead of a regular function. Creating generators in Python is very simple: instead of a single `return` value, a function should `yield` a series of values instead. Usually the `yield` statement is put in some kind of loop. Here's an example of an generator that simply counts up to a given number:
77
+
78
+ ```python
79
+ def my_generator(x):
80
+ for i in range(x):
81
+ yield i
82
+ ```
83
+
84
+ You supply a generator into Gradio the same way as you would a regular function. For example, here's a a (fake) image generation model that generates noise for several steps before outputting an image using the `gr.Interface` class:
85
+
86
+ $code_fake_diffusion
87
+ $demo_fake_diffusion
88
+
89
+ Note that we've added a `time.sleep(1)` in the iterator to create an artificial pause between steps so that you are able to observe the steps of the iterator (in a real image generation model, this probably wouldn't be necessary).
90
+
91
+ ## Streaming inputs
92
+
93
+ Similarly, Gradio can handle streaming inputs, e.g. a live audio stream that can gets transcribed to text in real time, or an image generation model that reruns every time a user types a letter in a textbox. This is covered in more details in our guide on building [reactive Interfaces](/guides/reactive-interfaces).
94
+
95
+ ## Alert modals
96
+
97
+ You may wish to raise alerts to the user. To do so, raise a `gr.Error("custom message")` to display an error message. You can also issue `gr.Warning("message")` and `gr.Info("message")` by having them as standalone lines in your function, which will immediately display modals while continuing the execution of your function. Queueing needs to be enabled for this to work.
98
+
99
+ Note below how the `gr.Error` has to be raised, while the `gr.Warning` and `gr.Info` are single lines.
100
+
101
+ ```python
102
+ def start_process(name):
103
+ gr.Info("Starting process")
104
+ if name is None:
105
+ gr.Warning("Name is empty")
106
+ ...
107
+ if success == False:
108
+ raise gr.Error("Process failed")
109
+ ```
110
+
111
+
112
+
113
+ ## Styling
114
+
115
+ Gradio themes are the easiest way to customize the look and feel of your app. You can choose from a variety of themes, or create your own. To do so, pass the `theme=` kwarg to the `Interface` constructor. For example:
116
+
117
+ ```python
118
+ demo = gr.Interface(..., theme=gr.themes.Monochrome())
119
+ ```
120
+
121
+ Gradio comes with a set of prebuilt themes which you can load from `gr.themes.*`. You can extend these themes or create your own themes from scratch - see the [theming guide](https://gradio.app/guides/theming-guide) for more details.
122
+
123
+ For additional styling ability, you can pass any CSS (as well as custom JavaScript) to your Gradio application. This is discussed in more detail in our [custom JS and CSS guide](/guides/custom-CSS-and-JS).
124
+
125
+
126
+ ## Progress bars
127
+
128
+ Gradio supports the ability to create custom Progress Bars so that you have customizability and control over the progress update that you show to the user. In order to enable this, simply add an argument to your method that has a default value of a `gr.Progress` instance. Then you can update the progress levels by calling this instance directly with a float between 0 and 1, or using the `tqdm()` method of the `Progress` instance to track progress over an iterable, as shown below.
129
+
130
+ $code_progress_simple
131
+ $demo_progress_simple
132
+
133
+ If you use the `tqdm` library, you can even report progress updates automatically from any `tqdm.tqdm` that already exists within your function by setting the default argument as `gr.Progress(track_tqdm=True)`!
134
+
135
+ ## Batch functions
136
+
137
+ Gradio supports the ability to pass _batch_ functions. Batch functions are just
138
+ functions which take in a list of inputs and return a list of predictions.
139
+
140
+ For example, here is a batched function that takes in two lists of inputs (a list of
141
+ words and a list of ints), and returns a list of trimmed words as output:
142
+
143
+ ```py
144
+ import time
145
+
146
+ def trim_words(words, lens):
147
+ trimmed_words = []
148
+ time.sleep(5)
149
+ for w, l in zip(words, lens):
150
+ trimmed_words.append(w[:int(l)])
151
+ return [trimmed_words]
152
+ ```
153
+
154
+ The advantage of using batched functions is that if you enable queuing, the Gradio server can automatically _batch_ incoming requests and process them in parallel,
155
+ potentially speeding up your demo. Here's what the Gradio code looks like (notice the `batch=True` and `max_batch_size=16`)
156
+
157
+ With the `gr.Interface` class:
158
+
159
+ ```python
160
+ demo = gr.Interface(
161
+ fn=trim_words,
162
+ inputs=["textbox", "number"],
163
+ outputs=["output"],
164
+ batch=True,
165
+ max_batch_size=16
166
+ )
167
+
168
+ demo.launch()
169
+ ```
170
+
171
+ With the `gr.Blocks` class:
172
+
173
+ ```py
174
+ import gradio as gr
175
+
176
+ with gr.Blocks() as demo:
177
+ with gr.Row():
178
+ word = gr.Textbox(label="word")
179
+ leng = gr.Number(label="leng")
180
+ output = gr.Textbox(label="Output")
181
+ with gr.Row():
182
+ run = gr.Button()
183
+
184
+ event = run.click(trim_words, [word, leng], output, batch=True, max_batch_size=16)
185
+
186
+ demo.launch()
187
+ ```
188
+
189
+ In the example above, 16 requests could be processed in parallel (for a total inference time of 5 seconds), instead of each request being processed separately (for a total
190
+ inference time of 80 seconds). Many Hugging Face `transformers` and `diffusers` models work very naturally with Gradio's batch mode: here's [an example demo using diffusers to
191
+ generate images in batches](https://github.com/gradio-app/gradio/blob/main/demo/diffusers_with_batching/run.py)
192
+
193
+
194
+
sources/02_streaming-outputs.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Streaming outputs
3
+
4
+ In some cases, you may want to stream a sequence of outputs rather than show a single output at once. For example, you might have an image generation model and you want to show the image that is generated at each step, leading up to the final image. Or you might have a chatbot which streams its response one token at a time instead of returning it all at once.
5
+
6
+ In such cases, you can supply a **generator** function into Gradio instead of a regular function. Creating generators in Python is very simple: instead of a single `return` value, a function should `yield` a series of values instead. Usually the `yield` statement is put in some kind of loop. Here's an example of an generator that simply counts up to a given number:
7
+
8
+ ```python
9
+ def my_generator(x):
10
+ for i in range(x):
11
+ yield i
12
+ ```
13
+
14
+ You supply a generator into Gradio the same way as you would a regular function. For example, here's a a (fake) image generation model that generates noise for several steps before outputting an image using the `gr.Interface` class:
15
+
16
+ $code_fake_diffusion
17
+ $demo_fake_diffusion
18
+
19
+ Note that we've added a `time.sleep(1)` in the iterator to create an artificial pause between steps so that you are able to observe the steps of the iterator (in a real image generation model, this probably wouldn't be necessary).
20
+
21
+ Similarly, Gradio can handle streaming inputs, e.g. an image generation model that reruns every time a user types a letter in a textbox. This is covered in more details in our guide on building [reactive Interfaces](/guides/reactive-interfaces).
sources/03_alerts.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Alerts
3
+
4
+ You may wish to display alerts to the user. To do so, raise a `gr.Error("custom message")` in your function to halt the execution of your function and display an error message to the user.
5
+
6
+ Alternatively, can issue `gr.Warning("custom message")` or `gr.Info("custom message")` by having them as standalone lines in your function, which will immediately display modals while continuing the execution of your function. The only difference between `gr.Info()` and `gr.Warning()` is the color of the alert.
7
+
8
+ ```python
9
+ def start_process(name):
10
+ gr.Info("Starting process")
11
+ if name is None:
12
+ gr.Warning("Name is empty")
13
+ ...
14
+ if success == False:
15
+ raise gr.Error("Process failed")
16
+ ```
17
+
18
+ Tip: Note that `gr.Error()` is an exception that has to be raised, while `gr.Warning()` and `gr.Info()` are functions that are called directly.
19
+
sources/03_interface-state.md ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Interface State
3
+
4
+ So far, we've assumed that your demos are *stateless*: that they do not persist information beyond a single function call. What if you want to modify the behavior of your demo based on previous interactions with the demo? There are two approaches in Gradio: *global state* and *session state*.
5
+
6
+ ## Global State
7
+
8
+ If the state is something that should be accessible to all function calls and all users, you can create a variable outside the function call and access it inside the function. For example, you may load a large model outside the function and use it inside the function so that every function call does not need to reload the model.
9
+
10
+ $code_score_tracker
11
+
12
+ In the code above, the `scores` array is shared between all users. If multiple users are accessing this demo, their scores will all be added to the same list, and the returned top 3 scores will be collected from this shared reference.
13
+
14
+ ## Session State
15
+
16
+ Another type of data persistence Gradio supports is session state, where data persists across multiple submits within a page session. However, data is _not_ shared between different users of your model. To store data in a session state, you need to do three things:
17
+
18
+ 1. Pass in an extra parameter into your function, which represents the state of the interface.
19
+ 2. At the end of the function, return the updated value of the state as an extra return value.
20
+ 3. Add the `'state'` input and `'state'` output components when creating your `Interface`
21
+
22
+ Here's a simple app to illustrate session state - this app simply stores users previous submissions and displays them back to the user:
23
+
24
+
25
+ $code_interface_state
26
+ $demo_interface_state
27
+
28
+
29
+ Notice how the state persists across submits within each page, but if you load this demo in another tab (or refresh the page), the demos will not share chat history. Here, we could not store the submission history in a global variable, otherwise the submission history would then get jumbled between different users.
30
+
31
+ The initial value of the `State` is `None` by default. If you pass a parameter to the `value` argument of `gr.State()`, it is used as the default value of the state instead.
32
+
33
+ Note: the `Interface` class only supports a single session state variable (though it can be a list with multiple elements). For more complex use cases, you can use Blocks, [which supports multiple `State` variables](/guides/state-in-blocks/). Alternatively, if you are building a chatbot that maintains user state, consider using the `ChatInterface` abstraction, [which manages state automatically](/guides/creating-a-chatbot-fast).
sources/03_state-in-blocks.md ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # State in Blocks
3
+
4
+ We covered [State in Interfaces](https://gradio.app/interface-state), this guide takes a look at state in Blocks, which works mostly the same.
5
+
6
+ ## Global State
7
+
8
+ Global state in Blocks works the same as in Interface. Any variable created outside a function call is a reference shared between all users.
9
+
10
+ ## Session State
11
+
12
+ Gradio supports session **state**, where data persists across multiple submits within a page session, in Blocks apps as well. To reiterate, session data is _not_ shared between different users of your model. To store data in a session state, you need to do three things:
13
+
14
+ 1. Create a `gr.State()` object. If there is a default value to this stateful object, pass that into the constructor.
15
+ 2. In the event listener, put the `State` object as an input and output.
16
+ 3. In the event listener function, add the variable to the input parameters and the return value.
17
+
18
+ Let's take a look at a game of hangman.
19
+
20
+ $code_hangman
21
+ $demo_hangman
22
+
23
+ Let's see how we do each of the 3 steps listed above in this game:
24
+
25
+ 1. We store the used letters in `used_letters_var`. In the constructor of `State`, we set the initial value of this to `[]`, an empty list.
26
+ 2. In `btn.click()`, we have a reference to `used_letters_var` in both the inputs and outputs.
27
+ 3. In `guess_letter`, we pass the value of this `State` to `used_letters`, and then return an updated value of this `State` in the return statement.
28
+
29
+ With more complex apps, you will likely have many State variables storing session state in a single Blocks app.
30
+
31
+ Learn more about `State` in the [docs](https://gradio.app/docs/state).
sources/04_dynamic-apps-with-render-decorator.md ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Dynamic Apps with the Render Decorator
3
+
4
+ The components and event listeners you define in a Blocks so far have been fixed - once the demo was launched, new components and listeners could not be added, and existing one could not be removed.
5
+
6
+ The `@gr.render` decorator introduces the ability to dynamically change this. Let's take a look.
7
+
8
+ ## Dynamic Number of Components
9
+
10
+ In the example below, we will create a variable number of Textboxes. When the user edits the input Textbox, we create a Textbox for each letter in the input. Try it out below:
11
+
12
+ $code_render_split_simple
13
+ $demo_render_split_simple
14
+
15
+ See how we can now create a variable number of Textboxes using our custom logic - in this case, a simple `for` loop. The `@gr.render` decorator enables this with the following steps:
16
+
17
+ 1. Create a function and attach the @gr.render decorator to it.
18
+ 2. Add the input components to the `inputs=` argument of @gr.render, and create a corresponding argument in your function for each component. This function will automatically re-run on any change to a component.
19
+ 3. Add all components inside the function that you want to render based on the inputs.
20
+
21
+ Now whenever the inputs change, the function re-runs, and replaces the components created from the previous function run with the latest run. Pretty straightforward! Let's add a little more complexity to this app:
22
+
23
+ $code_render_split
24
+ $demo_render_split
25
+
26
+ By default, `@gr.render` re-runs are triggered by the `.load` listener to the app and the `.change` listener to any input component provided. We can override this by explicitly setting the triggers in the decorator, as we have in this app to only trigger on `input_text.submit` instead.
27
+ If you are setting custom triggers, and you also want an automatic render at the start of the app, make sure to add `demo.load` to your list of triggers.
28
+
29
+ ## Dynamic Event Listeners
30
+
31
+ If you're creating components, you probably want to attach event listeners to them as well. Let's take a look at an example that takes in a variable number of Textbox as input, and merges all the text into a single box.
32
+
33
+ $code_render_merge_simple
34
+ $demo_render_merge_simple
35
+
36
+ Let's take a look at what's happening here:
37
+
38
+ 1. The state variable `text_count` is keeping track of the number of Textboxes to create. By clicking on the Add button, we increase `text_count` which triggers the render decorator.
39
+ 2. Note that in every single Textbox we create in the render function, we explicitly set a `key=` argument. This key allows us to preserve the value of this Component between re-renders. If you type in a value in a textbox, and then click the Add button, all the Textboxes re-render, but their values aren't cleared because the `key=` maintains the the value of a Component across a render.
40
+ 3. We've stored the Textboxes created in a list, and provide this list as input to the merge button event listener. Note that **all event listeners that use Components created inside a render function must also be defined inside that render function**. The event listener can still reference Components outside the render function, as we do here by referencing `merge_btn` and `output` which are both defined outside the render function.
41
+
42
+ Just as with Components, whenever a function re-renders, the event listeners created from the previous render are cleared and the new event listeners from the latest run are attached.
43
+
44
+ This allows us to create highly customizable and complex interactions!
45
+
46
+ ## Putting it Together
47
+
48
+ Let's look at two examples that use all the features above. First, try out the to-do list app below:
49
+
50
+ $code_todo_list
51
+ $demo_todo_list
52
+
53
+ Note that almost the entire app is inside a single `gr.render` that reacts to the tasks `gr.State` variable. This variable is a nested list, which presents some complexity. If you design a `gr.render` to react to a list or dict structure, ensure you do the following:
54
+
55
+ 1. Any event listener that modifies a state variable in a manner that should trigger a re-render must set the state variable as an output. This lets Gradio know to check if the variable has changed behind the scenes.
56
+ 2. In a `gr.render`, if a variable in a loop is used inside an event listener function, that variable should be "frozen" via setting it to itself as a default argument in the function header. See how we have `task=task` in both `mark_done` and `delete`. This freezes the variable to its "loop-time" value.
57
+
58
+ Let's take a look at one last example that uses everything we learned. Below is an audio mixer. Provide multiple audio tracks and mix them together.
59
+
60
+ $code_audio_mixer
61
+ $demo_audio_mixer
62
+
63
+ Two things to not in this app:
64
+ 1. Here we provide `key=` to all the components! We need to do this so that if we add another track after setting the values for an existing track, our input values to the existing track do not get reset on re-render.
65
+ 2. When there are lots of components of different types and arbitrary counts passed to an event listener, it is easier to use the set and dictionary notation for inputs rather than list notation. Above, we make one large set of all the input `gr.Audio` and `gr.Slider` components when we pass the inputs to the `merge` function. In the function body we query the component values as a dict.
66
+
67
+ The `gr.render` expands gradio capabilities extensively - see what you can make out of it!
sources/04_reactive-interfaces.md ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Reactive Interfaces
3
+
4
+ Finally, we cover how to get Gradio demos to refresh automatically or continuously stream data.
5
+
6
+ ## Live Interfaces
7
+
8
+ You can make interfaces automatically refresh by setting `live=True` in the interface. Now the interface will recalculate as soon as the user input changes.
9
+
10
+ $code_calculator_live
11
+ $demo_calculator_live
12
+
13
+ Note there is no submit button, because the interface resubmits automatically on change.
14
+
15
+ ## Streaming Components
16
+
17
+ Some components have a "streaming" mode, such as `Audio` component in microphone mode, or the `Image` component in webcam mode. Streaming means data is sent continuously to the backend and the `Interface` function is continuously being rerun.
18
+
19
+ The difference between `gr.Audio(source='microphone')` and `gr.Audio(source='microphone', streaming=True)`, when both are used in `gr.Interface(live=True)`, is that the first `Component` will automatically submit data and run the `Interface` function when the user stops recording, whereas the second `Component` will continuously send data and run the `Interface` function _during_ recording.
20
+
21
+ Here is example code of streaming images from the webcam.
22
+
23
+ $code_stream_frames
24
+
25
+ Streaming can also be done in an output component. A `gr.Audio(streaming=True)` output component can take a stream of audio data yielded piece-wise by a generator function and combines them into a single audio file.
26
+
27
+ $code_stream_audio_out
28
+
29
+ For a more detailed example, see our guide on performing [automatic speech recognition](/guides/real-time-speech-recognition) with Gradio.
sources/04_styling.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Styling
3
+
4
+ Gradio themes are the easiest way to customize the look and feel of your app. You can choose from a variety of themes, or create your own. To do so, pass the `theme=` kwarg to the `Interface` constructor. For example:
5
+
6
+ ```python
7
+ demo = gr.Interface(..., theme=gr.themes.Monochrome())
8
+ ```
9
+
10
+ Gradio comes with a set of prebuilt themes which you can load from `gr.themes.*`. You can extend these themes or create your own themes from scratch - see the [theming guide](https://gradio.app/guides/theming-guide) for more details.
11
+
12
+ For additional styling ability, you can pass any CSS (as well as custom JavaScript) to your Gradio application. This is discussed in more detail in our [custom JS and CSS guide](/guides/custom-CSS-and-JS).
13
+
sources/05_custom-CSS-and-JS.md ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Customizing your demo with CSS and Javascript
3
+
4
+ Gradio allows you to customize your demo in several ways. You can customize the layout of your demo, add custom HTML, and add custom theming as well. This tutorial will go beyond that and walk you through how to add custom CSS and JavaScript code to your demo in order to add custom styling, animations, custom UI functionality, analytics, and more.
5
+
6
+ ## Adding custom CSS to your demo
7
+
8
+ Gradio themes are the easiest way to customize the look and feel of your app. You can choose from a variety of themes, or create your own. To do so, pass the `theme=` kwarg to the `Blocks` constructor. For example:
9
+
10
+ ```python
11
+ with gr.Blocks(theme=gr.themes.Glass()):
12
+ ...
13
+ ```
14
+
15
+ Gradio comes with a set of prebuilt themes which you can load from `gr.themes.*`. You can extend these themes or create your own themes from scratch - see the [Theming guide](/guides/theming-guide) for more details.
16
+
17
+ For additional styling ability, you can pass any CSS to your app using the `css=` kwarg. You can either the filepath to a CSS file, or a string of CSS code.
18
+
19
+ **Warning**: The use of query selectors in custom JS and CSS is _not_ guaranteed to work across Gradio versions as the Gradio HTML DOM may change. We recommend using query selectors sparingly.
20
+
21
+ The base class for the Gradio app is `gradio-container`, so here's an example that changes the background color of the Gradio app:
22
+
23
+ ```python
24
+ with gr.Blocks(css=".gradio-container {background-color: red}") as demo:
25
+ ...
26
+ ```
27
+
28
+ If you'd like to reference external files in your css, preface the file path (which can be a relative or absolute path) with `"file="`, for example:
29
+
30
+ ```python
31
+ with gr.Blocks(css=".gradio-container {background: url('file=clouds.jpg')}") as demo:
32
+ ...
33
+ ```
34
+
35
+ Note: By default, files in the host machine are not accessible to users running the Gradio app. As a result, you should make sure that any referenced files (such as `clouds.jpg` here) are either URLs or allowed via the `allow_list` parameter in `launch()`. Read more in our [section on Security and File Access](/guides/sharing-your-app#security-and-file-access).
36
+
37
+
38
+ ## The `elem_id` and `elem_classes` Arguments
39
+
40
+ You can `elem_id` to add an HTML element `id` to any component, and `elem_classes` to add a class or list of classes. This will allow you to select elements more easily with CSS. This approach is also more likely to be stable across Gradio versions as built-in class names or ids may change (however, as mentioned in the warning above, we cannot guarantee complete compatibility between Gradio versions if you use custom CSS as the DOM elements may themselves change).
41
+
42
+ ```python
43
+ css = """
44
+ #warning {background-color: #FFCCCB}
45
+ .feedback textarea {font-size: 24px !important}
46
+ """
47
+
48
+ with gr.Blocks(css=css) as demo:
49
+ box1 = gr.Textbox(value="Good Job", elem_classes="feedback")
50
+ box2 = gr.Textbox(value="Failure", elem_id="warning", elem_classes="feedback")
51
+ ```
52
+
53
+ The CSS `#warning` ruleset will only target the second Textbox, while the `.feedback` ruleset will target both. Note that when targeting classes, you might need to put the `!important` selector to override the default Gradio styles.
54
+
55
+ ## Adding custom JavaScript to your demo
56
+
57
+ There are 3 ways to add javascript code to your Gradio demo:
58
+
59
+ 1. You can add JavaScript code as a string or as a filepath to the `js` parameter of the `Blocks` or `Interface` initializer. This will run the JavaScript code when the demo is first loaded.
60
+
61
+ Below is an example of adding custom js to show an animated welcome message when the demo first loads.
62
+
63
+ $code_blocks_js_load
64
+ $demo_blocks_js_load
65
+
66
+ Note: You can also supply your custom js code as a file path. For example, if you have a file called `custom.js` in the same directory as your Python script, you can add it to your demo like so: `with gr.Blocks(js="custom.js") as demo:`. Same goes for `Interface` (ex: `gr.Interface(..., js="custom.js")`).
67
+
68
+ 2. When using `Blocks` and event listeners, events have a `js` argument that can take a JavaScript function as a string and treat it just like a Python event listener function. You can pass both a JavaScript function and a Python function (in which case the JavaScript function is run first) or only Javascript (and set the Python `fn` to `None`). Take a look at the code below:
69
+
70
+ $code_blocks_js_methods
71
+ $demo_blocks_js_methods
72
+
73
+ 3. Lastly, you can add JavaScript code to the `head` param of the `Blocks` initializer. This will add the code to the head of the HTML document. For example, you can add Google Analytics to your demo like so:
74
+
75
+
76
+ ```python
77
+ head = f"""
78
+ <script async src="https://www.googletagmanager.com/gtag/js?id={google_analytics_tracking_id}"></script>
79
+ <script>
80
+ window.dataLayer = window.dataLayer || [];
81
+ function gtag(){{dataLayer.push(arguments);}}
82
+ gtag('js', new Date());
83
+ gtag('config', '{google_analytics_tracking_id}');
84
+ </script>
85
+ """
86
+
87
+ with gr.Blocks(head=head) as demo:
88
+ ...demo code...
89
+ ```
90
+
91
+ The `head` parameter accepts any HTML tags you would normally insert into the `<head>` of a page. For example, you can also include `<meta>` tags to `head`.
92
+
93
+ Note that injecting custom HTML can affect browser behavior and compatibility (e.g. keyboard shortcuts). You should test your interface across different browsers and be mindful of how scripts may interact with browser defaults.
94
+ Here's an example where pressing `Shift + s` triggers the `click` event of a specific `Button` component if the browser focus is _not_ on an input component (e.g. `Textbox` component):
95
+
96
+ ```python
97
+ import gradio as gr
98
+
99
+ shortcut_js = """
100
+ <script>
101
+ function shortcuts(e) {
102
+ var event = document.all ? window.event : e;
103
+ switch (e.target.tagName.toLowerCase()) {
104
+ case "input":
105
+ case "textarea":
106
+ break;
107
+ default:
108
+ if (e.key.toLowerCase() == "s" && e.shiftKey) {
109
+ document.getElementById("my_btn").click();
110
+ }
111
+ }
112
+ }
113
+ document.addEventListener('keypress', shortcuts, false);
114
+ </script>
115
+ """
116
+
117
+ with gr.Blocks(head=shortcut_js) as demo:
118
+ action_button = gr.Button(value="Name", elem_id="my_btn")
119
+ textbox = gr.Textbox()
120
+ action_button.click(lambda : "button pressed", None, textbox)
121
+
122
+ demo.launch()
123
+ ```
sources/05_four-kinds-of-interfaces.md ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # The 4 Kinds of Gradio Interfaces
3
+
4
+ So far, we've always assumed that in order to build an Gradio demo, you need both inputs and outputs. But this isn't always the case for machine learning demos: for example, _unconditional image generation models_ don't take any input but produce an image as the output.
5
+
6
+ It turns out that the `gradio.Interface` class can actually handle 4 different kinds of demos:
7
+
8
+ 1. **Standard demos**: which have both separate inputs and outputs (e.g. an image classifier or speech-to-text model)
9
+ 2. **Output-only demos**: which don't take any input but produce on output (e.g. an unconditional image generation model)
10
+ 3. **Input-only demos**: which don't produce any output but do take in some sort of input (e.g. a demo that saves images that you upload to a persistent external database)
11
+ 4. **Unified demos**: which have both input and output components, but the input and output components _are the same_. This means that the output produced overrides the input (e.g. a text autocomplete model)
12
+
13
+ Depending on the kind of demo, the user interface (UI) looks slightly different:
14
+
15
+ ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/interfaces4.png)
16
+
17
+ Let's see how to build each kind of demo using the `Interface` class, along with examples:
18
+
19
+ ## Standard demos
20
+
21
+ To create a demo that has both the input and the output components, you simply need to set the values of the `inputs` and `outputs` parameter in `Interface()`. Here's an example demo of a simple image filter:
22
+
23
+ $code_sepia_filter
24
+ $demo_sepia_filter
25
+
26
+ ## Output-only demos
27
+
28
+ What about demos that only contain outputs? In order to build such a demo, you simply set the value of the `inputs` parameter in `Interface()` to `None`. Here's an example demo of a mock image generation model:
29
+
30
+ $code_fake_gan_no_input
31
+ $demo_fake_gan_no_input
32
+
33
+ ## Input-only demos
34
+
35
+ Similarly, to create a demo that only contains inputs, set the value of `outputs` parameter in `Interface()` to be `None`. Here's an example demo that saves any uploaded image to disk:
36
+
37
+ $code_save_file_no_output
38
+ $demo_save_file_no_output
39
+
40
+ ## Unified demos
41
+
42
+ A demo that has a single component as both the input and the output. It can simply be created by setting the values of the `inputs` and `outputs` parameter as the same component. Here's an example demo of a text generation model:
43
+
44
+ $code_unified_demo_text_generation
45
+ $demo_unified_demo_text_generation
sources/05_progress-bars.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Progress Bars
3
+
4
+ Gradio supports the ability to create custom Progress Bars so that you have customizability and control over the progress update that you show to the user. In order to enable this, simply add an argument to your method that has a default value of a `gr.Progress` instance. Then you can update the progress levels by calling this instance directly with a float between 0 and 1, or using the `tqdm()` method of the `Progress` instance to track progress over an iterable, as shown below.
5
+
6
+ $code_progress_simple
7
+ $demo_progress_simple
8
+
9
+ If you use the `tqdm` library, you can even report progress updates automatically from any `tqdm.tqdm` that already exists within your function by setting the default argument as `gr.Progress(track_tqdm=True)`!
sources/06_batch-functions.md ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Batch functions
3
+
4
+ Gradio supports the ability to pass _batch_ functions. Batch functions are just
5
+ functions which take in a list of inputs and return a list of predictions.
6
+
7
+ For example, here is a batched function that takes in two lists of inputs (a list of
8
+ words and a list of ints), and returns a list of trimmed words as output:
9
+
10
+ ```py
11
+ import time
12
+
13
+ def trim_words(words, lens):
14
+ trimmed_words = []
15
+ time.sleep(5)
16
+ for w, l in zip(words, lens):
17
+ trimmed_words.append(w[:int(l)])
18
+ return [trimmed_words]
19
+ ```
20
+
21
+ The advantage of using batched functions is that if you enable queuing, the Gradio server can automatically _batch_ incoming requests and process them in parallel,
22
+ potentially speeding up your demo. Here's what the Gradio code looks like (notice the `batch=True` and `max_batch_size=16`)
23
+
24
+ With the `gr.Interface` class:
25
+
26
+ ```python
27
+ demo = gr.Interface(
28
+ fn=trim_words,
29
+ inputs=["textbox", "number"],
30
+ outputs=["output"],
31
+ batch=True,
32
+ max_batch_size=16
33
+ )
34
+
35
+ demo.launch()
36
+ ```
37
+
38
+ With the `gr.Blocks` class:
39
+
40
+ ```py
41
+ import gradio as gr
42
+
43
+ with gr.Blocks() as demo:
44
+ with gr.Row():
45
+ word = gr.Textbox(label="word")
46
+ leng = gr.Number(label="leng")
47
+ output = gr.Textbox(label="Output")
48
+ with gr.Row():
49
+ run = gr.Button()
50
+
51
+ event = run.click(trim_words, [word, leng], output, batch=True, max_batch_size=16)
52
+
53
+ demo.launch()
54
+ ```
55
+
56
+ In the example above, 16 requests could be processed in parallel (for a total inference time of 5 seconds), instead of each request being processed separately (for a total
57
+ inference time of 80 seconds). Many Hugging Face `transformers` and `diffusers` models work very naturally with Gradio's batch mode: here's [an example demo using diffusers to
58
+ generate images in batches](https://github.com/gradio-app/gradio/blob/main/demo/diffusers_with_batching/run.py)
59
+
60
+
sources/06_using-blocks-like-functions.md ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Using Gradio Blocks Like Functions
3
+
4
+ Tags: TRANSLATION, HUB, SPACES
5
+
6
+ **Prerequisite**: This Guide builds on the Blocks Introduction. Make sure to [read that guide first](https://gradio.app/blocks-and-event-listeners).
7
+
8
+ ## Introduction
9
+
10
+ Did you know that apart from being a full-stack machine learning demo, a Gradio Blocks app is also a regular-old python function!?
11
+
12
+ This means that if you have a gradio Blocks (or Interface) app called `demo`, you can use `demo` like you would any python function.
13
+
14
+ So doing something like `output = demo("Hello", "friend")` will run the first event defined in `demo` on the inputs "Hello" and "friend" and store it
15
+ in the variable `output`.
16
+
17
+ If I put you to sleep 🥱, please bear with me! By using apps like functions, you can seamlessly compose Gradio apps.
18
+ The following section will show how.
19
+
20
+ ## Treating Blocks like functions
21
+
22
+ Let's say we have the following demo that translates english text to german text.
23
+
24
+ $code_english_translator
25
+
26
+ I already went ahead and hosted it in Hugging Face spaces at [gradio/english_translator](https://huggingface.co/spaces/gradio/english_translator).
27
+
28
+ You can see the demo below as well:
29
+
30
+ $demo_english_translator
31
+
32
+ Now, let's say you have an app that generates english text, but you wanted to additionally generate german text.
33
+
34
+ You could either:
35
+
36
+ 1. Copy the source code of my english-to-german translation and paste it in your app.
37
+
38
+ 2. Load my english-to-german translation in your app and treat it like a normal python function.
39
+
40
+ Option 1 technically always works, but it often introduces unwanted complexity.
41
+
42
+ Option 2 lets you borrow the functionality you want without tightly coupling our apps.
43
+
44
+ All you have to do is call the `Blocks.load` class method in your source file.
45
+ After that, you can use my translation app like a regular python function!
46
+
47
+ The following code snippet and demo shows how to use `Blocks.load`.
48
+
49
+ Note that the variable `english_translator` is my english to german app, but its used in `generate_text` like a regular function.
50
+
51
+ $code_generate_english_german
52
+
53
+ $demo_generate_english_german
54
+
55
+ ## How to control which function in the app to use
56
+
57
+ If the app you are loading defines more than one function, you can specify which function to use
58
+ with the `fn_index` and `api_name` parameters.
59
+
60
+ In the code for our english to german demo, you'll see the following line:
61
+
62
+ ```python
63
+ translate_btn.click(translate, inputs=english, outputs=german, api_name="translate-to-german")
64
+ ```
65
+
66
+ The `api_name` gives this function a unique name in our app. You can use this name to tell gradio which
67
+ function in the upstream space you want to use:
68
+
69
+ ```python
70
+ english_generator(text, api_name="translate-to-german")[0]["generated_text"]
71
+ ```
72
+
73
+ You can also use the `fn_index` parameter.
74
+ Imagine my app also defined an english to spanish translation function.
75
+ In order to use it in our text generation app, we would use the following code:
76
+
77
+ ```python
78
+ english_generator(text, fn_index=1)[0]["generated_text"]
79
+ ```
80
+
81
+ Functions in gradio spaces are zero-indexed, so since the spanish translator would be the second function in my space,
82
+ you would use index 1.
83
+
84
+ ## Parting Remarks
85
+
86
+ We showed how treating a Blocks app like a regular python helps you compose functionality across different apps.
87
+ Any Blocks app can be treated like a function, but a powerful pattern is to `load` an app hosted on
88
+ [Hugging Face Spaces](https://huggingface.co/spaces) prior to treating it like a function in your own app.
89
+ You can also load models hosted on the [Hugging Face Model Hub](https://huggingface.co/models) - see the [Using Hugging Face Integrations](/using_hugging_face_integrations) guide for an example.
90
+
91
+ ### Happy building! ⚒️
sources/07_resource-cleanup.md ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Resource Cleanup
3
+
4
+ Your Gradio application may create resources during its lifetime.
5
+ Examples of resources are `gr.State` variables, any variables you create and explicitly hold in memory, or files you save to disk.
6
+ Over time, these resources can use up all of your server's RAM or disk space and crash your application.
7
+
8
+ Gradio provides some tools for you to clean up the resources created by your app:
9
+
10
+ 1. Automatic deletion of `gr.State` variables.
11
+ 2. Automatic cache cleanup with the `delete_cache` parameter.
12
+ 2. The `Blocks.unload` event.
13
+
14
+ Let's take a look at each of them individually.
15
+
16
+ ## Automatic deletion of `gr.State`
17
+
18
+ When a user closes their browser tab, Gradio will automatically delete any `gr.State` variables associated with that user session after 60 minutes. If the user connects again within those 60 minutes, no state will be deleted.
19
+
20
+ You can control the deletion behavior further with the following two parameters of `gr.State`:
21
+
22
+ 1. `delete_callback` - An arbitrary function that will be called when the variable is deleted. This function must take the state value as input. This function is useful for deleting variables from GPU memory.
23
+ 2. `time_to_live` - The number of seconds the state should be stored for after it is created or updated. This will delete variables before the session is closed, so it's useful for clearing state for potentially long running sessions.
24
+
25
+ ## Automatic cache cleanup via `delete_cache`
26
+
27
+ Your Gradio application will save uploaded and generated files to a special directory called the cache directory. Gradio uses a hashing scheme to ensure that duplicate files are not saved to the cache but over time the size of the cache will grow (especially if your app goes viral 😉).
28
+
29
+ Gradio can periodically clean up the cache for you if you specify the `delete_cache` parameter of `gr.Blocks()`, `gr.Interface()`, or `gr.ChatInterface()`.
30
+ This parameter is a tuple of the form `[frequency, age]` both expressed in number of seconds.
31
+ Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created.
32
+ For example, setting this to (86400, 86400) will delete temporary files every day if they are older than a day old.
33
+ Additionally, the cache will be deleted entirely when the server restarts.
34
+
35
+ ## The `unload` event
36
+
37
+ Additionally, Gradio now includes a `Blocks.unload()` event, allowing you to run arbitrary cleanup functions when users disconnect (this does not have a 60 minute delay).
38
+ Unlike other gradio events, this event does not accept inputs or outptus.
39
+ You can think of the `unload` event as the opposite of the `load` event.
40
+
41
+ ## Putting it all together
42
+
43
+ The following demo uses all of these features. When a user visits the page, a special unique directory is created for that user.
44
+ As the user interacts with the app, images are saved to disk in that special directory.
45
+ When the user closes the page, the images created in that session are deleted via the `unload` event.
46
+ The state and files in the cache are cleaned up automatically as well.
47
+
48
+ $code_state_cleanup
49
+ $demo_state_cleanup
sources/08_environment-variables.md ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Environment Variables
3
+
4
+ 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.
5
+
6
+ ## Key Environment Variables
7
+
8
+ ### 1. `GRADIO_SERVER_PORT`
9
+
10
+ - **Description**: Specifies the port on which the Gradio app will run.
11
+ - **Default**: `7860`
12
+ - **Example**:
13
+ ```bash
14
+ export GRADIO_SERVER_PORT=8000
15
+ ```
16
+
17
+ ### 2. `GRADIO_SERVER_NAME`
18
+
19
+ - **Description**: Defines the host name for the Gradio server. To make Gradio accessible from any IP address, set this to `"0.0.0.0"`
20
+ - **Default**: `"127.0.0.1"`
21
+ - **Example**:
22
+ ```bash
23
+ export GRADIO_SERVER_NAME="0.0.0.0"
24
+ ```
25
+
26
+ ### 3. `GRADIO_ANALYTICS_ENABLED`
27
+
28
+ - **Description**: Whether Gradio should provide
29
+ - **Default**: `"True"`
30
+ - **Options**: `"True"`, `"False"`
31
+ - **Example**:
32
+ ```sh
33
+ export GRADIO_ANALYTICS_ENABLED="True"
34
+ ```
35
+
36
+ ### 4. `GRADIO_DEBUG`
37
+
38
+ - **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.
39
+ - **Default**: `0`
40
+ - **Example**:
41
+ ```sh
42
+ export GRADIO_DEBUG=1
43
+ ```
44
+
45
+ ### 5. `GRADIO_ALLOW_FLAGGING`
46
+
47
+ - **Description**: Controls whether users can flag inputs/outputs in the Gradio interface. See [the Guide on flagging](/guides/using-flagging) for more details.
48
+ - **Default**: `"manual"`
49
+ - **Options**: `"never"`, `"manual"`, `"auto"`
50
+ - **Example**:
51
+ ```sh
52
+ export GRADIO_ALLOW_FLAGGING="never"
53
+ ```
54
+
55
+ ### 6. `GRADIO_TEMP_DIR`
56
+
57
+ - **Description**: Specifies the directory where temporary files created by Gradio are stored.
58
+ - **Default**: System default temporary directory
59
+ - **Example**:
60
+ ```sh
61
+ export GRADIO_TEMP_DIR="/path/to/temp"
62
+ ```
63
+
64
+ ### 7. `GRADIO_ROOT_PATH`
65
+
66
+ - **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).
67
+ - **Default**: `""`
68
+ - **Example**:
69
+ ```sh
70
+ export GRADIO_ROOT_PATH="/myapp"
71
+ ```
72
+
73
+ ### 8. `GRADIO_SHARE`
74
+
75
+ - **Description**: Enables or disables sharing the Gradio app.
76
+ - **Default**: `"False"`
77
+ - **Options**: `"True"`, `"False"`
78
+ - **Example**:
79
+ ```sh
80
+ export GRADIO_SHARE="True"
81
+ ```
82
+
83
+ ### 9. `GRADIO_ALLOWED_PATHS`
84
+
85
+ - **Description**: Sets a list of complete filepaths or parent directories that gradio is allowed to serve. Must be absolute paths. Warning: if you provide directories, any files in these directories or their subdirectories are accessible to all users of your app. Multiple items can be specified by separating items with commas.
86
+ - **Default**: `""`
87
+ - **Example**:
88
+ ```sh
89
+ export GRADIO_ALLOWED_PATHS="/mnt/sda1,/mnt/sda2"
90
+ ```
91
+
92
+ ### 10. `GRADIO_BLOCKED_PATHS`
93
+
94
+ - **Description**: Sets a list of complete filepaths or parent directories that gradio is not allowed to serve (i.e. users of your app are not allowed to access). Must be absolute paths. Warning: takes precedence over `allowed_paths` and all other directories exposed by Gradio by default. Multiple items can be specified by separating items with commas.
95
+ - **Default**: `""`
96
+ - **Example**:
97
+ ```sh
98
+ export GRADIO_BLOCKED_PATHS="/users/x/gradio_app/admin,/users/x/gradio_app/keys"
99
+ ```
100
+
101
+
102
+ ## How to Set Environment Variables
103
+
104
+ To set environment variables in your terminal, use the `export` command followed by the variable name and its value. For example:
105
+
106
+ ```sh
107
+ export GRADIO_SERVER_PORT=8000
108
+ ```
109
+
110
+ If you're using a `.env` file to manage your environment variables, you can add them like this:
111
+
112
+ ```sh
113
+ GRADIO_SERVER_PORT=8000
114
+ GRADIO_SERVER_NAME="localhost"
115
+ ```
116
+
117
+ Then, use a tool like `dotenv` to load these variables when running your application.
118
+
sources/09_sharing-your-app.md ADDED
@@ -0,0 +1,482 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Sharing Your App
3
+
4
+ In this Guide, we dive more deeply into the various aspects of sharing a Gradio app with others. We will cover:
5
+
6
+ 1. [Sharing demos with the share parameter](#sharing-demos)
7
+ 2. [Hosting on HF Spaces](#hosting-on-hf-spaces)
8
+ 3. [Embedding hosted spaces](#embedding-hosted-spaces)
9
+ 4. [Using the API page](#api-page)
10
+ 5. [Accessing network requests](#accessing-the-network-request-directly)
11
+ 6. [Mounting within FastAPI](#mounting-within-another-fast-api-app)
12
+ 7. [Authentication](#authentication)
13
+ 8. [Security and file access](#security-and-file-access)
14
+ 9. [Analytics](#analytics)
15
+
16
+ ## Sharing Demos
17
+
18
+ Gradio demos can be easily shared publicly by setting `share=True` in the `launch()` method. Like this:
19
+
20
+ ```python
21
+ import gradio as gr
22
+
23
+ def greet(name):
24
+ return "Hello " + name + "!"
25
+
26
+ demo = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
27
+
28
+ demo.launch(share=True) # Share your demo with just 1 extra parameter 🚀
29
+ ```
30
+
31
+ This generates a public, shareable link that you can send to anybody! When you send this link, the user on the other side can try out the model in their browser. Because the processing happens on your device (as long as your device stays on), you don't have to worry about any packaging any dependencies.
32
+
33
+ ![sharing](https://github.com/gradio-app/gradio/blob/main/guides/assets/sharing.svg?raw=true)
34
+
35
+
36
+ A share link usually looks something like this: **https://07ff8706ab.gradio.live**. Although the link is served through the Gradio Share Servers, these servers are only a proxy for your local server, and do not store any data sent through your app. Share links expire after 72 hours. (it is [also possible to set up your own Share Server](https://github.com/huggingface/frp/) on your own cloud server to overcome this restriction.)
37
+
38
+ Tip: Keep in mind that share links are publicly accessible, meaning that anyone can use your model for prediction! Therefore, make sure not to expose any sensitive information through the functions you write, or allow any critical changes to occur on your device. Or you can [add authentication to your Gradio app](#authentication) as discussed below.
39
+
40
+ Note that by default, `share=False`, which means that your server is only running locally. (This is the default, except in Google Colab notebooks, where share links are automatically created). As an alternative to using share links, you can use use [SSH port-forwarding](https://www.ssh.com/ssh/tunneling/example) to share your local server with specific users.
41
+
42
+
43
+ ## Hosting on HF Spaces
44
+
45
+ If you'd like to have a permanent link to your Gradio demo on the internet, use Hugging Face Spaces. [Hugging Face Spaces](http://huggingface.co/spaces/) provides the infrastructure to permanently host your machine learning model for free!
46
+
47
+ After you have [created a free Hugging Face account](https://huggingface.co/join), you have two methods to deploy your Gradio app to Hugging Face Spaces:
48
+
49
+ 1. From terminal: run `gradio deploy` in your app directory. The CLI will gather some basic metadata and then launch your app. To update your space, you can re-run this command or enable the Github Actions option to automatically update the Spaces on `git push`.
50
+
51
+ 2. From your browser: Drag and drop a folder containing your Gradio model and all related files [here](https://huggingface.co/new-space). See [this guide how to host on Hugging Face Spaces](https://huggingface.co/blog/gradio-spaces) for more information, or watch the embedded video:
52
+
53
+ <video autoplay muted loop>
54
+ <source src="https://github.com/gradio-app/gradio/blob/main/guides/assets/hf_demo.mp4?raw=true" type="video/mp4" />
55
+ </video>
56
+
57
+
58
+ ## Embedding Hosted Spaces
59
+
60
+ Once you have hosted your app on Hugging Face Spaces (or on your own server), you may want to embed the demo on a different website, such as your blog or your portfolio. Embedding an interactive demo allows people to try out the machine learning model that you have built, without needing to download or install anything — right in their browser! The best part is that you can embed interactive demos even in static websites, such as GitHub pages.
61
+
62
+ There are two ways to embed your Gradio demos. You can find quick links to both options directly on the Hugging Face Space page, in the "Embed this Space" dropdown option:
63
+
64
+ ![Embed this Space dropdown option](https://github.com/gradio-app/gradio/blob/main/guides/assets/embed_this_space.png?raw=true)
65
+
66
+ ### Embedding with Web Components
67
+
68
+ Web components typically offer a better experience to users than IFrames. Web components load lazily, meaning that they won't slow down the loading time of your website, and they automatically adjust their height based on the size of the Gradio app.
69
+
70
+ To embed with Web Components:
71
+
72
+ 1. Import the gradio JS library into into your site by adding the script below in your site (replace {GRADIO_VERSION} in the URL with the library version of Gradio you are using).
73
+
74
+ ```html
75
+ <script
76
+ type="module"
77
+ src="https://gradio.s3-us-west-2.amazonaws.com/{GRADIO_VERSION}/gradio.js"
78
+ ></script>
79
+ ```
80
+
81
+ 2. Add
82
+
83
+ ```html
84
+ <gradio-app src="https://$your_space_host.hf.space"></gradio-app>
85
+ ```
86
+
87
+ element where you want to place the app. Set the `src=` attribute to your Space's embed URL, which you can find in the "Embed this Space" button. For example:
88
+
89
+ ```html
90
+ <gradio-app
91
+ src="https://abidlabs-pytorch-image-classifier.hf.space"
92
+ ></gradio-app>
93
+ ```
94
+
95
+ <script>
96
+ fetch("https://pypi.org/pypi/gradio/json"
97
+ ).then(r => r.json()
98
+ ).then(obj => {
99
+ let v = obj.info.version;
100
+ content = document.querySelector('.prose');
101
+ content.innerHTML = content.innerHTML.replaceAll("{GRADIO_VERSION}", v);
102
+ });
103
+ </script>
104
+
105
+ You can see examples of how web components look <a href="https://www.gradio.app">on the Gradio landing page</a>.
106
+
107
+ You can also customize the appearance and behavior of your web component with attributes that you pass into the `<gradio-app>` tag:
108
+
109
+ - `src`: as we've seen, the `src` attributes links to the URL of the hosted Gradio demo that you would like to embed
110
+ - `space`: an optional shorthand if your Gradio demo is hosted on Hugging Face Space. Accepts a `username/space_name` instead of a full URL. Example: `gradio/Echocardiogram-Segmentation`. If this attribute attribute is provided, then `src` does not need to be provided.
111
+ - `control_page_title`: a boolean designating whether the html title of the page should be set to the title of the Gradio app (by default `"false"`)
112
+ - `initial_height`: the initial height of the web component while it is loading the Gradio app, (by default `"300px"`). Note that the final height is set based on the size of the Gradio app.
113
+ - `container`: whether to show the border frame and information about where the Space is hosted (by default `"true"`)
114
+ - `info`: whether to show just the information about where the Space is hosted underneath the embedded app (by default `"true"`)
115
+ - `autoscroll`: whether to autoscroll to the output when prediction has finished (by default `"false"`)
116
+ - `eager`: whether to load the Gradio app as soon as the page loads (by default `"false"`)
117
+ - `theme_mode`: whether to use the `dark`, `light`, or default `system` theme mode (by default `"system"`)
118
+ - `render`: an event that is triggered once the embedded space has finished rendering.
119
+
120
+ Here's an example of how to use these attributes to create a Gradio app that does not lazy load and has an initial height of 0px.
121
+
122
+ ```html
123
+ <gradio-app
124
+ space="gradio/Echocardiogram-Segmentation"
125
+ eager="true"
126
+ initial_height="0px"
127
+ ></gradio-app>
128
+ ```
129
+
130
+ Here's another example of how to use the `render` event. An event listener is used to capture the `render` event and will call the `handleLoadComplete()` function once rendering is complete.
131
+
132
+ ```html
133
+ <script>
134
+ function handleLoadComplete() {
135
+ console.log("Embedded space has finished rendering");
136
+ }
137
+
138
+ const gradioApp = document.querySelector("gradio-app");
139
+ gradioApp.addEventListener("render", handleLoadComplete);
140
+ </script>
141
+ ```
142
+
143
+ _Note: While Gradio's CSS will never impact the embedding page, the embedding page can affect the style of the embedded Gradio app. Make sure that any CSS in the parent page isn't so general that it could also apply to the embedded Gradio app and cause the styling to break. Element selectors such as `header { ... }` and `footer { ... }` will be the most likely to cause issues._
144
+
145
+ ### Embedding with IFrames
146
+
147
+ To embed with IFrames instead (if you cannot add javascript to your website, for example), add this element:
148
+
149
+ ```html
150
+ <iframe src="https://$your_space_host.hf.space"></iframe>
151
+ ```
152
+
153
+ Again, you can find the `src=` attribute to your Space's embed URL, which you can find in the "Embed this Space" button.
154
+
155
+ Note: if you use IFrames, you'll probably want to add a fixed `height` attribute and set `style="border:0;"` to remove the boreder. In addition, if your app requires permissions such as access to the webcam or the microphone, you'll need to provide that as well using the `allow` attribute.
156
+
157
+ ## API Page
158
+
159
+ You can use almost any Gradio app as an API! In the footer of a Gradio app [like this one](https://huggingface.co/spaces/gradio/hello_world), you'll see a "Use via API" link.
160
+
161
+ ![Use via API](https://github.com/gradio-app/gradio/blob/main/guides/assets/use_via_api.png?raw=true)
162
+
163
+ This is a page that lists the endpoints that can be used to query the Gradio app, via our supported clients: either [the Python client](https://gradio.app/guides/getting-started-with-the-python-client/), or [the JavaScript client](https://gradio.app/guides/getting-started-with-the-js-client/). For each endpoint, Gradio automatically generates the parameters and their types, as well as example inputs, like this.
164
+
165
+ ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/view-api.png)
166
+
167
+ The endpoints are automatically created when you launch a Gradio `Interface`. If you are using Gradio `Blocks`, you can also set up a Gradio API page, though we recommend that you explicitly name each event listener, such as
168
+
169
+ ```python
170
+ btn.click(add, [num1, num2], output, api_name="addition")
171
+ ```
172
+
173
+ This will add and document the endpoint `/api/addition/` to the automatically generated API page. Otherwise, your API endpoints will appear as "unnamed" endpoints.
174
+
175
+ ## Accessing the Network Request Directly
176
+
177
+ When a user makes a prediction to your app, you may need the underlying network request, in order to get the request headers (e.g. for advanced authentication), log the client's IP address, getting the query parameters, or for other reasons. Gradio supports this in a similar manner to FastAPI: simply add a function parameter whose type hint is `gr.Request` and Gradio will pass in the network request as that parameter. Here is an example:
178
+
179
+ ```python
180
+ import gradio as gr
181
+
182
+ def echo(text, request: gr.Request):
183
+ if request:
184
+ print("Request headers dictionary:", request.headers)
185
+ print("IP address:", request.client.host)
186
+ print("Query parameters:", dict(request.query_params))
187
+ return text
188
+
189
+ io = gr.Interface(echo, "textbox", "textbox").launch()
190
+ ```
191
+
192
+ Note: if your function is called directly instead of through the UI (this happens, for
193
+ example, when examples are cached, or when the Gradio app is called via API), then `request` will be `None`.
194
+ You should handle this case explicitly to ensure that your app does not throw any errors. That is why
195
+ we have the explicit check `if request`.
196
+
197
+ ## Mounting Within Another FastAPI App
198
+
199
+ In some cases, you might have an existing FastAPI app, and you'd like to add a path for a Gradio demo.
200
+ You can easily do this with `gradio.mount_gradio_app()`.
201
+
202
+ Here's a complete example:
203
+
204
+ $code_custom_path
205
+
206
+ Note that this approach also allows you run your Gradio apps on custom paths (`http://localhost:8000/gradio` in the example above).
207
+
208
+
209
+ ## Authentication
210
+
211
+ ### Password-protected app
212
+
213
+ You may wish to put an authentication page in front of your app to limit who can open your app. With the `auth=` keyword argument in the `launch()` method, you can provide a tuple with a username and password, or a list of acceptable username/password tuples; Here's an example that provides password-based authentication for a single user named "admin":
214
+
215
+ ```python
216
+ demo.launch(auth=("admin", "pass1234"))
217
+ ```
218
+
219
+ For more complex authentication handling, you can even pass a function that takes a username and password as arguments, and returns `True` to allow access, `False` otherwise.
220
+
221
+ Here's an example of a function that accepts any login where the username and password are the same:
222
+
223
+ ```python
224
+ def same_auth(username, password):
225
+ return username == password
226
+ demo.launch(auth=same_auth)
227
+ ```
228
+
229
+ If you have multiple users, you may wish to customize the content that is shown depending on the user that is logged in. You can retrieve the logged in user by [accessing the network request directly](#accessing-the-network-request-directly) as discussed above, and then reading the `.username` attribute of the request. Here's an example:
230
+
231
+
232
+ ```python
233
+ import gradio as gr
234
+
235
+ def update_message(request: gr.Request):
236
+ return f"Welcome, {request.username}"
237
+
238
+ with gr.Blocks() as demo:
239
+ m = gr.Markdown()
240
+ demo.load(update_message, None, m)
241
+
242
+ demo.launch(auth=[("Abubakar", "Abubakar"), ("Ali", "Ali")])
243
+ ```
244
+
245
+ Note: For authentication to work properly, third party cookies must be enabled in your browser. This is not the case by default for Safari or for Chrome Incognito Mode.
246
+
247
+ If users visit the `/logout` page of your Gradio app, they will automatically be logged out and session cookies deleted. This allows you to add logout functionality to your Gradio app as well. Let's update the previous example to include a log out button:
248
+
249
+ ```python
250
+ import gradio as gr
251
+
252
+ def update_message(request: gr.Request):
253
+ return f"Welcome, {request.username}"
254
+
255
+ with gr.Blocks() as demo:
256
+ m = gr.Markdown()
257
+ logout_button = gr.Button("Logout", link="/logout")
258
+ demo.load(update_message, None, m)
259
+
260
+ demo.launch(auth=[("Pete", "Pete"), ("Dawood", "Dawood")])
261
+ ```
262
+
263
+ Note: Gradio's built-in authentication provides a straightforward and basic layer of access control but does not offer robust security features for applications that require stringent access controls (e.g. multi-factor authentication, rate limiting, or automatic lockout policies).
264
+
265
+ ### OAuth (Login via Hugging Face)
266
+
267
+ Gradio natively supports OAuth login via Hugging Face. In other words, you can easily add a _"Sign in with Hugging Face"_ button to your demo, which allows you to get a user's HF username as well as other information from their HF profile. Check out [this Space](https://huggingface.co/spaces/Wauplin/gradio-oauth-demo) for a live demo.
268
+
269
+ To enable OAuth, you must set `hf_oauth: true` as a Space metadata in your README.md file. This will register your Space
270
+ as an OAuth application on Hugging Face. Next, you can use `gr.LoginButton` to add a login button to
271
+ your Gradio app. Once a user is logged in with their HF account, you can retrieve their profile by adding a parameter of type
272
+ `gr.OAuthProfile` to any Gradio function. The user profile will be automatically injected as a parameter value. If you want
273
+ to perform actions on behalf of the user (e.g. list user's private repos, create repo, etc.), you can retrieve the user
274
+ token by adding a parameter of type `gr.OAuthToken`. You must define which scopes you will use in your Space metadata
275
+ (see [documentation](https://huggingface.co/docs/hub/spaces-oauth#scopes) for more details).
276
+
277
+ Here is a short example:
278
+
279
+ ```py
280
+ import gradio as gr
281
+ from huggingface_hub import whoami
282
+
283
+ def hello(profile: gr.OAuthProfile | None) -> str:
284
+ if profile is None:
285
+ return "I don't know you."
286
+ return f"Hello {profile.name}"
287
+
288
+ def list_organizations(oauth_token: gr.OAuthToken | None) -> str:
289
+ if oauth_token is None:
290
+ return "Please log in to list organizations."
291
+ org_names = [org["name"] for org in whoami(oauth_token.token)["orgs"]]
292
+ return f"You belong to {', '.join(org_names)}."
293
+
294
+ with gr.Blocks() as demo:
295
+ gr.LoginButton()
296
+ m1 = gr.Markdown()
297
+ m2 = gr.Markdown()
298
+ demo.load(hello, inputs=None, outputs=m1)
299
+ demo.load(list_organizations, inputs=None, outputs=m2)
300
+
301
+ demo.launch()
302
+ ```
303
+
304
+ When the user clicks on the login button, they get redirected in a new page to authorize your Space.
305
+
306
+ <center>
307
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio-guides/oauth_sign_in.png" style="width:300px; max-width:80%">
308
+ </center>
309
+
310
+ Users can revoke access to their profile at any time in their [settings](https://huggingface.co/settings/connected-applications).
311
+
312
+ As seen above, OAuth features are available only when your app runs in a Space. However, you often need to test your app
313
+ locally before deploying it. To test OAuth features locally, your machine must be logged in to Hugging Face. Please run `huggingface-cli login` or set `HF_TOKEN` as environment variable with one of your access token. You can generate a new token in your settings page (https://huggingface.co/settings/tokens). Then, clicking on the `gr.LoginButton` will login your local Hugging Face profile, allowing you to debug your app with your Hugging Face account before deploying it to a Space.
314
+
315
+
316
+ ### OAuth (with external providers)
317
+
318
+ It is also possible to authenticate with external OAuth providers (e.g. Google OAuth) in your Gradio apps. To do this, first mount your Gradio app within a FastAPI app ([as discussed above](#mounting-within-another-fast-api-app)). Then, you must write an *authentication function*, which gets the user's username from the OAuth provider and returns it. This function should be passed to the `auth_dependency` parameter in `gr.mount_gradio_app`.
319
+
320
+ Similar to [FastAPI dependency functions](https://fastapi.tiangolo.com/tutorial/dependencies/), the function specified by `auth_dependency` will run before any Gradio-related route in your FastAPI app. The function should accept a single parameter: the FastAPI `Request` and return either a string (representing a user's username) or `None`. If a string is returned, the user will be able to access the Gradio-related routes in your FastAPI app.
321
+
322
+ First, let's show a simplistic example to illustrate the `auth_dependency` parameter:
323
+
324
+ ```python
325
+ from fastapi import FastAPI, Request
326
+ import gradio as gr
327
+
328
+ app = FastAPI()
329
+
330
+ def get_user(request: Request):
331
+ return request.headers.get("user")
332
+
333
+ demo = gr.Interface(lambda s: f"Hello {s}!", "textbox", "textbox")
334
+
335
+ app = gr.mount_gradio_app(app, demo, path="/demo", auth_dependency=get_user)
336
+
337
+ if __name__ == '__main__':
338
+ uvicorn.run(app)
339
+ ```
340
+
341
+ In this example, only requests that include a "user" header will be allowed to access the Gradio app. Of course, this does not add much security, since any user can add this header in their request.
342
+
343
+ Here's a more complete example showing how to add Google OAuth to a Gradio app (assuming you've already created OAuth Credentials on the [Google Developer Console](https://console.cloud.google.com/project)):
344
+
345
+ ```python
346
+ import os
347
+ from authlib.integrations.starlette_client import OAuth, OAuthError
348
+ from fastapi import FastAPI, Depends, Request
349
+ from starlette.config import Config
350
+ from starlette.responses import RedirectResponse
351
+ from starlette.middleware.sessions import SessionMiddleware
352
+ import uvicorn
353
+ import gradio as gr
354
+
355
+ app = FastAPI()
356
+
357
+ # Replace these with your own OAuth settings
358
+ GOOGLE_CLIENT_ID = "..."
359
+ GOOGLE_CLIENT_SECRET = "..."
360
+ SECRET_KEY = "..."
361
+
362
+ config_data = {'GOOGLE_CLIENT_ID': GOOGLE_CLIENT_ID, 'GOOGLE_CLIENT_SECRET': GOOGLE_CLIENT_SECRET}
363
+ starlette_config = Config(environ=config_data)
364
+ oauth = OAuth(starlette_config)
365
+ oauth.register(
366
+ name='google',
367
+ server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
368
+ client_kwargs={'scope': 'openid email profile'},
369
+ )
370
+
371
+ SECRET_KEY = os.environ.get('SECRET_KEY') or "a_very_secret_key"
372
+ app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)
373
+
374
+ # Dependency to get the current user
375
+ def get_user(request: Request):
376
+ user = request.session.get('user')
377
+ if user:
378
+ return user['name']
379
+ return None
380
+
381
+ @app.get('/')
382
+ def public(user: dict = Depends(get_user)):
383
+ if user:
384
+ return RedirectResponse(url='/gradio')
385
+ else:
386
+ return RedirectResponse(url='/login-demo')
387
+
388
+ @app.route('/logout')
389
+ async def logout(request: Request):
390
+ request.session.pop('user', None)
391
+ return RedirectResponse(url='/')
392
+
393
+ @app.route('/login')
394
+ async def login(request: Request):
395
+ redirect_uri = request.url_for('auth')
396
+ # If your app is running on https, you should ensure that the
397
+ # `redirect_uri` is https, e.g. uncomment the following lines:
398
+ #
399
+ # from urllib.parse import urlparse, urlunparse
400
+ # redirect_uri = urlunparse(urlparse(str(redirect_uri))._replace(scheme='https'))
401
+ return await oauth.google.authorize_redirect(request, redirect_uri)
402
+
403
+ @app.route('/auth')
404
+ async def auth(request: Request):
405
+ try:
406
+ access_token = await oauth.google.authorize_access_token(request)
407
+ except OAuthError:
408
+ return RedirectResponse(url='/')
409
+ request.session['user'] = dict(access_token)["userinfo"]
410
+ return RedirectResponse(url='/')
411
+
412
+ with gr.Blocks() as login_demo:
413
+ gr.Button("Login", link="/login")
414
+
415
+ app = gr.mount_gradio_app(app, login_demo, path="/login-demo")
416
+
417
+ def greet(request: gr.Request):
418
+ return f"Welcome to Gradio, {request.username}"
419
+
420
+ with gr.Blocks() as main_demo:
421
+ m = gr.Markdown("Welcome to Gradio!")
422
+ gr.Button("Logout", link="/logout")
423
+ main_demo.load(greet, None, m)
424
+
425
+ app = gr.mount_gradio_app(app, main_demo, path="/gradio", auth_dependency=get_user)
426
+
427
+ if __name__ == '__main__':
428
+ uvicorn.run(app)
429
+ ```
430
+
431
+ There are actually two separate Gradio apps in this example! One that simply displays a log in button (this demo is accessible to any user), while the other main demo is only accessible to users that are logged in. You can try this example out on [this Space](https://huggingface.co/spaces/gradio/oauth-example).
432
+
433
+
434
+
435
+ ## Security and File Access
436
+
437
+ Sharing your Gradio app with others (by hosting it on Spaces, on your own server, or through temporary share links) **exposes** certain files on the host machine to users of your Gradio app.
438
+
439
+ In particular, Gradio apps ALLOW users to access to four kinds of files:
440
+
441
+ - **Temporary files created by Gradio.** These are files that are created by Gradio as part of running your prediction function. For example, if your prediction function returns a video file, then Gradio will save that video to a temporary cache on your device and then send the path to the file to the front end. You can customize the location of temporary cache files created by Gradio by setting the environment variable `GRADIO_TEMP_DIR` to an absolute path, such as `/home/usr/scripts/project/temp/`. You can delete the files created by your app when it shuts down with the `delete_cache` parameter of `gradio.Blocks`, `gradio.Interface`, and `gradio.ChatInterface`. This parameter is a tuple of integers of the form `[frequency, age]` where `frequency` is how often to delete files and `age` is the time in seconds since the file was last modified.
442
+
443
+
444
+ - **Cached examples created by Gradio.** These are files that are created by Gradio as part of caching examples for faster runtimes, if you set `cache_examples=True` or `cache_examples="lazy"` in `gr.Interface()`, `gr.ChatInterface()` or in `gr.Examples()`. By default, these files are saved in the `gradio_cached_examples/` subdirectory within your app's working directory. You can customize the location of cached example files created by Gradio by setting the environment variable `GRADIO_EXAMPLES_CACHE` to an absolute path or a path relative to your working directory.
445
+
446
+ - **Files that you explicitly allow via the `allowed_paths` parameter in `launch()`**. This parameter allows you to pass in a list of additional directories or exact filepaths you'd like to allow users to have access to. (By default, this parameter is an empty list).
447
+
448
+ - **Static files that you explicitly set via the `gr.set_static_paths` function**. This parameter allows you to pass in a list of directories or filenames that will be considered static. This means that they will not be copied to the cache and will be served directly from your computer. This can help save disk space and reduce the time your app takes to launch but be mindful of possible security implications.
449
+
450
+ Gradio DOES NOT ALLOW access to:
451
+
452
+ - **Files that you explicitly block via the `blocked_paths` parameter in `launch()`**. You can pass in a list of additional directories or exact filepaths to the `blocked_paths` parameter in `launch()`. This parameter takes precedence over the files that Gradio exposes by default or by the `allowed_paths`.
453
+
454
+ - **Any other paths on the host machine**. Users should NOT be able to access other arbitrary paths on the host.
455
+
456
+ Sharing your Gradio application will also allow users to upload files to your computer or server. You can set a maximum file size for uploads to prevent abuse and to preserve disk space. You can do this with the `max_file_size` parameter of `.launch`. For example, the following two code snippets limit file uploads to 5 megabytes per file.
457
+
458
+ ```python
459
+ import gradio as gr
460
+
461
+ demo = gr.Interface(lambda x: x, "image", "image")
462
+
463
+ demo.launch(max_file_size="5mb")
464
+ # or
465
+ demo.launch(max_file_size=5 * gr.FileSize.MB)
466
+ ```
467
+
468
+ Please make sure you are running the latest version of `gradio` for these security settings to apply.
469
+
470
+ ## Analytics
471
+
472
+ By default, Gradio collects certain analytics to help us better understand the usage of the `gradio` library. This includes the following information:
473
+
474
+ * What environment the Gradio app is running on (e.g. Colab Notebook, Hugging Face Spaces)
475
+ * What input/output components are being used in the Gradio app
476
+ * Whether the Gradio app is utilizing certain advanced features, such as `auth` or `show_error`
477
+ * The IP address which is used solely to measure the number of unique developers using Gradio
478
+ * The version of Gradio that is running
479
+
480
+ No information is collected from _users_ of your Gradio app. If you'd like to diable analytics altogether, you can do so by setting the `analytics_enabled` parameter to `False` in `gr.Blocks`, `gr.Interface`, or `gr.ChatInterface`. Or, you can set the GRADIO_ANALYTICS_ENABLED environment variable to `"False"` to apply this to all Gradio apps created across your system.
481
+
482
+ *Note*: this reflects the analytics policy as of `gradio>=4.32.0`.