Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,14 +56,16 @@ def show_f(repo,name,token):
|
|
| 56 |
rem_2=rem_1.split("/",1)[1]
|
| 57 |
if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
|
| 58 |
os.makedirs(f'{name}/{dir_1}/{dir_2}')
|
| 59 |
-
sf=rem_2.split(".",1)[1]
|
| 60 |
-
pf=rem_2.split(".",1)[0]
|
| 61 |
-
f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
|
|
|
|
| 62 |
|
| 63 |
else:
|
| 64 |
-
sf=rem_1.split(".",1)[1]
|
| 65 |
-
pf=rem_1.split(".",1)[0]
|
| 66 |
-
f_name=f'{dir_1}/{pf}.{sf}'
|
|
|
|
| 67 |
print(f_name)
|
| 68 |
|
| 69 |
else:
|
|
@@ -78,6 +80,7 @@ def show_f(repo,name,token):
|
|
| 78 |
#print (r.text)
|
| 79 |
uid = uuid.uuid4()
|
| 80 |
file = open(f'{name}/{f_name}','wb')
|
|
|
|
| 81 |
file.write(r.content)
|
| 82 |
file.close()
|
| 83 |
file_list.append(f'{name}/{f_name}')
|
|
@@ -97,209 +100,11 @@ def show_f(repo,name,token):
|
|
| 97 |
gr.update(choices=[f for f in f_ist],interactive=True),
|
| 98 |
api.get_space_runtime(f'{repo}/{name}'))
|
| 99 |
|
| 100 |
-
def
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
print (f_ist)
|
| 106 |
-
file_list = []
|
| 107 |
-
file_out = []
|
| 108 |
-
if not os.path.exists(name):
|
| 109 |
-
os.makedirs(name)
|
| 110 |
-
|
| 111 |
-
for d_app in f_ist:
|
| 112 |
-
|
| 113 |
-
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
|
| 114 |
-
#print (r.text)
|
| 115 |
-
uid = uuid.uuid4()
|
| 116 |
-
|
| 117 |
-
sf=d_app.split(".",1)[1]
|
| 118 |
-
pf=d_app.split(".",1)[0]
|
| 119 |
-
f_name=f'{pf}.{sf}'
|
| 120 |
-
file = open(f'{name}/{f_name}','w')
|
| 121 |
-
file.writelines(r.text)
|
| 122 |
-
file.close()
|
| 123 |
-
file_list.append(Path(f'{name}/{f_name}'))
|
| 124 |
-
file_out.append(d_app)
|
| 125 |
-
|
| 126 |
-
with ZipFile(f"{name}.zip", "w") as zipObj:
|
| 127 |
-
for idx, file in enumerate(f_ist):
|
| 128 |
-
zipObj.write(f'{name}/{file}')
|
| 129 |
-
file_list.append(f'{name}.zip')
|
| 130 |
-
|
| 131 |
-
return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist],interactive=True), file_list)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def show_f_cont(repo,name,file,token):
|
| 135 |
-
html_text = '<html>\n<body>\n<div>\n'
|
| 136 |
-
images=[".png" , ".jpg" , ".gif" , ".webm" , ".mp4"]
|
| 137 |
-
is_im=False
|
| 138 |
-
for x in images:
|
| 139 |
-
if x in file:
|
| 140 |
-
html_text += f'<object data="https://huggingface.co/spaces/{repo}/{name}/resolve/main/{file}" style="width:100%;font-size:small;"></object>'
|
| 141 |
-
out_text = "Image File"
|
| 142 |
-
is_im=True
|
| 143 |
-
else:
|
| 144 |
-
pass
|
| 145 |
-
if is_im==False:
|
| 146 |
-
print(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
|
| 147 |
-
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
|
| 148 |
-
text=r.text
|
| 149 |
-
html_text += f'<pre style="text-wrap:pretty;">{text}</pre>\n'
|
| 150 |
-
out_text = r.text
|
| 151 |
-
else:
|
| 152 |
-
pass
|
| 153 |
-
html_text += '</div>\n</body>\n</html>'
|
| 154 |
-
return(html_text)
|
| 155 |
-
|
| 156 |
-
def show_f_frame(repo,name,file,token):
|
| 157 |
-
file_url=f'https://huggingface.co/spaces/{repo}/{name}/blob/main/{file}'
|
| 158 |
-
out=f"""<div><iframe src='{file_url}' height='1200px' width='100%'></iframe>"""
|
| 159 |
-
return out
|
| 160 |
-
def show_f_frame2(repo,name,file,token):
|
| 161 |
-
file_url=f'https://huggingface.co/spaces/{repo}/{name}'
|
| 162 |
-
out=f"""<div><iframe src='{file_url}' height='1200px' width='100%'></iframe>"""
|
| 163 |
-
return out
|
| 164 |
-
def show_all(author,token):
|
| 165 |
-
spaces=[]
|
| 166 |
-
api = HfApi(token=token)
|
| 167 |
-
#author=name
|
| 168 |
-
s_ist = (api.list_spaces(author=author))
|
| 169 |
-
file_list = []
|
| 170 |
-
file_list_ea=[]
|
| 171 |
-
for i,space in enumerate(s_ist):
|
| 172 |
-
space_ea = space.id.split("/",1)[1]
|
| 173 |
-
spaces.append(space_ea)
|
| 174 |
-
#print (space_ea)
|
| 175 |
-
f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
|
| 176 |
-
#print (f_ist)
|
| 177 |
-
if not os.path.exists(space_ea):
|
| 178 |
-
os.makedirs(space_ea)
|
| 179 |
-
|
| 180 |
-
for d_app in f_ist:
|
| 181 |
-
|
| 182 |
-
r = requests.get(f'https://huggingface.co/spaces/{author}/{space_ea}/raw/main/{d_app}')
|
| 183 |
-
#print (r.text)
|
| 184 |
-
uid = uuid.uuid4()
|
| 185 |
-
try:
|
| 186 |
-
sf=d_app.split(".",1)[1]
|
| 187 |
-
pf=d_app.split(".",1)[0]
|
| 188 |
-
f_name=f'{pf}.{sf}'
|
| 189 |
-
file = open(f'{space_ea}/{f_name}','w')
|
| 190 |
-
file.writelines(r.text)
|
| 191 |
-
file.close()
|
| 192 |
-
#file_list_ea.append(Path(f'{space}/{f_name}'))
|
| 193 |
-
except Exception:
|
| 194 |
-
pass
|
| 195 |
-
with ZipFile(f"{space_ea}.zip", "w") as zipObj:
|
| 196 |
-
for idx, file in enumerate(f_ist):
|
| 197 |
-
try:
|
| 198 |
-
zipObj.write(f'{space_ea}/{file}')
|
| 199 |
-
except Exception:
|
| 200 |
-
pass
|
| 201 |
-
|
| 202 |
-
file_list.append(f'{space_ea}.zip')
|
| 203 |
-
yield file_list
|
| 204 |
-
with ZipFile(f"{author}.zip", "w") as zipObj:
|
| 205 |
-
for idx, file in enumerate(file_list):
|
| 206 |
-
try:
|
| 207 |
-
zipObj.write(f'{file}')
|
| 208 |
-
except Exception:
|
| 209 |
-
pass
|
| 210 |
-
file_list.append(f'{author}.zip')
|
| 211 |
-
yield file_list
|
| 212 |
-
|
| 213 |
-
def show_all_z(author,token):
|
| 214 |
-
spaces=[]
|
| 215 |
-
api = HfApi(token=token)
|
| 216 |
-
#author=name
|
| 217 |
-
s_ist = (api.list_spaces(author=author))
|
| 218 |
-
file_list = []
|
| 219 |
-
file_list_ea=[]
|
| 220 |
-
for i,space in enumerate(s_ist):
|
| 221 |
-
space_ea = space.id.split("/",1)[1]
|
| 222 |
-
spaces.append(space_ea)
|
| 223 |
-
#print (space_ea)
|
| 224 |
-
f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
|
| 225 |
-
#print (f_ist)
|
| 226 |
-
if not os.path.exists(space_ea):
|
| 227 |
-
os.makedirs(space_ea)
|
| 228 |
-
file= snapshot_download(repo_id=f'{author}/{space_ea}', repo_type="space")
|
| 229 |
-
shutil.make_archive(f"{space_ea}", 'zip', file)
|
| 230 |
-
file_list.append(f'{space_ea}.zip')
|
| 231 |
-
yield file_list
|
| 232 |
-
with ZipFile(f"{author}.zip", "w") as zipObj:
|
| 233 |
-
for idx, file in enumerate(file_list):
|
| 234 |
-
try:
|
| 235 |
-
zipObj.write(f'{file}')
|
| 236 |
-
except Exception:
|
| 237 |
-
pass
|
| 238 |
-
file_list.append(f'{author}.zip')
|
| 239 |
-
yield file_list
|
| 240 |
-
|
| 241 |
-
def dl_checked_fn(author,checklist,token):
|
| 242 |
-
spaces=[]
|
| 243 |
-
api = HfApi(token=token)
|
| 244 |
-
#author=name
|
| 245 |
-
s_ist = checklist
|
| 246 |
-
file_list = []
|
| 247 |
-
file_list_ea=[]
|
| 248 |
-
for i,space_ea in enumerate(s_ist):
|
| 249 |
-
#spaces.append(space_ea)
|
| 250 |
-
#print (space_ea)
|
| 251 |
-
f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
|
| 252 |
-
#print (f_ist)
|
| 253 |
-
if not os.path.exists(space_ea):
|
| 254 |
-
os.makedirs(space_ea)
|
| 255 |
-
file= snapshot_download(repo_id=f'{author}/{space_ea}', repo_type="space")
|
| 256 |
-
shutil.make_archive(f"{space_ea}", 'zip', file)
|
| 257 |
-
file_list.append(f'{space_ea}.zip')
|
| 258 |
-
yield file_list
|
| 259 |
-
with ZipFile(f"{author}.zip", "w") as zipObj:
|
| 260 |
-
for idx, file in enumerate(file_list):
|
| 261 |
-
try:
|
| 262 |
-
zipObj.write(f'{file}')
|
| 263 |
-
except Exception:
|
| 264 |
-
pass
|
| 265 |
-
file_list.append(f'{author}.zip')
|
| 266 |
-
yield file_list
|
| 267 |
-
def get_space_runtime(author,checklist,token):
|
| 268 |
-
api=HfApi(token=token)
|
| 269 |
-
space_info=[]
|
| 270 |
-
for space in checklist:
|
| 271 |
-
outp=api.get_space_runtime(f'{author}/{space}')
|
| 272 |
-
space_info.append(outp['stage'])
|
| 273 |
-
return space_info
|
| 274 |
-
def delete_checked(confirm_val,author,checklist,token):
|
| 275 |
-
if confirm_val=="CONFIRM":
|
| 276 |
-
api = HfApi(token=token)
|
| 277 |
-
s_ist = checklist
|
| 278 |
-
delete_list = []
|
| 279 |
-
for i,space_ea in enumerate(s_ist):
|
| 280 |
-
try:
|
| 281 |
-
api.delete_repo(repo_id=f'{author}/{space_ea}',repo_type='space')
|
| 282 |
-
delete_list.append(f'Deleted:: {space_ea}\n')
|
| 283 |
-
yield delete_list,gr.update(visible=False)
|
| 284 |
-
except Exception as e:
|
| 285 |
-
yield e
|
| 286 |
-
else:
|
| 287 |
-
yield "Not Deleting", gr.update(visible=False)
|
| 288 |
-
|
| 289 |
-
def checkp(p):
|
| 290 |
-
if p == o:
|
| 291 |
-
return gr.update(visible=False), gr.update(visible=True)
|
| 292 |
-
elif p != o:
|
| 293 |
-
return None, None
|
| 294 |
-
def update_checked_message(inp):
|
| 295 |
-
html=""
|
| 296 |
-
for ea in inp:
|
| 297 |
-
html=f'{html}<br>{ea}'
|
| 298 |
-
return html
|
| 299 |
-
|
| 300 |
-
def ru_sure_fn():
|
| 301 |
-
return gr.update(visible=True)
|
| 302 |
-
|
| 303 |
with gr.Blocks(css=css) as build:
|
| 304 |
with gr.Row(visible=False) as no:
|
| 305 |
pass_box=gr.Textbox()
|
|
@@ -319,6 +124,9 @@ with gr.Blocks(css=css) as build:
|
|
| 319 |
with gr.Group():
|
| 320 |
space_radio=gr.Radio(label="Spaces",choices=[])
|
| 321 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
|
|
|
| 322 |
with gr.Tab("Space Info"):
|
| 323 |
with gr.Row():
|
| 324 |
with gr.Column(scale=3):
|
|
@@ -366,27 +174,8 @@ with gr.Blocks(css=css) as build:
|
|
| 366 |
sure_check=gr.Textbox(visible=False,value="")
|
| 367 |
'''
|
| 368 |
|
| 369 |
-
|
| 370 |
-
#dl_checked_btn.click(dl_checked_fn,[r_name,space_check,token],files)
|
| 371 |
-
#delete_checked_btn.click(ru_sure_fn,None,sure_row)
|
| 372 |
-
#ru_sure_btn.click(delete_checked,[ru_sure_box,r_name,space_check,token],[del_message_box,sure_row]).then(show_s,[r_name,token],[s_name,space_radio,space_check])
|
| 373 |
-
|
| 374 |
-
#space_check.change(update_checked_message,space_check,message_box)
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
#show_all_btn1.click(show_all,[r_name,token],files)
|
| 378 |
-
#show_all_btn2.click(show_all_z,[r_name,token],files)
|
| 379 |
-
|
| 380 |
-
#s_btn.click(show_s,[r_name,token],[s_name,space_radio,space_check])
|
| 381 |
s_btn.click(show_s,[r_name,token],[space_radio])
|
| 382 |
-
#l_btn.click(show_f,[r_name,token], [files,file_radio])
|
| 383 |
-
#f_name.change(show_f_frame,[r_name,s_name,f_name],[file_frame])
|
| 384 |
-
#s_name.change(show_f_frame2,[r_name,s_name,f_name],[file_frame])
|
| 385 |
-
|
| 386 |
-
#s_name.change(show_f,[r_name,s_name,token],[d_app,files])
|
| 387 |
space_radio.change(show_f,[r_name,space_radio,token],[files,file_radio,space_info_json])
|
| 388 |
file_radio.change(show_f_cont,[r_name,space_radio,file_radio,token],[file_contents])
|
| 389 |
-
|
| 390 |
-
pass_btn.click(checkp,pass_box,[no,yes])
|
| 391 |
-
#inbut.click(build_space,[token,t_name,s_type,m_type,r_type,d_app,d_css],output_html)
|
| 392 |
build.queue(default_concurrency_limit=10).launch(show_api=False)
|
|
|
|
| 56 |
rem_2=rem_1.split("/",1)[1]
|
| 57 |
if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
|
| 58 |
os.makedirs(f'{name}/{dir_1}/{dir_2}')
|
| 59 |
+
#sf=rem_2.split(".",1)[1]
|
| 60 |
+
#pf=rem_2.split(".",1)[0]
|
| 61 |
+
#f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
|
| 62 |
+
f_name=f'{dir_1}/{dir2}/{rem_2}'
|
| 63 |
|
| 64 |
else:
|
| 65 |
+
#sf=rem_1.split(".",1)[1]
|
| 66 |
+
#pf=rem_1.split(".",1)[0]
|
| 67 |
+
#f_name=f'{dir_1}/{pf}.{sf}'
|
| 68 |
+
f_name=f'{dir_1}/{rem_1}'
|
| 69 |
print(f_name)
|
| 70 |
|
| 71 |
else:
|
|
|
|
| 80 |
#print (r.text)
|
| 81 |
uid = uuid.uuid4()
|
| 82 |
file = open(f'{name}/{f_name}','wb')
|
| 83 |
+
#file = open(f'{name}/{f_name}','wb')
|
| 84 |
file.write(r.content)
|
| 85 |
file.close()
|
| 86 |
file_list.append(f'{name}/{f_name}')
|
|
|
|
| 100 |
gr.update(choices=[f for f in f_ist],interactive=True),
|
| 101 |
api.get_space_runtime(f'{repo}/{name}'))
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
with gr.Blocks(css=css) as build:
|
| 109 |
with gr.Row(visible=False) as no:
|
| 110 |
pass_box=gr.Textbox()
|
|
|
|
| 124 |
with gr.Group():
|
| 125 |
space_radio=gr.Radio(label="Spaces",choices=[])
|
| 126 |
with gr.Column(scale=3):
|
| 127 |
+
with gr.Row():
|
| 128 |
+
dest=gr.Textbox(label="Destination")
|
| 129 |
+
move_btn=gr.Button("Move Repo")
|
| 130 |
with gr.Tab("Space Info"):
|
| 131 |
with gr.Row():
|
| 132 |
with gr.Column(scale=3):
|
|
|
|
| 174 |
sure_check=gr.Textbox(visible=False,value="")
|
| 175 |
'''
|
| 176 |
|
| 177 |
+
move_btn.click(move,[r_name,space_radio,dest,token])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
s_btn.click(show_s,[r_name,token],[space_radio])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
space_radio.change(show_f,[r_name,space_radio,token],[files,file_radio,space_info_json])
|
| 180 |
file_radio.change(show_f_cont,[r_name,space_radio,file_radio,token],[file_contents])
|
|
|
|
|
|
|
|
|
|
| 181 |
build.queue(default_concurrency_limit=10).launch(show_api=False)
|