Omnibus commited on
Commit
83e9e0e
·
1 Parent(s): 8a03e1b

Create app.py

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