acecalisto3 commited on
Commit
d9a3439
·
verified ·
1 Parent(s): f059638

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -6
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import random
4
  import os
@@ -221,11 +221,62 @@ def deploy_button_click(app_name: str, code: str) -> str:
221
  if not app_name:
222
  return "Please enter a valid app name."
223
 
224
- # Deploy the web app instance
225
- # ... (Implement deployment logic here)
226
 
227
- # Return a success message
228
- return f"Web app '{app_name}' deployed successfully!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
  # Function to handle the "Local Host" button click
231
  def local_host_button_click(app_name: str, code: str) -> str:
@@ -237,6 +288,17 @@ def local_host_button_click(app_name: str, code: str) -> str:
237
  if not app_name:
238
  return "Please enter a valid app name."
239
 
 
 
 
 
 
 
 
 
 
 
 
240
  # Start the local server
241
  os.chdir(app_name)
242
  subprocess.Popen(['gradio', 'run', 'app.py', '--share', '--server_port', str(LOCAL_HOST_PORT)])
@@ -343,6 +405,7 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
343
  deploy_button = gr.Button("Deploy")
344
  local_host_button = gr.Button("Local Host")
345
  ship_button = gr.Button("Ship")
 
346
 
347
  # Web App Creation Logic
348
  create_web_app_button.click(
@@ -354,7 +417,7 @@ with gr.Blocks(theme='ParityError/Interstellar') as demo:
354
  # Deploy the web app
355
  deploy_button.click(
356
  deploy_button_click,
357
- inputs=[app_name_input, code_output],
358
  outputs=[gr.Textbox(label="Status", interactive=False)],
359
  )
360
 
 
1
+ from huggingface_hub import InferenceClient, hf_hub_url
2
  import gradio as gr
3
  import random
4
  import os
 
221
  if not app_name:
222
  return "Please enter a valid app name."
223
 
224
+ # Get Hugging Face token
225
+ hf_token = gr.Textbox.get("hf_token").strip()
226
 
227
+ # Validate Hugging Face token
228
+ if not hf_token:
229
+ return "Please enter a valid Hugging Face token."
230
+
231
+ # Create a new directory for the app
232
+ os.makedirs(app_name, exist_ok=True)
233
+
234
+ # Copy the code to the app directory
235
+ with open(os.path.join(app_name, 'app.py'), 'w') as f:
236
+ f.write(code)
237
+
238
+ # Create the requirements.txt file
239
+ with open(os.path.join(app_name, 'requirements.txt'), 'w') as f:
240
+ f.write("gradio\nhuggingface_hub")
241
+
242
+ # Deploy the app to Hugging Face Spaces
243
+ try:
244
+ subprocess.run(
245
+ ['huggingface-cli', 'login', '--token', hf_token],
246
+ check=True,
247
+ )
248
+ subprocess.run(
249
+ ['huggingface-cli', 'space', 'create', app_name, '--repo_type', 'spaces', '--private', '--branch', 'main'],
250
+ check=True,
251
+ )
252
+ subprocess.run(
253
+ ['git', 'init'],
254
+ cwd=app_name,
255
+ check=True,
256
+ )
257
+ subprocess.run(
258
+ ['git', 'add', '.'],
259
+ cwd=app_name,
260
+ check=True,
261
+ )
262
+ subprocess.run(
263
+ ['git', 'commit', '-m', 'Initial commit'],
264
+ cwd=app_name,
265
+ check=True,
266
+ )
267
+ subprocess.run(
268
+ ['git', 'remote', 'add', 'origin', hf_hub_url(username='your_username', repo_id=app_name)],
269
+ cwd=app_name,
270
+ check=True,
271
+ )
272
+ subprocess.run(
273
+ ['git', 'push', '-u', 'origin', 'main'],
274
+ cwd=app_name,
275
+ check=True,
276
+ )
277
+ return f"Web app '{app_name}' deployed successfully to Hugging Face Spaces!"
278
+ except subprocess.CalledProcessError as e:
279
+ return f"Error: {e}"
280
 
281
  # Function to handle the "Local Host" button click
282
  def local_host_button_click(app_name: str, code: str) -> str:
 
288
  if not app_name:
289
  return "Please enter a valid app name."
290
 
291
+ # Create a new directory for the app
292
+ os.makedirs(app_name, exist_ok=True)
293
+
294
+ # Copy the code to the app directory
295
+ with open(os.path.join(app_name, 'app.py'), 'w') as f:
296
+ f.write(code)
297
+
298
+ # Create the requirements.txt file
299
+ with open(os.path.join(app_name, 'requirements.txt'), 'w') as f:
300
+ f.write("gradio\nhuggingface_hub")
301
+
302
  # Start the local server
303
  os.chdir(app_name)
304
  subprocess.Popen(['gradio', 'run', 'app.py', '--share', '--server_port', str(LOCAL_HOST_PORT)])
 
405
  deploy_button = gr.Button("Deploy")
406
  local_host_button = gr.Button("Local Host")
407
  ship_button = gr.Button("Ship")
408
+ hf_token_input = gr.Textbox(label="Hugging Face Token", placeholder="Enter your Hugging Face token")
409
 
410
  # Web App Creation Logic
411
  create_web_app_button.click(
 
417
  # Deploy the web app
418
  deploy_button.click(
419
  deploy_button_click,
420
+ inputs=[app_name_input, code_output, hf_token_input],
421
  outputs=[gr.Textbox(label="Status", interactive=False)],
422
  )
423