File size: 5,108 Bytes
c45a381
 
 
 
 
 
 
 
 
27ae1cd
c45a381
2bebbc7
c45a381
bffdcb5
c45a381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa8ee9
c45a381
 
 
 
 
 
3104dbb
c45a381
 
 
 
 
 
 
 
 
7f57d30
11c1635
c45a381
 
 
 
 
7f57d30
c45a381
 
 
 
 
 
 
6f4dbd0
c45a381
 
 
 
 
 
 
 
27ae1cd
a914fc6
 
c45a381
 
 
 
 
 
 
 
 
 
 
 
6f4dbd0
c45a381
 
 
 
 
 
 
 
 
 
 
cd61055
c45a381
cd61055
c45a381
 
 
 
 
 
 
 
 
 
 
 
 
845f8f8
c45a381
 
845f8f8
c45a381
 
 
 
 
 
 
 
 
 
 
 
 
c56f84e
c45a381
 
 
 
 
 
 
 
 
c56f84e
 
 
c45a381
 
 
 
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
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,get_full_repo_name,upload_file,CommitOperationAdd,HfApi,snapshot_download)
uid = uuid.uuid4()
#token = os.environ['HF_TOKEN']
#token_self = os.environ['HF_TOKEN']
#o=os.environ['P']


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]
        spaces.append(space_ea)
        print (space_ea)
    return(gr.Dropdown.update(label="Spaces", choices=[s for s in spaces]))

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:
        
        r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
        #print (r.text)
        uid = uuid.uuid4()

        sf=d_app.split(".",1)[1]
        pf=d_app.split(".",1)[0]
        f_name=f'{pf}.{sf}'
        file = open(f'{name}/{f_name}','w')
        file.writelines(r.text)
        file.close()
        file_list.append(Path(f'{name}/{f_name}'))
        file_out.append(d_app)

    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')

    return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist]), file_list)

def show_all(author,token):
    spaces=[]
    api = HfApi(token=token)
    #author=name
    s_ist = (api.list_spaces(author=author))
    file_list = []
    file_list_ea=[]
    for i,space in enumerate(s_ist):
        space_ea = space.id.split("/",1)[1]
        spaces.append(space_ea)
        #print (space_ea)
        f_ist = (api.list_repo_files(repo_id=f'{author}/{space_ea}', repo_type="space"))
        #print (f_ist)
        if not os.path.exists(space_ea):
               os.makedirs(space_ea)
        file= snapshot_download(repo_id=f'{author}/{space_ea}', repo_type="space")
        shutil.make_archive("temp", 'zip', file)
        return "temp.zip"
        for d_app in f_ist:
            
            r = requests.get(f'https://huggingface.co/spaces/{author}/{space_ea}/raw/main/{d_app}')
            #print (r.text)
            uid = uuid.uuid4()
            try:
                sf=d_app.split(".",1)[1]
                pf=d_app.split(".",1)[0]
                f_name=f'{pf}.{sf}'
                file = open(f'{space_ea}/{f_name}','w')
                file.writelines(r.text)
                file.close()
                #file_list_ea.append(Path(f'{space}/{f_name}'))
            except Exception:
                pass
        with ZipFile(f"{space_ea}.zip", "w") as zipObj:
            for idx, file in enumerate(f_ist):
                try:
                    zipObj.write(f'{space_ea}/{file}')   
                except Exception:
                    pass
                
        file_list.append(f'{space_ea}.zip')
    with ZipFile(f"{author}.zip", "w") as zipObj:
        for idx, file in enumerate(file_list):
            try:
                zipObj.write(f'{file}')   
            except Exception:
                pass
    file_list.append(f'{author}.zip')
    return file_list


        
def checkp(p):
    if p == o:
        return gr.update(visible=False), gr.update(visible=True)
    elif p != o:
        return None, None 
with gr.Blocks() as build:
    with gr.Row(visible=False) as no:
        pass_box=gr.Textbox()
        pass_btn=gr.Button()
    with gr.Box(visible=True) as yes:
        with gr.Row():
            gr.Column(scale=1)
            with gr.Column(scale=3):
                with gr.Box():
                    with gr.Row():
                        r_name = gr.Textbox(label="Repo") 
                        token = gr.Textbox(label="auth")
                    with gr.Row():
                        s_name = gr.Dropdown(label="Spaces", choices=[])
                        d_app = gr.Dropdown(label="Files", choices=[])
                    with gr.Row():
                        s_btn = gr.Button("Show")
                        l_btn = gr.Button("Load")
            show_all_btn = gr.Button("Load All")
            gr.Column(scale=1)
        files=gr.File(file_count="directory")
        with gr.Row():
            r_t=gr.Textbox(max_lines=100)
            m_t=gr.Textbox(max_lines=100)
        uu=gr.Textbox(visible=False)

    show_all_btn.click(show_all,[r_name,token],files)
    
    s_btn.click(show_s,[r_name,token],s_name)    
    s_name.change(show_f,[r_name,s_name,token],[d_app,files])    
    l_btn.click(show_f,[r_name,s_name,token], [d_app, files])
    
    pass_btn.click(checkp,pass_box,[no,yes])
    #inbut.click(build_space,[token,t_name,s_type,m_type,r_type,d_app,d_css],output_html)
build.queue(concurrency_count=10).launch()