File size: 2,726 Bytes
a082be3
4018f06
 
 
c3373ba
 
0a800aa
4018f06
a082be3
 
d76eabc
0a800aa
d76eabc
 
 
 
 
 
1091c7c
0a800aa
4018f06
 
 
 
 
 
 
 
f64fe4d
4018f06
 
 
 
 
 
 
9911949
 
 
 
 
 
 
 
 
7d4e20d
 
664642f
 
d76eabc
7d4e20d
 
 
a082be3
 
 
 
 
 
 
 
 
 
d76eabc
a082be3
 
 
d76eabc
 
 
 
 
 
 
 
a082be3
 
 
 
 
 
 
 
 
 
 
693822c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import tempfile
import os
from git import Repo
from huggingface_hub import HfApi, create_repo, upload_folder
from huggingface_hub.utils import HfHubHTTPError
api = HfApi()


def process(git_repo_url, space_destination, user_token):
    
    your_username = api.whoami(token=user_token)["name"]

    # Check if space ID is correct including username
    if str(your_username) in str(space_destination):
        print "username is present: good to continue"
    else:
        space_destination = f"{your_username}/{space_destination}"
        print("user forgot to mention his username in space_destination")
    
    # Create a temporary directory to clone the repository
    tmp_dir = tempfile.mkdtemp()
        
    # Clone the Hugging Face Spaces repository
    repo = Repo.clone_from(git_repo_url, tmp_dir)

    # Log the contents of the tmp_dir with its subfolders
    print("Contents of the cloned repository:")
    for root, dirs, files in os.walk(tmp_dir):
        level = root.replace(tmp_dir, '').count(os.sep)
        indent = ' ' * 4 * level
        print(f"{indent}{os.path.basename(root)}/")
        sub_indent = ' ' * 4 * (level + 1)
        for f in files:
            print(f"{sub_indent}{f}")

    try: 
        api.upload_folder(
            folder_path=tmp_dir,
            repo_id=space_destination,
            repo_type="space",
            token=user_token
        )
        return "Done"
        
    except HfHubHTTPError as e:
        # Check if the error is a "Repository Not Found" error
        if "404 Client Error" in str(e) and "Repository Not Found" in str(e):
            print("Repository not found. Maybe we should Create the repository...")
            return "Repository not found. Maybe we should Create the repository..."
        else:
            # Re-raise the exception if it's not a "Repository Not Found" error
            raise e

css = """
div#col-container{
    margin: 0 auto;
    max-width: 720px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# Clone GitHub repo to Space")
        git_repo_url = gr.Textbox(
            label="Git repo to clone"
        )
        with gr.Row():
            space_destination = gr.Textbox(
                label="Space ID"
            )
            user_token = gr.Textbox(
                label="Write permissions token",
                type="password"
            )
        submit_btn = gr.Button("Clone git repo to my space")
        status = gr.Textbox(
            label="Status"
        )

    submit_btn.click(
        fn=process,
        inputs=[git_repo_url, space_destination, user_token],
        outputs=[status]
    )

demo.launch(show_error=True)