File size: 9,291 Bytes
b78e131
 
 
 
 
 
4b8e09b
b78e131
 
 
 
6ac3085
b78e131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff2e96
6ac3085
3182be6
b78e131
 
6ac3085
b78e131
 
 
 
 
 
 
3182be6
 
b78e131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ac3085
 
b78e131
4b8e09b
b78e131
 
 
4b8e09b
b78e131
 
 
 
 
4b8e09b
 
 
 
 
6ac3085
 
 
4b8e09b
 
b78e131
4b8e09b
 
 
 
 
 
 
 
 
 
 
 
6ac3085
 
 
4b8e09b
 
 
 
 
 
 
 
 
 
bf1b6d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff2e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
975ffba
6ff2e96
 
 
 
 
 
3182be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf1b6d5
6ac3085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b8e09b
b78e131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ac3085
 
 
 
 
 
 
 
 
b78e131
 
 
 
 
 
6ac3085
b78e131
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import sys
import uuid
import gradio as gr
from pathlib import Path 
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
uid = uuid.uuid4()

#token_self = os.environ['HF_TOKEN']
o=os.environ['P']
model_list = [
    "",
    "CompVis/stable-diffusion-v1-4",
    "runwayml/stable-diffusion-v1-5",
    "prompthero/openjourney",
    "stabilityai/stable-diffusion-2-1",
    "stabilityai/stable-diffusion-2-1-base",
    "andite/anything-v4.0",
    "Linaqruf/anything-v3.0",
    "eimiss/EimisAnimeDiffusion_1.0v",
    "nitrosocke/Nitro-Diffusion",
    "wavymulder/portraitplus",
    "22h/vintedois-diffusion-v0-1",
    "dreamlike-art/dreamlike-photoreal-2.0",
    "dreamlike-art/dreamlike-diffusion-1.0",
    "wavymulder/Analog-Diffusion",
    "nitrosocke/redshift-diffusion",
    "claudfuen/photorealistic-fuen-v1",
    "prompthero/openjourney-v2",
    "johnslegers/epic-diffusion",
    "nitrosocke/Arcane-Diffusion",
    "darkstorm2150/Protogen_x5.8_Official_Release",

]

types=[
    "A",
    "B",
    "C",
    "D",
    "R1",
    "Auto Image Space"
]
 
