File size: 7,646 Bytes
d7d4ad7
 
 
 
 
 
 
 
 
b008ad4
d7d4ad7
 
 
 
 
 
9cafdcd
 
d7d4ad7
 
 
b278c94
bdb6f6b
 
d7d4ad7
b278c94
d7d4ad7
 
 
 
 
 
 
 
bdb6f6b
 
 
b278c94
d7d4ad7
077ccc7
db0f00d
d7d4ad7
2b55725
8155b56
2b55725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d4ad7
2b55725
 
d7d4ad7
2b55725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6880472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1b2d02
 
 
 
 
b278c94
85f2c29
 
b278c94
059f608
b278c94
 
 
85f2c29
 
 
b278c94
85f2c29
ad75c7f
85f2c29
ad75c7f
 
b278c94
ad75c7f
 
b278c94
d7d4ad7
b278c94
d7d4ad7
 
 
d9e6d35
 
d7d4ad7
 
 
 
 
 
2b55725
d7d4ad7
 
 
 
 
 
 
8155b56
d7d4ad7
 
 
 
 
8155b56
d7d4ad7
 
 
 
 
8155b56
 
 
89b6f07
d7d4ad7
f6e9dd3
 
4b9928f
 
 
d7d4ad7
059f608
f6e9dd3
b278c94
059f608
b278c94
 
2b55725
d7d4ad7
db0f00d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import sys
import uuid
import gradio as gr
import requests
import json
from zipfile import ZipFile
import shutil
from pathlib import Path 
from huggingface_hub import (create_repo,move_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi,snapshot_download)
from PIL import Image
uid = uuid.uuid4()
#token = os.environ['HF_TOKEN']
#token_self = os.environ['HF_TOKEN']
#o=os.environ['P']
css="""
label.svelte-1mhtq7j.svelte-1mhtq7j.svelte-1mhtq7j {
    width: 100%;
}
"""

def show_s(name,token,save_list):
    if not save_list:
        save_list=[]
    spaces=[]
    #spaces.append("")

    api = HfApi(token=token)
    author=name
    s_ist = (api.list_spaces(author=author))
    for i,space in enumerate(s_ist):
        space_ea = space.id.split("/",1)[1]
        #s_info=api.space_info(f'{name}/{space}',files_metadata=True)
        #print(s_info)
        if space_ea in save_list:
            pass
        else:
            spaces.append(space_ea)
        #print (space_ea)
    return(gr.update(label="Spaces", choices=[s for s in spaces]))
           #gr.update(choices=[s for s in spaces],interactive=True))

def show_f(repo,name,token,tog):
    api = HfApi(token=token)
    if tog:
        f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
        print (f_ist)
        file_list = []
        #file_out = []
        
        if not os.path.exists(name):
               os.makedirs(name)
        for d_app in f_ist:
            if "/" in d_app:
                dir_1=d_app.split("/",1)[0]
                rem_1=d_app.split("/",1)[1]
                if not os.path.exists(f'{name}/{dir_1}'):
                       os.makedirs(f'{name}/{dir_1}')
                if "/" in rem_1:
                    dir_2=rem_1.split("/",1)[0]
                    rem_2=rem_1.split("/",1)[1]
                    if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
                           os.makedirs(f'{name}/{dir_1}/{dir_2}')                
                    f_name=f'{dir_1}/{dir2}/{rem_2}'
                
                else:
                    f_name=f'{dir_1}/{rem_1}'
                print(f_name)
            else:
                f_name=f'{d_app}'            
                pass
    
            r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
            print(d_app)
            #print (r.text)
            uid = uuid.uuid4()
            file = open(f'{name}/{f_name}','wb')
            #file = open(f'{name}/{f_name}','wb')
            file.write(r.content)
            file.close()
            file_list.append(f'{name}/{f_name}')
    
        with ZipFile(f"{name}.zip", "w") as zipObj:
            for idx, file in enumerate(f_ist):
                zipObj.write(f'{name}/{file}')         
        file_list.append(f'{name}.zip')
    
        s_info=api.space_info(f'{repo}/{name}',files_metadata=True)
        '''
        with open(f'{uid}-tmp.json','w') as f:
            json.dump(s_info,f,indent=4)
        f.close()
        '''
        return(file_list,
              gr.update(choices=[f for f in f_ist],interactive=True),
              api.get_space_runtime(f'{repo}/{name}'))
    else:
        return(None,
              None,
              api.get_space_runtime(f'{repo}/{name}'))        
