thomas-yanxin commited on
Commit
90914fa
·
verified ·
1 Parent(s): 5045817

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +224 -0
app.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ os.system('pip install huggingface_hub')
4
+
5
+
6
+ import shutil
7
+ import time
8
+
9
+ import gradio as gr
10
+ from huggingface_hub import HfApi
11
+
12
+ CACHE_DIR = "hf2ms_cache"
13
+ LOCAL_DIR = "hf2ms_local"
14
+ USERNAME = "dailingx"
15
+ EMAIL = "468551414@qq.com"
16
+
17
+
18
+ def clone_from_ms(
19
+ ms_token: str,
20
+ ms_repo_id: str,
21
+ clone_dir: str
22
+ ):
23
+ if os.path.exists(clone_dir):
24
+ ori_dir = os.getcwd()
25
+ os.chdir(clone_dir)
26
+ os.system("GIT_LFS_SKIP_SMUDGE=1 git pull")
27
+ os.chdir(ori_dir)
28
+ message = f"the repo already exists, so just pull successfully!"
29
+ command = f"GIT_LFS_SKIP_SMUDGE=1 git clone https://oauth2:{ms_token}@www.modelscope.cn/datasets/{ms_repo_id}.git"
30
+ os.system(command)
31
+ configuration_path = os.path.join(clone_dir, "configuration.json")
32
+ if not os.path.exists(configuration_path):
33
+ open(configuration_path, "w").close()
34
+ # message
35
+ message = f"clone from https://oauth2:{ms_token}@www.modelscope.cn/datasets/{ms_repo_id}.git successfully!"
36
+ return message
37
+
38
+
39
+ def hf_list_repo_files(
40
+ hf_token: str,
41
+ hf_repo_id: str,
42
+ repo_type: str = "dataset"
43
+ ):
44
+ hf_api = HfApi(token=hf_token)
45
+ files = hf_api.list_repo_files(repo_id=hf_repo_id, repo_type=repo_type)
46
+ return files
47
+
48
+
49
+ def pull_from_hf(
50
+ hf_token: str,
51
+ hf_repo_id: str,
52
+ filename: str,
53
+ ):
54
+ if not hf_repo_id:
55
+ raise gr.Error("Please enter the repo_id of huggingface")
56
+ if not filename:
57
+ raise gr.Error("Please enter the filename")
58
+
59
+ if "," in filename:
60
+ filename_list = filename.split(",")
61
+ for _filename in filename_list:
62
+ save_path = os.path.join(LOCAL_DIR, _filename)
63
+ if os.path.exists(save_path):
64
+ message = "the file already exists!"
65
+ return message
66
+
67
+ # download
68
+ hf_api = HfApi(token=hf_token)
69
+ hf_api.hf_hub_download(
70
+ repo_id=hf_repo_id,
71
+ repo_type="dataset",
72
+ filename=_filename,
73
+ cache_dir=CACHE_DIR,
74
+ local_dir=LOCAL_DIR,
75
+ local_dir_use_symlinks=False
76
+ )
77
+ else:
78
+ save_path = os.path.join(LOCAL_DIR, filename)
79
+ if os.path.exists(save_path):
80
+ message = "the file already exists!"
81
+ return message
82
+
83
+ # download
84
+ hf_api = HfApi(token=hf_token)
85
+ hf_api.hf_hub_download(
86
+ repo_id=hf_repo_id,
87
+ repo_type="dataset",
88
+ filename=filename,
89
+ cache_dir=CACHE_DIR,
90
+ local_dir=LOCAL_DIR,
91
+ local_dir_use_symlinks=False
92
+ )
93
+
94
+ # message
95
+ message = f"Pull from https://huggingface.co/datasets/{hf_repo_id} successfully!"
96
+ print(message)
97
+ return message
98
+
99
+
100
+ def move_file_from_local_to_clone_dir(
101
+ filename: str,
102
+ clone_dir: str
103
+ ):
104
+ # move to the clone dir
105
+ if "," in filename:
106
+ filename_list = filename.split(",")
107
+ for _filename in filename_list:
108
+ if "/" in _filename:
109
+ dirname = os.path.dirname(_filename)
110
+ src_dir = os.path.join(clone_dir, dirname)
111
+ if not os.path.exists(src_dir):
112
+ os.makedirs(src_dir)
113
+ src = os.path.join(LOCAL_DIR, _filename)
114
+ dst = os.path.join(clone_dir, _filename)
115
+ shutil.move(src=src, dst=dst)
116
+ else:
117
+ if "/" in filename:
118
+ dirname = os.path.dirname(filename)
119
+ src_dir = os.path.join(clone_dir, dirname)
120
+ if not os.path.exists(src_dir):
121
+ os.makedirs(src_dir)
122
+ src = os.path.join(LOCAL_DIR, filename)
123
+ dst = os.path.join(clone_dir, filename)
124
+ shutil.move(src=src, dst=dst)
125
+
126
+ # message
127
+ message = f"move the file from {src} to {dst} successfully!"
128
+ print(message)
129
+ return message
130
+
131
+
132
+ def push_to_ms(
133
+ username: str,
134
+ email: str,
135
+ ms_repo_id: str,
136
+ clone_dir: str,
137
+ filename: str
138
+ ):
139
+ # push to ms
140
+ ori_dir = os.getcwd()
141
+ os.chdir(clone_dir)
142
+ os.system("apt-get install git-lfs")
143
+ os.system("git lfs install")
144
+ os.system(f"git config --global user.email {email}")
145
+ os.system(f"git config --global user.name {username}")
146
+ if "," in filename:
147
+ filename_list = filename.split(",")
148
+ num = len(filename_list)
149
+ for _filename in filename_list:
150
+ os.system(f"git lfs track '{_filename}'")
151
+ os.system("git add .")
152
+ os.system(f"git commit -m 'upload {num} files'")
153
+ os.system(f"git push")
154
+ os.chdir(ori_dir)
155
+ else:
156
+ os.system(f"git lfs track '{filename}'")
157
+ os.system("git add .")
158
+ os.system(f"git commit -m 'upload {filename}'")
159
+ os.system(f"git push")
160
+ os.chdir(ori_dir)
161
+ # remove clone dir
162
+ if os.path.exists(clone_dir):
163
+ shutil.rmtree(clone_dir)
164
+ message = f'Pushed to https://www.modelscope.cn/datasets/{ms_repo_id} successfully!'
165
+ print(message)
166
+ return message
167
+
168
+
169
+ def handle(
170
+ hf_token: str,
171
+ ms_token: str,
172
+ repo_type: str,
173
+ hf_repo: str,
174
+ ms_repo: str,
175
+ ):
176
+ clone_dir = ms_repo.split("/")[-1]
177
+ hf_file_list = hf_list_repo_files(hf_token, hf_repo, repo_type)
178
+ print(f"all file in hf: {hf_file_list}")
179
+
180
+ for filename in hf_file_list:
181
+ print(f"begin to process file: {filename}")
182
+ clone_from_ms(ms_token, ms_repo, clone_dir)
183
+ time.sleep(1)
184
+ pull_from_hf(hf_token, hf_repo, filename)
185
+ time.sleep(1)
186
+ move_file_from_local_to_clone_dir(filename, clone_dir)
187
+ time.sleep(1)
188
+ push_to_ms(USERNAME, EMAIL, ms_repo, clone_dir, filename)
189
+ print(f"process file finish: {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
+ repo_type = gr.Textbox(label="Repo Type", value="dataset")
209
+ hf_repo = gr.Textbox(label="HuggingFace Repo")
210
+ ms_repo = gr.Textbox(label="ModelScope Repo")
211
+
212
+ with gr.Row():
213
+ button = gr.Button("Submit", variant="primary")
214
+ clear = gr.Button("Clear")
215
+
216
+ button.click(
217
+ handle,
218
+ [hf_token, ms_token, repo_type, hf_repo, ms_repo],
219
+ outputs=None
220
+ )
221
+
222
+ if __name__ == "__main__":
223
+ demo.queue(max_size=1)
224
+ demo.launch(share=False, max_threads=1)