import os
import sys
import cv2
import uuid
from PIL import Image
import gradio as gr
from pathlib import Path
from zipfile import ZipFile
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
uid = uuid.uuid4()
token_self = os.environ['HF_TOKEN']
models =[
"",
"CompVis/stable-diffusion-v1-4",
"runwayml/stable-diffusion-v1-5",
"prompthero/openjourney",
"stabilityai/stable-diffusion-2-1",
"stabilityai/stable-diffusion-2-1-base",
"SG161222/Realistic_Vision_V1.4",
"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",
]
current_model=models[5]
models2=[
gr.load(f"models/{models[1]}"),
gr.load(f"models/{models[2]}"),
gr.load(f"models/{models[3]}"),
gr.load(f"models/{models[4]}"),
gr.load(f"models/{models[5]}"),
gr.load(f"models/{models[6]}"),
gr.load(f"models/{models[7]}"),
gr.load(f"models/{models[8]}"),
gr.load(f"models/{models[9]}"),
gr.load(f"models/{models[10]}"),
gr.load(f"models/{models[11]}"),
gr.load(f"models/{models[12]}"),
gr.load(f"models/{models[13]}"),
gr.load(f"models/{models[14]}"),
gr.load(f"models/{models[15]}"),
gr.load(f"models/{models[16]}"),
gr.load(f"models/{models[17]}"),
gr.load(f"models/{models[18]}"),
gr.load(f"models/{models[19]}"),
gr.load(f"models/{models[20]}"),
]
def slide_fn(val):
im =(f'tmpim22-{uid}.png')
v=int(val)
img1 = Image.open(im)
#img1.thumbnail((500,500), Image.Resampling.LANCZOS)
#ww=500
#hh=int(500*0.5625)
img2 = img1.crop((0,0+v,500,280+v))
img2.save(f'tmpim2-{uid}.png')
return img2
def make_files(t_space,t_title,t_description=None,t_name=None,t_redirect=None,t_image=None,t_image_url=None,token=None):
if t_description == None:
t_description = f'Embedded App'
if t_redirect == None:
t_redirect = t_space
if t_name == None:
t_name = t_space.split("//",1)[1].split(".",1)[0]
#https://omnibus-social-card-maker-dl.hf.space
print (t_name)
model_id=t_name
files=[]
if t_image_url != None and t_image_url != "" and t_image==None:
t_image=t_image_url
img1 = Image.open(t_image)
img1.thumbnail((500,500), Image.Resampling.LANCZOS)
img1.save(f'tmpim-{uid}.png')
output_pro=cv2.imread(f'tmpim-{uid}.png')
#output_pro2=cv2.imread(f'tmpim2-{uid}.png')
t_image2=output_pro.crop((0,140,500,140))
cv2.imwrite("card_im.png",output_pro)
cv2.imwrite("card_im_crop.png",t_image2)
files.append("card_im.png")
files.append("card_im_crop.png")
#t_image = "card_im.png"
t_image2 = "card_im_crop.png"
print(f"Using image URL {t_image}")
pass
elif t_image != None:
img1 = Image.open(t_image)
img1.thumbnail((500,500), Image.Resampling.LANCZOS)
img1.save(f'tmpim-{uid}.png')
output_pro=cv2.imread(f'tmpim-{uid}.png')
output_pro2=cv2.imread(f'tmpim2-{uid}.png')
cv2.imwrite("card_im.png",output_pro)
cv2.imwrite("card_im_crop.png",output_pro2)
files.append("card_im.png")
files.append("card_im_crop.png")
t_image = "card_im.png"
t_image2 = "card_im_crop.png"
pass
else:
print("Default Image")
t_image="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
img1 = Image.open(t_image)
img1.thumbnail((500,500), Image.Resampling.LANCZOS)
img1.save(f'tmpim-{uid}.png')
output_pro=cv2.imread(f'tmpim-{uid}.png')
#output_pro2=cv2.imread(f'tmpim2-{uid}.png')
t_image2=output_pro.crop((0,140,500,140))
cv2.imwrite("card_im.png",output_pro)
cv2.imwrite("card_im_crop.png",t_image2)
files.append("card_im.png")
files.append("card_im_crop.png")
t_image = "card_im.png"
t_image2 = "card_im_crop.png"
pass
try:
api_url = f'https://huggingface.co/api/spaces/{model_id}'
t_iframe = t_space
t_link3= f"/masto.html"
t_link2= f"/gab.html"
t_link = f"/ai.html"
t_space = f"/back.html"
t_space2 = f"/back_gab.html"
########## norm
with open("template/ai.html", "r") as f:
app = f.read()
app = app.replace("$space", t_space)
app = app.replace("$title", t_title)
app = app.replace("$description", t_description)
app = app.replace("$image", t_image)
app = app.replace("$redirect", t_redirect)
with open("ai.html", "w") as f:
f.write(app)
files.append("ai.html")
######## gab
with open("template/ai.html", "r") as f:
app = f.read()
app = app.replace("$space", t_space2)
app = app.replace("$title", t_title)
app = app.replace("$description", t_description)
app = app.replace("$image", t_image2)
app = app.replace("$redirect", t_redirect)
with open("gab.html", "w") as f:
f.write(app)
files.append("gab.html")
######## masto
with open("template/ai.html", "r") as f:
app = f.read()
app = app.replace("$space", t_space)
app = app.replace("$title", t_title)
app = app.replace("$description", t_description)
app = app.replace("$image", t_image2)
app = app.replace("$redirect", t_redirect)
with open("masto.html", "w") as f:
f.write(app)
files.append("masto.html")
##################
with open("template/front.html", "r") as f:
app = f.read()
app = app.replace("$iframe", t_iframe)
with open("index.html", "w") as f:
f.write(app)
files.append("index.html")
######### norm
with open("template/back.html", "r") as f:
app = f.read()
app = app.replace("$iframe", t_iframe)
app = app.replace("$pad", "100%")
with open("back.html", "w") as f:
f.write(app)
files.append("back.html")
####### gab
with open("template/back.html", "r") as f:
app = f.read()
app = app.replace("$iframe", t_iframe)
app = app.replace("$pad", "56%")
with open("back_gab.html", "w") as f:
f.write(app)
files.append("back_gab.html")
if t_name == None or t_name =="":
t_name = t_iframe.split("www.", 1)[1].replace(".","_").replace("/", "-")
with ZipFile(f"{t_name}.zip", "w") as zipObj:
for idx, file in enumerate(files):
zipObj.write(f'{file}')
files.append(f'{t_name}.zip')
return (files)
except Exception as e:
print (e)
return None
def build_space(t_space,t_title,t_description=None,t_name=None,t_redirect=None,t_image=None,t_image_url=None,token=None):
if t_description == None or t_description == "":
t_description = f'Embedded App'
if t_redirect == None or t_redirect =="":
t_redirect = t_space
if t_name == None or t_name =="":
print (t_space)
t_name = t_space.split("//",1)[1]
print (t_name)
t_name = t_name.strip(".hf.space")
print (f't_name::{t_name}')
model_id=t_name
#model_id="omnibus/fff"
if token==None or token=="":
token = token_self
else:
token = token
pass
api = HfApi(token=token)
repo_name = get_full_repo_name(model_id=model_id, token=token)
try:
print(t_space.rsplit('.hf', 1)[-1])
if t_space.rsplit('.hf', 1)[-1] == ".space":
repo_url = api.create_repo(
repo_id=model_id,
repo_type="space",
space_sdk="static",
private=False,
)
print(f"""Space Built at {repo_name}""")
else:
model_id=None
repo_name=None
t_space=None
t_image=None
t_image_url=None
api=None
pass
return gr.HTML.update(f"""Invalid Direct URL: must be a Huggingface Space
Be sure URL is in lowercase""")
except Exception as e:
return gr.HTML.update(f"""{str(e)}""")
if t_image_url != None and t_image_url != "" and t_image==None:
t_image=t_image_url
print(f"Using image URL {t_image}")
pass
elif t_image != None:
img1 = Image.open(t_image)
img1.thumbnail((500,500), Image.Resampling.LANCZOS)
img1.save(f'tmpim-{uid}.png')
output_pro=cv2.imread(f'tmpim-{uid}.png')
output_pro2=cv2.imread(f'tmpim2-{uid}.png')
cv2.imwrite(f"./img-{uid}.png",output_pro)
cv2.imwrite(f"./img2-{uid}.png",output_pro2)
try:
api.upload_file(
path_or_fileobj=f"./img-{uid}.png",
path_in_repo="card_im.png",
repo_id=repo_name,
token=token,
repo_type="space",
)
t_image = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/card_im.png"
os.remove(f"./img-{uid}.png")
print(f"Image Uploaded to: {t_image}")
except Exception as e:
return gr.HTML.update(f"""{str(e)}""")
try:
api.upload_file(
path_or_fileobj=f"./img2-{uid}.png",
path_in_repo="card_im_crop.png",
repo_id=repo_name,
token=token,
repo_type="space",
)
t_image2 = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/card_im_crop.png"
os.remove(f"./img2-{uid}.png")
print(f"Image Uploaded to: {t_image2}")
except Exception as e:
return gr.HTML.update(f"""{str(e)}""")
pass
else:
print("Default Image")
t_image="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
pass
try:
api_url = f'https://huggingface.co/api/spaces/{model_id}'
t_iframe = t_space
t_link3= f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/masto.html"
t_link2= f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/gab.html"
t_link = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/ai.html"
t_space = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/back.html"
t_space2 = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/back_gab.html"
########## norm
with open("template/ai.html", "r") as f:
app = f.read()
app = app.replace("$space", t_space)
app = app.replace("$title", t_title)
app = app.replace("$description", t_description)
app = app.replace("$image", t_image)
app = app.replace("$redirect", t_redirect)
with open("ai.html", "w") as f:
f.write(app)
api.upload_file(
path_or_fileobj="ai.html",
path_in_repo="ai.html",
repo_id=repo_name,
token=token,
repo_type="space",
)
os.remove("ai.html")
######## gab
with open("template/ai.html", "r") as f:
app = f.read()
app = app.replace("$space", t_space2)
app = app.replace("$title", t_title)
app = app.replace("$description", t_description)
app = app.replace("$image", t_image2)
app = app.replace("$redirect", t_redirect)
with open("gab.html", "w") as f:
f.write(app)
api.upload_file(
path_or_fileobj="gab.html",
path_in_repo="gab.html",
repo_id=repo_name,
token=token,
repo_type="space",
)
os.remove("gab.html")
######## masto
with open("template/ai.html", "r") as f:
app = f.read()
app = app.replace("$space", t_space)
app = app.replace("$title", t_title)
app = app.replace("$description", t_description)
app = app.replace("$image", t_image2)
app = app.replace("$redirect", t_redirect)
with open("masto.html", "w") as f:
f.write(app)
api.upload_file(
path_or_fileobj="masto.html",
path_in_repo="masto.html",
repo_id=repo_name,
token=token,
repo_type="space",
)
os.remove("masto.html")
with open("template/front.html", "r") as f:
app = f.read()
app = app.replace("$iframe", t_iframe)
with open("front.html", "w") as f:
f.write(app)
api.upload_file(
path_or_fileobj="front.html",
path_in_repo="index.html",
repo_id=repo_name,
token=token,
repo_type="space",
)
os.remove("front.html")
######### norm
with open("template/back.html", "r") as f:
app = f.read()
app = app.replace("$iframe", t_iframe)
app = app.replace("$pad", "100%")
# 3. save the new app.py file
with open("back.html", "w") as f:
f.write(app)
api.upload_file(
path_or_fileobj="back.html",
path_in_repo="back.html",
repo_id=repo_name,
token=token,
repo_type="space",
)
os.remove("back.html")
####### gab
with open("template/back.html", "r") as f:
app = f.read()
app = app.replace("$iframe", t_iframe)
app = app.replace("$pad", "56%")
# 3. save the new app.py file
with open("back_gab.html", "w") as f:
f.write(app)
api.upload_file(
path_or_fileobj="back_gab.html",
path_in_repo="back_gab.html",
repo_id=repo_name,
token=token,
repo_type="space",
)
os.remove("back_gab.html")
return gr.HTML.update(f'''