Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
4 |
+
|
5 |
+
text_gen = gr.Blocks.load(name="spaces/Gustavosta/MagicPrompt-Stable-Diffusion")
|
6 |
+
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
|
7 |
+
|
8 |
+
def get_images(prompt):
|
9 |
+
gallery_dir = stable_diffusion(prompt, fn_index=2)
|
10 |
+
sd_output = [os.path.join(gallery_dir, image) for image in os.listdir(gallery_dir)]
|
11 |
+
return sd_output, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
12 |
+
|
13 |
+
def get_prompts(uploaded_image):
|
14 |
+
return text_gen(uploaded_image, fn_index=1)[0]
|
15 |
+
|
16 |
+
css = '''
|
17 |
+
.animate-spin {
|
18 |
+
animation: spin 1s linear infinite;
|
19 |
+
}
|
20 |
+
@keyframes spin {
|
21 |
+
from {
|
22 |
+
transform: rotate(0deg);
|
23 |
+
}
|
24 |
+
to {
|
25 |
+
transform: rotate(360deg);
|
26 |
+
}
|
27 |
+
}
|
28 |
+
#share-btn-container {
|
29 |
+
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;
|
30 |
+
}
|
31 |
+
#share-btn {
|
32 |
+
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;
|
33 |
+
}
|
34 |
+
#share-btn * {
|
35 |
+
all: unset;
|
36 |
+
}
|
37 |
+
#share-btn-container div:nth-child(-n+2){
|
38 |
+
width: auto !important;
|
39 |
+
min-height: 0px !important;
|
40 |
+
}
|
41 |
+
#share-btn-container .wrap {
|
42 |
+
display: none !important;
|
43 |
+
}
|
44 |
+
a {text-decoration-line: underline;}
|
45 |
+
'''
|
46 |
+
|
47 |
+
with gr.Blocks(css=css) as demo:
|
48 |
+
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
49 |
+
<div
|
50 |
+
style="
|
51 |
+
display: inline-flex;
|
52 |
+
align-items: center;
|
53 |
+
gap: 0.8rem;
|
54 |
+
font-size: 1.75rem;
|
55 |
+
"
|
56 |
+
>
|
57 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
58 |
+
Stable Diffusion Prism 🎆🌌
|
59 |
+
</h1>
|
60 |
+
</div>
|
61 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
62 |
+
Sends an image in to <a href="https://huggingface.co/spaces/pharma/CLIP-Interrogator" target="_blank">CLIP Interrogator</a>
|
63 |
+
to generate a text prompt which is then run through
|
64 |
+
<a href="https://huggingface.co/spaces/stabilityai/stable-diffusion" target="_blank">Stable Diffusion</a>
|
65 |
+
to generate new forms of the original!
|
66 |
+
</p>
|
67 |
+
</div>""")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column():
|
71 |
+
input_text = gr.TextBox(elem_id="input-text")
|
72 |
+
with gr.Row():
|
73 |
+
see_prompts = gr.Button("Feed in your text!")
|
74 |
+
|
75 |
+
with gr.Column():
|
76 |
+
text_output = gr.Textbox(
|
77 |
+
label="Generated text prompt",
|
78 |
+
lines=4,
|
79 |
+
elem_id="translated"
|
80 |
+
)
|
81 |
+
with gr.Row():
|
82 |
+
diffuse_btn = gr.Button(value="Diffuse it!")
|
83 |
+
with gr.Column(elem_id="generated-gallery"):
|
84 |
+
sd_output = gr.Gallery().style(grid=2, height="auto")
|
85 |
+
with gr.Group(elem_id="share-btn-container"):
|
86 |
+
community_icon = gr.HTML(community_icon_html, visible=False)
|
87 |
+
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
88 |
+
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
89 |
+
|
90 |
+
see_prompts.click(get_prompts,
|
91 |
+
inputs = input_text,
|
92 |
+
outputs = [
|
93 |
+
text_output
|
94 |
+
])
|
95 |
+
diffuse_btn.click(get_images,
|
96 |
+
inputs = [
|
97 |
+
text_output
|
98 |
+
],
|
99 |
+
outputs = [sd_output, community_icon, loading_icon, share_button]
|
100 |
+
)
|
101 |
+
share_button.click(None, [], [], _js=share_js)
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
demo.launch()
|