davanstrien HF staff commited on
Commit
f539398
1 Parent(s): 7f2b4c3

Add function to extract slug from Hugging Face

Browse files
Files changed (1) hide show
  1. app.py +10 -0
app.py CHANGED
@@ -1,11 +1,21 @@
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(
 
1
  from huggingface_hub import HfApi
2
  import gradio as gr
3
+ import re
4
+
5
+
6
+ def extract_slug(url):
7
+ pattern = r"https://huggingface\.co/collections/(.*)"
8
+ return match.group(1) if (match := re.search(pattern, url)) else None
9
 
10
 
11
  def clone_collection(
12
  source_slug, dest_namespace, dest_title, token, private=False, exist_ok=False
13
  ):
14
  api = HfApi(token=token)
15
+ source_slug = source_slug.strip()
16
+ # check if formatted as url
17
+ if source_slug.startswith("https://huggingface.co/collections/"):
18
+ source_slug = extract_slug(source_slug)
19
  collection = api.get_collection(source_slug)
20
  if not collection:
21
  raise gr.Error(