def show_f_cont(repo,name,file,token):
    html_text = '<html>\n<body>\n<div>\n'
    images=[".png" , ".jpg" , ".gif" , ".webm" , ".mp4"]
    is_im=False 
    for x in images:
        if x in file:
            html_text += f'<object data="https://huggingface.co/spaces/{repo}/{name}/resolve/main/{file}" style="width:100%;font-size:small;"></object>'
            out_text = "Image File"
            is_im=True
        else:
            pass
    if is_im==False:
        print(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
        r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{file}')
        text=r.text
        html_text += f'<pre style="text-wrap:pretty;">{text}</pre>\n'
        out_text = r.text
    else:
        pass
    html_text += '</div>\n</body>\n</html>'
    return(html_text)



def move(repo,space,dest,token):
    from_id=f'{repo}/{space}'
    to_id=f'{dest}/{space}'
    move_repo(from_id=from_id,to_id=to_id,repo_type='space',token=token)
    print("REPO MOVED")

def add_save(space,save_state):
    save_state=[]if not save_state else save_state
    this = True
    for ea in save_state:
        if space == ea:
            this = False
    if this:
        #save_list.append(space)
        save_state.append(space)
    return save_state,gr.update(choices=[f for f in save_state],interactive=True)
    
def rem_save(space,save_state):
    save_out=[]
    for ea in save_state:
        print(space)
        print(ea)
        if not space == ea:
            save_out.append(space)
    return save_out, gr.update(choices=[f for f in save_out],interactive=True)
    
with gr.Blocks(css=css) as build:
    save_state=gr.State()
    with gr.Row(visible=False) as no:
        pass_box=gr.Textbox()
        pass_btn=gr.Button()
    with gr.Group(visible=True) as yes:
        with gr.Group():
            with gr.Row():
                with gr.Column(scale=2):
                    r_name = gr.Textbox(label="Repo") 
                    token = gr.Textbox(label="auth")        
                    s_btn = gr.Button("Show Spaces")
                with gr.Column(scale=1):
                    tog=gr.Checkbox(label="Show Files",value=True)
                    files=gr.File(file_count="directory")
        with gr.Tab("View 1"):
            with gr.Row():
                with gr.Column(scale=1):
                    with gr.Group():
                        space_radio=gr.Radio(label="Spaces",choices=[])
                with gr.Column(scale=3):

                    with gr.Tab("Space Info"):
                        with gr.Row():
                            with gr.Column(scale=3):
                                space_info_json=gr.JSON()
                            gr.Column(scale=1)
                     
                    with gr.Tab("Files"):
                        with gr.Column(scale=1):
                            file_radio=gr.Radio(label="Files",choices=[])
                        with gr.Column(scale=3):
                            file_contents=gr.HTML()
                    with gr.Tab("Move"):
                        with gr.Row():
                            dest=gr.Textbox(label="Destination")
                            move_btn=gr.Button("Move Space")                               
                        with gr.Row():
                            with gr.Column():
                                save_list=gr.Radio(label="Protected",choices=[])
                            with gr.Column():
                                save_btn=gr.Button("Protect")                            
                                rem_btn=gr.Button("Remove")                            

    save_btn.click(add_save,[space_radio,save_state],[save_state,save_list]).then(show_s,[r_name,token,save_state],[space_radio])
    rem_btn.click(rem_save,[space_radio,save_state],[save_state,save_list]).then(show_s,[r_name,token,save_state],[space_radio])
    
    move_btn.click(move,[r_name,space_radio,dest,token]).then(show_s,[r_name,token,save_state],[space_radio])
    s_btn.click(show_s,[r_name,token,save_list],[space_radio])    
    
    space_radio.change(show_f,[r_name,space_radio,token,tog],[files,file_radio,space_info_json])    
    file_radio.change(show_f_cont,[r_name,space_radio,file_radio,token],[file_contents])
build.queue(default_concurrency_limit=10).launch(show_api=False)