Spaces:
Runtime error
Runtime error
Commit
·
bbc8fc2
1
Parent(s):
4ad74f8
Add Collection Cloner feature using Hugging Face
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def clone_collection(
|
| 6 |
+
source_slug, dest_namespace, dest_title, token, private=False, exist_ok=False
|
| 7 |
+
):
|
| 8 |
+
api = HfApi(token=token)
|
| 9 |
+
collection = api.get_collection(source_slug)
|
| 10 |
+
if not collection:
|
| 11 |
+
raise gr.Error(
|
| 12 |
+
f"Collection {source_slug} does not exist or you do not have access to it."
|
| 13 |
+
)
|
| 14 |
+
description = f"Copy of {collection.title}"
|
| 15 |
+
|
| 16 |
+
new_collection = api.create_collection(
|
| 17 |
+
dest_title,
|
| 18 |
+
namespace=dest_namespace,
|
| 19 |
+
exists_ok=exist_ok,
|
| 20 |
+
private=private,
|
| 21 |
+
description=description,
|
| 22 |
+
token=token,
|
| 23 |
+
)
|
| 24 |
+
for item in collection.items:
|
| 25 |
+
api.add_collection_item(
|
| 26 |
+
new_collection.slug, item.item_id, item_type=item.item_type
|
| 27 |
+
)
|
| 28 |
+
return f"[Collection]({collection.url}) has been cloned into [new_collection.slug]({new_collection.url})"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
title = (
|
| 32 |
+
"""<h1 style='text-align: center;'> 🧬 Collection Cloner 🧬</h1>"""
|
| 33 |
+
)
|
| 34 |
+
description = """<div style='text-align: center;'>
|
| 35 |
+
<p>This space allows you to clone a [Collection](https://huggingface.co/docs/hub/collections) from the Hugging Face Hub into your own namespace.
|
| 36 |
+
You can then edit the collection to your liking!</p>
|
| 37 |
+
</div>"""
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.HTML(title)
|
| 41 |
+
gr.Markdown(description)
|
| 42 |
+
with gr.Row():
|
| 43 |
+
token = gr.Textbox(
|
| 44 |
+
label="Token",
|
| 45 |
+
type="password",
|
| 46 |
+
)
|
| 47 |
+
with gr.Column():
|
| 48 |
+
gr.Markdown("## Source Collection")
|
| 49 |
+
source_slug = gr.Textbox(
|
| 50 |
+
label="Source Collection Slug",
|
| 51 |
+
)
|
| 52 |
+
gr.Markdown("## Destination Collection info")
|
| 53 |
+
dest_namespace = gr.Textbox(
|
| 54 |
+
label="Destination Namespace",
|
| 55 |
+
interactive=True,
|
| 56 |
+
)
|
| 57 |
+
dest_title = gr.Textbox(
|
| 58 |
+
label="Destination Title",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
private = gr.Checkbox(
|
| 63 |
+
False,
|
| 64 |
+
label="Private",
|
| 65 |
+
)
|
| 66 |
+
overwrite = gr.Checkbox(
|
| 67 |
+
False,
|
| 68 |
+
label="Overwrite existing collection?",
|
| 69 |
+
)
|
| 70 |
+
submit_btn = gr.Button("Clone Collection")
|
| 71 |
+
response = gr.Markdown()
|
| 72 |
+
submit_btn.click(
|
| 73 |
+
clone_collection,
|
| 74 |
+
[
|
| 75 |
+
source_slug,
|
| 76 |
+
dest_namespace,
|
| 77 |
+
dest_title,
|
| 78 |
+
token,
|
| 79 |
+
private,
|
| 80 |
+
overwrite,
|
| 81 |
+
],
|
| 82 |
+
response,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
demo.launch()
|