File size: 2,706 Bytes
bbc8fc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e925d44
bbc8fc2
 
 
0be785e
 
 
 
e925d44
eb06eb9
3d02299
 
 
bbc8fc2
 
 
 
 
 
 
 
eb06eb9
bbc8fc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from huggingface_hub import HfApi
import gradio as gr


def clone_collection(
    source_slug, dest_namespace, dest_title, token, private=False, exist_ok=False
):
    api = HfApi(token=token)
    collection = api.get_collection(source_slug)
    if not collection:
        raise gr.Error(
            f"Collection {source_slug} does not exist or you do not have access to it."
        )
    description = f"Copy of {collection.title}"

    new_collection = api.create_collection(
        dest_title,
        namespace=dest_namespace,
        exists_ok=exist_ok,
        private=private,
        description=description,
        token=token,
    )
    for item in collection.items:
        api.add_collection_item(
            new_collection.slug, item.item_id, item_type=item.item_type
        )
    return f"[Collection]({collection.url}) has been cloned into [new_collection.slug]({new_collection.url})"


title = (
    """<h1 style='text-align: center;'> &#129516; Collection Cloner &#129516;</h1>"""
)


with gr.Blocks() as demo:
    gr.HTML(title)
    gr.HTML(
        """<p style='text-align: center;'>
This space allows you to clone a <a href="https://huggingface.co./docs/hub/collections">Collection</a> from the Hugging Face Hub into your own namespace.
You can then edit the collection to your liking!</p>"""
    )
    gr.Markdown("## Authentication")
    gr.Markdown(
        "Token is required to create a new collection and clone private collections. You can get your token from your [profile page](https://huggingface.co./settings/token)."
    )
    with gr.Row():
        token = gr.Textbox(
            label="Token",
            type="password",
        )
    with gr.Column():
        gr.Markdown("## Source Collection")
        source_slug = gr.Textbox(
            label="Source Collection Slug", placeholder="e.g. username/collection-slug"
        )
        gr.Markdown("## Destination Collection info")
        dest_namespace = gr.Textbox(
            label="Destination Namespace",
            interactive=True,
        )
        dest_title = gr.Textbox(
            label="Destination Title",
        )
        with gr.Row():
            private = gr.Checkbox(
                False,
                label="Private",
            )
            overwrite = gr.Checkbox(
                False,
                label="Overwrite existing collection?",
            )
    submit_btn = gr.Button("Clone Collection")
    response = gr.Markdown()
    submit_btn.click(
        clone_collection,
        [
            source_slug,
            dest_namespace,
            dest_title,
            token,
            private,
            overwrite,
        ],
        response,
    )

demo.launch()