bin / app.py
xp3857's picture
Update app.py
3182be6
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()