Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
text_gen = gr.load(name="spaces/Ashrafb/MagicPrompt-Stable-Diffusiongust")
|
| 6 |
-
stable_diffusion = gr.load(name="spaces/runwayml/stable-diffusion-v1-5")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
| 33 |
<div
|
| 34 |
style="
|
|
@@ -48,41 +89,50 @@ with gr.Blocks(css=css) as demo:
|
|
| 48 |
</p>
|
| 49 |
</div>""")
|
| 50 |
|
| 51 |
-
with gr.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with gr.Row():
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
with gr.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
see_prompts.click(get_prompts,
|
| 74 |
-
inputs = [input_text],
|
| 75 |
-
outputs = [
|
| 76 |
-
text_output
|
| 77 |
-
])
|
| 78 |
-
diffuse_btn.click(get_images,
|
| 79 |
-
inputs = [
|
| 80 |
-
text_output
|
| 81 |
-
],
|
| 82 |
-
outputs = [sd_output, loading_icon]
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
+
import random
|
| 5 |
+
import string
|
| 6 |
+
import time
|
| 7 |
+
from queue import Queue
|
| 8 |
+
from threading import Thread
|
| 9 |
+
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
text_gen = gr.load(name="spaces/Ashrafb/MagicPrompt-Stable-Diffusiongust")
|
| 13 |
+
proc1 = gr.Interface.load("models/runwayml/stable-diffusion-v1-5")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def get_prompts(prompt_text):
|
| 16 |
return text_gen(prompt_text)
|
| 17 |
+
|
| 18 |
+
def restart_script_periodically():
|
| 19 |
+
while True:
|
| 20 |
+
random_time = random.randint(540, 600)
|
| 21 |
+
time.sleep(random_time)
|
| 22 |
+
os.execl(sys.executable, sys.executable, *sys.argv)
|
| 23 |
+
|
| 24 |
+
restart_thread = Thread(target=restart_script_periodically, daemon=True)
|
| 25 |
+
restart_thread.start()
|
| 26 |
+
|
| 27 |
+
queue = Queue()
|
| 28 |
+
queue_threshold = 100
|
| 29 |
+
|
| 30 |
+
def add_random_noise(prompt, noise_level=0.00):
|
| 31 |
+
if noise_level == 0:
|
| 32 |
+
noise_level = 0.00
|
| 33 |
+
percentage_noise = noise_level * 5
|
| 34 |
+
num_noise_chars = int(len(prompt) * (percentage_noise / 100))
|
| 35 |
+
noise_indices = random.sample(range(len(prompt)), num_noise_chars)
|
| 36 |
+
prompt_list = list(prompt)
|
| 37 |
+
noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
|
| 38 |
+
noise_chars.extend(['๐', '๐ฉ', '๐', '๐ค', '๐', '๐ค', '๐ญ', '๐', '๐ท', '๐คฏ', '๐คซ', '๐ฅด', '๐ด', '๐คฉ', '๐ฅณ', '๐', '๐ฉ', '๐คช', '๐', '๐คข', '๐', '๐น', '๐ป', '๐ค', '๐ฝ', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฎ', 'โค๏ธ', '๐', '๐', '๐', '๐', '๐ถ', '๐ฑ', '๐ญ', '๐น', '๐ฆ', '๐ป', '๐จ', '๐ฏ', '๐ฆ', '๐', '๐ฅ', '๐ง๏ธ', '๐', '๐', '๐ฅ', '๐ด', '๐', '๐บ', '๐ป', '๐ธ', '๐จ', '๐
', '๐', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', '๐ค๏ธ', 'โ
๏ธ', '๐ฅ๏ธ', '๐ฆ๏ธ', '๐ง๏ธ', '๐ฉ๏ธ', '๐จ๏ธ', '๐ซ๏ธ', 'โ๏ธ', '๐ฌ๏ธ', '๐จ', '๐ช๏ธ', '๐'])
|
| 39 |
+
for index in noise_indices:
|
| 40 |
+
prompt_list[index] = random.choice(noise_chars)
|
| 41 |
+
return "".join(prompt_list)
|
| 42 |
+
|
| 43 |
+
# Existing code...
|
| 44 |
+
|
| 45 |
+
import uuid # Import the UUID library
|
| 46 |
+
|
| 47 |
+
# Existing code...
|
| 48 |
+
|
| 49 |
+
# Existing code...
|
| 50 |
|
| 51 |
+
request_counter = 0 # Global counter to track requests
|
| 52 |
+
|
| 53 |
+
def send_it1(inputs, noise_level, proc=proc1):
|
| 54 |
+
global request_counter
|
| 55 |
+
request_counter += 1
|
| 56 |
+
timestamp = f"{time.time()}_{request_counter}"
|
| 57 |
+
prompt_with_noise = add_random_noise(inputs, noise_level) + f" - {timestamp}"
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
while queue.qsize() >= queue_threshold:
|
| 61 |
+
time.sleep(2)
|
| 62 |
+
queue.put(prompt_with_noise)
|
| 63 |
+
output = proc(prompt_with_noise)
|
| 64 |
+
return output
|
| 65 |
+
except Exception as e:
|
| 66 |
+
# Display a generic error message to the user
|
| 67 |
+
raise gr.Error("Experiencing high demand. Please retry shortly. Thank you for your patience.")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
with gr.Blocks(css="footer{display:none !important;}",) as demo:
|
| 73 |
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
| 74 |
<div
|
| 75 |
style="
|
|
|
|
| 89 |
</p>
|
| 90 |
</div>""")
|
| 91 |
|
| 92 |
+
with gr.Column(elem_id="col-container"):
|
| 93 |
+
with gr.Row(variant="compact"):
|
| 94 |
+
input_text = gr.Textbox(
|
| 95 |
+
lines=4,
|
| 96 |
+
label="Short text prompt",
|
| 97 |
+
|
| 98 |
+
max_lines=8,
|
| 99 |
+
placeholder="",
|
| 100 |
+
).style(
|
| 101 |
+
|
| 102 |
+
textarea={'height': '400px'}
|
| 103 |
+
)
|
| 104 |
+
see_prompts = gr.Button("โจ Feed in your text! โจ")
|
| 105 |
+
|
| 106 |
+
with gr.Row(variant="compact"):
|
| 107 |
+
prompt = gr.Textbox(
|
| 108 |
+
lines=4,
|
| 109 |
+
label="Prettified text prompt",
|
| 110 |
+
|
| 111 |
+
max_lines=10,
|
| 112 |
+
placeholder="Full Prompt",
|
| 113 |
+
).style(
|
| 114 |
+
|
| 115 |
+
textarea={'height': '400px'}
|
| 116 |
+
)
|
| 117 |
+
run = gr.Button("Diffuse the Prompt!")
|
| 118 |
+
|
| 119 |
with gr.Row():
|
| 120 |
+
with gr.Row():
|
| 121 |
+
noise_level = gr.Slider(minimum=0.0, maximum=3, step=0.1, label="Noise Level")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
with gr.Row():
|
| 125 |
+
output1 = gr.Image(label="Dreamlike Diffusion 1.0", show_label=False, show_share_button=False)
|
| 126 |
+
output2 = gr.Image(label="Dreamlike Diffusion 1.0", show_label=False, show_share_button=False)
|
| 127 |
+
output3 = gr.Image(label="Dreamlike Diffusion 1.0", show_label=False, show_share_button=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
+
see_prompts.click(get_prompts, inputs=[input_text], outputs=[prompt], queue=False)
|
| 131 |
+
run.click(send_it1, inputs=[prompt, noise_level], outputs=[output1])
|
| 132 |
+
run.click(send_it1, inputs=[prompt, noise_level], outputs=[output2])
|
| 133 |
+
run.click(send_it1, inputs=[prompt, noise_level], outputs=[output3])
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
demo.launch(enable_queue=True, inline=True)
|
| 138 |
|
|
|