Spaces:
Running
Running
Create dl.py
Browse files
dl.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
11 |
+
uid = uuid.uuid4()
|
12 |
+
token = os.environ['HF_TOKEN']
|
13 |
+
#token_self = os.environ['HF_TOKEN']
|
14 |
+
o=os.environ['P']
|
15 |
+
model_list = [
|
16 |
+
|
17 |
+
]
|
18 |
+
|
19 |
+
types=[
|
20 |
+
"A",
|
21 |
+
"B",
|
22 |
+
"C",
|
23 |
+
"R1",
|
24 |
+
]
|
25 |
+
|
26 |
+
|
27 |
+
def show_s(name,token):
|
28 |
+
spaces=[]
|
29 |
+
spaces.append("")
|
30 |
+
|
31 |
+
api = HfApi(token=token)
|
32 |
+
author=name
|
33 |
+
s_ist = (api.list_spaces(author=author))
|
34 |
+
for i,space in enumerate(s_ist):
|
35 |
+
space_ea = space.id.split("/",1)[1]
|
36 |
+
spaces.append(space_ea)
|
37 |
+
print (space_ea)
|
38 |
+
return(gr.Dropdown.update(label="Spaces", choices=[s for s in spaces]))
|
39 |
+
|
40 |
+
def show_f(repo,name,token):
|
41 |
+
api = HfApi(token=token)
|
42 |
+
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
|
43 |
+
print (f_ist)
|
44 |
+
file_list = []
|
45 |
+
if not os.path.exists(name):
|
46 |
+
os.makedirs(name)
|
47 |
+
|
48 |
+
for d_app in f_ist:
|
49 |
+
|
50 |
+
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
|
51 |
+
print (r.text)
|
52 |
+
uid = uuid.uuid4()
|
53 |
+
|
54 |
+
sf=d_app.split(".",1)[1]
|
55 |
+
pf=d_app.split(".",1)[0]
|
56 |
+
f_name=f'{pf}.{sf}'
|
57 |
+
file = open(f'{name}/{f_name}','w')
|
58 |
+
file.writelines(r.text)
|
59 |
+
file.close()
|
60 |
+
file_list.append(Path(f'{name}/{f_name}'))
|
61 |
+
with ZipFile(f"{name}.zip", "w") as zipObj:
|
62 |
+
for idx, file in enumerate(f_ist):
|
63 |
+
zipObj.write(f'{name}/{file}')
|
64 |
+
file_list.append(f'{name}.zip')
|
65 |
+
|
66 |
+
return(gr.Dropdown.update(label="Spaces", choices=[f for f in f_ist]), file_list)
|
67 |
+
|
68 |
+
def show_all(author,token):
|
69 |
+
spaces=[]
|
70 |
+
api = HfApi(token=token)
|
71 |
+
#author=name
|
72 |
+
s_ist = (api.list_spaces(author=author))
|
73 |
+
file_list = []
|
74 |
+
for i,space in enumerate(s_ist):
|
75 |
+
space_ea = space.id.split("/",1)[1]
|
76 |
+
spaces.append(space_ea)
|
77 |
+
#print (space_ea)
|
78 |
+
f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
|
79 |
+
#print (f_ist)
|
80 |
+
if not os.path.exists(space_ea):
|
81 |
+
os.makedirs(space_ea)
|
82 |
+
|
83 |
+
for d_app in f_ist:
|
84 |
+
|
85 |
+
r = requests.get(f'https://huggingface.co/spaces/{author}/{space_ea}/raw/main/{d_app}')
|
86 |
+
#print (r.text)
|
87 |
+
uid = uuid.uuid4()
|
88 |
+
try:
|
89 |
+
sf=d_app.split(".",1)[1]
|
90 |
+
pf=d_app.split(".",1)[0]
|
91 |
+
f_name=f'{pf}.{sf}'
|
92 |
+
file = open(f'{space_ea}/{f_name}','w')
|
93 |
+
file.writelines(r.text)
|
94 |
+
file.close()
|
95 |
+
#file_list.append(Path(f'{space}/{f_name}'))
|
96 |
+
except Exception:
|
97 |
+
pass
|
98 |
+
with ZipFile(f"{space_ea}.zip", "w") as zipObj:
|
99 |
+
for idx, file in enumerate(f_ist):
|
100 |
+
try:
|
101 |
+
zipObj.write(f'{space_ea}/{file}')
|
102 |
+
except Exception:
|
103 |
+
pass
|
104 |
+
|
105 |
+
file_list.append(f'{space_ea}.zip')
|
106 |
+
with ZipFile(f"{author}.zip", "w") as zipObj:
|
107 |
+
for idx, file in enumerate(s_ist):
|
108 |
+
try:
|
109 |
+
zipObj.write(f'{space_ea}.zip')
|
110 |
+
except Exception:
|
111 |
+
pass
|
112 |
+
file_list.append(f'{author}.zip')
|
113 |
+
return file_list
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
def checkp(p):
|
118 |
+
if p == o:
|
119 |
+
return gr.update(visible=False), gr.update(visible=True)
|
120 |
+
elif p != o:
|
121 |
+
return None, None
|
122 |
+
with gr.Blocks() as build:
|
123 |
+
with gr.Row(visible=True) as no:
|
124 |
+
pass_box=gr.Textbox()
|
125 |
+
pass_btn=gr.Button()
|
126 |
+
with gr.Box(visible=False) as yes:
|
127 |
+
with gr.Row():
|
128 |
+
gr.Column(scale=1)
|
129 |
+
with gr.Column(scale=3):
|
130 |
+
with gr.Box():
|
131 |
+
with gr.Row():
|
132 |
+
r_name = gr.Textbox(label="Repo")
|
133 |
+
token = gr.Textbox(label="auth")
|
134 |
+
with gr.Row():
|
135 |
+
s_name = gr.Dropdown(label="Spaces", choices=[])
|
136 |
+
d_app = gr.Dropdown(label="Files", choices=[])
|
137 |
+
with gr.Row():
|
138 |
+
s_btn = gr.Button("Show")
|
139 |
+
l_btn = gr.Button("Load")
|
140 |
+
show_all_btn = gr.Button("Show All")
|
141 |
+
gr.Column(scale=1)
|
142 |
+
files=gr.File(file_count="directory")
|
143 |
+
with gr.Row():
|
144 |
+
r_t=gr.Textbox(max_lines=100)
|
145 |
+
m_t=gr.Textbox(max_lines=100)
|
146 |
+
uu=gr.Textbox(visible=False)
|
147 |
+
|
148 |
+
show_all_btn.click(show_all,[r_name,token],files)
|
149 |
+
|
150 |
+
s_btn.click(show_s,[r_name,token],s_name)
|
151 |
+
s_name.change(show_f,[r_name,s_name,token],[d_app,files])
|
152 |
+
|
153 |
+
|
154 |
+
pass_btn.click(checkp,pass_box,[no,yes])
|
155 |
+
#inbut.click(build_space,[token,t_name,s_type,m_type,r_type,d_app,d_css],output_html)
|
156 |
+
build.queue(concurrency_count=10).launch()
|