File size: 10,657 Bytes
cecee98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84052a8
cecee98
ed5371c
f093c7f
 
84052a8
cecee98
 
 
 
 
84052a8
cecee98
 
 
 
 
 
 
 
 
84052a8
cecee98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb265fd
cecee98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185294d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cecee98
 
 
 
 
 
 
 
d2d04fc
cecee98
 
 
d2d04fc
cecee98
cb265fd
cecee98
 
cb265fd
cecee98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb265fd
 
cecee98
 
 
 
 
 
2f2ee34
cecee98
84052a8
 
fcc814c
f3fb233
84052a8
cecee98
 
 
 
 
 
cb265fd
 
 
 
185294d
 
 
 
bb1888e
cecee98
 
 
bb1888e
 
 
 
185294d
 
 
 
cecee98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os, sys, random, string, time, logging
from threading import Thread
from pathlib import Path
from queue import Queue
import gradio as App

logging.basicConfig(level=logging.INFO, format=f'[%(asctime)s] %(message)s', datefmt='%H:%M:%S')

logging.info('Starting Dreamlike Grouped')

logging.info('Loading MagicPrompt')
MagicPrompt=App.Interface.load('spaces/phenomenon1981/MagicPrompt-Stable-Diffusion')
def get_prompts(prompt_text):
    if prompt_text:
        return MagicPrompt('dreamlikeart, ' + prompt_text)
    else:
        return MagicPrompt('')
logging.info('loading Dreamlike Diffusion')
DreamDiffusion=App.Interface.load('models/dreamlike-art/dreamlike-photoreal-2.0') # Credits to Dreamlike
logging.info('Loading Dreamlike PhotoReal')
DreamPhotoReal = DreamDiffusion
# DreamPhotoReal=App.Interface.load('models/dreamlike-art/dreamlike-photoreal-2.0') # Credits to Dreamlike

def RestartScript():
    while True:
        RandomTime = random.randint(540, 600)
        time.sleep(RandomTime)
        logging.info('Restarting')
        os.execl(sys.executable, sys.executable, *sys.argv)

logging.info('Starting Auto-Restarter')
RestartThread = Thread(target=RestartScript, daemon=True)
RestartThread.start()

queue = Queue()
queue_threshold = 100

def AddNoise(Prompt, NoiseLevel=0.00):
    if NoiseLevel == 0:
        NoiseLevel = 0.00
    PercentageNoise = NoiseLevel * 5
    NumberNoiseCharacters = int(len(Prompt) * (PercentageNoise/100))
    NoiseIndices = random.sample(range(len(Prompt)), NumberNoiseCharacters)
    PromptList = list(Prompt)
    NoiseCharacters = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
    NoiseCharacters.extend(['๐Ÿ˜', '๐Ÿ’ฉ', '๐Ÿ˜‚', '๐Ÿค”', '๐Ÿ˜Š', '๐Ÿค—', '๐Ÿ˜ญ', '๐Ÿ™„', '๐Ÿ˜ท', '๐Ÿคฏ', '๐Ÿคซ', '๐Ÿฅด', '๐Ÿ˜ด', '๐Ÿคฉ', '๐Ÿฅณ', '๐Ÿ˜”', '๐Ÿ˜ฉ', '๐Ÿคช', '๐Ÿ˜‡', '๐Ÿคข', '๐Ÿ˜ˆ', '๐Ÿ‘น', '๐Ÿ‘ป', '๐Ÿค–', '๐Ÿ‘ฝ', '๐Ÿ’€', '๐ŸŽƒ', '๐ŸŽ…', '๐ŸŽ„', '๐ŸŽ', '๐ŸŽ‚', '๐ŸŽ‰', '๐ŸŽˆ', '๐ŸŽŠ', '๐ŸŽฎ', 'โค๏ธ', '๐Ÿ’”', '๐Ÿ’•', '๐Ÿ’–', '๐Ÿ’—', '๐Ÿถ', '๐Ÿฑ', '๐Ÿญ', '๐Ÿน', '๐ŸฆŠ', '๐Ÿป', '๐Ÿจ', '๐Ÿฏ', '๐Ÿฆ', '๐Ÿ˜', '๐Ÿ”ฅ', '๐ŸŒง๏ธ', '๐ŸŒž', '๐ŸŒˆ', '๐Ÿ’ฅ', '๐ŸŒด', '๐ŸŒŠ', '๐ŸŒบ', '๐ŸŒป', '๐ŸŒธ', '๐ŸŽจ', '๐ŸŒ…', '๐ŸŒŒ', 'โ˜๏ธ', 'โ›ˆ๏ธ', 'โ„๏ธ', 'โ˜€๏ธ', '๐ŸŒค๏ธ', 'โ›…๏ธ', '๐ŸŒฅ๏ธ', '๐ŸŒฆ๏ธ', '๐ŸŒง๏ธ', '๐ŸŒฉ๏ธ', '๐ŸŒจ๏ธ', '๐ŸŒซ๏ธ', 'โ˜”๏ธ', '๐ŸŒฌ๏ธ', '๐Ÿ’จ', '๐ŸŒช๏ธ', '๐ŸŒˆ'])
    for Index in NoiseIndices:
        PromptList[Index] = random.choice(NoiseCharacters)
    return ''.join(PromptList)

