0
# Space Management
1
2
Hugging Face Space operations including duplication for private use, hardware configuration, and deployment as Discord bots.
3
4
## Capabilities
5
6
### Space Duplication
7
8
Create private copies of public Hugging Face Spaces for unlimited usage and custom configurations.
9
10
```python { .api }
11
@classmethod
12
def duplicate(
13
cls,
14
from_id: str,
15
to_id: str | None = None,
16
hf_token: str | None = None,
17
private: bool = True,
18
hardware: Literal[
19
"cpu-basic",
20
"cpu-upgrade",
21
"t4-small",
22
"t4-medium",
23
"a10g-small",
24
"a10g-large",
25
"a100-large",
26
] | SpaceHardware | None = None,
27
secrets: dict[str, str] | None = None,
28
sleep_timeout: int = 5,
29
max_workers: int = 40,
30
verbose: bool = True,
31
) -> Client:
32
"""
33
Duplicate a Hugging Face Space for private use.
34
35
Parameters:
36
- from_id: Source Space identifier (e.g., "abidlabs/whisper")
37
- to_id: Target Space name (auto-generated if None)
38
- hf_token: Hugging Face token for authentication
39
- private: Whether the duplicated Space should be private (default: True)
40
- hardware: Hardware configuration for the duplicated Space
41
- secrets: Environment secrets to set in the duplicated Space
42
- sleep_timeout: Time to wait between status checks during duplication
43
- max_workers: Maximum thread workers for the new client
44
- verbose: Whether to print status messages
45
46
Returns:
47
Client instance connected to the duplicated Space
48
49
Raises:
50
- AuthenticationError: If HF token is invalid or missing for private operations
51
- ValueError: If duplication fails or Space not found
52
"""
53
```
54
55
### Discord Bot Deployment
56
57
Deploy Gradio applications as Discord bots with configurable API endpoints and interaction patterns.
58
59
```python { .api }
60
def deploy_discord(
61
self,
62
discord_bot_token: str | None = None,
63
api_names: list[str | tuple[str, str]] | None = None,
64
to_id: str | None = None,
65
hf_token: str | None = None,
66
private: bool = False,
67
) -> None:
68
"""
69
Deploy the Gradio app as a Discord bot.
70
71
Parameters:
72
- discord_bot_token: Discord bot token for authentication (optional if explained in space)
73
- api_names: List of API endpoint names or (name, display_name) tuples to expose via Discord
74
- to_id: Target Space name for the Discord bot deployment (auto-generated if None)
75
- hf_token: Hugging Face token for Space operations
76
- private: Whether the space hosting the discord bot should be private
77
78
Raises:
79
- ValueError: If Discord token is invalid or deployment fails
80
- AuthenticationError: If HF token is required but invalid
81
"""
82
```
83
84
### Hardware Configuration Types
85
86
```python { .api }
87
# Hardware options for Space duplication
88
SpaceHardware = Literal[
89
"cpu-basic",
90
"cpu-upgrade",
91
"t4-small",
92
"t4-medium",
93
"a10g-small",
94
"a10g-large",
95
"a100-large"
96
]
97
```
98
99
## Usage Examples
100
101
### Basic Space Duplication
102
103
```python
104
from gradio_client import Client
105
106
# Duplicate a public Space for unlimited usage
107
client = Client.duplicate("abidlabs/whisper-large-v2")
108
109
# The client is now connected to your private copy
110
result = client.predict("audio.wav")
111
print(result)
112
```
113
114
### Advanced Space Duplication
115
116
```python
117
from gradio_client import Client
118
119
# Duplicate with custom configuration
120
client = Client.duplicate(
121
from_id="abidlabs/whisper-large-v2",
122
to_id="my-whisper-copy",
123
hf_token="hf_your_token_here",
124
hardware="t4-small",
125
private=True,
126
timeout=600 # 10 minute timeout
127
)
128
129
# Use the duplicated Space
130
result = client.predict("my_audio.wav")
131
```
132
133
### Hardware Upgrade
134
135
```python
136
from gradio_client import Client
137
138
# Duplicate with GPU hardware for faster processing
139
client = Client.duplicate(
140
"abidlabs/stable-diffusion",
141
hardware="a10g-small", # Use A10G GPU
142
hf_token="hf_your_token"
143
)
144
145
# Generate images with GPU acceleration
146
image = client.predict(
147
"a beautiful landscape",
148
api_name="/generate"
149
)
150
```
151
152
### Discord Bot Deployment
153
154
```python
155
from gradio_client import Client
156
157
# Connect to a Gradio app
158
client = Client("abidlabs/whisper-large-v2")
159
160
# Deploy as Discord bot
161
client.deploy_discord(
162
discord_bot_token="your_discord_bot_token",
163
api_names=["/predict"], # Expose the prediction endpoint
164
to_id="whisper-discord-bot"
165
)
166
167
# The bot is now deployed and can be invited to Discord servers
168
```
169
170
### Managing Existing Duplications
171
172
```python
173
from gradio_client import Client
174
175
# Connect to previously duplicated Space
176
# If Space already exists, it will connect instead of duplicating again
177
client = Client.duplicate(
178
"abidlabs/whisper",
179
to_id="my-existing-whisper",
180
exist_ok=True # Connect to existing if already duplicated
181
)
182
183
# Check if this is a new duplication or existing connection
184
print(f"Connected to: {client.src}")
185
```
186
187
### Cost-Conscious Usage
188
189
```python
190
from gradio_client import Client
191
192
# Duplicate with basic CPU for cost efficiency
193
client = Client.duplicate(
194
"username/text-processing-space",
195
hardware="cpu-basic", # Most cost-effective option
196
hf_token="hf_your_token"
197
)
198
199
# Process text data efficiently
200
result = client.predict("input text", api_name="/process")
201
202
# Close client to stop billing when done
203
client.close()
204
```
205
206
### Batch Processing with Private Space
207
208
```python
209
from gradio_client import Client
210
import time
211
212
# Duplicate for batch processing
213
client = Client.duplicate(
214
"abidlabs/image-classifier",
215
hardware="t4-medium",
216
hf_token="hf_your_token"
217
)
218
219
# Process multiple images
220
image_files = ["img1.jpg", "img2.jpg", "img3.jpg"]
221
results = []
222
223
for image_file in image_files:
224
result = client.predict(image_file, api_name="/classify")
225
results.append(result)
226
time.sleep(0.1) # Rate limiting
227
228
print(f"Processed {len(results)} images")
229
230
# Clean up
231
client.close()
232
```