File size: 4,438 Bytes
a082be3
4018f06
 
 
c3373ba
 
0a800aa
4018f06
a082be3
0d14e49
d76eabc
0a800aa
d76eabc
 
d187fa4
c5e64ee
d76eabc
 
d187fa4
0a800aa
4018f06
 
 
 
 
 
d187fa4
 
 
 
 
 
 
 
 
4018f06
 
f64fe4d
4018f06
 
 
 
 
 
 
9911949
 
 
 
 
 
 
d187fa4
9911949
7d4e20d
 
664642f
 
d187fa4
 
 
 
 
 
0d14e49
d187fa4
 
 
 
 
 
 
 
2d11ae6
 
 
 
 
 
 
d187fa4
 
 
7d4e20d
 
 
a082be3
 
 
 
 
 
 
 
 
 
d76eabc
53168c3
 
 
 
d76eabc
53168c3
 
 
 
 
 
 
 
 
 
 
 
 
 
d76eabc
a082be3
 
 
 
 
 
 
0d14e49
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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, space_sdk):
    
    your_username = api.whoami(token=user_token)["name"]

    # Check if space ID is correct including username
    if f"{your_username}/" in str(space_destination):
        print("username is present: good to continue")
    else:
        space_destination = f"{your_username}/{space_destination}"
        print(f"user forgot to mention a username in space_destination: {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)

    # Check if README.md already exists in tmp_dir
    readme_path = os.path.join(tmp_dir, "README.md")
    original_readme_path = os.path.join(tmp_dir, "ORIGINAL_README.md")
    
    # Rename the existing README.md if it exists
    if os.path.exists(readme_path):
        print("Existing README.md found, renaming to ORIGINAL_README.md")
        os.rename(readme_path, original_readme_path)

    # 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 f"Repository cloned ! Find it at hf.co/spaces/{space_destination}"
        
    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...")

            api.create_repo(
                repo_id=space_destination,
                token=user_token,
                private=True,
                repo_type="space",
                space_sdk=space_sdk
            )

            print(f"New space repo created: {space_destination}")

            api.upload_folder(
                folder_path=tmp_dir,
                repo_id=space_destination,
                repo_type="space",
                token=user_token,
                commit_message="Migrated from GitHub",
                ignore_patterns=[
                    "*.git*",
                    "*.DS_Store",
                    "*.env",
                ]
            )
                
            return f"Repository cloned ! Find it at hf.co/spaces/{space_destination}"
        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")
        with gr.Group():
            git_repo_url = gr.Textbox(
                label="Git repo URL to clone",
                placeholder="https://github.com/username/my-repo.git"
            )
            with gr.Row():
                space_destination = gr.Textbox(
                    label="Space ID",
                    placeholder="your_hf_username/space_name"
                )
                user_token = gr.Textbox(
                    label="WRITE permissions HF token",
                    type="password"
                )
            space_sdk = gr.Radio(
                label="Space SDK",
                info="If specified space ID doesn't exist, a new private space will be created. In that case, please choose a SDK",
                choices=["gradio", "docker", "static"],
                value="gradio"
            )
        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, space_sdk],
        outputs=[status]
    )

demo.launch(show_error=True)