Spaces:
Runtime error
Runtime error
File size: 3,342 Bytes
e33485b e032f0c e00cbb6 e032f0c f25f3c0 e032f0c 3a4f4b2 e032f0c 3604e33 e032f0c e00cbb6 88f6b3a ebee332 e00cbb6 84838d7 c3179cf 84838d7 282f153 84838d7 ee6544b 84838d7 e032f0c 84838d7 ee6544b 8fe4b23 e00cbb6 3a4f4b2 e00cbb6 f25f3c0 84838d7 3a4f4b2 e032f0c 3bb7ca9 e032f0c 84838d7 |
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 |
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/pharma/CLIP-Interrogator")
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
def get_images(prompt):
gallery_dir = stable_diffusion(prompt, 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, fn_index=1)[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;
}
'''
with gr.Blocks(css=css) as demo:
gr.Markdown(
"""
## Stable Diffusion Perception ππ
Want to figure out what a good prompt might be to create new images like an existing one?
Use [CLIP Interrogator](https://huggingface.co/spaces/pharma/CLIP-Interrogator) to generate a prompt.
Afterward, you can use Stable Diffusion to generate images based on the prompt.
"""
)
with gr.Row():
with gr.Column():
input_img = gr.Image(type="filepath", elem_id="input-img")
with gr.Row():
see_prompts = gr.Button("Check how your image prompts your model!")
with gr.Column():
img2text_output = gr.Textbox(
label="Convert your image to text!",
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()
|