chansung commited on
Commit
f1063ce
β€’
1 Parent(s): 1d35de6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +365 -0
app.py ADDED
@@ -0,0 +1,365 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Adapted from https://huggingface.co/spaces/stabilityai/stable-diffusion
3
+ """
4
+
5
+ from tensorflow import keras
6
+
7
+ import time
8
+ import json
9
+ import requests
10
+
11
+ import gradio as gr
12
+
13
+ def avaliable_providers():
14
+ providers = []
15
+
16
+ headers = {
17
+ "Content-Type": "application/json",
18
+ }
19
+ endpoint_url = "https://api.endpoints.huggingface.cloud/provider"
20
+ response = requests.get(endpoint_url, headers=headers)
21
+
22
+ for provider in response.json()['items']:
23
+ if provider['status'] == 'available':
24
+ providers.append(provider['vendor'])
25
+
26
+ return providers
27
+
28
+ def update_regions(provider):
29
+ avalialbe_regions = []
30
+
31
+ headers = {
32
+ "Content-Type": "application/json",
33
+ }
34
+ endpoint_url = f"https://api.endpoints.huggingface.cloud/provider/{provider}/region"
35
+ response = requests.get(endpoint_url, headers=headers)
36
+
37
+ for region in response.json()['items']:
38
+ if region['status'] == 'available':
39
+ avalialbe_regions.append(f"{region['region']}/{region['label']}")
40
+
41
+ return gr.Dropdown.update(
42
+ choices=avalialbe_regions,
43
+ value=avalialbe_regions[0] if len(avalialbe_regions) > 0 else None
44
+ )
45
+
46
+ def update_compute_options(provider, region):
47
+ region = region.split("/")[0]
48
+ avalialbe_compute_options = []
49
+
50
+ headers = {
51
+ "Content-Type": "application/json",
52
+ }
53
+ endpoint_url = f"https://api.endpoints.huggingface.cloud/provider/{provider}/region/{region}/compute"
54
+ print(endpoint_url)
55
+ response = requests.get(endpoint_url, headers=headers)
56
+
57
+ for compute in response.json()['items']:
58
+ if compute['status'] == 'available':
59
+ accelerator = compute['accelerator']
60
+ numAccelerators = compute['numAccelerators']
61
+ memoryGb = compute['memoryGb'].replace("Gi", "GB")
62
+ architecture = compute['architecture']
63
+ instanceType = compute['instanceType']
64
+
65
+ type = f"{numAccelerators}vCPU {memoryGb} Β· {architecture}" if accelerator == "cpu" else f"{numAccelerators}x {architecture}"
66
+
67
+ avalialbe_compute_options.append(
68
+ f"{compute['accelerator'].upper()} [{compute['instanceSize']}] Β· {type} Β· {instanceType}"
69
+ )
70
+
71
+ return gr.Dropdown.update(
72
+ choices=avalialbe_compute_options,
73
+ value=avalialbe_compute_options[0] if len(avalialbe_compute_options) > 0 else None
74
+ )
75
+
76
+ def submit(
77
+ hf_account_input,
78
+ hf_token_input,
79
+ endpoint_name_input,
80
+ provider_selector,
81
+ region_selector,
82
+ repository_selector,
83
+ task_selector,
84
+ framework_selector,
85
+ compute_selector,
86
+ min_node_selector,
87
+ max_node_selector,
88
+ security_selector
89
+ ):
90
+ compute_resources = compute_selector.split("Β·")
91
+ accelerator = compute_resources[0][:3].strip()
92
+
93
+ size_l_index = compute_resources[0].index("[") - 1
94
+ size_r_index = compute_resources[0].index("]")
95
+ size = compute_resources[0][size_l_index : size_r_index].strip()
96
+
97
+ type = compute_resources[-1].strip()
98
+
99
+ payload = {
100
+ "accountId": hf_account_input.strip(),
101
+ "compute": {
102
+ "accelerator": accelerator.lower(),
103
+ "instanceSize": size[1:],
104
+ "instanceType": type,
105
+ "scaling": {
106
+ "maxReplica": int(max_node_selector),
107
+ "minReplica": int(min_node_selector)
108
+ }
109
+ },
110
+ "model": {
111
+ "framework": framework_selector.lower(),
112
+ "image": {
113
+ "huggingface": {}
114
+ },
115
+ "repository": repository_selector.lower(),
116
+ "revision": head_sha,
117
+ "task": task_selector.lower()
118
+ },
119
+ "name": endpoint_name_input.strip(),
120
+ "provider": {
121
+ "region": region_selector.split("/")[0].lower(),
122
+ "vendor": provider_selector.lower()
123
+ },
124
+ "type": security_selector.lower()
125
+ }
126
+
127
+ print(payload)
128
+
129
+ payload = json.dumps(payload)
130
+ print(payload)
131
+
132
+ headers = {
133
+ "Authorization": f"Bearer {hf_token_input.strip()}",
134
+ "Content-Type": "application/json",
135
+ }
136
+ endpoint_url = f"https://api.endpoints.huggingface.cloud/endpoint"
137
+ print(endpoint_url)
138
+
139
+ response = requests.post(endpoint_url, headers=headers, data=payload)
140
+
141
+ if response.status_code == 400:
142
+ return f"{response.text}. Malformed data in {payload}"
143
+ elif response.status_code == 401:
144
+ return "Invalid token"
145
+ elif response.status_code == 409:
146
+ return f"Endpoint {endpoint_name_input} already exists"
147
+ elif response.status_code == 202:
148
+ return f"Endpoint {endpoint_name_input} created successfully on {provider_selector.lower()} using {repository_selector.lower()}@{head_sha}.\nPlease check out the progress at https://ui.endpoints.huggingface.co/endpoints."
149
+ else:
150
+ return f"something went wrong {response.status_code} = {response.text}"
151
+
152
+ with gr.Blocks() as hf_endpoint:
153
+ providers = avaliable_providers()
154
+
155
+ gr.Markdown(
156
+ """
157
+ ## Deploy Stable Diffusion on πŸ€— Endpoint
158
+ ---
159
+ """)
160
+
161
+ gr.Markdown("""
162
+ #### Your πŸ€— Account ID(Name)
163
+ """)
164
+ hf_account_input = gr.Textbox(
165
+ show_label=False,
166
+ )
167
+
168
+ gr.Markdown("""
169
+ #### Your πŸ€— Access Token
170
+ """)
171
+ hf_token_input = gr.Textbox(
172
+ show_label=False,
173
+ type="password"
174
+ )
175
+
176
+ gr.Markdown("""
177
+ #### Decide the Endpoint name
178
+ """)
179
+ endpoint_name_input = gr.Textbox(
180
+ show_label=False
181
+ )
182
+
183
+ with gr.Row():
184
+ gr.Markdown("""
185
+ #### Cloud Provider
186
+ """)
187
+
188
+ gr.Markdown("""
189
+ #### Cloud Region
190
+ """)
191
+
192
+ with gr.Row():
193
+ provider_selector = gr.Dropdown(
194
+ choices=providers,
195
+ interactive=True,
196
+ show_label=False,
197
+ )
198
+
199
+ region_selector = gr.Dropdown(
200
+ [],
201
+ value="",
202
+ interactive=True,
203
+ show_label=False,
204
+ )
205
+
206
+ provider_selector.change(update_regions, inputs=provider_selector, outputs=region_selector)
207
+
208
+ with gr.Row():
209
+ gr.Markdown("""
210
+ #### Target Model
211
+ """)
212
+
213
+ gr.Markdown("""
214
+ #### Target Model Version(branch)
215
+ """)
216
+
217
+ with gr.Row():
218
+ repository_selector = gr.Textbox(
219
+ value="chansung/my-kitty",
220
+ interactive=False,
221
+ show_label=False,
222
+ )
223
+
224
+ revision_selector = gr.Textbox(
225
+ value=f"v1673447112/{head_sha[:7]}",
226
+ interactive=False,
227
+ show_label=False,
228
+ )
229
+
230
+ with gr.Row():
231
+ gr.Markdown("""
232
+ #### Task
233
+ """)
234
+
235
+ gr.Markdown("""
236
+ #### Framework
237
+ """)
238
+
239
+ with gr.Row():
240
+ task_selector = gr.Textbox(
241
+ value="Custom",
242
+ interactive=False,
243
+ show_label=False,
244
+ )
245
+
246
+ framework_selector = gr.Textbox(
247
+ value="TensorFlow",
248
+ interactive=False,
249
+ show_label=False,
250
+ )
251
+
252
+ gr.Markdown("""
253
+
254
+ #### Select Compute Instance Type
255
+ """)
256
+ compute_selector = gr.Dropdown(
257
+ [],
258
+ value="",
259
+ interactive=True,
260
+ show_label=False,
261
+ )
262
+ region_selector.change(update_compute_options, inputs=[provider_selector, region_selector], outputs=compute_selector)
263
+
264
+ with gr.Row():
265
+ gr.Markdown("""
266
+ #### Min Number of Nodes
267
+ """)
268
+
269
+ gr.Markdown("""
270
+ #### Max Number of Nodes
271
+ """)
272
+
273
+ gr.Markdown("""
274
+ #### Security Level
275
+ """)
276
+
277
+ with gr.Row():
278
+ min_node_selector = gr.Number(
279
+ value=1,
280
+ interactive=True,
281
+ show_label=False,
282
+ )
283
+
284
+ max_node_selector = gr.Number(
285
+ value=1,
286
+ interactive=True,
287
+ show_label=False,
288
+ )
289
+
290
+ security_selector = gr.Radio(
291
+ choices=["Protected", "Public", "Private"],
292
+ value="Public",
293
+ interactive=True,
294
+ show_label=False,
295
+ )
296
+
297
+ submit_button = gr.Button(
298
+ value="Submit",
299
+ )
300
+
301
+ status_txt = gr.Textbox(
302
+ value="any status update will be displayed here",
303
+ interactive=False
304
+ )
305
+
306
+ submit_button.click(
307
+ submit,
308
+ inputs=[
309
+ hf_account_input,
310
+ hf_token_input,
311
+ endpoint_name_input,
312
+ provider_selector,
313
+ region_selector,
314
+ repository_selector,
315
+ task_selector,
316
+ framework_selector,
317
+ compute_selector,
318
+ min_node_selector,
319
+ max_node_selector,
320
+ security_selector],
321
+ outputs=status_txt)
322
+
323
+ gr.Markdown("""
324
+ #### Pricing Table(CPU) - 2023/1/11
325
+ """)
326
+
327
+ gr.Dataframe(
328
+ headers=["provider", "size", "$/h", "vCPUs", "Memory", "Architecture"],
329
+ datatype=["str", "str", "str", "number", "str", "str"],
330
+ row_count=8,
331
+ col_count=(6, "fixed"),
332
+ value=[
333
+ ["aws", "small", "$0.06", 1, "2GB", "Intel Xeon - Ice Lake"],
334
+ ["aws", "medium", "$0.12", 2, "4GB", "Intel Xeon - Ice Lake"],
335
+ ["aws", "large", "$0.24", 4, "8GB", "Intel Xeon - Ice Lake"],
336
+ ["aws", "xlarge", "$0.48", 8, "16GB", "Intel Xeon - Ice Lake"],
337
+ ["azure", "small", "$0.06", 1, "2GB", "Intel Xeon"],
338
+ ["azure", "medium", "$0.12", 2, "4GB", "Intel Xeon"],
339
+ ["azure", "large", "$0.24", 4, "8GB", "Intel Xeon"],
340
+ ["azure", "xlarge", "$0.48", 8, "16GB", "Intel Xeon"],
341
+ ]
342
+ )
343
+
344
+ gr.Markdown("""
345
+ #### Pricing Table(GPU) - 2023/1/11
346
+ """)
347
+
348
+ gr.Dataframe(
349
+ headers=["provider", "size", "$/h", "GPUs", "Memory", "Architecture"],
350
+ datatype=["str", "str", "str", "number", "str", "str"],
351
+ row_count=6,
352
+ col_count=(6, "fixed"),
353
+ value=[
354
+ ["aws", "small", "$0.60", 1, "14GB", "NVIDIA T4"],
355
+ ["aws", "medium", "$1.30", 1, "24GB", "NVIDIA A10G"],
356
+ ["aws", "large", "$4.50", 4, "156B", "NVIDIA T4"],
357
+ ["aws", "xlarge", "$6.50", 1, "80GB", "NVIDIA A100"],
358
+ ["aws", "xxlarge", "$7.00", 4, "96GB", "NVIDIA A10G"],
359
+ ["aws", "xxxlarge", "$45.0", 8, "640GB", "NVIDIA A100"],
360
+ ]
361
+ )
362
+
363
+ gr.TabbedInterface(
364
+ [demoInterface, hf_endpoint], ["Playground", " Deploy on πŸ€— Endpoint"]
365
+ ).launch(enable_queue=True)