File size: 6,875 Bytes
d7d4ad7
 
 
 
 
 
 
 
 
b008ad4
d7d4ad7
 
 
 
 
 
9cafdcd
 
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
077ccc7
db0f00d
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1b2d02
 
 
 
d7d4ad7
 
e1b2d02
 
 
 
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
e1b2d02
d7d4ad7
 
b008ad4
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
9cafdcd
 
d7d4ad7
 
e1b2d02
 
 
 
 
d7d4ad7
 
 
 
d9e6d35
 
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
 
e1b2d02
 
 
d7d4ad7
 
 
 
 
 
 
 
 
 
db0f00d
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
db0f00d
d7d4ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db0f00d
d7d4ad7
e1b2d02
c36510f
 
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
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):
    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)
        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):
    api = HfApi(token=token)
    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}')                
                #sf=rem_2.split(".",1)[1]
                #pf=rem_2.split(".",1)[0]
                #f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
                f_name=f'{dir_1}/{dir2}/{rem_2}'
            
            else:
                #sf=rem_1.split(".",1)[1]
                #pf=rem_1.split(".",1)[0]
                #f_name=f'{dir_1}/{pf}.{sf}'
                f_name=f'{dir_1}/{rem_1}'
            print(f_name)

        else:
    
            sf=d_app.split(".",1)[1]
            pf=d_app.split(".",1)[0]
            f_name=f'{pf}.{sf}'            
            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}'))

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")
with gr.Blocks(css=css) as build:
    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):
                    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.Row():
                        dest=gr.Textbox(label="Destination")
                        move_btn=gr.Button("Move Repo")
                    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("View 2"):
            with gr.Row():
                with gr.Column(scale=2):
                    with gr.Group():
                        with gr.Row():
                            s_name = gr.Dropdown(label="Spaces", choices=[])
                            f_name = gr.Dropdown(label="Files", choices=[])
                        with gr.Row():
                            l_btn = gr.Button("Load Space")
                        with gr.Row():
                            show_all_btn1 = gr.Button("Load All 1",visible=False)
                            show_all_btn2 = gr.Button("Load All 2",visible=False)
            uu=gr.Textbox(visible=False)
            file_frame=gr.HTML()
    
        with gr.Tab("View 3"):
            with gr.Row():
                with gr.Column(scale=1):
                    with gr.Group():
                        space_check=gr.CheckboxGroup(elem_id="my_checkbox",label="Spaces",choices=[])
                with gr.Column(scale=1):
                    message_box=gr.HTML()
                with gr.Column(scale=1):
                    dl_checked_btn=gr.Button("Download Checked")
                    delete_checked_btn=gr.Button("Delete Checked")
                    with gr.Row(visible=False) as sure_row:
                        with gr.Column():
                            ru_sure_box=gr.Textbox(label="type: 'CONFIRM' to confirm", value="")
                            ru_sure_btn=gr.Button("Confirm Delete")
                            
                    with gr.Row():
                        del_message_box=gr.HTML()
                gr.Column()
        sure_check=gr.Textbox(visible=False,value="")
        '''

    move_btn.click(move,[r_name,space_radio,dest,token])
    s_btn.click(show_s,[r_name,token],[space_radio])    
    space_radio.change(show_f,[r_name,space_radio,token],[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)