Skip to content

Commit 992f84b

Browse files
Fix zerogu-guide (#11370)
* Add code * add changeset * Fix * Format * Pass in * add changeset * typos --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 0d60b46 commit 992f84b

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

.changeset/poor-brooms-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"website": patch
3+
---
4+
5+
fix:Fix zerogu-guide

js/_website/src/lib/templates/python-client/gradio_client/03_using-zero-gpu-spaces.svx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,28 @@ How to do this will be explained in the following section.
2424

2525
### Avoiding Rate Limits
2626

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.
2929
The following hypothetical text-to-image application shows how this is done.
3030
<br>
3131

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-
3832

3933
```python
4034
import gradio as gr
4135
from gradio_client import Client
4236

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})
4440
img = client.predict(prompt, api_name="/predict")
4541
return img
4642

4743

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-
5444
with gr.Blocks() as demo:
55-
client = gr.State()
5645
image = gr.Image()
5746
prompt = gr.Textbox(max_lines=1)
47+
prompt.submit(text_to_image, [prompt], [image])
5848

59-
prompt.submit(text_to_image, [client, prompt], [image])
60-
61-
demo.load(set_client_for_session, None, client)
49+
demo.launch()
6250
```
6351

0 commit comments

Comments
 (0)