Canstralian commited on
Commit
d418a31
·
verified ·
1 Parent(s): 3572ac0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -1,39 +1,48 @@
1
  import gradio as gr
2
  from huggingface_hub import HfApi
3
  from git import Repo
4
- import uuid
5
  from slugify import slugify
 
 
6
 
7
- def clone(profile: gr.OAuthProfile, oauth_token: gr.OAuthToken, repo_git, repo_hf, sdk_type):
8
- folder = str(uuid.uuid4())
9
- cloned_repo = Repo.clone_from(repo_git, folder)
 
 
 
10
 
11
- #Upload to HF
12
- api = HfApi(token=oauth_token.token)
13
- api.create_repo(
14
- f"{profile.username}/{slugify(repo_hf)}",
15
- repo_type="space",
16
- space_sdk=sdk_type
17
- )
18
- api.upload_folder(
19
- folder_path=folder,
20
- repo_id=f"{profile.username}/{slugify(repo_hf)}",
21
- repo_type="space",
22
- )
23
- return f"https://huggingface.co/spaces/{profile.username}/{slugify(repo_hf)}"
24
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  with gr.Blocks() as demo:
27
  gr.LoginButton()
28
  with gr.Row():
29
  with gr.Column():
30
- repo_git = gr.Textbox(label="GitHub Repository")
31
- repo_hf = gr.Textbox(label="Hugging Face Space name")
32
- sdk_choices = gr.Radio(["gradio", "streamlit", "docker", "static"], label="SDK Choices")
33
  with gr.Column():
34
- output = gr.Textbox(label="Output repo")
 
35
  btn = gr.Button("Bring over!")
36
- btn.click(fn=clone, inputs=[repo_git, repo_hf, sdk_choices], outputs=output)
37
 
38
  if __name__ == "__main__":
39
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import HfApi
3
  from git import Repo
4
+ import uuid
5
  from slugify import slugify
6
+ import os
7
+ import shutil
8
 
9
+ def clone(progress=gr.Progress(), profile=None, oauth_token=None, repo_git=None, repo_hf=None, sdk_type=None):
10
+ """Clones and deploys with progress updates."""
11
+ try:
12
+ progress(0.1, desc="Cloning repository...")
13
+ folder = str(uuid.uuid4())
14
+ Repo.clone_from(repo_git, folder)
15
 
16
+ progress(0.4, desc="Creating Hugging Face Space...")
17
+ api = HfApi(token=oauth_token.token)
18
+ repo_id = f"{profile.username}/{slugify(repo_hf)}"
19
+ api.create_repo(repo_id=repo_id, repo_type="space", space_sdk=sdk_type)
 
 
 
 
 
 
 
 
 
20
 
21
+ progress(0.7, desc="Uploading files...")
22
+ with open(os.path.join(folder, "space_config.json"), "w") as f:
23
+ import json
24
+ json.dump({"sdk": sdk_type}, f)
25
+ api.upload_folder(folder_path=folder, repo_id=repo_id, repo_type="space")
26
+
27
+ shutil.rmtree(folder)
28
+ progress(1.0, desc="Deployment complete!")
29
+ return f"Deployment successful! [View your space](https://huggingface.co/spaces/{repo_id})"
30
+
31
+ except Exception as e:
32
+ return f"<span style='color:red;'>Error: {e}</span>"
33
 
34
  with gr.Blocks() as demo:
35
  gr.LoginButton()
36
  with gr.Row():
37
  with gr.Column():
38
+ repo_git = gr.Textbox(label="GitHub Repo", placeholder="https://github.com/...", tooltip="Enter the GitHub repository URL.")
39
+ repo_hf = gr.Textbox(label="Space Name", placeholder="my-space", tooltip="Enter a name for your Hugging Face Space.")
40
+ sdk_choices = gr.Radio(["gradio", "streamlit", "docker", "static"], label="SDK", tooltip="Select the SDK for your Space.")
41
  with gr.Column():
42
+ output = gr.Markdown(label="Output")
43
+ progress = gr.Progress()
44
  btn = gr.Button("Bring over!")
45
+ btn.click(fn=clone, inputs=[progress, gr.OAuthProfile(), gr.OAuthToken(), repo_git, repo_hf, sdk_choices], outputs=output)
46
 
47
  if __name__ == "__main__":
48
+ demo.launch(auth_required=True)