File size: 5,832 Bytes
46df0b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import random
import time
from zipfile import ZipFile
import tempfile
import string
import time



import argparse
import ast
import gradio as gr
from os.path import isdir
#from data_measurements.dataset_statistics import DatasetStatisticsCacheClass as dmt_cls
import utils
from utils import dataset_utils
from utils import gradio_utils as gr_utils
import widgets
import app as ap
from app import load_or_prepare_widgets


logs = utils.prepare_logging(__file__)

# Utility for sidebar description and selection of the dataset
DATASET_NAME_TO_DICT = dataset_utils.get_dataset_info_dicts()

directory = tempfile.mkdtemp(dir="./")

imagens = []

model = gr.Interface.load(
	"models/dreamlike-art/dreamlike-photoreal-2.0",
)

#o = os.getenv("P")
o = "V"

m_out = ("""
<div id="restart">
<h3 id="head">Loading Time Limit Reached.</h3><br>
<h4 id="cont">Please choose a Simpler Prompt, or <b>Upgrade</b> for faster loading.</h4>
</div>
""")
loading=("""
<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>""")

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

def build():
	def zip_files():
		zip_name = f"{b.prompt.split(' ')[0]}_{random.randint(0, 10000)}.zip"
		with ZipFile(zip_name, "w") as zipObj:
			for file in b.imagens:
				zipObj.write(file, os.path.basename(file))
		b.imagens = []
		return zip_name
	def clear():
		return gr.update(value=0),gr.update(value=0)
	def start():
		stamp = time.time()
		return gr.update(value=stamp),gr.update(value=0)
	def end(stamp):
		ts = stamp + 360
		ti = time.time()
		if ti > ts and stamp != 0:
			return gr.update(value=1),gr.HTML.update(f"{m_out}",visible=True)
		else:
			return gr.update(value=0),None
	def im_fn(prompt,noise_level,h=None):
		try:
			if h == o:
				prompt_with_noise = add_random_noise(prompt, noise_level)
				imagem = model(prompt_with_noise)
				b.prompt = prompt
				b.imagens.append(imagem)
				return imagem
			elif h != o:
				return(None,None)
		except Exception as E:
			return None, None 
	def cl_fac():
		return "",gr.HTML.update(f"{loading}")
	with gr.Blocks() as b:
		b.imagens: list = []
		with gr.Row():
			with gr.Column():
				prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt")
				noise_level = gr.Slider(minimum=0.0, maximum=10, step=0.1, label="Noise Level between images.")
			with gr.Column():
				with gr.Row():
					btn1 = gr.Button("Generate")
					btn2 = gr.Button("Clear")
		message=gr.HTML("<div></div>")
		message2=gr.HTML("",visible=False)

		with gr.Row():
			out1 = gr.Image()
			out2 = gr.Image()
		with gr.Row():
			out3 = gr.Image()
			out4 = gr.Image()
		with gr.Row():
			out5 = gr.Image()
			out6 = gr.Image()
		with gr.Row():
			# btn3 = gr.Button("Download")
			caixa = gr.File(file_count="multiple", file_types=["text", ".json", ".csv", "image"])

		with gr.Row(visible=False):
			h_variavel=gr.Textbox(value="V")
			t_state=gr.Number()
			t_switch=gr.Textbox(value=0)
			auto= gr.Image()
		def clear_all():
			return "",None,None,None,None,None,None,None,None,1,gr.HTML.update("<div></div>")
		fac_b = gr.Textbox(value="",visible=False)

		def noth():
			return gr.HTML.update("<div></div>")
		#a1=btn1.click(noth,None,btn1,every=1)
		btn1.click(cl_fac,None,[fac_b,message],show_progress=False)
		b1=btn1.click(start,None,[t_state,t_switch],show_progress=True)
		sta = t_state.change(end,t_state,[t_switch,message2],every=1,show_progress=True)
		b2=btn1.click(im_fn,[prompt,noise_level,h_variavel],[out1,], show_progress=True)
		b3=out1.change(im_fn,[prompt,noise_level,h_variavel],[out2,], show_progress=True)
		b4=out2.change(im_fn,[prompt,noise_level,h_variavel],[out3,], show_progress=True)
		b5=out3.change(im_fn,[prompt,noise_level,h_variavel],[out4,], show_progress=True)
		b6=out4.change(im_fn,[prompt,noise_level,h_variavel],[out5,], show_progress=True)
		b7=out5.change(im_fn,[prompt,noise_level,h_variavel],[out6], show_progress=True)
		b8=out6.change(noth,None,[message], show_progress=False)
		b8=out6.change(zip_files,None,[caixa], show_progress=False)
		swi=t_switch.change(clear,None,[t_switch,fac_b], cancels=[sta,b2,b3,b4,b5,b6,b7],show_progress=False)
		#btn2.click(noth,None,message,cancels=[b1,sta,b2,b3,b4,b5,swi],show_progress=False)
		btn2.click(clear_all, None,[fac_b,prompt,out1,out2,out3,out4,out5,out6,t_state,t_switch,message],cancels=[b1,sta,b2,b3,b4,b5,b6,b7,b8,swi],show_progress=False)
		# btn3.click(zip_files,None,[caixa],show_progress=False)
		# caixa.change(noth,None,[message],show_progress=False)
	b.queue(concurrency_count=100).launch(show_api=False)
build()


## check that it works with a text prompt as variables parsed into build()