def build_space(token,t_name,s_type,m_type=None,r_type=None,d_app=None,d_css=None):
    uid = uuid.uuid4()
    model_id=t_name
    if token==None or token=="":
        token = token_self
    else:
        token = token
        pass
    if r_type != "" or r_type != None:
        m_type=r_type
    api = HfApi(token=token)
    repo_name = get_full_repo_name(model_id=model_id, token=token)
    try:
        repo_url = api.create_repo(
            repo_id=model_id,
            repo_type="space",
            space_sdk="gradio",
            private=True,
        )
        print(f"{repo_name} built")
    except Exception as e:
        return gr.HTML.update(f"""{str(e)}""")

    try:
        api_url = f'https://huggingface.co/api/spaces/{model_id}'
        if s_type == "A":
            with open("a.py", "r") as f:
                app = f.read()
            app = app.replace("$name2", m_type)
            app = app.replace("$app2", d_app)
            app = app.replace("$css2", d_css)
   
            with open(f"{uid}at.py", "w") as f:
                f.write(app)
    
            api.upload_file(
                path_or_fileobj=f"{uid}at.py",
                path_in_repo="app.py",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}at.py")
        elif s_type == "B":
            with open("b.py", "r") as f:
                app = f.read()
            app = app.replace("$name2", m_type)
            app = app.replace("$app2", d_app)
            app = app.replace("$css2", d_css)
            
            with open(f"{uid}bt.py", "w") as f:
                f.write(app)
    
            api.upload_file(
                path_or_fileobj=f"{uid}bt.py",
                path_in_repo="app.py",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}bt.py")    
        elif s_type == "C":
            with open("c.py", "r") as f:
                app = f.read()
            app = app.replace("$name2", m_type)
            app = app.replace("$app2", d_app)
            app = app.replace("$css2", d_css)
            
            with open(f"{uid}ct.py", "w") as f:
                f.write(app)
    
            api.upload_file(
                path_or_fileobj=f"{uid}ct.py",
                path_in_repo="app.py",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}ct.py") 

            with open("css.css", "r") as f:
                app = f.read()
            with open(f"{uid}css.css", "w") as f:
                f.write(app)
    
            api.upload_file(
                path_or_fileobj=f"{uid}css.css",
                path_in_repo="css.css",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}css.css")

        elif s_type == "D":
            with open("d.py", "r") as f:
                app = f.read()
            app = app.replace("$name2", m_type)
            app = app.replace("$app2", d_app)
            app = app.replace("$css2", d_css)
            with open(f"{uid}ct.py", "w") as f:
                f.write(app)
            api.upload_file(
                path_or_fileobj=f"{uid}ct.py",
                path_in_repo="app.py",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}ct.py") 
            with open("css.css", "r") as f:
                app = f.read()
            with open(f"{uid}css.css", "w") as f:
                f.write(app)
            api.upload_file(
                path_or_fileobj=f"{uid}css.css",
                path_in_repo="css.css",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}css.css")
            with open("readme.bac", "r") as f:
                app = f.read()
            with open(f"{uid}read.txt", "w") as f:
                f.write(app)
            api.upload_file(
                path_or_fileobj=f"{uid}read.txt",
                path_in_repo="README.md",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}read.txt")

        elif s_type == "Auto Image Space":
            with open("auto-image-space.py", "r") as f:
                app = f.read()
            app = app.replace("$name2", m_type)
            app = app.replace("$app2", d_app)
            app = app.replace("$css2", d_css)
            with open(f"{uid}ct.py", "w") as f:
                f.write(app)
            api.upload_file(
                path_or_fileobj=f"{uid}ct.py",
                path_in_repo="app.py",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}ct.py") 
            with open("css.css", "r") as f:
                app = f.read()
            app.replace("Gray","Yellow")
            with open(f"{uid}css.css", "w") as f:
                f.write(app)
            api.upload_file(
                path_or_fileobj=f"{uid}css.css",
                path_in_repo="css.css",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}css.css")
            with open("readme.bac", "r") as f:
                app = f.read()
            with open(f"{uid}read.txt", "w") as f:
                f.write(app)
            api.upload_file(
                path_or_fileobj=f"{uid}read.txt",
                path_in_repo="README.md",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}read.txt")
            
        elif s_type == "R1":
            with open("r1.py", "r") as f:
                app = f.read()
            app = app.replace("$name2", r_type)
            app = app.replace("$app2", d_app)
            app = app.replace("$css2", d_css)
            
            with open(f"{uid}r1t.py", "w") as f:
                f.write(app)
    
            api.upload_file(
                path_or_fileobj=f"{uid}r1t.py",
                path_in_repo="app.py",
                repo_id=repo_name,
                token=token,
                repo_type="space",
            )
            os.remove(f"{uid}r1t.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.Row(visible=False) as yes:
        gr.Column(scale=1)
        with gr.Column(scale=3):
            with gr.Box():
                token = gr.Textbox(label="auth")
                t_name = gr.Textbox(label="Space Name") 
                with gr.Row():
                    with gr.Column():
                        s_type = gr.Dropdown(label="Space Type",choices=[t for t in types])
                        d_app = gr.Textbox(label="App File", value="app.py")
                        d_css = gr.Textbox(label="CSS File", value="css.css")
                    with gr.Column():
                        m_type = gr.Dropdown(label="Model", choices=[m for m in model_list])
                        r_type = gr.Textbox(label="repo/name")
                with gr.Row():
                    inbut = gr.Button()
                output_html = gr.HTML("""""")
                files=gr.File()
        gr.Column(scale=1)
    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()