def GetRandomPrompt():
    with open('Prompts.txt', 'r') as Prompts:
        Prompts = Prompts.readlines()
        return random.choice(Prompts)

def SendIt1(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    logging.info('Creating Image On 4 Threads')
    logging.info(f'Using Prompt: {Inputs}')
    logging.info('Creating Image On Thread 1')
    NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    while queue.qsize() >= queue_threshold:
        time.sleep(2)
    queue.put(NoisedPrompt)
    Output1 = DreamDiffusion(NoisedPrompt)
    logging.info('Done Creating Image On Thread 1')
    return Output1

def SendIt2(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    logging.info('Creating Image On Thread 2')
    NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    while queue.qsize() >= queue_threshold:
        time.sleep(2)
    queue.put(NoisedPrompt)
    Output2 = DreamDiffusion(NoisedPrompt)
    logging.info('Done Creating Image On Thread 2')
    return Output2

def SendIt3(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    logging.info('Creating Image On Thread 3')
    NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    while queue.qsize() >= queue_threshold:
        time.sleep(2)
    queue.put(NoisedPrompt)
    Output3 = DreamDiffusion(NoisedPrompt)
    logging.info('Done Creating Image On Thread 3')
    return Output3

def SendIt4(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    logging.info('Creating Image On Thread 4')
    NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    while queue.qsize() >= queue_threshold:
        time.sleep(2)
    queue.put(NoisedPrompt)
    Output4 = DreamDiffusion(NoisedPrompt)
    logging.info('Done Creating Image On Thread 4')
    return Output4

#def SendIt5(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    #logging.info('Creating Image On Thread 5')
    #NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    #while queue.qsize() >= queue_threshold:
        #time.sleep(2)
    #queue.put(NoisedPrompt)
    #Output5 = DreamPhotoReal(NoisedPrompt)
    #logging.info('Done Creating Image On Thread 5')
    #return Output5

#def SendIt6(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    #logging.info('Creating Image On Thread 6')
    #NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    #while queue.qsize() >= queue_threshold:
        #time.sleep(2)
    #queue.put(NoisedPrompt)
    #Output6 = DreamPhotoReal(NoisedPrompt)
    #logging.info('Done Creating Image On Thread 6')
    #return Output6

#def SendIt7(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    #logging.info('Creating Image On Thread 7')
    #NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    #while queue.qsize() >= queue_threshold:
        #time.sleep(2)
    #queue.put(NoisedPrompt)
    #Output7 = DreamPhotoReal(NoisedPrompt)
    #logging.info('Done Creating Image On Thread 7')
    #return Output7

#def SendIt8(Inputs, NoiseLevel, DreamDiffusion=DreamDiffusion):
    #logging.info('Creating Image On Thread 8')
    #NoisedPrompt = AddNoise(Inputs, NoiseLevel)
    #while queue.qsize() >= queue_threshold:
        #time.sleep(2)
    #queue.put(NoisedPrompt)
    #Output8 = DreamPhotoReal(NoisedPrompt)
    #logging.info('Done Creating Image On Thread 8')
    #return Output8

logging.info('Loading Interface')
with App.Blocks(css='style.css') as demo:
    App.HTML(
        '''
            <div style='text-align: center; max-width: 650px; margin: 0 auto;'>
              <div>
                <h1 style='font-weight: 900; font-size: 3rem; margin-bottom:20px;'>
                  Dreamlike PhotoReal 2.0 <sub>(x4)</sub>
                </h1>
              </div>
              <p style='margin-bottom: 10px; font-size: 96%'>
              Dreamlike PhotoReal 2.0 - running on CPUโšก <br>
              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,
                <a href="https://twitter.com/DavidJohnstonxx/">created by Phenomenon1981</a>.
              </p>
              <p style='margin-bottom: 10px; font-size: 98%'>
              โšก Press the Like Button if you enjoy this space! โšก</a>
              </p>
            </div>
        '''
    )
    with App.Column(elem_id='col-container'):
        with App.Row(variant='compact'):
            input_text = App.Textbox(
                label='Short Prompt',
                show_label=False,
                max_lines=4,
                placeholder='Enter a basic idea and click "Magic Prompt". Got no ideas? No problem, Simply just hit the magic button!',
            ).style(
                container=False,
            )
            output_prompt = App.Textbox(
                label='Random Prompt',
                show_label=False,
                max_lines=4,
                placeholder='Click "Random Prompt" to get a random prompt from a list!',
            ).style(
                container=False,
            )
            SeePrompts = App.Button('โœจ Magic Prompt').style(full_width=False)
            RandomPrompt = App.Button('๐Ÿ”„๏ธ Random Prompt').style(full_width=False)

        
        with App.Row(variant='compact'):
            prompt = App.Textbox(
                label='Enter your prompt',
                show_label=False,
                max_lines=4,
                placeholder='Full Prompt',
            ).style(
                container=False,
            )
            Run = App.Button('Generate Images').style(full_width=False)
        
        with App.Row():
            with App.Row():
                NoiseLevel = App.Slider(minimum=0.1, maximum=3, step=0.1, label='Noise Level', value=0.5)

        with App.Row():
            with App.Row():
                Output1=App.Image(label='Dreamlike PhotoReal 2.0',show_label=True)
                Output2=App.Image(label='Dreamlike PhotoReal 2.0',show_label=False)
                Output3=App.Image(label='Dreamlike PhotoReal 2.0',show_label=False)
                Output4=App.Image(label='Dreamlike PhotoReal 2.0',show_label=False)
                #Output5=App.Image(label='Dreamlike PhotoReal 2.0',show_label=True)
                #Output6=App.Image(label='Dreamlike PhotoReal 2.0',show_label=False)
                #Output7=App.Image(label='Dreamlike PhotoReal 2.0',show_label=False)
                #Output8=App.Image(label='Dreamlike PhotoReal 2.0',show_label=False)
        

        SeePrompts.click(get_prompts, inputs=[input_text], outputs=[prompt], queue=False)
        RandomPrompt.click(GetRandomPrompt, outputs=[prompt], queue=False)
        Run.click(SendIt1, inputs=[prompt, NoiseLevel], outputs=[Output1])
        Run.click(SendIt2, inputs=[prompt, NoiseLevel], outputs=[Output2])
        Run.click(SendIt3, inputs=[prompt, NoiseLevel], outputs=[Output3])
        Run.click(SendIt4, inputs=[prompt, NoiseLevel], outputs=[Output4])
        #Run.click(SendIt5, inputs=[prompt, NoiseLevel], outputs=[Output5])
        #Run.click(SendIt6, inputs=[prompt, NoiseLevel], outputs=[Output6])
        #Run.click(SendIt7, inputs=[prompt, NoiseLevel], outputs=[Output7])
        #Run.click(SendIt8, inputs=[prompt, NoiseLevel], outputs=[Output8])


        with App.Row():
                App.HTML(    
    '''
        <div class='footer'>
        <p> Demo for <a href='https://huggingface.co/dreamlike-art/dreamlike-photoreal-2.0'>Dreamlike PhotoReal 2.0</a> Stable Diffusion model
</p>
</div>
        <div class='acknowledgments' style='font-size: 115%'>
            <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!
            </p>
        </div>
    '''
)

    logging.info('Using Demo With 200 Concurrency Count')
    demo.launch(enable_queue=True, inline=True, share=False)
    block.queue(concurrency_count=300)