world432 commited on
Commit
c59ba2b
โ€ข
1 Parent(s): 6d95eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -1
app.py CHANGED
@@ -1,3 +1,160 @@
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- gr.Interface.load("models/xiaolxl/Gf_style2").launch()
 
 
1
+
2
+
3
  import gradio as gr
4
+ import os
5
+ import sys
6
+ from pathlib import Path
7
+ import random
8
+ import string
9
+ import time
10
+ from queue import Queue
11
+ from threading import Thread
12
+ import emoji
13
+
14
+
15
+ text_gen=gr.Interface.load("models/xiaolxl/Gf_style2")
16
+ def get_prompts(prompt_text):
17
+ if prompt_text:
18
+ return text_gen("photo, " + prompt_text)
19
+ else:
20
+ return text_gen("")
21
+ proc1=gr.Interface.load("models/xiaolxl/Gf_style2")
22
+
23
+ def restart_script_periodically():
24
+ while True:
25
+ random_time = random.randint(480, 600)
26
+ time.sleep(random_time)
27
+ os.execl(sys.executable, sys.executable, *sys.argv)
28
+
29
+
30
+ restart_thread = Thread(target=restart_script_periodically, daemon=True)
31
+ restart_thread.start()
32
+
33
+
34
+ queue = Queue()
35
+ queue_threshold = 100
36
+
37
+ def add_random_noise(prompt, noise_level=0.07):
38
+ if noise_level == 0:
39
+ noise_level = 0.07
40
+ percentage_noise = noise_level * 5
41
+ num_noise_chars = int(len(prompt) * (percentage_noise/100))
42
+ noise_indices = random.sample(range(len(prompt)), num_noise_chars)
43
+ prompt_list = list(prompt)
44
+ noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
45
+ noise_chars.extend(['๐Ÿ˜', '๐Ÿ’ฉ', '๐Ÿ˜‚', '๐Ÿค”', '๐Ÿ˜Š', '๐Ÿค—', '๐Ÿ˜ญ', '๐Ÿ™„', '๐Ÿ˜ท', '๐Ÿคฏ', '๐Ÿคซ', '๐Ÿฅด', '๐Ÿ˜ด', '๐Ÿคฉ', '๐Ÿฅณ', '๐Ÿ˜”', '๐Ÿ˜ฉ', '๐Ÿคช', '๐Ÿ˜‡', '๐Ÿคข', '๐Ÿ˜ˆ', '๐Ÿ‘น', '๐Ÿ‘ป', '๐Ÿค–', '๐Ÿ‘ฝ', '๐Ÿ’€', '๐ŸŽƒ', '๐ŸŽ…', '๐ŸŽ„', '๐ŸŽ', '๐ŸŽ‚', '๐ŸŽ‰', '๐ŸŽˆ', '๐ŸŽŠ', '๐ŸŽฎ', 'โค๏ธ', '๐Ÿ’”', '๐Ÿ’•', '๐Ÿ’–', '๐Ÿ’—', '๐Ÿถ', '๐Ÿฑ', '๐Ÿญ', '๐Ÿน', '๐ŸฆŠ', '๐Ÿป', '๐Ÿจ', '๐Ÿฏ', '๐Ÿฆ', '๐Ÿ˜', '๐Ÿ”ฅ', '๐ŸŒง๏ธ', '๐ŸŒž', '๐ŸŒˆ', '๐Ÿ’ฅ', '๐ŸŒด', '๐ŸŒŠ', '๐ŸŒบ', '๐ŸŒป', '๐ŸŒธ', '๐ŸŽจ', '๐ŸŒ…', '๐ŸŒŒ', 'โ˜๏ธ', 'โ›ˆ๏ธ', 'โ„๏ธ', 'โ˜€๏ธ', '๐ŸŒค๏ธ', 'โ›…๏ธ', '๐ŸŒฅ๏ธ', '๐ŸŒฆ๏ธ', '๐ŸŒง๏ธ', '๐ŸŒฉ๏ธ', '๐ŸŒจ๏ธ', '๐ŸŒซ๏ธ', 'โ˜”๏ธ', '๐ŸŒฌ๏ธ', '๐Ÿ’จ', '๐ŸŒช๏ธ', '๐ŸŒˆ'])
46
+ for index in noise_indices:
47
+ prompt_list[index] = random.choice(noise_chars)
48
+ return "".join(prompt_list)
49
+
50
+
51
+ def send_it1(inputs, noise_level, proc1=proc1):
52
+ prompt_with_noise = add_random_noise(inputs, noise_level)
53
+ while queue.qsize() >= queue_threshold:
54
+ time.sleep(2)
55
+ queue.put(prompt_with_noise)
56
+ output1 = proc1(prompt_with_noise)
57
+ return output1
58
+
59
+ def send_it2(inputs, noise_level, proc1=proc1):
60
+ prompt_with_noise = add_random_noise(inputs, noise_level)
61
+ while queue.qsize() >= queue_threshold:
62
+ time.sleep(2)
63
+ queue.put(prompt_with_noise)
64
+ output2 = proc1(prompt_with_noise)
65
+ return output2
66
+
67
+ #def send_it3(inputs, noise_level, proc1=proc1):
68
+ #prompt_with_noise = add_random_noise(inputs, noise_level)
69
+ #while queue.qsize() >= queue_threshold:
70
+ #time.sleep(2)
71
+ #queue.put(prompt_with_noise)
72
+ #output3 = proc1(prompt_with_noise)
73
+ #return output3
74
+
75
+ #def send_it4(inputs, noise_level, proc1=proc1):
76
+ #prompt_with_noise = add_random_noise(inputs, noise_level)
77
+ #while queue.qsize() >= queue_threshold:
78
+ #time.sleep(2)
79
+ #queue.put(prompt_with_noise)
80
+ #output4 = proc1(prompt_with_noise)
81
+ #return output4
82
+
83
+
84
+
85
+ with gr.Blocks(css='style.css') as demo:
86
+ gr.HTML(
87
+ """
88
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
89
+ <div>
90
+ <h1 style="font-weight: 900; font-size: 3rem; margin-bottom:20px;">
91
+ Dreamlike Photoreal 2.0
92
+ </h1>
93
+ </div>
94
+ <p style="margin-bottom: 10px; font-size: 96%">
95
+ 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,
96
+ <a href="https://twitter.com/DavidJohnstonxx/">created by Phenomenon1981</a>.
97
+ </p>
98
+ <p style="margin-bottom: 10px; font-size: 98%">
99
+ โค๏ธ Press the Like Button if you enjoy my space! โค๏ธ</a>
100
+ </p>
101
+ </div>
102
+ """
103
+ )
104
+ with gr.Column(elem_id="col-container"):
105
+ with gr.Row(variant="compact"):
106
+ input_text = gr.Textbox(
107
+ label="Short Prompt",
108
+ show_label=False,
109
+ max_lines=2,
110
+ placeholder="Enter a basic idea and click 'Magic Prompt'. Got no ideas? No problem, Simply just hit the magic button!",
111
+ ).style(
112
+ container=False,
113
+ )
114
+ see_prompts = gr.Button("โœจ Magic Prompt โœจ").style(full_width=False)
115
+
116
+
117
+ with gr.Row(variant="compact"):
118
+ prompt = gr.Textbox(
119
+ label="Enter your prompt",
120
+ show_label=False,
121
+ max_lines=2,
122
+ placeholder="Full Prompt",
123
+ ).style(
124
+ container=False,
125
+ )
126
+ run = gr.Button("Generate Images").style(full_width=False)
127
+
128
+ with gr.Row():
129
+ with gr.Row():
130
+ noise_level = gr.Slider(minimum=0.0, maximum=3, step=0.1, label="Noise Level")
131
+ with gr.Row():
132
+ with gr.Row():
133
+ output1=gr.Image(label="Dreamlike-photoreal-2.0",show_label=False)
134
+ output2=gr.Image(label="Dreamlike-photoreal-2.0",show_label=False)
135
+
136
+ #with gr.Row():
137
+ #output1=gr.Image()
138
+
139
+ see_prompts.click(get_prompts, inputs=[input_text], outputs=[prompt], queue=False)
140
+ run.click(send_it1, inputs=[prompt, noise_level], outputs=[output1])
141
+ run.click(send_it2, inputs=[prompt, noise_level], outputs=[output2])
142
+
143
+
144
+
145
+ with gr.Row():
146
+ gr.HTML(
147
+ """
148
+ <div class="footer">
149
+ <p> Demo for <a href="https://huggingface.co/dreamlike-art/dreamlike-photoreal-2.0">Dreamlike Photoreal 2.0</a> Stable Diffusion model
150
+ </p>
151
+ </div>
152
+ <div class="acknowledgments" style="font-size: 115%">
153
+ <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!
154
+ </p>
155
+ </div>
156
+ """
157
+ )
158
 
159
+ demo.launch(enable_queue=True, inline=True)
160
+ block.queue(concurrency_count=100)