John6666 commited on
Commit
2d41b92
Β·
verified Β·
1 Parent(s): bee3749

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +13 -12
  2. app.py +92 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
- ---
2
- title: Space Splitter
3
- emoji: πŸƒ
4
- colorFrom: indigo
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 5.22.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
+ ---
2
+ title: Space Splitter
3
+ emoji: πŸ™„
4
+ colorFrom: indigo
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.22.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+
3
+ @spaces.GPU
4
+ def dummy_gpu():
5
+ pass
6
+
7
+ import gradio as gr
8
+ from huggingface_hub import HfApi, snapshot_download
9
+ from huggingface_hub.hf_api import RepoFile
10
+ from typing import Union
11
+ import tempfile
12
+ import shutil
13
+ from pathlib import Path
14
+ import time
15
+
16
+ def split_repo(repo_id: str, user_name: str, space_name: str, storage_name: str, storage_type: str, is_private: bool=True, threshold: int=10 * 1024 * 1024, hf_token: Union[str, None]="", progress=gr.Progress(track_tqdm=True)):
17
+ TEMP_DIR = tempfile.mkdtemp()
18
+ exist_ok = False
19
+ use_dupe_api = False
20
+ info_md = ""
21
+ dl_code = ""
22
+ space_id = f"{user_name}/{space_name}"
23
+ storage_id = f"{user_name}/{storage_name}"
24
+ try:
25
+ kwargs = {}
26
+ if hf_token: kwargs["token"] = hf_token
27
+ else: raise Exception("Token not found.")
28
+ api = HfApi()
29
+ if not exist_ok:
30
+ if api.repo_exists(repo_id=space_id, repo_type="space", **kwargs): raise Exception(f"{space_id} already exists.")
31
+ if api.repo_exists(repo_id=storage_id, repo_type=storage_type, **kwargs): raise Exception(f"{space_id} already exists.")
32
+ info = api.list_repo_tree(repo_id=repo_id, repo_type="space", recursive=True, **kwargs)
33
+ lfiles = []
34
+ sfiles = []
35
+ for i in info:
36
+ if not isinstance(i, RepoFile): continue
37
+ if i.lfs is not None and i.lfs.size > threshold: lfiles.append(i.path)
38
+ else: sfiles.append(i.path)
39
+ #print("Large files: ", lfiles)
40
+ #print("Small files: ", sfiles)
41
+ if len(lfiles) == 0: raise Exception("Large file not found.")
42
+ lfiles_str = "[" + ", ".join(['"' + s + '"' for s in lfiles]) + "]"
43
+ sv = api.get_space_variables(repo_id=repo_id, **kwargs)
44
+ sv = [{str(k): str(v)} for k, v in sv.items()] if sv and len(sv) > 0 else []
45
+ if api.repo_exists(repo_id=space_id, repo_type="space", **kwargs) and exist_ok: api.delete_repo(repo_id=space_id, repo_type="space", **kwargs)
46
+ if use_dupe_api:
47
+ api.duplicate_space(from_id=repo_id, to_id=space_id, exist_ok=exist_ok, private=is_private, hardware="cpu-basic", variables=sv, **kwargs)
48
+ time.sleep(10) # wait for finishing of space duplication
49
+ api.delete_files(repo_id=space_id, repo_type="space", delete_patterns=lfiles, **kwargs)
50
+ else:
51
+ snapshot_download(repo_id=repo_id, repo_type="space", ignore_patterns=lfiles, local_dir=TEMP_DIR, **kwargs)
52
+ api.create_repo(repo_id=space_id, repo_type="space", space_hardware="cpu-basic", space_variables=sv, space_sdk="gradio", exist_ok=exist_ok, private=is_private, **kwargs)
53
+ api.upload_folder(repo_id=space_id, repo_type="space", ignore_patterns=lfiles, folder_path=TEMP_DIR, path_in_repo=".", **kwargs)
54
+ snapshot_download(repo_id=repo_id, repo_type="space", allow_patterns=lfiles, local_dir=TEMP_DIR, **kwargs)
55
+ api.create_repo(repo_id=storage_id, repo_type=storage_type, exist_ok=exist_ok, private=is_private, **kwargs)
56
+ api.upload_folder(repo_id=storage_id, repo_type=storage_type, allow_patterns=lfiles, folder_path=TEMP_DIR, path_in_repo=".", **kwargs)
57
+ lfiles_str = "[" + ", ".join(['"' + s + '"' for s in lfiles]) + "]"
58
+ dl_code = f'from huggingface_hub import snapshot_download\nlarge_files = {lfiles_str}\nsnapshot_download(repo_id="{storage_id}", repo_type="{storage_type}", allow_patterns=large_files, local_dir=".")\n'
59
+ info_md = f'## Your new space URL: [{space_id}](https://hf.co/spaces/{space_id})<br>\n## Your new storage URL: [{storage_id}](https://hf.co/{storage_id if storage_type == "model" else "datasets/" + storage_id})'
60
+ except Exception as e:
61
+ print(e)
62
+ gr.Warning(f"Error: {e}")
63
+ finally:
64
+ if Path(TEMP_DIR).exists() and Path(TEMP_DIR).is_dir(): shutil.rmtree(TEMP_DIR)
65
+ return info_md, dl_code
66
+
67
+ css = """
68
+ .title { font-size: 3em; align-items: center; text-align: center; }
69
+ .info { align-items: center; text-align: center; }
70
+ .block.result { margin: 1em 0; padding: 1em; box-shadow: 0 0 3px 3px #664422, 0 0 3px 2px #664422 inset; border-radius: 6px; background: #665544; }
71
+ .desc [src$='#float'] { float: right; margin: 20px; }
72
+ """
73
+
74
+ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css, delete_cache=(60, 3600)) as demo:
75
+ repo_id = gr.Textbox(label="Source repo ID", placeholder="levihsu/OOTDiffusion", value="")
76
+ with gr.Row(equal_height=True):
77
+ user_name = gr.Textbox(label="Your user name", value="")
78
+ space_name = gr.Textbox(label="Destination repo name", placeholder="OOTDiffusion", value="")
79
+ storage_name = gr.Textbox(label="Storage repo name", placeholder="OOTDiffusion-storage", value="")
80
+ storage_type = gr.Radio(label="Storage repo type", choices=["dataset", "model"], value="model")
81
+ with gr.Column():
82
+ hf_token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="")
83
+ gr.Markdown("Your token is available at [hf.co/settings/tokens](https://huggingface.co/settings/tokens).", elem_classes="info")
84
+ threshold = gr.Number(label="Size threshold (bytes)", value=10 * 1024 * 1024, minimum=1, maximum=5 * 1024 * 1024 * 1024, step=1)
85
+ is_private = gr.Checkbox(label="Private", value=True)
86
+ run_button = gr.Button("Submit", variant="primary")
87
+ dl_code = gr.Textbox(label="Code", value="", show_copy_button=True)
88
+ info_md = gr.Markdown("<br><br><br>", elem_classes="result")
89
+
90
+ run_button.click(split_repo, [repo_id, user_name, space_name, storage_name, storage_type, is_private, threshold, hf_token], [info_md, dl_code])
91
+
92
+ demo.queue().launch(ssr_mode=False)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ huggingface_hub