File size: 9,045 Bytes
bf7e776
 
 
 
c096185
1287876
1309821
bf7e776
 
 
2f99034
 
 
 
bf7e776
 
 
1494593
bf7e776
 
 
 
 
 
 
 
c10604f
 
54f77d8
30dfcea
2e3fad5
bac476a
 
2f99034
f439ae6
f3b4a31
2f99034
f3b4a31
e1bafb8
bfb3a95
f3b4a31
ccb94ef
32bcc3f
e1bafb8
bfb3a95
 
fcf6547
0ec69ec
54f77d8
d6a90d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54f77d8
 
9a0829f
9098a37
54f77d8
2e3fad5
81e7494
2e3fad5
436dec2
c10604f
 
ca5c6d6
 
f12b930
fcf6547
 
 
 
 
 
 
 
 
0159c24
4be1ecc
 
 
 
 
5dd7b2f
436dec2
 
c10604f
ee1f0ca
436dec2
180d475
e08238f
436dec2
c10604f
6a7e928
 
 
 
90e8610
5fc780a
90d3d82
c2127e1
 
 
90d3d82
 
5652deb
1309821
90d3d82
9c76c30
830a4d4
d6ce862
 
5652deb
a27697b
 
0ad33df
a27697b
892925c
 
c10604f
faa3ea4
bf7e776
 
 
 
 
ca5c6d6
436dec2
ca5c6d6
436dec2
ca5c6d6
436dec2
bf7e776
436dec2
c096185
bf7e776
c7763ca
436dec2
 
c7763ca
 
 
 
faa3ea4
 
c7763ca
 
bf7e776
 
 
 
 
 
 
 
 
 
 
 
 
d8875c8
7aac00c
 
 
 
 
436dec2
 
 
 
fcf6547
 
436dec2
 
 
 
fcf6547
 
 
7aac00c
 
 
 
 
436dec2
30394af
 
 
948b7af
7aac00c
 
ba0835f
6a7e928
 
 
0ec69ec
 
 
892925c
 
 
6f25544
 
7736d1d
fcf6547
6f25544
 
f12b930
 
 
 
7736d1d
fcf6547
f12b930
 
f54ce4a
c10604f
6a7e928
2359485
 
5dd7b2f
0159c24
f12b930
892925c
2359485
fcf6547
d6a90d0
fcf6547
 
 
2359485
0ec69ec
54f77d8
 
f12b930
a38c6be
c10604f
bf7e776
c23e907
bf7e776
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import sys
import uuid
import gradio as gr
import requests
import json
import tarfile
from pathlib import Path 
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
uid = uuid.uuid4()
print(uid)
isExist = os.path.exists(f'{uid}')
if not isExist:
   os.makedirs(f'{uid}')
#token_self = os.environ['HF_TOKEN']
o=os.environ['P']
model_list = [
    
]

types=[
    "A",
    "B",
    "C",
    "R1",
]


def show_s(name,d_app,token):
    api = HfApi(token=token)
    author=name
    s_ist = (api.list_spaces(author=author))
    print (s_ist)
    file_j = open(f'{uid}/tmp_j','w')
    file_j.writelines(str(s_ist))
    file_j.close()
    file_b = open(f'{uid}/tmp_j','r')
    r_file = file_b.readlines()
    spaces=[]
    spaces2=[]
    for line in r_file:
        if "'id':" in line:
            line = line.split(":",1)[1]
            line = line.split("/",1)[1]
            line2 = line.replace("'","").replace(",","").replace("\n","")
            spaces.append(line2)
            #print(f"{line2}")
    return(gr.update(label="Spaces", choices=[s for s in spaces]),gr.update(label="Spaces", choices=[s for s in spaces]))

