John6666 commited on
Commit
3932109
1 Parent(s): 6691f6c

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +89 -72
  2. requirements.txt +2 -2
app.py CHANGED
@@ -1,73 +1,90 @@
1
- import gradio as gr
2
- import requests
3
- from huggingface_hub import whoami
4
- from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
5
- from gradio_huggingfacehub_search import HuggingfaceHubSearch
6
-
7
- ENDPOINT = "https://huggingface.co"
8
- # ENDPOINT = "http://localhost:5564"
9
-
10
- REPO_TYPES = ["model", "dataset", "space"]
11
-
12
-
13
- def duplicate(source_repo, dst_repo, repo_type, private, oauth_token: gr.OAuthToken | None):
14
- print(oauth_token.token)
15
- try:
16
- if not repo_type in REPO_TYPES:
17
- raise ValueError("need to select valid repo type")
18
- _ = whoami(oauth_token.token)
19
- # ^ this will throw if token is invalid
20
-
21
- r = requests.post(
22
- f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
23
- headers=build_hf_headers(token=oauth_token.token),
24
- json={"repository": dst_repo, "private": private},
25
- )
26
- hf_raise_for_status(r)
27
-
28
- repo_url = r.json().get("url")
29
-
30
- return (
31
- f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
32
- "sp.jpg",
33
- )
34
-
35
- except Exception as e:
36
- raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
37
-
38
- interface = gr.Interface(
39
- fn=duplicate,
40
- inputs=[
41
- HuggingfaceHubSearch(
42
- placeholder="Source repository (e.g. osanseviero/src)",
43
- search_type=["model", "dataset", "space"],
44
- sumbit_on_select=False,
45
- ),
46
- gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
47
- gr.Dropdown(choices=REPO_TYPES, value="model"),
48
- gr.Checkbox(label="Make new repo private?"),
49
- ],
50
- outputs=[
51
- gr.Markdown(label="output"),
52
- gr.Image(show_label=False),
53
- ],
54
- title="Duplicate your repo!",
55
- description="Duplicate a Hugging Face repository! This Space is a an experimental demo.",
56
- allow_flagging="never",
57
- live=False
58
- )
59
-
60
- def swap_visibilty(profile: gr.OAuthProfile | None):
61
- return gr.update(elem_classes=["main_ui_logged_in"]) if profile else gr.update(elem_classes=["main_ui_logged_out"])
62
-
63
- css = '''
64
- .main_ui_logged_out{opacity: 0.3; pointer-events: none}
65
- '''
66
- with gr.Blocks(css=css) as demo:
67
- gr.LoginButton()
68
- with gr.Column(elem_classes="main_ui_logged_out") as main_ui:
69
- interface.render()
70
- demo.load(fn=swap_visibilty, outputs=main_ui)
71
-
72
- demo.queue()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  demo.launch()
 
1
+ import gradio as gr
2
+ import requests
3
+ from pathlib import Path
4
+ from huggingface_hub import whoami, HfApi, hf_hub_download, hf_hub_url
5
+ from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
6
+ from gradio_huggingfacehub_search import HuggingfaceHubSearch
7
+
8
+ ENDPOINT = "https://huggingface.co"
9
+ # ENDPOINT = "http://localhost:5564"
10
+
11
+ REPO_TYPES = ["model", "dataset", "space"]
12
+
13
+
14
+ def duplicate(source_repo, dst_repo, repo_type, private, overwrite, oauth_token: gr.OAuthToken | None, progress=gr.Progress(track_tqdm=True)):
15
+ print(oauth_token.token)
16
+ hf_token = oauth_token.token
17
+ api = HfApi(token=hf_token)
18
+ try:
19
+ if not repo_type in REPO_TYPES:
20
+ raise ValueError("need to select valid repo type")
21
+ _ = whoami(oauth_token.token)
22
+ # ^ this will throw if token is invalid
23
+
24
+ if overwrite and api.repo_exists(repo_id=dst_repo, repo_type=repo_type, token=hf_token):
25
+ for path in api.list_repo_files(repo_id=source_repo, repo_type=repo_type, token=hf_token):
26
+ file = hf_hub_download(repo_id=source_repo, filename=path, repo_type=repo_type, token=hf_token)
27
+ if not Path(file).exists(): continue
28
+ if Path(file).is_dir():
29
+ api.upload_folder(repo_id=dst_repo, folder_path=file, path_in_repo=Path(file).name, repo_type=repo_type, token=hf_token)
30
+ elif Path(file).is_file():
31
+ api.upload_file(repo_id=dst_repo, path_or_fileobj=file, path_in_repo=Path(file).name, repo_type=repo_type, token=hf_token)
32
+ Path(file).unlink()
33
+ if repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}"
34
+ elif repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}"
35
+ else: repo_url = f"https://huggingface.co/{dst_repo}"
36
+ else:
37
+ r = requests.post(
38
+ f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
39
+ headers=build_hf_headers(token=oauth_token.token),
40
+ json={"repository": dst_repo, "private": private},
41
+ )
42
+ hf_raise_for_status(r)
43
+
44
+ repo_url = r.json().get("url")
45
+
46
+ return (
47
+ f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
48
+ "sp.jpg",
49
+ )
50
+
51
+ except Exception as e:
52
+ raise gr.Error(f"""Oops, you forgot to login. Please use the loggin button on the top left to migrate your repo {e}""")
53
+
54
+ interface = gr.Interface(
55
+ fn=duplicate,
56
+ inputs=[
57
+ HuggingfaceHubSearch(
58
+ placeholder="Source repository (e.g. osanseviero/src)",
59
+ search_type=["model", "dataset", "space"],
60
+ sumbit_on_select=False,
61
+ ),
62
+ gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
63
+ gr.Dropdown(choices=REPO_TYPES, value="model"),
64
+ gr.Checkbox(label="Make new repo private?"),
65
+ gr.Checkbox(label="Overwrite existing repo?"),
66
+ ],
67
+ outputs=[
68
+ gr.Markdown(label="output"),
69
+ gr.Image(show_label=False),
70
+ ],
71
+ title="Duplicate your repo!",
72
+ description="Duplicate a Hugging Face repository! This Space is a an experimental demo.",
73
+ allow_flagging="never",
74
+ live=False
75
+ )
76
+
77
+ def swap_visibilty(profile: gr.OAuthProfile | None):
78
+ return gr.update(elem_classes=["main_ui_logged_in"]) if profile else gr.update(elem_classes=["main_ui_logged_out"])
79
+
80
+ css = '''
81
+ .main_ui_logged_out{opacity: 0.3; pointer-events: none}
82
+ '''
83
+ with gr.Blocks(css=css) as demo:
84
+ gr.LoginButton()
85
+ with gr.Column(elem_classes="main_ui_logged_out") as main_ui:
86
+ interface.render()
87
+ demo.load(fn=swap_visibilty, outputs=main_ui)
88
+
89
+ demo.queue()
90
  demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- huggingface_hub==0.22.2
2
- gradio_huggingfacehub_search==0.0.7
 
1
+ huggingface_hub>=0.22.2
2
+ gradio_huggingfacehub_search>=0.0.7