Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import requests
|
4 |
import random
|
5 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
models=[
|
10 |
-
gr.Interface.load(f"models/{name2}"),
|
11 |
-
]
|
12 |
#o = os.getenv("P")
|
13 |
o = "V"
|
14 |
|
@@ -20,7 +27,28 @@ m_out = ("""
|
|
20 |
""")
|
21 |
loading=("""
|
22 |
<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def build():
|
|
|
|
|
|
|
|
|
|
|
24 |
def clear():
|
25 |
return gr.update(value=0),gr.update(value=0)
|
26 |
def start():
|
@@ -33,15 +61,14 @@ def build():
|
|
33 |
return gr.update(value=1),gr.HTML.update(f"{m_out}",visible=True)
|
34 |
else:
|
35 |
return gr.update(value=0),None
|
36 |
-
def im_fn(
|
37 |
try:
|
38 |
if h == o:
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
return model(put),fac
|
45 |
elif h != o:
|
46 |
return(None,None)
|
47 |
except Exception as E:
|
@@ -49,18 +76,20 @@ def build():
|
|
49 |
def cl_fac():
|
50 |
return "",gr.HTML.update(f"{loading}")
|
51 |
with gr.Blocks() as b:
|
|
|
52 |
with gr.Row():
|
53 |
with gr.Column():
|
54 |
-
|
|
|
55 |
with gr.Column():
|
56 |
with gr.Row():
|
57 |
-
btn1 = gr.Button("
|
58 |
btn2 = gr.Button("Clear")
|
59 |
message=gr.HTML("<div></div>")
|
60 |
message2=gr.HTML("",visible=False)
|
61 |
|
62 |
with gr.Row():
|
63 |
-
out1 = gr.Image()
|
64 |
out2 = gr.Image()
|
65 |
with gr.Row():
|
66 |
out3 = gr.Image()
|
@@ -68,13 +97,16 @@ def build():
|
|
68 |
with gr.Row():
|
69 |
out5 = gr.Image()
|
70 |
out6 = gr.Image()
|
|
|
|
|
|
|
71 |
|
72 |
with gr.Row(visible=False):
|
73 |
h_variavel=gr.Textbox(value="V")
|
74 |
t_state=gr.Number()
|
75 |
t_switch=gr.Textbox(value=0)
|
76 |
def clear_all():
|
77 |
-
return "",None,None,None,None,None,None,1,gr.HTML.update("<div></div>")
|
78 |
fac_b = gr.Textbox(value="",visible=False)
|
79 |
|
80 |
def noth():
|
@@ -83,15 +115,17 @@ def build():
|
|
83 |
btn1.click(cl_fac,None,[fac_b,message],show_progress=False)
|
84 |
b1=btn1.click(start,None,[t_state,t_switch],show_progress=True)
|
85 |
sta = t_state.change(end,t_state,[t_switch,message2],every=1,show_progress=True)
|
86 |
-
b2=btn1.click(im_fn,[
|
87 |
-
b3=out1.change(im_fn,[
|
88 |
-
b4=out2.change(im_fn,[
|
89 |
-
b5=out3.change(im_fn,[
|
90 |
-
b6=out4.change(im_fn,[
|
91 |
-
b7=out5.change(im_fn,[
|
92 |
-
b8=out6.change(noth,None,message, show_progress=False)
|
93 |
swi=t_switch.change(clear,None,[t_switch,fac_b], cancels=[sta,b2,b3,b4,b5,b6,b7],show_progress=False)
|
94 |
#btn2.click(noth,None,message,cancels=[b1,sta,b2,b3,b4,b5,swi],show_progress=False)
|
95 |
-
btn2.click(clear_all, None,[fac_b,
|
|
|
|
|
96 |
b.queue(concurrency_count=100).launch(show_api=False)
|
97 |
build()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
3 |
import random
|
4 |
import time
|
5 |
+
from zipfile import ZipFile
|
6 |
+
import tempfile
|
7 |
+
import string
|
8 |
+
import time
|
9 |
+
|
10 |
+
directory = tempfile.mkdtemp(dir="./")
|
11 |
+
|
12 |
+
imagens = []
|
13 |
|
14 |
+
model = gr.Interface.load(
|
15 |
+
"models/dreamlike-art/dreamlike-photoreal-2.0",
|
16 |
+
api_key="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",
|
17 |
+
)
|
18 |
|
|
|
|
|
|
|
19 |
#o = os.getenv("P")
|
20 |
o = "V"
|
21 |
|
|
|
27 |
""")
|
28 |
loading=("""
|
29 |
<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>""")
|
30 |
+
|
31 |
+
def add_random_noise(prompt, noise_level=1.00):
|
32 |
+
if noise_level == 0:
|
33 |
+
noise_level = 0.00
|
34 |
+
if noise_level == None:
|
35 |
+
noise_level = 1.00
|
36 |
+
percentage_noise = noise_level * 5
|
37 |
+
num_noise_chars = int(len(prompt) * (percentage_noise/100))
|
38 |
+
noise_indices = random.sample(range(len(prompt)), num_noise_chars)
|
39 |
+
prompt_list = list(prompt)
|
40 |
+
noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
|
41 |
+
noise_chars.extend(['๐', '๐ฉ', '๐', '๐ค', '๐', '๐ค', '๐ญ', '๐', '๐ท', '๐คฏ', '๐คซ', '๐ฅด', '๐ด', '๐คฉ', '๐ฅณ', '๐', '๐ฉ', '๐คช', '๐', '๐คข', '๐', '๐น', '๐ป', '๐ค', '๐ฝ', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฎ', 'โค๏ธ', '๐', '๐', '๐', '๐', '๐ถ', '๐ฑ', '๐ญ', '๐น', '๐ฆ', '๐ป', '๐จ', '๐ฏ', '๐ฆ', '๐', '๐ฅ', '๐ง๏ธ', '๐', '๐', '๐ฅ', '๐ด', '๐', '๐บ', '๐ป', '๐ธ', '๐จ', '๐
', '๐', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', '๐ค๏ธ', 'โ
๏ธ', '๐ฅ๏ธ', '๐ฆ๏ธ', '๐ง๏ธ', '๐ฉ๏ธ', '๐จ๏ธ', '๐ซ๏ธ', 'โ๏ธ', '๐ฌ๏ธ', '๐จ', '๐ช๏ธ', '๐'])
|
42 |
+
for index in noise_indices:
|
43 |
+
prompt_list[index] = random.choice(noise_chars)
|
44 |
+
return "".join(prompt_list)
|
45 |
+
|
46 |
def build():
|
47 |
+
def zip_files():
|
48 |
+
with ZipFile(f"{b.prompt.split(' ')[0]}.zip", "w") as zipObj:
|
49 |
+
for file in b.imagens:
|
50 |
+
zipObj.write(file, os.path.basename(file))
|
51 |
+
return f"{b.prompt.split(' ')[0]}.zip"
|
52 |
def clear():
|
53 |
return gr.update(value=0),gr.update(value=0)
|
54 |
def start():
|
|
|
61 |
return gr.update(value=1),gr.HTML.update(f"{m_out}",visible=True)
|
62 |
else:
|
63 |
return gr.update(value=0),None
|
64 |
+
def im_fn(prompt,noise_level,h=None):
|
65 |
try:
|
66 |
if h == o:
|
67 |
+
prompt_with_noise = add_random_noise(prompt, noise_level)
|
68 |
+
imagem = model(prompt_with_noise)
|
69 |
+
b.prompt = prompt
|
70 |
+
b.imagens.append(imagem)
|
71 |
+
return imagem
|
|
|
72 |
elif h != o:
|
73 |
return(None,None)
|
74 |
except Exception as E:
|
|
|
76 |
def cl_fac():
|
77 |
return "",gr.HTML.update(f"{loading}")
|
78 |
with gr.Blocks() as b:
|
79 |
+
b.imagens: list = []
|
80 |
with gr.Row():
|
81 |
with gr.Column():
|
82 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt")
|
83 |
+
noise_level = gr.Slider(minimum=0.0, maximum=10, step=0.1, label="Noise Level between images.")
|
84 |
with gr.Column():
|
85 |
with gr.Row():
|
86 |
+
btn1 = gr.Button("Generate")
|
87 |
btn2 = gr.Button("Clear")
|
88 |
message=gr.HTML("<div></div>")
|
89 |
message2=gr.HTML("",visible=False)
|
90 |
|
91 |
with gr.Row():
|
92 |
+
out1 = gr.Image(allow_download=True)
|
93 |
out2 = gr.Image()
|
94 |
with gr.Row():
|
95 |
out3 = gr.Image()
|
|
|
97 |
with gr.Row():
|
98 |
out5 = gr.Image()
|
99 |
out6 = gr.Image()
|
100 |
+
with gr.Row():
|
101 |
+
btn3 = gr.Button("Download")
|
102 |
+
caixa = gr.File(file_count="multiple", file_types=["text", ".json", ".csv", "image"])
|
103 |
|
104 |
with gr.Row(visible=False):
|
105 |
h_variavel=gr.Textbox(value="V")
|
106 |
t_state=gr.Number()
|
107 |
t_switch=gr.Textbox(value=0)
|
108 |
def clear_all():
|
109 |
+
return "",None,None,None,None,None,None,None,None,1,gr.HTML.update("<div></div>")
|
110 |
fac_b = gr.Textbox(value="",visible=False)
|
111 |
|
112 |
def noth():
|
|
|
115 |
btn1.click(cl_fac,None,[fac_b,message],show_progress=False)
|
116 |
b1=btn1.click(start,None,[t_state,t_switch],show_progress=True)
|
117 |
sta = t_state.change(end,t_state,[t_switch,message2],every=1,show_progress=True)
|
118 |
+
b2=btn1.click(im_fn,[prompt,noise_level,h_variavel],[out1,], show_progress=True)
|
119 |
+
b3=out1.change(im_fn,[prompt,noise_level,h_variavel],[out2,], show_progress=True)
|
120 |
+
b4=out2.change(im_fn,[prompt,noise_level,h_variavel],[out3,], show_progress=True)
|
121 |
+
b5=out3.change(im_fn,[prompt,noise_level,h_variavel],[out4,], show_progress=True)
|
122 |
+
b6=out4.change(im_fn,[prompt,noise_level,h_variavel],[out5,], show_progress=True)
|
123 |
+
b7=out5.change(im_fn,[prompt,noise_level,h_variavel],[out6,], show_progress=True)
|
124 |
+
b8=out6.change(noth,None,[message], show_progress=False)
|
125 |
swi=t_switch.change(clear,None,[t_switch,fac_b], cancels=[sta,b2,b3,b4,b5,b6,b7],show_progress=False)
|
126 |
#btn2.click(noth,None,message,cancels=[b1,sta,b2,b3,b4,b5,swi],show_progress=False)
|
127 |
+
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)
|
128 |
+
btn3.click(zip_files,None,[caixa],show_progress=False)
|
129 |
+
# caixa.change(noth,None,[message],show_progress=False)
|
130 |
b.queue(concurrency_count=100).launch(show_api=False)
|
131 |
build()
|