Omnibus commited on
Commit
d7d4ad7
·
verified ·
1 Parent(s): 3959e42

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +395 -0
app.py ADDED
@@ -0,0 +1,395 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import uuid
4
+ import gradio as gr
5
+ import requests
6
+ import json
7
+ from zipfile import ZipFile
8
+ import shutil
9
+ from pathlib import Path
10
+ from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi,snapshot_download)
11
+ from PIL import Image
12
+ uid = uuid.uuid4()
13
+ #token = os.environ['HF_TOKEN']
14
+ #token_self = os.environ['HF_TOKEN']
15
+ #o=os.environ['P']
16
+ css="""
17
+ .wrap.svelte-vm32wk.svelte-vm32wk.svelte-vm32wk {
18
+ display:block;
19
+ }
20
+ .wrap.svelte-1k4wjf2.svelte-1k4wjf2.svelte-1k4wjf2{
21
+ display:block;
22
+ }
23
+ """
24
+
25
+ def show_s(name,token):
26
+ spaces=[]
27
+ spaces.append("")
28
+
29
+ api = HfApi(token=token)
30
+ author=name
31
+ s_ist = (api.list_spaces(author=author))
32
+ for i,space in enumerate(s_ist):
33
+ space_ea = space.id.split("/",1)[1]
34
+ #s_info=api.space_info(f'{name}/{space}',files_metadata=True)
35
+ #print(s_info)
36
+ spaces.append(space_ea)
37
+ #print (space_ea)
38
+ return(gr.Dropdown.update(label="Spaces", choices=[s for s in spaces]),
39
+ gr.update(choices=[s for s in spaces],interactive=True),
40
+ gr.update(choices=[s for s in spaces],interactive=True))
41
+
42
+ def show_f(repo,name,token):
43
+ api = HfApi(token=token)
44
+ f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
45
+ print (f_ist)
46
+ file_list = []
47
+ #file_out = []
48
+
49
+ if not os.path.exists(name):
50
+ os.makedirs(name)
51
+ for d_app in f_ist:
52
+ if "/" in d_app:
53
+ dir_1=d_app.split("/",1)[0]
54
+ rem_1=d_app.split("/",1)[1]
55
+ if not os.path.exists(f'{name}/{dir_1}'):
56
+ os.makedirs(f'{name}/{dir_1}')
57
+
58
+ if "/" in rem_1:
59
+ dir_2=rem_1.split("/",1)[0]
60
+ rem_2=rem_1.split("/",1)[1]
61
+ if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
62
+ os.makedirs(f'{name}/{dir_1}/{dir_2}')
63
+ sf=rem_2.split(".",1)[1]
64
+ pf=rem_2.split(".",1)[0]
65
+ f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
66
+
67
+ else:
68
+ sf=rem_1.split(".",1)[1]
69
+ pf=rem_1.split(".",1)[0]
70
+ f_name=f'{dir_1}/{pf}.{sf}'
71
+ print(f_name)
72
+
73
+ else:
74
+
75
+ sf=d_app.split(".",1)[1]
76
+ pf=d_app.split(".",1)[0]
77
+ f_name=f'{pf}.{sf}'
78
+ pass
79
+
80
+ r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
81
+ print(d_app)
82
+ #print (r.text)
83
+ uid = uuid.uuid4()
84
+ file = open(f'{name}/{f_name}','wb')
85
+ file.write(r.content)
86
+ file.close()
87
+ file_list.append(Path(f'{name}/{f_name}'))
88
+
89
+ with ZipFile(f"{name}.zip", "w") as zipObj:
90
+ for idx, file in enumerate(f_ist):
91
+ zipObj.write(f'{name}/{file}')
92
+ file_list.append(f'{name}.zip')
93
+
94
+ s_info=api.space_info(f'{repo}/{name}',files_metadata=True)
95
+ '''
96
+ with open(f'{uid}-tmp.json','w') as f:
97
+ json.dump(s_info,f,indent=4)
98
+ f.close()
99
+ '''
100
+ return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist],interactive=True),
101
+ file_list,
102
+ gr.update(choices=[f for f in f_ist],interactive=True),
103
+ api.get_space_runtime(f'{repo}/{name}'))
104
+
105
+ def show_f2(repo,name,token):
106
+ api = HfApi(token=token)
107
+ #f_ist = snapshot_download(repo_id=f'{repo}/{name}', repo_type="space")
108
+
109
+ f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
110
+ print (f_ist)
111
+ file_list = []
112
+ file_out = []
113
+ if not os.path.exists(name):
114
+ os.makedirs(name)
115
+
116
+ for d_app in f_ist:
117
+
118
+ r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
119
+ #print (r.text)
120
+ uid = uuid.uuid4()
121
+
122
+ sf=d_app.split(".",1)[1]
123
+ pf=d_app.split(".",1)[0]
124
+ f_name=f'{pf}.{sf}'
125
+ file = open(f'{name}/{f_name}','w')
126
+ file.writelines(r.text)
127
+ file.close()
128
+ file_list.append(Path(f'{name}/{f_name}'))
129
+ file_out.append(d_app)
130
+
131
+ with ZipFile(f"{name}.zip", "w") as zipObj:
132
+ for idx, file in enumerate(f_ist):
133
+ zipObj.write(f'{name}/{file}')
134
+ file_list.append(f'{name}.zip')
135
+
136
+ return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist],interactive=True), file_list)
137
+
138
+
139
+ def show_f_cont(repo,name,file,token):
140
+ html_text = '<html>\n<body>\n<div>\n'
141
+ images=[".png" , ".jpg" , ".gif" , ".webm" , ".mp4"]
142
+ is_im=False
143
+ for x in images:
144
+ if x in file:
145
+ html_text += f'<object data="https://huggingface.co/spaces/{repo}/{name}/resolve/main/{file}" style="width:100%;font-size:small;"></object>'
146
+ out_text = "Image File"
147
+ is_im=True
148
+ else:
149
+ pass
150
+ if is_im==False:
151
+ print(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
152
+ r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
153
+ text=r.text
154
+ html_text += f'<pre style="text-wrap:pretty;">{text}</pre>\n'
155
+ out_text = r.text
156
+ else:
157
+ pass
158
+ html_text += '</div>\n</body>\n</html>'
159
+ return(html_text)
160
+
161
+ def show_f_frame(repo,name,file,token):
162
+ file_url=f'https://huggingface.co/spaces/{repo}/{name}/blob/main/{file}'
163
+ out=f"""<div><iframe src='{file_url}' height='1200px' width='100%'></iframe>"""
164
+ return out
165
+ def show_f_frame2(repo,name,file,token):
166
+ file_url=f'https://huggingface.co/spaces/{repo}/{name}'
167
+ out=f"""<div><iframe src='{file_url}' height='1200px' width='100%'></iframe>"""
168
+ return out
169
+ def show_all(author,token):
170
+ spaces=[]
171
+ api = HfApi(token=token)
172
+ #author=name
173
+ s_ist = (api.list_spaces(author=author))
174
+ file_list = []
175
+ file_list_ea=[]
176
+ for i,space in enumerate(s_ist):
177
+ space_ea = space.id.split("/",1)[1]
178
+ spaces.append(space_ea)
179
+ #print (space_ea)
180
+ f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
181
+ #print (f_ist)
182
+ if not os.path.exists(space_ea):
183
+ os.makedirs(space_ea)
184
+
185
+ for d_app in f_ist:
186
+
187
+ r = requests.get(f'https://huggingface.co/spaces/{author}/{space_ea}/raw/main/{d_app}')
188
+ #print (r.text)
189
+ uid = uuid.uuid4()
190
+ try:
191
+ sf=d_app.split(".",1)[1]
192
+ pf=d_app.split(".",1)[0]
193
+ f_name=f'{pf}.{sf}'
194
+ file = open(f'{space_ea}/{f_name}','w')
195
+ file.writelines(r.text)
196
+ file.close()
197
+ #file_list_ea.append(Path(f'{space}/{f_name}'))
198
+ except Exception:
199
+ pass
200
+ with ZipFile(f"{space_ea}.zip", "w") as zipObj:
201
+ for idx, file in enumerate(f_ist):
202
+ try:
203
+ zipObj.write(f'{space_ea}/{file}')
204
+ except Exception:
205
+ pass
206
+
207
+ file_list.append(f'{space_ea}.zip')
208
+ yield file_list
209
+ with ZipFile(f"{author}.zip", "w") as zipObj:
210
+ for idx, file in enumerate(file_list):
211
+ try:
212
+ zipObj.write(f'{file}')
213
+ except Exception:
214
+ pass
215
+ file_list.append(f'{author}.zip')
216
+ yield file_list
217
+
218
+ def show_all_z(author,token):
219
+ spaces=[]
220
+ api = HfApi(token=token)
221
+ #author=name
222
+ s_ist = (api.list_spaces(author=author))
223
+ file_list = []
224
+ file_list_ea=[]
225
+ for i,space in enumerate(s_ist):
226
+ space_ea = space.id.split("/",1)[1]
227
+ spaces.append(space_ea)
228
+ #print (space_ea)
229
+ f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
230
+ #print (f_ist)
231
+ if not os.path.exists(space_ea):
232
+ os.makedirs(space_ea)
233
+ file= snapshot_download(repo_id=f'{author}/{space_ea}', repo_type="space")
234
+ shutil.make_archive(f"{space_ea}", 'zip', file)
235
+ file_list.append(f'{space_ea}.zip')
236
+ yield file_list
237
+ with ZipFile(f"{author}.zip", "w") as zipObj:
238
+ for idx, file in enumerate(file_list):
239
+ try:
240
+ zipObj.write(f'{file}')
241
+ except Exception:
242
+ pass
243
+ file_list.append(f'{author}.zip')
244
+ yield file_list
245
+
246
+ def dl_checked_fn(author,checklist,token):
247
+ spaces=[]
248
+ api = HfApi(token=token)
249
+ #author=name
250
+ s_ist = checklist
251
+ file_list = []
252
+ file_list_ea=[]
253
+ for i,space_ea in enumerate(s_ist):
254
+ #spaces.append(space_ea)
255
+ #print (space_ea)
256
+ f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
257
+ #print (f_ist)
258
+ if not os.path.exists(space_ea):
259
+ os.makedirs(space_ea)
260
+ file= snapshot_download(repo_id=f'{author}/{space_ea}', repo_type="space")
261
+ shutil.make_archive(f"{space_ea}", 'zip', file)
262
+ file_list.append(f'{space_ea}.zip')
263
+ yield file_list
264
+ with ZipFile(f"{author}.zip", "w") as zipObj:
265
+ for idx, file in enumerate(file_list):
266
+ try:
267
+ zipObj.write(f'{file}')
268
+ except Exception:
269
+ pass
270
+ file_list.append(f'{author}.zip')
271
+ yield file_list
272
+ def get_space_runtime(author,checklist,token):
273
+ api=HfApi(token=token)
274
+ space_info=[]
275
+ for space in checklist:
276
+ outp=api.get_space_runtime(f'{author}/{space}')
277
+ space_info.append(outp['stage'])
278
+ return space_info
279
+ def delete_checked(confirm_val,author,checklist,token):
280
+ if confirm_val=="CONFIRM":
281
+ api = HfApi(token=token)
282
+ s_ist = checklist
283
+ delete_list = []
284
+ for i,space_ea in enumerate(s_ist):
285
+ try:
286
+ api.delete_repo(repo_id=f'{author}/{space_ea}',repo_type='space')
287
+ delete_list.append(f'Deleted:: {space_ea}\n')
288
+ yield delete_list,gr.update(visible=False)
289
+ except Exception as e:
290
+ yield e
291
+ else:
292
+ yield "Not Deleting", gr.update(visible=False)
293
+
294
+ def checkp(p):
295
+ if p == o:
296
+ return gr.update(visible=False), gr.update(visible=True)
297
+ elif p != o:
298
+ return None, None
299
+ def update_checked_message(inp):
300
+ html=""
301
+ for ea in inp:
302
+ html=f'{html}<br>{ea}'
303
+ return html
304
+
305
+ def ru_sure_fn():
306
+ return gr.update(visible=True)
307
+
308
+ with gr.Blocks(css=css) as build:
309
+ with gr.Row(visible=False) as no:
310
+ pass_box=gr.Textbox()
311
+ pass_btn=gr.Button()
312
+ with gr.Box(visible=True) as yes:
313
+ with gr.Box():
314
+ with gr.Row():
315
+ with gr.Column(scale=2):
316
+ r_name = gr.Textbox(label="Repo")
317
+ token = gr.Textbox(label="auth")
318
+ s_btn = gr.Button("Show Spaces")
319
+ with gr.Column(scale=1):
320
+ files=gr.File(file_count="directory")
321
+ with gr.Tab("View 1"):
322
+ with gr.Row():
323
+ with gr.Column(scale=1):
324
+ with gr.Group():
325
+ space_radio=gr.Radio(label="Spaces",choices=[])
326
+ with gr.Column(scale=3):
327
+ with gr.Tab("Space Info"):
328
+ with gr.Row():
329
+ with gr.Column(scale=3):
330
+ space_info_json=gr.JSON()
331
+ gr.Column(scale=1)
332
+ with gr.Tab("Files"):
333
+ with gr.Column(scale=1):
334
+ file_radio=gr.Radio(label="Files",choices=[])
335
+ with gr.Column(scale=3):
336
+ file_contents=gr.HTML()
337
+ with gr.Tab("View 2"):
338
+ with gr.Row():
339
+ with gr.Column(scale=2):
340
+ with gr.Group():
341
+ with gr.Row():
342
+ s_name = gr.Dropdown(label="Spaces", choices=[])
343
+ f_name = gr.Dropdown(label="Files", choices=[])
344
+ with gr.Row():
345
+ l_btn = gr.Button("Load Space")
346
+ with gr.Row():
347
+ show_all_btn1 = gr.Button("Load All 1",visible=False)
348
+ show_all_btn2 = gr.Button("Load All 2",visible=False)
349
+ uu=gr.Textbox(visible=False)
350
+ file_frame=gr.HTML()
351
+
352
+ with gr.Tab("View 3"):
353
+ with gr.Row():
354
+ with gr.Column(scale=1):
355
+ with gr.Group():
356
+ space_check=gr.CheckboxGroup(elem_id="my_checkbox",label="Spaces",choices=[])
357
+ with gr.Column(scale=1):
358
+ message_box=gr.HTML()
359
+ with gr.Column(scale=1):
360
+ dl_checked_btn=gr.Button("Download Checked")
361
+ delete_checked_btn=gr.Button("Delete Checked")
362
+ with gr.Row(visible=False) as sure_row:
363
+ with gr.Column():
364
+ ru_sure_box=gr.Textbox(label="type: 'CONFIRM' to confirm", value="")
365
+ ru_sure_btn=gr.Button("Confirm Delete")
366
+
367
+ with gr.Row():
368
+ del_message_box=gr.HTML()
369
+ gr.Column()
370
+ sure_check=gr.Textbox(visible=False,value="")
371
+
372
+
373
+
374
+ dl_checked_btn.click(dl_checked_fn,[r_name,space_check,token],files)
375
+ delete_checked_btn.click(ru_sure_fn,None,sure_row)
376
+ 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])
377
+
378
+ space_check.change(update_checked_message,space_check,message_box)
379
+
380
+
381
+ show_all_btn1.click(show_all,[r_name,token],files)
382
+ show_all_btn2.click(show_all_z,[r_name,token],files)
383
+
384
+ s_btn.click(show_s,[r_name,token],[s_name,space_radio,space_check])
385
+ l_btn.click(show_f,[r_name,s_name,token], [f_name, files,file_radio])
386
+ #f_name.change(show_f_frame,[r_name,s_name,f_name],[file_frame])
387
+ s_name.change(show_f_frame2,[r_name,s_name,f_name],[file_frame])
388
+
389
+ #s_name.change(show_f,[r_name,s_name,token],[d_app,files])
390
+ space_radio.change(show_f,[r_name,space_radio,token],[f_name, files,file_radio,space_info_json])
391
+ file_radio.change(show_f_cont,[r_name,space_radio,file_radio,token],[file_contents])
392
+
393
+ pass_btn.click(checkp,pass_box,[no,yes])
394
+ #inbut.click(build_space,[token,t_name,s_type,m_type,r_type,d_app,d_css],output_html)
395
+ build.queue(concurrency_count=10).launch(show_api=False)