Spaces:
Runtime error
Runtime error
File size: 20,830 Bytes
7f8ba04 089e7b8 7f8ba04 5998b3b 7f8ba04 80725eb 525ddf9 80725eb 7f8ba04 11bb3b2 e990d68 41dd361 11bb3b2 7f8ba04 019a533 a173f2a a7b60fe e5862c2 a7b60fe e5862c2 a7b60fe bfa0baf a173f2a a7b60fe a173f2a 5f83b79 bfa0baf 05dd262 a173f2a a7b60fe e5862c2 a7b60fe e5862c2 a7b60fe bfa0baf a173f2a 70824c8 854c909 a173f2a 854c909 a173f2a 04f8eee 854c909 089e7b8 7036943 a173f2a 7036943 a173f2a 7f8ba04 019a533 6a8ef96 019a533 6a8ef96 019a533 6a8ef96 e312479 21bcef9 e312479 019a533 7f8ba04 102233c 7f8ba04 fe65b41 7f8ba04 df3482a 7f8ba04 fe65b41 7f8ba04 df3482a 7f8ba04 fd4405f 7f8ba04 ae677e3 7f8ba04 ae677e3 102233c a173f2a 7f8ba04 80725eb 8f85ab1 e309b10 80725eb 8f85ab1 80725eb eb89384 e4bf02f cd5c639 e6c61c7 cd5c639 080d48d cd5c639 80725eb 7f8ba04 25deeb9 7f8ba04 5998b3b 7f8ba04 5998b3b 7f8ba04 6a05f4d 4d5a6d7 c60963d f34c7d6 ada9a4e 80725eb 2bbc358 5ba1aa1 a173f2a 80725eb a173f2a 80725eb a173f2a 7f8ba04 4d5a6d7 ada9a4e 80725eb 7f8ba04 019a533 e312479 a173f2a 7f8ba04 5595206 |
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
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"
t_space3 = f"/back_masto.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_space3)
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")
####### masto
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_masto.html", "w") as f:
f.write(app)
files.append("back_masto.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<br>Be sure URL is in lowercase""")
except Exception as e:
return gr.HTML(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(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(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 (f'''<center>Your Interactive Twitter Card Embed Link is:<br><a href="{t_link}" target="_blank">{t_link}</a><br>
Your Interactive Mastodon Card Embed Link is:<br><a href="{t_link3}" target="_blank">{t_link3}</a><br>
Your Interactive Gab Card Embed Link is:<br><a href="{t_link2}" target="_blank">{t_link2}</a><br>
</center><br>''')
except Exception as e:
print (e)
return gr.HTML(f"""{str(e)}""")
def d_im():
output="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
return output
def up_c(im,c_slider):
img1 = Image.open(im)
img1.thumbnail((500,500), Image.Resampling.LANCZOS)
img1.save(f'tmpim22-{uid}.png')
out = slide_fn(c_slider)
return out
#text_gen=gr.load("spaces/Omnibus/MagicPrompt-Stable-Diffusion_link")
#im_gen=gr.Interface.load("spaces/Omnibus/Diffusionator-mod")
def get_prompts(prompt_text):
gen=f'{prompt_text} (feature unavailable)'
#gen=text_gen(prompt_text)
return gen
def gen_im(prompt,model_choice=5,count=1):
if count >4:
count =4
t=0
output_list=[]
while t<int(count):
proc1=models2[int(model_choice)]
output1=proc1(prompt)
output_list.append(output1)
prompt=f"{prompt} "
t+=1
return(output_list)
with gr.Blocks() as build_app:
gr.Markdown("""<center><h1>Interactive Twitter Card Builder</h1></center>""")
with gr.Row():
gr.Column(scale=1)
with gr.Column(scale=3):
with gr.Accordion("Details",open=False):
with gr.Tab("Description"):
gr.Markdown("""<h7>This space will build an Interactive Twitter Card<br>
where Twitter (and other) users can run your Gradio demo<br>
right on their Social Media feed.<br><br>
*Only works through browser, not Twitter App<br><br>
Enter the required information and paste the link that is returned<br>
as the only link, along with your message and tags, in your Tweet.<br>
When posted, the Interactive Twitter Card should have a Play button<br>
which will display the Space you provided.<br><br>
Twitter will remember this card associated with the link you post,<br>
so any changes will often require a differently named link.<br><br>
You can test your link first by omitting your Write token<br>
which will assign the default REPO: "portal"<br>(Link: portal-NAME.hf.space)<br><br>
This Space is a Demo, and is a Work in Progress<br>
No Warranties, Guarantees, or Permissions implied
""")
with gr.Tab("Credits"):
gr.Markdown("""<h7><a href='https://huggingface.co/spaces/anzorq/sd-space-creator'>anzorq/sd-space-creator</a><br>""")
with gr.Group():
option_token = gr.Textbox(label="HF Write Token to write to your REPO", placeholder="Optional")
t_name = gr.Textbox(label="NAME (Link will be: repo-NAME.hf.space)", placeholder="Optional")
t_space = gr.Textbox(label="Direct URL to your Space (https://you-repo-you-space.hf.space)")
t_title = gr.Textbox(label="Title for Twitter Card")
t_description = gr.Textbox(label="Short Description for Twitter Card", placeholder="Optional")
t_redirect = gr.Textbox(label="Redirect URL for Twitter Card Click", placeholder="Optional")
gr.Markdown("""Image for your Card (min: 300x300 [Square]""")
t_image_url = gr.Textbox(label="Image URL", placeholder="Optional URL Input")
t_image = gr.Image(label="Twitter Card Image", type="filepath",interactive=True)
with gr.Row():
def_im = gr.Button("Load Default Image")
#gen_im = gr.Button("Generate Image")
with gr.Accordion("Generated Image Controls", open=False):
prompt_input=gr.Textbox(label = "Seed Prompt")
with gr.Row():
model_name1 = gr.Dropdown(show_label=False, choices=[m for m in models], type="index", value=current_model, interactive=True)
count = gr.Number(value=1)
with gr.Row():
magic_btn=gr.Button("Magic Prompt")
gen_btn=gr.Button("Generate Images")
gen_im_out=gr.Gallery(columns=2)
c_image=gr.Image(label="Cropped Image", type="filepath",interactive=False)
c_slider=gr.Slider(0,220,value=0,step=1,label="Adjust Vertical Crop")
c_upbtn=gr.Button("Update Crop")
with gr.Tab("Build"):
build_button = gr.Button("Build")
output_html = gr.HTML("""""")
with gr.Tab("Download"):
dl_button = gr.Button("Download")
files_out = gr.Files()
gr.Column(scale=1)
magic_btn.click(get_prompts,prompt_input,prompt_input)
gen_btn.click(gen_im,[prompt_input,model_name1,count],gen_im_out)
t_image.change(up_c,[t_image,c_slider],c_image)
c_upbtn.click(slide_fn,c_slider,c_image)
build_button.click(build_space,[t_space,t_title,t_description,t_name,t_redirect,t_image,t_image_url,option_token],output_html)
dl_button.click(make_files,[t_space,t_title,t_description,t_name,t_redirect,t_image,t_image_url,option_token],files_out)
def_im.click(d_im,None,t_image)
build_app.launch(max_threads=20) |