Spaces:
Running
Running
import gradio as gr | |
import os | |
from share_btn import community_icon_html, loading_icon_html, share_js | |
img_to_text = gr.Blocks.load(name="spaces/pharmapsychotic/CLIP-Interrogator") | |
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion") | |
def get_images(prompt): | |
gallery_dir = stable_diffusion(prompt, "", 7.5, fn_index=2) | |
sd_output = [os.path.join(gallery_dir, image) for image in os.listdir(gallery_dir)] | |
return sd_output, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) | |
def get_prompts(uploaded_image): | |
return img_to_text(uploaded_image, "ViT-L (best for Stable Diffusion 1.*)", "best", fn_index=2)[0] | |
css = ''' | |
.animate-spin { | |
animation: spin 1s linear infinite; | |
} | |
@keyframes spin { | |
from { | |
transform: rotate(0deg); | |
} | |
to { | |
transform: rotate(360deg); | |
} | |
} | |
#share-btn-container { | |
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem; | |
} | |
#share-btn { | |
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important; | |
} | |
#share-btn * { | |
all: unset; | |
} | |
#share-btn-container div:nth-child(-n+2){ | |
width: auto !important; | |
min-height: 0px !important; | |
} | |
#share-btn-container .wrap { | |
display: none !important; | |
} | |
a {text-decoration-line: underline;} | |
''' | |
with gr.Blocks(css=css) as demo: | |
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;"> | |
<div | |
style=" | |
display: inline-flex; | |
align-items: center; | |
gap: 0.8rem; | |
font-size: 1.75rem; | |
" | |
> | |
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;"> | |
Stable Diffusion Prism ππ | |
</h1> | |
</div> | |
<p style="margin-bottom: 10px; font-size: 94%"> | |
Sends an image in to <a href="https://huggingface.co/spaces/pharma/CLIP-Interrogator" target="_blank">CLIP Interrogator</a> | |
to generate a text prompt which is then run through | |
<a href="https://huggingface.co/spaces/stabilityai/stable-diffusion" target="_blank">Stable Diffusion</a> | |
to generate new forms of the original! | |
</p> | |
</div>""") | |
with gr.Row(): | |
with gr.Column(): | |
input_img = gr.Image(type="filepath", elem_id="input-img") | |
with gr.Row(): | |
see_prompts = gr.Button("Feed in your image!") | |
with gr.Column(): | |
img2text_output = gr.Textbox( | |
label="Generated text prompt", | |
lines=4, | |
elem_id="translated" | |
) | |
with gr.Row(): | |
diffuse_btn = gr.Button(value="Diffuse it!") | |
with gr.Column(elem_id="generated-gallery"): | |
sd_output = gr.Gallery().style(grid=2, height="auto") | |
with gr.Group(elem_id="share-btn-container"): | |
community_icon = gr.HTML(community_icon_html, visible=False) | |
loading_icon = gr.HTML(loading_icon_html, visible=False) | |
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False) | |
see_prompts.click(get_prompts, | |
inputs = input_img, | |
outputs = [ | |
img2text_output | |
]) | |
diffuse_btn.click(get_images, | |
inputs = [ | |
img2text_output | |
], | |
outputs = [sd_output, community_icon, loading_icon, share_button] | |
) | |
share_button.click(None, [], [], _js=share_js) | |
demo.launch() | |