Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.Interface.load(name="spaces/Gustavosta/MagicPrompt-Stable-Diffusion")
|
| 6 |
+
stable_diffusion = gr.Blocks.load(name="spaces/runwayml/stable-diffusion-v1-5")
|
| 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(prompt_text):
|
| 14 |
+
return text_gen(prompt_text)
|
| 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 |
+
Magic Diffusion 🪄
|
| 59 |
+
</h1>
|
| 60 |
+
</div>
|
| 61 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
| 62 |
+
This Space prettifies your prompt using <a href="https://huggingface.co/spaces/Gustavosta/MagicPrompt-Stable-Diffusion" target="_blank">MagicPrompt</a>
|
| 63 |
+
and then runs it through Stable Diffusion to create aesthetically pleasing images. Simply enter a few concepts and let it improve your prompt. You can then diffuse the prompt.
|
| 64 |
+
</p>
|
| 65 |
+
</div>""")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
input_text = gr.Textbox(label="Short text prompt",
|
| 70 |
+
lines=4, elem_id="input-text")
|
| 71 |
+
with gr.Row():
|
| 72 |
+
see_prompts = gr.Button("Feed in your text!")
|
| 73 |
+
|
| 74 |
+
with gr.Column():
|
| 75 |
+
text_output = gr.Textbox(
|
| 76 |
+
label="Prettified text prompt",
|
| 77 |
+
lines=4,
|
| 78 |
+
elem_id="translated"
|
| 79 |
+
)
|
| 80 |
+
with gr.Row():
|
| 81 |
+
diffuse_btn = gr.Button(value="Diffuse the Prompt!")
|
| 82 |
+
with gr.Column(elem_id="generated-gallery"):
|
| 83 |
+
sd_output = gr.Gallery().style(grid=2, height="auto")
|
| 84 |
+
with gr.Group(elem_id="share-btn-container"):
|
| 85 |
+
community_icon = gr.HTML(community_icon_html, visible=False)
|
| 86 |
+
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
| 87 |
+
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
| 88 |
+
|
| 89 |
+
see_prompts.click(get_prompts,
|
| 90 |
+
inputs = [input_text],
|
| 91 |
+
outputs = [
|
| 92 |
+
text_output
|
| 93 |
+
])
|
| 94 |
+
diffuse_btn.click(get_images,
|
| 95 |
+
inputs = [
|
| 96 |
+
text_output
|
| 97 |
+
],
|
| 98 |
+
outputs = [sd_output, community_icon, loading_icon, share_button]
|
| 99 |
+
)
|
| 100 |
+
share_button.click(None, [], [], _js=share_js)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
demo.launch(debug=True)
|