Spaces:
Runtime error
Runtime error
Dagfinn1962
commited on
Commit
•
8346a93
1
Parent(s):
70a731b
Update app.py
Browse files
app.py
CHANGED
@@ -1,336 +1,327 @@
|
|
1 |
-
import numpy as np
|
2 |
import gradio as gr
|
3 |
-
import ast
|
4 |
import requests
|
5 |
-
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
from
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
{"name": "❤ STABLE DIFFUSION MODELS ==========", "url": "stabilityai/stable-diffusion-2-1"},
|
22 |
-
{"name": "SD ComVis 1.2","url": "CompVis/stable-diffusion-v1-2"},
|
23 |
-
{"name": "SD Comvis 1.4","url": "CompVis/stable-diffusion-v1-4"},
|
24 |
-
{"name": "SD runawayml 1.5","url": "runwayml/stable-diffusion-v1-5"},
|
25 |
-
{"name": "SD stable-diffusion xl base 1.0","url": "timothymhowe/stable-diffusion-xl-base-1.0"},
|
26 |
-
{"name": "SD NSFW","url": "digiplay/CamelliaMix_NSFW_diffusers_v1.1"},
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
{"name": "stability AI", "url": "stabilityai/stable-diffusion-2-1-base"},
|
44 |
-
{"name": "Compressed-S-D", "url": "nota-ai/bk-sdm-small"},
|
45 |
-
{"name": "Future Diffusion", "url": "nitrosocke/Future-Diffusion"},
|
46 |
-
{"name": "JWST Deep Space Diffusion", "url": "dallinmackay/JWST-Deep-Space-diffusion"},
|
47 |
-
{"name": "Robo Diffusion 3 Base", "url": "nousr/robo-diffusion-2-base"},
|
48 |
-
{"name": "Robo Diffusion", "url": "nousr/robo-diffusion"},
|
49 |
-
{"name": "Tron Legacy Diffusion", "url": "dallinmackay/Tron-Legacy-diffusion"},
|
50 |
-
{"name": "❤ 3D ART MODELS ==========", "url": "DucHaiten/DucHaitenAIart"},
|
51 |
-
{"name": "DucHaiten Art", "url": "DucHaiten/DucHaitenAIart"},
|
52 |
-
{"name": "DucHaiten ClassicAnime", "url": "DucHaiten/DH_ClassicAnime"},
|
53 |
-
{"name": "DucHaiten DreamWorld", "url": "DucHaiten/DucHaitenDreamWorld"},
|
54 |
-
{"name": "DucHaiten Journey", "url": "DucHaiten/DucHaitenJourney"},
|
55 |
-
{"name": "DucHaiten StyleLikeMe", "url": "DucHaiten/DucHaiten-StyleLikeMe"},
|
56 |
-
{"name": "DucHaiten SuperCute", "url": "DucHaiten/DucHaitenSuperCute"},
|
57 |
-
{"name": "Redshift Diffusion 768", "url": "nitrosocke/redshift-diffusion-768"},
|
58 |
-
{"name": "Redshift Diffusion", "url": "nitrosocke/redshift-diffusion"},
|
59 |
-
]
|
60 |
-
|
61 |
-
|
62 |
-
#### REM-BG
|
63 |
-
|
64 |
-
remove_bg_models = {
|
65 |
-
"TracerUniversalB7": "TracerUniversalB7",
|
66 |
-
"U2NET": "u2net",
|
67 |
-
"U2NET Human Seg": "u2net_human_seg",
|
68 |
-
"U2NET Cloth Seg": "u2net_cloth_seg"
|
69 |
-
}
|
70 |
-
|
71 |
-
model_choices = keys(remove_bg_models)
|
72 |
-
|
73 |
-
|
74 |
-
def predict(image, session, smoot, matting, bg_color):
|
75 |
-
|
76 |
-
session = new_session(remove_bg_models[session])
|
77 |
-
|
78 |
-
try:
|
79 |
-
return remove(session, image, smoot, matting, bg_color)
|
80 |
-
except ValueError as err:
|
81 |
-
logging.error(err)
|
82 |
-
return make_label(str(err)), None
|
83 |
-
|
84 |
-
|
85 |
-
def change_show_mask(chk_state):
|
86 |
-
return gr.Image.update(visible=chk_state)
|
87 |
-
|
88 |
-
|
89 |
-
def change_include_matting(chk_state):
|
90 |
-
return gr.Box.update(visible=chk_state), (0, 0, 0), 0, 0, 0
|
91 |
-
|
92 |
-
|
93 |
-
def change_foreground_threshold(fg_value, value):
|
94 |
-
fg, bg, erode = value
|
95 |
-
return fg_value, bg, erode
|
96 |
-
|
97 |
-
|
98 |
-
def change_background_threshold(bg_value, value):
|
99 |
-
fg, bg, erode = value
|
100 |
-
return fg, bg_value, erode
|
101 |
-
|
102 |
-
|
103 |
-
def change_erode_size(erode_value, value):
|
104 |
-
fg, bg, erode = value
|
105 |
-
return fg, bg, erode_value
|
106 |
-
|
107 |
-
|
108 |
-
def set_dominant_color(chk_state):
|
109 |
-
return chk_state, gr.ColorPicker.update(value=False, visible=not chk_state)
|
110 |
-
|
111 |
-
|
112 |
-
def change_picker_color(picker, dominant):
|
113 |
-
if not dominant:
|
114 |
-
return picker
|
115 |
-
return dominant
|
116 |
-
|
117 |
-
|
118 |
-
def change_background_mode(chk_state):
|
119 |
-
return gr.ColorPicker.update(value=False, visible=chk_state), \
|
120 |
-
gr.Checkbox.update(value=False, visible=chk_state)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
###########
|
125 |
-
|
126 |
-
text_gen = gr.Interface.load("spaces/Avenuenw/prompt-extend")
|
127 |
-
|
128 |
-
current_model = models[0]
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
model_url = f"models/{model['url']}"
|
133 |
-
loaded_model = gr.Interface.load(model_url, live=True, preprocess=True)
|
134 |
-
models2.append(loaded_model)
|
135 |
|
136 |
-
|
137 |
-
|
|
|
138 |
|
139 |
-
|
140 |
-
return x[::-1]
|
141 |
|
142 |
-
def
|
143 |
-
|
144 |
-
|
145 |
|
|
|
|
|
|
|
146 |
|
147 |
-
def
|
148 |
-
|
|
|
|
|
|
|
|
|
149 |
|
|
|
|
|
150 |
|
151 |
-
|
152 |
-
global current_model
|
153 |
-
current_model = models[current_model_index]
|
154 |
-
return gr.update(value=f"{current_model['name']}")
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
def get_initial_images():
|
159 |
-
response = requests.post(API_URL_INITIAL, json={
|
160 |
-
"data": []
|
161 |
-
}).json()
|
162 |
-
#data = response["data"][0]['data'][0][0][:-1]
|
163 |
-
response_dict = response['data'][0]
|
164 |
-
return response_dict #, [resp[0][:-1] for resp in response["data"][0]["data"]]
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
return [resp[0][:-1] for resp in response_dict["data"]]
|
169 |
|
170 |
-
|
171 |
-
initial = process_response(response_dict)
|
172 |
-
initial_imgs = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);">\n' + "\n".join(initial[:-1])
|
173 |
|
174 |
-
#Third: Load more images for the grid
|
175 |
-
def get_next10_images(response_dict, row_count):
|
176 |
-
row_count = int(row_count)
|
177 |
-
#print("(1)",type(response_dict))
|
178 |
-
#Convert the string to a dictionary
|
179 |
-
if isinstance(response_dict, dict) == False :
|
180 |
-
response_dict = ast.literal_eval(response_dict)
|
181 |
-
response = requests.post(API_URL_NEXT10, json={
|
182 |
-
"data": [response_dict, row_count ] #len(initial)-1
|
183 |
-
}).json()
|
184 |
-
row_count+=10
|
185 |
-
response_dict = response['data'][0]
|
186 |
-
#print("(2)",type(response))
|
187 |
-
#print("(3)",type(response['data'][0]))
|
188 |
-
next_set = [resp[0][:-1] for resp in response_dict["data"]]
|
189 |
-
next_set_images = '<div style="display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); grid-gap: 0; background-color: #fff; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2); ">\n' + "\n".join(next_set[:-1])
|
190 |
-
return response_dict, row_count, next_set_images #response['data'][0]
|
191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
-
with gr.Tab("TEXT TO IMAGE"):
|
197 |
-
|
198 |
-
##model = ("stabilityai/stable-diffusion-2-1")
|
199 |
-
model_name1 = gr.Dropdown(
|
200 |
-
label="Choose Model",
|
201 |
-
choices=[m["name"] for m in models],
|
202 |
-
type="index",
|
203 |
-
value=current_model["name"],
|
204 |
-
interactive=True,
|
205 |
-
)
|
206 |
-
input_text = gr.Textbox(label="Prompt idea",)
|
207 |
-
|
208 |
-
## run = gr.Button("Generate Images")
|
209 |
-
with gr.Row():
|
210 |
-
see_prompts = gr.Button("Generate Prompts")
|
211 |
-
run = gr.Button("Generate Images", variant="primary")
|
212 |
-
|
213 |
-
with gr.Row():
|
214 |
-
magic1 = gr.Textbox(label="Generated Prompt", lines=2)
|
215 |
-
output1 = gr.Image(label="")
|
216 |
-
|
217 |
-
|
218 |
-
with gr.Row():
|
219 |
-
magic2 = gr.Textbox(label="Generated Prompt", lines=2)
|
220 |
-
output2 = gr.Image(label="")
|
221 |
-
|
222 |
-
|
223 |
-
run.click(send_it, inputs=[magic1, model_name1], outputs=[output1])
|
224 |
-
run.click(send_it, inputs=[magic2, model_name1], outputs=[output2])
|
225 |
-
see_prompts.click(text_it, inputs=[input_text], outputs=[magic1])
|
226 |
-
see_prompts.click(text_it, inputs=[input_text], outputs=[magic2])
|
227 |
-
|
228 |
-
model_name1.change(set_model, inputs=model_name1, outputs=[output1, output2,])
|
229 |
-
|
230 |
-
with gr.Tab("AI Library"):
|
231 |
-
#Using Gradio Demos as API - This is Hot!
|
232 |
-
#get_next10_images(response_dict=response_dict, row_count=9)
|
233 |
-
#position: fixed; top: 0; left: 0; width: 100%; padding: 20px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
|
234 |
-
|
235 |
-
#Defining the Blocks layout
|
236 |
-
# with gr.Blocks(css = """#img_search img {width: 100%; height: 100%; object-fit: cover;}""") as demo:
|
237 |
-
gr.HTML(value="top of page", elem_id="top",visible=False)
|
238 |
-
gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
239 |
-
<div
|
240 |
-
style="
|
241 |
-
display: inline-flex;
|
242 |
-
align-items: center;
|
243 |
-
gap: 0.8rem;
|
244 |
-
font-size: 1.75rem;
|
245 |
-
"
|
246 |
-
>
|
247 |
-
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
248 |
-
Using Gradio API - 2 </h1><br></div>
|
249 |
-
<div><h4 style="font-weight: 500; margin-bottom: 7px; margin-top: 5px;">
|
250 |
-
Stream < href="https://huggingface.co/collections/Dagfinn1962/images-64fc02ca304b8cb412ccda28" target="_blank">Collection Images</a> ina beautiful grid</h4><br>
|
251 |
-
</div>""")
|
252 |
-
with gr.Tab("Gallery"):
|
253 |
-
#with gr.Tab(): #(elem_id = "col-container"):
|
254 |
-
#gr.Column(): #(elem_id = "col-container"):
|
255 |
-
b1 = gr.Button("Load More Images").style(full_width=False)
|
256 |
-
df = gr.Textbox(visible=False,elem_id='dataframe', value=response_dict)
|
257 |
-
row_count = gr.Number(visible=False, value=19 )
|
258 |
-
img_search = gr.HTML(label = 'Images from PlaygroundAI dataset', elem_id="img_search",
|
259 |
-
value=initial_imgs ) #initial[:-1] )
|
260 |
-
|
261 |
-
|
262 |
-
b1.click(get_next10_images, [df, row_count], [df, row_count, img_search], api_name = "load_playgroundai_images" )
|
263 |
-
|
264 |
-
########################## REM-BG
|
265 |
-
with gr.Tab("Remove Background"):
|
266 |
-
|
267 |
-
color_state = gr.State(value=False)
|
268 |
-
matting_state = gr.State(value=(0, 0, 0))
|
269 |
-
gr.HTML("<center><h1>Remove Background Tool</h1></center>")
|
270 |
-
|
271 |
-
with gr.Row(equal_height=False):
|
272 |
-
with gr.Column():
|
273 |
-
input_img = gr.Image(type="pil", label="Input image")
|
274 |
-
drp_models = gr.Dropdown(choices=model_choices, label="Model Segment", value="TracerUniversalB7")
|
275 |
-
|
276 |
with gr.Row():
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
mask_img = gr.Image(type="pil", label="Image Mask", visible=False)
|
298 |
-
gr.ClearButton(components=[input_img, output_img, mask_img])
|
299 |
-
|
300 |
-
chk_include_matting.change(change_include_matting, inputs=[chk_include_matting],
|
301 |
-
outputs=[slider_matting, matting_state,
|
302 |
-
slr_fg_threshold, slr_bg_threshold, slr_erode_size])
|
303 |
-
|
304 |
-
slr_bg_threshold.change(change_background_threshold, inputs=[slr_bg_threshold, matting_state],
|
305 |
-
outputs=[matting_state])
|
306 |
-
|
307 |
-
slr_fg_threshold.change(change_foreground_threshold, inputs=[slr_fg_threshold, matting_state],
|
308 |
-
outputs=[matting_state])
|
309 |
-
|
310 |
-
slr_erode_size.change(change_erode_size, inputs=[slr_erode_size, matting_state],
|
311 |
-
outputs=[matting_state])
|
312 |
-
|
313 |
-
chk_show_mask.change(change_show_mask, inputs=[chk_show_mask], outputs=[mask_img])
|
314 |
-
|
315 |
-
chk_change_color.change(change_background_mode, inputs=[chk_change_color],
|
316 |
-
outputs=[pkr_color, chk_dominant])
|
317 |
-
|
318 |
-
pkr_color.change(change_picker_color, inputs=[pkr_color, chk_dominant], outputs=[color_state])
|
319 |
-
|
320 |
-
chk_dominant.change(set_dominant_color, inputs=[chk_dominant], outputs=[color_state, pkr_color])
|
321 |
-
|
322 |
-
run_btn.click(predict, inputs=[input_img, drp_models, chk_smoot_mask, matting_state, color_state],
|
323 |
-
outputs=[output_img, mask_img])
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
# text_input = gr.Textbox() ## Diffuser
|
328 |
-
# image_output = gr.Image()
|
329 |
-
# image_button = gr.Button("Flip")
|
330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
|
333 |
-
|
334 |
-
# image_button.click(flip_image, inputs=image_input, outputs=image_output)
|
335 |
-
pan.queue(concurrency_count=200)
|
336 |
-
pan.launch(inline=True, show_api=True, max_threads=400 )
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import requests
|
3 |
+
import time
|
4 |
+
import json
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
from io import BytesIO
|
8 |
+
import html
|
9 |
+
import re
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
class Prodia:
|
14 |
+
def __init__(self, api_key, base=None):
|
15 |
+
self.base = base or "https://api.prodia.com/v1"
|
16 |
+
self.headers = {
|
17 |
+
"X-Prodia-Key": api_key
|
18 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def generate(self, params):
|
21 |
+
response = self._post(f"{self.base}/sd/generate", params)
|
22 |
+
return response.json()
|
23 |
+
|
24 |
+
def transform(self, params):
|
25 |
+
response = self._post(f"{self.base}/sd/transform", params)
|
26 |
+
return response.json()
|
27 |
+
|
28 |
+
def controlnet(self, params):
|
29 |
+
response = self._post(f"{self.base}/sd/controlnet", params)
|
30 |
+
return response.json()
|
31 |
+
|
32 |
+
def get_job(self, job_id):
|
33 |
+
response = self._get(f"{self.base}/job/{job_id}")
|
34 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
def wait(self, job):
|
37 |
+
job_result = job
|
|
|
|
|
|
|
38 |
|
39 |
+
while job_result['status'] not in ['succeeded', 'failed']:
|
40 |
+
time.sleep(0.25)
|
41 |
+
job_result = self.get_job(job['job'])
|
42 |
|
43 |
+
return job_result
|
|
|
44 |
|
45 |
+
def list_models(self):
|
46 |
+
response = self._get(f"{self.base}/sd/models")
|
47 |
+
return response.json()
|
48 |
|
49 |
+
def list_samplers(self):
|
50 |
+
response = self._get(f"{self.base}/sd/samplers")
|
51 |
+
return response.json()
|
52 |
|
53 |
+
def _post(self, url, params):
|
54 |
+
headers = {
|
55 |
+
**self.headers,
|
56 |
+
"Content-Type": "application/json"
|
57 |
+
}
|
58 |
+
response = requests.post(url, headers=headers, data=json.dumps(params))
|
59 |
|
60 |
+
if response.status_code != 200:
|
61 |
+
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
62 |
|
63 |
+
return response
|
|
|
|
|
|
|
64 |
|
65 |
+
def _get(self, url):
|
66 |
+
response = requests.get(url, headers=self.headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
if response.status_code != 200:
|
69 |
+
raise Exception(f"Bad Prodia Response: {response.status_code}")
|
|
|
70 |
|
71 |
+
return response
|
|
|
|
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
def image_to_base64(image):
|
75 |
+
# Convert the image to bytes
|
76 |
+
buffered = BytesIO()
|
77 |
+
image.save(buffered, format="PNG") # You can change format to PNG if needed
|
78 |
+
|
79 |
+
# Encode the bytes to base64
|
80 |
+
img_str = base64.b64encode(buffered.getvalue())
|
81 |
+
|
82 |
+
return img_str.decode('utf-8') # Convert bytes to string
|
83 |
+
|
84 |
+
|
85 |
+
def remove_id_and_ext(text):
|
86 |
+
text = re.sub(r'\[.*\]$', '', text)
|
87 |
+
extension = text[-12:].strip()
|
88 |
+
if extension == "safetensors":
|
89 |
+
text = text[:-13]
|
90 |
+
elif extension == "ckpt":
|
91 |
+
text = text[:-4]
|
92 |
+
return text
|
93 |
+
|
94 |
+
|
95 |
+
def get_data(text):
|
96 |
+
results = {}
|
97 |
+
patterns = {
|
98 |
+
'prompt': r'(.*)',
|
99 |
+
'negative_prompt': r'Negative prompt: (.*)',
|
100 |
+
'steps': r'Steps: (\d+),',
|
101 |
+
'seed': r'Seed: (\d+),',
|
102 |
+
'sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)',
|
103 |
+
'model': r'Model:\s*([^\s,]+)',
|
104 |
+
'cfg_scale': r'CFG scale:\s*([\d\.]+)',
|
105 |
+
'size': r'Size:\s*([0-9]+x[0-9]+)'
|
106 |
+
}
|
107 |
+
for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']:
|
108 |
+
match = re.search(patterns[key], text)
|
109 |
+
if match:
|
110 |
+
results[key] = match.group(1)
|
111 |
+
else:
|
112 |
+
results[key] = None
|
113 |
+
if results['size'] is not None:
|
114 |
+
w, h = results['size'].split("x")
|
115 |
+
results['w'] = w
|
116 |
+
results['h'] = h
|
117 |
+
else:
|
118 |
+
results['w'] = None
|
119 |
+
results['h'] = None
|
120 |
+
return results
|
121 |
+
|
122 |
+
|
123 |
+
def send_to_txt2img(image):
|
124 |
+
|
125 |
+
result = {tabs: gr.update(selected="t2i")}
|
126 |
|
127 |
+
try:
|
128 |
+
text = image.info['parameters']
|
129 |
+
data = get_data(text)
|
130 |
+
result[prompt] = gr.update(value=data['prompt'])
|
131 |
+
result[negative_prompt] = gr.update(value=data['negative_prompt']) if data['negative_prompt'] is not None else gr.update()
|
132 |
+
result[steps] = gr.update(value=int(data['steps'])) if data['steps'] is not None else gr.update()
|
133 |
+
result[seed] = gr.update(value=int(data['seed'])) if data['seed'] is not None else gr.update()
|
134 |
+
result[cfg_scale] = gr.update(value=float(data['cfg_scale'])) if data['cfg_scale'] is not None else gr.update()
|
135 |
+
result[width] = gr.update(value=int(data['w'])) if data['w'] is not None else gr.update()
|
136 |
+
result[height] = gr.update(value=int(data['h'])) if data['h'] is not None else gr.update()
|
137 |
+
result[sampler] = gr.update(value=data['sampler']) if data['sampler'] is not None else gr.update()
|
138 |
+
if model in model_names:
|
139 |
+
result[model] = gr.update(value=model_names[model])
|
140 |
+
else:
|
141 |
+
result[model] = gr.update()
|
142 |
+
return result
|
143 |
+
|
144 |
+
except Exception as e:
|
145 |
+
print(e)
|
146 |
+
|
147 |
+
return result
|
148 |
+
|
149 |
+
|
150 |
+
prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
|
151 |
+
model_list = prodia_client.list_models()
|
152 |
+
model_names = {}
|
153 |
+
|
154 |
+
for model_name in model_list:
|
155 |
+
name_without_ext = remove_id_and_ext(model_name)
|
156 |
+
model_names[name_without_ext] = model_name
|
157 |
+
|
158 |
+
|
159 |
+
def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
|
160 |
+
result = prodia_client.generate({
|
161 |
+
"prompt": prompt,
|
162 |
+
"negative_prompt": negative_prompt,
|
163 |
+
"model": model,
|
164 |
+
"steps": steps,
|
165 |
+
"sampler": sampler,
|
166 |
+
"cfg_scale": cfg_scale,
|
167 |
+
"width": width,
|
168 |
+
"height": height,
|
169 |
+
"seed": seed
|
170 |
+
})
|
171 |
+
|
172 |
+
job = prodia_client.wait(result)
|
173 |
+
|
174 |
+
return job["imageUrl"]
|
175 |
+
|
176 |
+
|
177 |
+
def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
|
178 |
+
result = prodia_client.transform({
|
179 |
+
"imageData": image_to_base64(input_image),
|
180 |
+
"denoising_strength": denoising,
|
181 |
+
"prompt": prompt,
|
182 |
+
"negative_prompt": negative_prompt,
|
183 |
+
"model": model,
|
184 |
+
"steps": steps,
|
185 |
+
"sampler": sampler,
|
186 |
+
"cfg_scale": cfg_scale,
|
187 |
+
"width": width,
|
188 |
+
"height": height,
|
189 |
+
"seed": seed
|
190 |
+
})
|
191 |
+
|
192 |
+
job = prodia_client.wait(result)
|
193 |
+
|
194 |
+
return job["imageUrl"]
|
195 |
+
|
196 |
+
|
197 |
+
css = """
|
198 |
+
#generate {
|
199 |
+
height: 100%;
|
200 |
+
}
|
201 |
+
"""
|
202 |
+
|
203 |
+
with gr.Blocks(css=css) as demo:
|
204 |
+
with gr.Row():
|
205 |
+
with gr.Column(scale=6):
|
206 |
+
model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
|
207 |
+
|
208 |
+
with gr.Column(scale=1):
|
209 |
+
gr.Markdown(elem_id="powered-by-prodia", value="AUTOMATIC1111 Stable Diffusion Web UI.<br>Powered by [Prodia](https://prodia.com).<br>For more features and faster generation times check out our [API Docs](https://docs.prodia.com/reference/getting-started-guide).")
|
210 |
+
|
211 |
+
with gr.Tabs() as tabs:
|
212 |
+
with gr.Tab("txt2img", id='t2i'):
|
213 |
+
with gr.Row():
|
214 |
+
with gr.Column(scale=6, min_width=600):
|
215 |
+
prompt = gr.Textbox("1girl, Emma Watson, queen, pony tail hair,((full body photo)), photo realistic, high quality, 8k, white tanktop, white shorts,", placeholder="Prompt", show_label=False, lines=3)
|
216 |
+
negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="double body, double face, double features, incorrect posture, close up, two heads, two faces, plastic, Deformed, blurry, bad anatomy, bad eyes, crossed eyes, disfigured, poorly drawn face,")
|
217 |
+
with gr.Column():
|
218 |
+
text_button = gr.Button("Generate", variant='primary', elem_id="generate")
|
219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
with gr.Row():
|
221 |
+
with gr.Column(scale=3):
|
222 |
+
with gr.Tab("Generation"):
|
223 |
+
with gr.Row():
|
224 |
+
with gr.Column(scale=1):
|
225 |
+
sampler = gr.Dropdown(value="DPM++ 2M Karras", show_label=True, label="Sampling Method", choices=prodia_client.list_samplers())
|
226 |
+
|
227 |
+
with gr.Column(scale=1):
|
228 |
+
steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=25, value=20, step=1)
|
229 |
+
|
230 |
+
with gr.Row():
|
231 |
+
with gr.Column(scale=1):
|
232 |
+
width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
|
233 |
+
height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
|
234 |
+
|
235 |
+
with gr.Column(scale=1):
|
236 |
+
batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
|
237 |
+
batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
|
238 |
+
|
239 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
|
240 |
+
seed = gr.Number(label="Seed", value=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
+
with gr.Column(scale=2):
|
243 |
+
image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
|
244 |
+
|
245 |
+
text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height,
|
246 |
+
seed], outputs=image_output, concurrency_limit=64)
|
247 |
+
|
248 |
+
with gr.Tab("img2img", id='i2i'):
|
249 |
+
with gr.Row():
|
250 |
+
with gr.Column(scale=6, min_width=600):
|
251 |
+
i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
|
252 |
+
i2i_negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
|
253 |
+
with gr.Column():
|
254 |
+
i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
|
255 |
+
|
256 |
+
with gr.Row():
|
257 |
+
with gr.Column(scale=3):
|
258 |
+
with gr.Tab("Generation"):
|
259 |
+
i2i_image_input = gr.Image(type="pil")
|
260 |
+
|
261 |
+
with gr.Row():
|
262 |
+
with gr.Column(scale=1):
|
263 |
+
i2i_sampler = gr.Dropdown(value="Euler a", show_label=True, label="Sampling Method", choices=prodia_client.list_samplers())
|
264 |
+
|
265 |
+
with gr.Column(scale=1):
|
266 |
+
i2i_steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=25, value=20, step=1)
|
267 |
+
|
268 |
+
with gr.Row():
|
269 |
+
with gr.Column(scale=1):
|
270 |
+
i2i_width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
|
271 |
+
i2i_height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
|
272 |
+
|
273 |
+
with gr.Column(scale=1):
|
274 |
+
i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
|
275 |
+
i2i_batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
|
276 |
+
|
277 |
+
i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
|
278 |
+
i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)
|
279 |
+
i2i_seed = gr.Number(label="Seed", value=-1)
|
280 |
|
281 |
+
with gr.Column(scale=2):
|
282 |
+
i2i_image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
|
283 |
+
|
284 |
+
i2i_text_button.click(img2img, inputs=[i2i_image_input, i2i_denoising, i2i_prompt, i2i_negative_prompt,
|
285 |
+
model, i2i_steps, i2i_sampler, i2i_cfg_scale, i2i_width, i2i_height,
|
286 |
+
i2i_seed], outputs=i2i_image_output, concurrency_limit=64)
|
287 |
+
|
288 |
+
with gr.Tab("PNG Info"):
|
289 |
+
def plaintext_to_html(text, classname=None):
|
290 |
+
content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
|
291 |
+
|
292 |
+
return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"
|
293 |
+
|
294 |
+
|
295 |
+
def get_exif_data(image):
|
296 |
+
items = image.info
|
297 |
+
|
298 |
+
info = ''
|
299 |
+
for key, text in items.items():
|
300 |
+
info += f"""
|
301 |
+
<div>
|
302 |
+
<p><b>{plaintext_to_html(str(key))}</b></p>
|
303 |
+
<p>{plaintext_to_html(str(text))}</p>
|
304 |
+
</div>
|
305 |
+
""".strip()+"\n"
|
306 |
+
|
307 |
+
if len(info) == 0:
|
308 |
+
message = "Nothing found in the image."
|
309 |
+
info = f"<div><p>{message}<p></div>"
|
310 |
+
|
311 |
+
return info
|
312 |
+
|
313 |
+
with gr.Row():
|
314 |
+
with gr.Column():
|
315 |
+
image_input = gr.Image(type="pil")
|
316 |
+
|
317 |
+
with gr.Column():
|
318 |
+
exif_output = gr.HTML(label="EXIF Data")
|
319 |
+
send_to_txt2img_btn = gr.Button("Send to txt2img")
|
320 |
+
|
321 |
+
image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
|
322 |
+
send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt,
|
323 |
+
steps, seed, model, sampler,
|
324 |
+
width, height, cfg_scale],
|
325 |
+
concurrency_limit=64)
|
326 |
|
327 |
+
demo.queue(max_size=80, api_open=False).launch(max_threads=256, show_api=False)
|
|
|
|
|
|