def show_s_d(name,d_app,token):
    api = HfApi(token=token)
    author=name
    s_ist = (api.list_spaces(author=author))
    print (s_ist)
    file_j = open(f'{uid}/tmp_j','w')
    file_j.writelines(str(s_ist))
    file_j.close()
    file_b = open(f'{uid}/tmp_j','r')
    r_file = file_b.readlines()
    spaces=[]
    spaces2=[]
    for line in r_file:
        if "'id':" in line:
            line = line.split(":",1)[1]
            line = line.split("/",1)[1]
            line2 = line.replace("'","").replace(",","").replace("\n","")
            spaces.append(line2)
            #print(f"{line2}")
    return(gr.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"))
    return(gr.Dropdown.update(label="Files", choices=[f for f in f_ist]))

def load_space(r_name,s_name,d_app):
    print(f'https://huggingface.co/spaces/{r_name}/{s_name}/raw/main/{d_app}')
    r = requests.get(f'https://huggingface.co/spaces/{r_name}/{s_name}/raw/main/{d_app}') 
    file = open(f'{uid}/{d_app}','w')
    file.writelines(r.text)
    file.close()
    o_file = open(f'{uid}/mod_{d_app}','w')
    o_file.writelines(r.text)    
    return r.text,r.text
def load_space_d(r_name,s_name,d_app):
    print(f'https://huggingface.co/spaces/{r_name}/{s_name}/raw/main/{d_app}')
    r = requests.get(f'https://huggingface.co/spaces/{r_name}/{s_name}/raw/main/{d_app}') 
    file = open(f'{uid}/d{d_app}','w')
    file.writelines(r.text)
    file.close()
    o_file = open(f'{uid}/mod_d_{d_app}','w')
    o_file.writelines(r.text)    
    return r.text    
def upd(x,d_app):
    file = open(f'{uid}/{d_app}','w')
    file.writelines(x)
    file.close()
    o_file = open(f'{uid}/mod_{d_app}','w')
    o_file.writelines(x)    
    return x       
def repl(rs,rt,d_app):
    file = open(f'{uid}/{d_app}','r')
    r_file = file.read()
    r_file = r_file.replace(f'{rs}',f'{rt}')
    o_file = open(f'{uid}/mod_{d_app}','w')
    o_file = o_file.write(r_file)
    file.close()
    return r_file, f'{uid}/mod_{d_app}'

def get_f(r_name,spaces,token):
    api = HfApi(token=token)
    spaces_b=[]
    files_b=[]
    files_c=[]
    
    for space in spaces:
        isExist = os.path.exists(f'{space}')
        if not isExist:
           os.makedirs(f'{space}')
        f_ist = (api.list_repo_files(repo_id=f'{r_name}/{space}', repo_type="space"))
        for file in f_ist:
            t_file=open(f'{uid}/{space}-{file}','w')
            zz_file=open(f'{space}/{file}','w')
            r = requests.get(f'https://huggingface.co/spaces/{r_name}/{space}/raw/main/{file}')
            t_file.write(r.text)
            t_file.close()
            zz_file.write(r.text)
            zz_file.close()
            files_b.append(f'{uid}/{space}-{file}')
        with tarfile.open(f'{space}.tar.gz','w:gz') as tar:
            tar.add(f"./{space}",arcname=os.path.basename(f"./{space}"))

        spaces_b.append(f'{space}.tar.gz')
            
    return(files_b,spaces_b)

def build_space(token,r_name,s_name,d_app,rd_name,sd_name,dd_app,priv):
    if token==None or token=="":
        token = token_self
    else:
        token = token
        pass
    if dd_app=="" or dd_app==None:
        dd_app=d_app
    if rd_name=="" or rd_name==None:
        rd_name=r_name
    if sd_name=="" or sd_name==None:
        sd_name=s_name
    api = HfApi(token=token)
    repo_name=f'{rd_name}/{sd_name}'
    print (repo_name)
    try:
        api.upload_file(
            path_or_fileobj=f"{uid}/mod_{d_app}",
            path_in_repo=f"{dd_app}",
            repo_id=repo_name,
            token=token,
            repo_type="space",
        )
        if priv==True:
            api.update_repo_visibility(repo_id=repo_name,private=True,repo_type="space")
        #os.remove(f"{uid}at.py")
        return gr.HTML.update(f'''<center>Link to space <a href="https://huggingface.co/spaces/{repo_name}" target="_blank">https://huggingface.co/spaces/{repo_name}</a><center><br>''')
    except Exception as e:
        return gr.HTML.update(f"""{str(e)}""")

        
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=True) as no:
        pass_box=gr.Textbox()
        pass_btn=gr.Button()
    with gr.Box(visible=False) as yes:
        with gr.Row():
            gr.Column(scale=1)
            with gr.Column(scale=3):
                with gr.Box():
                    with gr.Row():
                        with gr.Column():
                            r_name = gr.Textbox(label="Source Repo") 
                            s_name = gr.Dropdown(label="Spaces", choices=[])
                            d_app = gr.Dropdown(label="Files", choices=[])                       
                            s_btn = gr.Button("Show")
                            l_btn = gr.Button("Load")
                        with gr.Column():
                            rd_name = gr.Textbox(label="Dest Repo") 
                            sd_name = gr.Dropdown(label="Spaces", choices=[])
                            dd_app = gr.Dropdown(label="Files", choices=[])              
                            sd_btn = gr.Button("Show")
                            ld_btn = gr.Button("Load")                            

                with gr.Box():
                    r_s = gr.Textbox(label="Replace")
                    r_w = gr.Textbox(label="with:")
                    r_btn = gr.Button("Replace")
                with gr.Column():
                    with gr.Row():
                        with gr.Column(scale=3):
                            token = gr.Textbox(label="auth")
                            inbut = gr.Button("Update Repo")
                        priv = gr.Checkbox(label="Private",value=True)
                    output_html = gr.HTML("""""")
            gr.Column(scale=1)
        with gr.Accordion("Files",open=False):
            with gr.Tab("Modify"):
                files=gr.File()
            with gr.Tab("Download"):
                with gr.Row():
                    ss_name = gr.Dropdown(label="Spaces", choices=[],multiselect=True)
                    f_btn=gr.Button()
                with gr.Row():
                    a_files=gr.File()
                    z_files=gr.File()
        with gr.Row():
            rm_but=gr.Button("Update Source")                    
        with gr.Tab("Source"):
            with gr.Row():
                r_t=gr.Textbox(lines=30,max_lines=100)
                m_t=gr.Textbox(lines=30,max_lines=100)
        with gr.Tab("Mod Source"):
            with gr.Row():
                rm_t=gr.Textbox(lines=30,max_lines=100,interactive=True)
                mm_t=gr.Textbox(lins=30,max_lines=100)
        with gr.Tab("Dest"):
            with gr.Row():
                rd_t=gr.Textbox(lines=30,max_lines=100)
                md_t=gr.Textbox(lins=30,max_lines=100)
        au=gr.Textbox(visible=False)
        uu=gr.Textbox(visible=False)
        
    def cyc(x):
        return x,None
 
    rm_but.click(upd,[rm_t,d_app],[r_t])
    
    f_btn.click(get_f,[r_name,ss_name,token],[a_files,z_files])
    r_t.change(cyc,r_t,[m_t,files])

    sd_btn.click(show_s_d,rd_name,[sd_name])    
    sd_name.change(show_f,[rd_name,sd_name],dd_app)    
    ld_btn.click(load_space_d,[rd_name,sd_name,dd_app],[rd_t])

    
    s_btn.click(show_s,r_name,[s_name,ss_name])    
    s_name.change(show_f,[r_name,s_name],d_app)    

    l_btn.click(load_space,[r_name,s_name,d_app],[r_t,rm_t])
    r_btn.click(repl,[r_s,r_w,d_app], [m_t,files])
    
    pass_btn.click(checkp,pass_box,[no,yes])
    inbut.click(build_space,[token,r_name,s_name,d_app,rd_name,sd_name,dd_app,priv],output_html)
build.queue(concurrency_count=10).launch()