spg commited on
Commit
dc09034
1 Parent(s): b29c3fa
Files changed (1) hide show
  1. app.py +157 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import sys
4
+ from pathlib import Path
5
+ import random
6
+ import string
7
+ import time
8
+ from queue import Queue
9
+ from threading import Thread
10
+ import emoji
11
+
12
+ text_gen=gr.Interface.load("spaces/phenomenon1981/MagicPrompt-Stable-Diffusion")
13
+ def get_prompts(prompt_text):
14
+ if prompt_text:
15
+ return text_gen("photo, " + prompt_text)
16
+ else:
17
+ return text_gen("")
18
+ proc1=gr.Interface.load("models/dreamlike-art/dreamlike-photoreal-2.0")
19
+
20
+ def restart_script_periodically():
21
+ while True:
22
+ random_time = random.randint(540, 600)
23
+ time.sleep(random_time)
24
+ os.execl(sys.executable, sys.executable, *sys.argv)
25
+
26
+
27
+ restart_thread = Thread(target=restart_script_periodically, daemon=True)
28
+ restart_thread.start()
29
+
30
+
31
+ queue = Queue()
32
+ queue_threshold = 100
33
+
34
+ def add_random_noise(prompt, noise_level=0.00):
35
+ if noise_level == 0:
36
+ noise_level = 0.00
37
+ percentage_noise = noise_level * 5
38
+ num_noise_chars = int(len(prompt) * (percentage_noise/100))
39
+ noise_indices = random.sample(range(len(prompt)), num_noise_chars)
40
+ prompt_list = list(prompt)
41
+ noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
42
+ noise_chars.extend(['😍', '💩', '😂', '🤔', '😊', '🤗', '😭', '🙄', '😷', '🤯', '🤫', '🥴', '😴', '🤩', '🥳', '😔', '😩', '🤪', '😇', '🤢', '😈', '👹', '👻', '🤖', '👽', '💀', '🎃', '🎅', '🎄', '🎁', '🎂', '🎉', '🎈', '🎊', '🎮', '❤️', '💔', '💕', '💖', '💗', '🐶', '🐱', '🐭', '🐹', '🦊', '🐻', '🐨', '🐯', '🦁', '🐘', '🔥', '🌧️', '🌞', '🌈', '💥', '🌴', '🌊', '🌺', '🌻', '🌸', '🎨', '🌅', '🌌', '☁️', '⛈️', '❄️', '☀️', '🌤️', '⛅️', '🌥️', '🌦️', '🌧️', '🌩️', '🌨️', '🌫️', '☔️', '🌬️', '💨', '🌪️', '🌈'])
43
+ for index in noise_indices:
44
+ prompt_list[index] = random.choice(noise_chars)
45
+ return "".join(prompt_list)
46
+
47
+
48
+ def send_it1(inputs, noise_level, proc1=proc1):
49
+ prompt_with_noise = add_random_noise(inputs, noise_level)
50
+ while queue.qsize() >= queue_threshold:
51
+ time.sleep(2)
52
+ queue.put(prompt_with_noise)
53
+ output1 = proc1(prompt_with_noise)
54
+ return output1
55
+
56
+ def send_it2(inputs, noise_level, proc1=proc1):
57
+ prompt_with_noise = add_random_noise(inputs, noise_level)
58
+ while queue.qsize() >= queue_threshold:
59
+ time.sleep(2)
60
+ queue.put(prompt_with_noise)
61
+ output2 = proc1(prompt_with_noise)
62
+ return output2
63
+
64
+ #def send_it3(inputs, noise_level, proc1=proc1):
65
+ #prompt_with_noise = add_random_noise(inputs, noise_level)
66
+ #while queue.qsize() >= queue_threshold:
67
+ #time.sleep(2)
68
+ #queue.put(prompt_with_noise)
69
+ #output3 = proc1(prompt_with_noise)
70
+ #return output3
71
+
72
+ #def send_it4(inputs, noise_level, proc1=proc1):
73
+ #prompt_with_noise = add_random_noise(inputs, noise_level)
74
+ #while queue.qsize() >= queue_threshold:
75
+ #time.sleep(2)
76
+ #queue.put(prompt_with_noise)
77
+ #output4 = proc1(prompt_with_noise)
78
+ #return output4
79
+
80
+
81
+
82
+ with gr.Blocks(css='style.css') as demo:
83
+ gr.HTML(
84
+ """
85
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
86
+ <div>
87
+ <h1 style="font-weight: 900; font-size: 3rem; margin-bottom:20px;">
88
+ Dreamlike Photoreal 2.0
89
+ </h1>
90
+ </div>
91
+ <p style="margin-bottom: 10px; font-size: 96%">
92
+ Noise Level: Controls how much randomness is added to the input before it is sent to the model. Higher noise level produces more diverse outputs, while lower noise level produces similar outputs,
93
+ <a href="https://twitter.com/DavidJohnstonxx/">created by Phenomenon1981</a>.
94
+ </p>
95
+ <p style="margin-bottom: 10px; font-size: 98%">
96
+ ❤️ Press the Like Button if you enjoy my space! ❤️</a>
97
+ </p>
98
+ </div>
99
+ """
100
+ )
101
+ with gr.Column(elem_id="col-container"):
102
+ with gr.Row(variant="compact"):
103
+ input_text = gr.Textbox(
104
+ label="Short Prompt",
105
+ show_label=False,
106
+ max_lines=2,
107
+ placeholder="Enter a basic idea and click 'Magic Prompt'. Got no ideas? No problem, Simply just hit the magic button!",
108
+ ).style(
109
+ container=False,
110
+ )
111
+ see_prompts = gr.Button("✨ Magic Prompt ✨").style(full_width=False)
112
+
113
+
114
+ with gr.Row(variant="compact"):
115
+ prompt = gr.Textbox(
116
+ label="Enter your prompt",
117
+ show_label=False,
118
+ max_lines=2,
119
+ placeholder="Full Prompt",
120
+ ).style(
121
+ container=False,
122
+ )
123
+ run = gr.Button("Generate Images").style(full_width=False)
124
+
125
+ with gr.Row():
126
+ with gr.Row():
127
+ noise_level = gr.Slider(minimum=0.0, maximum=3, step=0.1, label="Noise Level")
128
+ with gr.Row():
129
+ with gr.Row():
130
+ output1=gr.Image(label="Dreamlike-photoreal-2.0",show_label=False)
131
+ output2=gr.Image(label="Dreamlike-photoreal-2.0",show_label=False)
132
+
133
+ #with gr.Row():
134
+ #output1=gr.Image()
135
+
136
+ see_prompts.click(get_prompts, inputs=[input_text], outputs=[prompt], queue=False)
137
+ run.click(send_it1, inputs=[prompt, noise_level], outputs=[output1])
138
+ run.click(send_it2, inputs=[prompt, noise_level], outputs=[output2])
139
+
140
+
141
+
142
+ with gr.Row():
143
+ gr.HTML(
144
+ """
145
+ <div class="footer">
146
+ <p> Demo for <a href="https://huggingface.co/dreamlike-art/dreamlike-photoreal-2.0">Dreamlike Photoreal 2.0</a> Stable Diffusion model
147
+ </p>
148
+ </div>
149
+ <div class="acknowledgments" style="font-size: 115%">
150
+ <p> Unleash your creative side and generate mesmerizing images with just a few clicks! Enter a spark of inspiration in the "Basic Idea" text box and click the "Magic Prompt" button to elevate it to a polished masterpiece. Make any final tweaks in the "Full Prompt" box and hit the "Generate Images" button to watch your vision come to life. Experiment with the "Noise Level" for a diverse range of outputs, from similar to wildly unique. Let the fun begin!
151
+ </p>
152
+ </div>
153
+ """
154
+ )
155
+
156
+ demo.launch(enable_queue=True, inline=True)
157
+ block.queue(concurrency_count=100)