dailingx commited on
Commit
f84cf9c
·
verified ·
1 Parent(s): 9465925

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +229 -0
app.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('pip install huggingface_hub')
3
+
4
+
5
+ import time
6
+ import shutil
7
+ from huggingface_hub import HfApi
8
+ import gradio as gr
9
+
10
+ CACHE_DIR = "hf2ms_cache"
11
+ LOCAL_DIR = "hf2ms_local"
12
+ USERNAME = "heatingma"
13
+ EMAIL = "[email protected]"
14
+
15
+
16
+ def clone_from_ms(
17
+ ms_token: str,
18
+ ms_repo_id: str,
19
+ clone_dir: str
20
+ ):
21
+ if os.path.exists(clone_dir):
22
+ ori_dir = os.getcwd()
23
+ os.chdir(clone_dir)
24
+ os.system("GIT_LFS_SKIP_SMUDGE=1 git pull")
25
+ os.chdir(ori_dir)
26
+ message = f"the repo already exists, so just pull successfully!"
27
+ command = f"GIT_LFS_SKIP_SMUDGE=1 git clone https://oauth2:{ms_token}@www.modelscope.cn/datasets/{ms_repo_id}.git"
28
+ os.system(command)
29
+ configuration_path = os.path.join(clone_dir, "configuration.json")
30
+ if not os.path.exists(configuration_path):
31
+ open(configuration_path, "w").close()
32
+ # message
33
+ message = f"clone from https://oauth2:{ms_token}@www.modelscope.cn/datasets/{ms_repo_id}.git successfully!"
34
+ return message
35
+
36
+
37
+ def hf_list_repo_files(
38
+ hf_token: str,
39
+ hf_repo_id: str
40
+ ):
41
+ hf_api = HfApi(token=hf_token)
42
+ files = hf_api.list_repo_files(repo_id=hf_repo_id)
43
+ return files
44
+
45
+
46
+ def pull_from_hf(
47
+ hf_token: str,
48
+ hf_repo_id: str,
49
+ filename: str,
50
+ ):
51
+ if not hf_repo_id:
52
+ raise gr.Error("Please enter the repo_id of huggingface")
53
+ if not filename:
54
+ raise gr.Error("Please enter the filename")
55
+
56
+ if "," in filename:
57
+ filename_list = filename.split(",")
58
+ for _filename in filename_list:
59
+ save_path = os.path.join(LOCAL_DIR, _filename)
60
+ if os.path.exists(save_path):
61
+ message = "the file already exists!"
62
+ return message
63
+
64
+ # download
65
+ hf_api = HfApi(token=hf_token)
66
+ hf_api.hf_hub_download(
67
+ repo_id=hf_repo_id,
68
+ repo_type="dataset",
69
+ filename=_filename,
70
+ cache_dir=CACHE_DIR,
71
+ local_dir=LOCAL_DIR,
72
+ local_dir_use_symlinks=False
73
+ )
74
+ else:
75
+ save_path = os.path.join(LOCAL_DIR, filename)
76
+ if os.path.exists(save_path):
77
+ message = "the file already exists!"
78
+ return message
79
+
80
+ # download
81
+ hf_api = HfApi(token=hf_token)
82
+ hf_api.hf_hub_download(
83
+ repo_id=hf_repo_id,
84
+ repo_type="dataset",
85
+ filename=filename,
86
+ cache_dir=CACHE_DIR,
87
+ local_dir=LOCAL_DIR,
88
+ local_dir_use_symlinks=False
89
+ )
90
+
91
+ # message
92
+ message = f"Pull from https://huggingface.co/datasets/{hf_repo_id} successfully!"
93
+ print(message)
94
+ return message
95
+
96
+
97
+ def move_file_from_local_to_clone_dir(
98
+ filename: str,
99
+ clone_dir: str
100
+ ):
101
+ # move to the clone dir
102
+ if "," in filename:
103
+ filename_list = filename.split(",")
104
+ for _filename in filename_list:
105
+ if "/" in _filename:
106
+ dirname = os.path.dirname(_filename)
107
+ src_dir = os.path.join(clone_dir, dirname)
108
+ if not os.path.exists(src_dir):
109
+ os.makedirs(src_dir)
110
+ src = os.path.join(LOCAL_DIR, _filename)
111
+ dst = os.path.join(clone_dir, _filename)
112
+ shutil.move(src=src, dst=dst)
113
+ else:
114
+ if "/" in filename:
115
+ dirname = os.path.dirname(filename)
116
+ src_dir = os.path.join(clone_dir, dirname)
117
+ if not os.path.exists(src_dir):
118
+ os.makedirs(src_dir)
119
+ src = os.path.join(LOCAL_DIR, filename)
120
+ dst = os.path.join(clone_dir, filename)
121
+ shutil.move(src=src, dst=dst)
122
+
123
+ # message
124
+ message = f"move the file from {src} to {dst} successfully!"
125
+ print(message)
126
+ return message
127
+
128
+
129
+ def push_to_ms(
130
+ username: str,
131
+ email: str,
132
+ ms_repo_id: str,
133
+ clone_dir: str,
134
+ filename: str
135
+ ):
136
+ # push to ms
137
+ ori_dir = os.getcwd()
138
+ os.chdir(clone_dir)
139
+ os.system("apt-get install git-lfs")
140
+ os.system("git lfs install")
141
+ os.system(f"git config --global user.email {email}")
142
+ os.system(f"git config --global user.name {username}")
143
+ if "," in filename:
144
+ filename_list = filename.split(",")
145
+ num = len(filename_list)
146
+ for _filename in filename_list:
147
+ os.system(f"git lfs track '{_filename}'")
148
+ os.system("git add .")
149
+ os.system(f"git commit -m 'upload {num} files'")
150
+ os.system(f"git push")
151
+ os.chdir(ori_dir)
152
+ else:
153
+ os.system(f"git lfs track '{filename}'")
154
+ os.system("git add .")
155
+ os.system(f"git commit -m 'upload {filename}'")
156
+ os.system(f"git push")
157
+ os.chdir(ori_dir)
158
+ # remove clone dir
159
+ if os.path.exists(clone_dir):
160
+ shutil.rmtree(clone_dir)
161
+ message = f'Pushed to https://www.modelscope.cn/datasets/{ms_repo_id} successfully!'
162
+ print(message)
163
+ return message
164
+
165
+
166
+ def handle(
167
+ hf_token: str,
168
+ ms_token: str,
169
+ hf_repo: str,
170
+ ms_repo: str,
171
+ ):
172
+ clone_dir = ms_repo.split("/")[-1]
173
+ hf_file_list = hf_list_repo_files(hf_token, hf_repo)
174
+ return hf_file_list
175
+ # file_paths = []
176
+ # for idx in range(100):
177
+ # begin_idx = idx * 100
178
+ # end_idx = begin_idx + 100
179
+ # file_path = f"panda_011_{begin_idx:06d}_{end_idx:06d}.tar.gz"
180
+ # file_paths.append(file_path)
181
+
182
+ # for filename in file_paths:
183
+ # clone_from_ms(ms_token, ms_repo, clone_dir)
184
+ # time.sleep(1)
185
+ # pull_from_hf(hf_token, hf_repo, filename)
186
+ # time.sleep(1)
187
+ # move_file_from_local_to_clone_dir(filename, clone_dir)
188
+ # time.sleep(1)
189
+ # push_to_ms(USERNAME, EMAIL, ms_repo, clone_dir, filename)
190
+ # time.sleep(10)
191
+
192
+
193
+ with gr.Blocks() as demo:
194
+ gr.Markdown(
195
+ '''
196
+ This space uploads model from Huggingface to ModelScope.
197
+ **Please make sure that you're the owner of the repo or have permission from the owner to do so!**
198
+ # How to use this Space?
199
+ - Duplicate this Space and providing MS token (optional) and your read/write HF token (mandatory)
200
+ - Create your target model repo on HF. This step needs to be done manually. The Space doesn't do create an empty repo for you.
201
+ - In your own private Space, fill in information below.
202
+ - Click submit then watch for output in container log for progress.
203
+ - Create README.md file (since the metadata is not compatible with HF)
204
+ '''
205
+ )
206
+ hf_token = gr.Textbox(label="HuggingFace Token")
207
+ ms_token = gr.Textbox(label="ModelScope Git Token")
208
+ hf_repo = gr.Textbox(label="HuggingFace Repo")
209
+ ms_repo = gr.Textbox(label="ModelScope Repo")
210
+
211
+ # username = gr.Textbox(label="ModelScope username")
212
+ # email = gr.Textbox(label="ModelScope email")
213
+ # hf_repo_id = gr.Textbox(label="HF Model Repo ID (case sensitive). \nPlease make sure that this model has already been created")
214
+ # ms_repo_id = gr.Textbox(label="Target Model Scope Repo ID (case sensitive) \nPlease make sure that this model has already been created")
215
+ # filename = gr.Textbox(label="the path of the file")
216
+
217
+ with gr.Row():
218
+ button = gr.Button("Submit", variant="primary")
219
+ clear = gr.Button("Clear")
220
+
221
+ button.click(
222
+ handle,
223
+ [hf_token, ms_token, hf_repo, ms_repo],
224
+ outputs=None
225
+ )
226
+
227
+ if __name__ == "__main__":
228
+ demo.queue(max_size=1)
229
+ demo.launch(share=False, max_threads=1)