Spaces:
Runtime error
Runtime error
Commit
·
26205f8
1
Parent(s):
80d8645
Add commit scheduler and usage stats tracking to
Browse files
app.py
CHANGED
|
@@ -1,6 +1,47 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def extract_slug(url):
|
|
@@ -33,9 +74,16 @@ def clone_collection(
|
|
| 33 |
token=token,
|
| 34 |
)
|
| 35 |
for item in collection.items:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return f"[Collection]({collection.url}) has been cloned into [{new_collection.slug}]({new_collection.url})"
|
| 40 |
|
| 41 |
|
|
@@ -48,8 +96,12 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 48 |
gr.HTML(title)
|
| 49 |
gr.HTML(
|
| 50 |
"""<p style='text-align: center;'>
|
| 51 |
-
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
|
| 52 |
-
You can
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
gr.Markdown("## Authentication")
|
| 55 |
gr.Markdown(
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
import re
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
from huggingface_hub import CommitScheduler, HfApi
|
| 10 |
+
from huggingface_hub.utils import HfHubHTTPError
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
+
|
| 16 |
+
JSON_DATASET_DIR = Path("dataset")
|
| 17 |
+
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
| 18 |
+
JSON_DATASET_PATH = JSON_DATASET_DIR / "dataset.jsonl"
|
| 19 |
+
|
| 20 |
+
scheduler = CommitScheduler(
|
| 21 |
+
repo_id="librarian-bots/collection_cloner-usage-stats",
|
| 22 |
+
repo_type="dataset",
|
| 23 |
+
folder_path=JSON_DATASET_DIR,
|
| 24 |
+
path_in_repo=str(JSON_DATASET_PATH),
|
| 25 |
+
token=HF_TOKEN,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def save_json(source_slug: str, destination_slug: str) -> None:
|
| 30 |
+
with scheduler.lock:
|
| 31 |
+
with JSON_DATASET_PATH.open("a") as f:
|
| 32 |
+
if source_slug.startswith("hf_"): # catch people accidentally adding tokens
|
| 33 |
+
return None
|
| 34 |
+
if destination_slug.startswith("hf_"):
|
| 35 |
+
return None
|
| 36 |
+
json.dump(
|
| 37 |
+
{
|
| 38 |
+
"source_collection": source_slug,
|
| 39 |
+
"destination_collection": destination_slug,
|
| 40 |
+
"datetime": datetime.now().isoformat(),
|
| 41 |
+
},
|
| 42 |
+
f,
|
| 43 |
+
)
|
| 44 |
+
f.write("\n")
|
| 45 |
|
| 46 |
|
| 47 |
def extract_slug(url):
|
|
|
|
| 74 |
token=token,
|
| 75 |
)
|
| 76 |
for item in collection.items:
|
| 77 |
+
try:
|
| 78 |
+
api.add_collection_item(
|
| 79 |
+
new_collection.slug, item.item_id, item_type=item.item_type
|
| 80 |
+
)
|
| 81 |
+
except HfHubHTTPError as e:
|
| 82 |
+
gr.Info(
|
| 83 |
+
f"Failed to add item {item.item_id} to collection {new_collection.slug} because it already exists in this collection."
|
| 84 |
+
)
|
| 85 |
+
if not private:
|
| 86 |
+
save_json(collection.slug, new_collection.slug)
|
| 87 |
return f"[Collection]({collection.url}) has been cloned into [{new_collection.slug}]({new_collection.url})"
|
| 88 |
|
| 89 |
|
|
|
|
| 96 |
gr.HTML(title)
|
| 97 |
gr.HTML(
|
| 98 |
"""<p style='text-align: center;'>
|
| 99 |
+
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.<p>
|
| 100 |
+
<p style='text-align: center;'> You can edit this cloned Collection to your liking!</p>"""
|
| 101 |
+
)
|
| 102 |
+
gr.Markdown(
|
| 103 |
+
"""
|
| 104 |
+
**Note**: To track interest in this feature this Space keeps a record of clones which are cloned into public collection. Clones into private Collections are not tracked."""
|
| 105 |
)
|
| 106 |
gr.Markdown("## Authentication")
|
| 107 |
gr.Markdown(
|