@@ -24,40 +24,28 @@ How to do this will be explained in the following section.
24
24
25
25
### Avoiding Rate Limits
26
26
27
- When a user visits the page , we will extract their token from the `X-IP-Token` header of the incoming request.
28
- We will use this header value in all subsequent client requests .
27
+ When a user presses enter in the textbox , we will extract their token from the `X-IP-Token` header of the incoming request.
28
+ We will use this header when constructing the gradio client.
29
29
The following hypothetical text-to-image application shows how this is done.
30
30
<br>
31
31
32
- We use the `load` event to extract the user's `x-ip-token` header when they visit the page.
33
- We create a new client with this header passed to the `headers` parameter.
34
- This ensures all subsequent predictions pass this header to the ZeroGPU space.
35
- The client is saved in a State variable so that it's kept independent from other users.
36
- It will be deleted automatically when the user exits the page.
37
-
38
32
39
33
```python
40
34
import gradio as gr
41
35
from gradio_client import Client
42
36
43
- def text_to_image(client, prompt):
37
+ def text_to_image(prompt, request: gr.Request):
38
+ x_ip_token = request.headers['x-ip-token']
39
+ client = Client("hysts/SDXL", headers={"x-ip-token": x_ip_token})
44
40
img = client.predict(prompt, api_name="/predict")
45
41
return img
46
42
47
43
48
- def set_client_for_session(request: gr.Request):
49
- x_ip_token = request.headers['x-ip-token']
50
-
51
- # The "gradio/text-to-image" space is a ZeroGPU space
52
- return Client("gradio/text-to-image", headers={"X-IP-Token": x_ip_token})
53
-
54
44
with gr.Blocks() as demo:
55
- client = gr.State()
56
45
image = gr.Image()
57
46
prompt = gr.Textbox(max_lines=1)
47
+ prompt.submit(text_to_image, [prompt], [image])
58
48
59
- prompt.submit(text_to_image, [client, prompt], [image])
60
-
61
- demo.load(set_client_for_session, None, client)
49
+ demo.launch()
62
50
```
63
51
0 commit comments