anzorq radames commited on
Commit
1a1dca1
1 Parent(s): 5708566

Improvements for Hardware Selection (#4)

Browse files

- Improvements for Hardware Selection (63e70686c0557a2acf134691d0431f972ca878e3)


Co-authored-by: Radamés Ajna <radames@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import subprocess
3
  from huggingface_hub import HfApi, upload_folder
4
  import gradio as gr
5
-
6
  from huggingface_hub import whoami, list_models
7
 
8
 
@@ -69,7 +69,7 @@ def on_load_model(user_model_id, other_model_id, token):
69
  except Exception as e:
70
  return None, None, None, None, gr.update(value=error_str(e))
71
 
72
- def create_and_push(space_type, other_model_name, radio_model_names, model_id, title, description, prefix, update, token):
73
 
74
  try:
75
 
@@ -80,7 +80,14 @@ def create_and_push(space_type, other_model_name, radio_model_names, model_id, t
80
  exist_ok=update,
81
  repo_type="space",
82
  space_sdk="gradio",
 
83
  )
 
 
 
 
 
 
84
 
85
  # 2. Replace the name, title, and description in the template
86
  with open("template/app_simple.py" if space_type == "Simple" else "template/app_advanced.py", "r") as f:
@@ -137,6 +144,7 @@ with gr.Blocks() as demo:
137
  gr.Markdown("#### 1. Choose a model")
138
  input_token = gr.Textbox(
139
  max_lines=1,
 
140
  label="Enter your Hugging Face token",
141
  placeholder="WRITE permission is required!",
142
  )
@@ -158,8 +166,9 @@ with gr.Blocks() as demo:
158
  - **Advanced** - Runs on CPU by default, with the option to upgrade to GPU. You can control image generation parameters: guidance, number of steps, image size, etc. Also supports **image-to-image** generation.""")
159
  space_type =gr.Radio(label="Space type", choices=["Simple", "Advanced"], value="Simple")
160
  update = gr.Checkbox(label="Update the space if it already exists?")
161
-
162
-
 
163
  brn_create = gr.Button("Create the space")
164
 
165
  error_output = gr.Markdown(label="Output")
@@ -181,7 +190,7 @@ with gr.Blocks() as demo:
181
 
182
  brn_create.click(
183
  fn=create_and_push,
184
- inputs=[space_type, other_model_name, radio_model_names, name, title, description, prefix, update, input_token],
185
  outputs=[error_output],
186
  scroll_to_output=True
187
  )
@@ -197,4 +206,4 @@ with gr.Blocks() as demo:
197
  """)
198
 
199
  demo.queue()
200
- demo.launch(debug=True)
 
2
  import subprocess
3
  from huggingface_hub import HfApi, upload_folder
4
  import gradio as gr
5
+ import requests
6
  from huggingface_hub import whoami, list_models
7
 
8
 
 
69
  except Exception as e:
70
  return None, None, None, None, gr.update(value=error_str(e))
71
 
72
+ def create_and_push(space_type, hardware, private_space, other_model_name, radio_model_names, model_id, title, description, prefix, update, token):
73
 
74
  try:
75
 
 
80
  exist_ok=update,
81
  repo_type="space",
82
  space_sdk="gradio",
83
+ private=private_space
84
  )
85
+ api_url = f'https://huggingface.co/api/spaces/{model_id}'
86
+ headers = { "Authorization" : f"Bearer {token}"}
87
+ # add HUGGING_FACE_HUB_TOKEN secret to new space
88
+ requests.post(f'{api_url}/secrets', json={"key":"HUGGING_FACE_HUB_TOKEN","value":token}, headers=headers)
89
+ # set new Space Hardware flavor
90
+ requests.post(f'{api_url}/hardware', json={'flavor': hardware}, headers=headers)
91
 
92
  # 2. Replace the name, title, and description in the template
93
  with open("template/app_simple.py" if space_type == "Simple" else "template/app_advanced.py", "r") as f:
 
144
  gr.Markdown("#### 1. Choose a model")
145
  input_token = gr.Textbox(
146
  max_lines=1,
147
+ type="password",
148
  label="Enter your Hugging Face token",
149
  placeholder="WRITE permission is required!",
150
  )
 
166
  - **Advanced** - Runs on CPU by default, with the option to upgrade to GPU. You can control image generation parameters: guidance, number of steps, image size, etc. Also supports **image-to-image** generation.""")
167
  space_type =gr.Radio(label="Space type", choices=["Simple", "Advanced"], value="Simple")
168
  update = gr.Checkbox(label="Update the space if it already exists?")
169
+ private_space = gr.Checkbox(label="Private Space")
170
+ gr.Markdown("Choose the new Space Hardware <small>[check pricing page](https://huggingface.co/pricing#spaces), you need payment method to upgrade your Space hardware</small>")
171
+ hardware = gr.Dropdown(["cpu-basic","cpu-upgrade","t4-small","t4-medium","a10g-small","a10g-large"],value = "cpu-basic", label="Space Hardware")
172
  brn_create = gr.Button("Create the space")
173
 
174
  error_output = gr.Markdown(label="Output")
 
190
 
191
  brn_create.click(
192
  fn=create_and_push,
193
+ inputs=[space_type, hardware, private_space, other_model_name, radio_model_names, name, title, description, prefix, update, input_token],
194
  outputs=[error_output],
195
  scroll_to_output=True
196
  )
 
206
  """)
207
 
208
  demo.queue()
209
+ demo.launch(debug=True)