Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import uuid
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
|
| 7 |
+
uid = uuid.uuid4()
|
| 8 |
+
|
| 9 |
+
#token_self = os.environ['HF_TOKEN']
|
| 10 |
+
o=os.environ['P']
|
| 11 |
+
model_list = [
|
| 12 |
+
"",
|
| 13 |
+
"CompVis/stable-diffusion-v1-4",
|
| 14 |
+
"runwayml/stable-diffusion-v1-5",
|
| 15 |
+
"prompthero/openjourney",
|
| 16 |
+
"stabilityai/stable-diffusion-2-1",
|
| 17 |
+
"stabilityai/stable-diffusion-2-1-base",
|
| 18 |
+
"andite/anything-v4.0",
|
| 19 |
+
"Linaqruf/anything-v3.0",
|
| 20 |
+
"eimiss/EimisAnimeDiffusion_1.0v",
|
| 21 |
+
"nitrosocke/Nitro-Diffusion",
|
| 22 |
+
"wavymulder/portraitplus",
|
| 23 |
+
"22h/vintedois-diffusion-v0-1",
|
| 24 |
+
"dreamlike-art/dreamlike-photoreal-2.0",
|
| 25 |
+
"dreamlike-art/dreamlike-diffusion-1.0",
|
| 26 |
+
"wavymulder/Analog-Diffusion",
|
| 27 |
+
"nitrosocke/redshift-diffusion",
|
| 28 |
+
"claudfuen/photorealistic-fuen-v1",
|
| 29 |
+
"prompthero/openjourney-v2",
|
| 30 |
+
"johnslegers/epic-diffusion",
|
| 31 |
+
"nitrosocke/Arcane-Diffusion",
|
| 32 |
+
"darkstorm2150/Protogen_x5.8_Official_Release",
|
| 33 |
+
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
types=[
|
| 37 |
+
"A",
|
| 38 |
+
"B",
|
| 39 |
+
"C",
|
| 40 |
+
"R1",
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
def build_space(token,t_name,s_type,m_type=None,r_type=None,d_app=None,d_css=None):
|
| 44 |
+
uid = uuid.uuid4()
|
| 45 |
+
model_id=t_name
|
| 46 |
+
if token==None or token=="":
|
| 47 |
+
token = token_self
|
| 48 |
+
else:
|
| 49 |
+
token = token
|
| 50 |
+
pass
|
| 51 |
+
api = HfApi(token=token)
|
| 52 |
+
repo_name = get_full_repo_name(model_id=model_id, token=token)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
api_url = f'https://huggingface.co/api/spaces/{model_id}'
|
| 57 |
+
if s_type == "A":
|
| 58 |
+
with open(f'{api_url}/app.py', "r") as f:
|
| 59 |
+
app = f.read()
|
| 60 |
+
app = app.replace("gr.Blocks", "edited")
|
| 61 |
+
#app = app.replace("$app2", d_app)
|
| 62 |
+
#app = app.replace("$css2", d_css)
|
| 63 |
+
|
| 64 |
+
with open(f"{uid}at.py", "w") as f:
|
| 65 |
+
f.write(app)
|
| 66 |
+
|
| 67 |
+
api.upload_file(
|
| 68 |
+
path_or_fileobj=f"{uid}at.py",
|
| 69 |
+
path_in_repo="app.py",
|
| 70 |
+
repo_id=repo_name,
|
| 71 |
+
token=token,
|
| 72 |
+
repo_type="space",
|
| 73 |
+
)
|
| 74 |
+
os.remove(f"{uid}at.py")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
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>''')
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return gr.HTML.update(f"""{str(e)}""")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def checkp(p):
|
| 83 |
+
if p == o:
|
| 84 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 85 |
+
elif p != o:
|
| 86 |
+
return None, None
|
| 87 |
+
with gr.Blocks() as build:
|
| 88 |
+
with gr.Row(visible=True) as no:
|
| 89 |
+
pass_box=gr.Textbox()
|
| 90 |
+
pass_btn=gr.Button()
|
| 91 |
+
with gr.Row(visible=False) as yes:
|
| 92 |
+
gr.Column(scale=1)
|
| 93 |
+
with gr.Column(scale=3):
|
| 94 |
+
with gr.Box():
|
| 95 |
+
token = gr.Textbox(label="auth")
|
| 96 |
+
t_name = gr.Textbox(label="Space Name")
|
| 97 |
+
with gr.Row():
|
| 98 |
+
with gr.Column():
|
| 99 |
+
s_type = gr.Dropdown(label="Space Type",choices=[t for t in types])
|
| 100 |
+
d_app = gr.Textbox(label="App File", value="app.py")
|
| 101 |
+
d_css = gr.Textbox(label="CSS File", value="css.css")
|
| 102 |
+
with gr.Column():
|
| 103 |
+
m_type = gr.Dropdown(label="Model", choices=[m for m in model_list])
|
| 104 |
+
r_type = gr.Textbox(label="repo/name")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
inbut = gr.Button()
|
| 107 |
+
output_html = gr.HTML("""""")
|
| 108 |
+
files=gr.File()
|
| 109 |
+
gr.Column(scale=1)
|
| 110 |
+
pass_btn.click(checkp,pass_box,[no,yes])
|
| 111 |
+
inbut.click(build_space,[token,t_name,s_type,m_type,r_type,d_app,d_css],output_html)
|
| 112 |
+
build.queue(concurrency_count=10).launch()
|