montyanderson zenafey commited on
Commit
b62f01b
1 Parent(s): 2f14be5

Add "send to txt2img" button to PNGInfo (#9)

Browse files

- Add "send to txt2img" button to PNGInfo (bf9ad1e6fbede10b78960336c6829133a61025ba)
- Replace static model list with function for removing extension and id (33f7d493b13c46fe5889e7067b6ca1bbb67b42a9)
- Update app.py (516f2ad246531f25395b79d0e3d2cfbf8726db81)


Co-authored-by: Zenafey Baenro <[email protected]>

Files changed (1) hide show
  1. app.py +169 -89
app.py CHANGED
@@ -80,9 +80,86 @@ def image_to_base64(image_path):
80
 
81
  return img_str.decode('utf-8') # Convert bytes to string
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
 
85
  prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
 
 
 
 
 
 
86
 
87
  def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
88
  result = prodia_client.generate({
@@ -110,7 +187,7 @@ css = """
110
 
111
  with gr.Blocks(css=css) as demo:
112
 
113
-
114
  with gr.Row():
115
  with gr.Column(scale=6):
116
  model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
@@ -118,96 +195,99 @@ with gr.Blocks(css=css) as demo:
118
  with gr.Column(scale=1):
119
  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 gen times check out our [API Docs](https://docs.prodia.com/reference/getting-started-guide)")
120
 
121
- with gr.Tab("txt2img"):
122
- with gr.Row():
123
- with gr.Column(scale=6, min_width=600):
124
- prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
125
- negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
126
- with gr.Column():
127
- text_button = gr.Button("Generate", variant='primary', elem_id="generate")
128
-
129
- with gr.Row():
130
- with gr.Column(scale=3):
131
- with gr.Tab("Generation"):
132
- with gr.Row():
133
- with gr.Column(scale=1):
134
- sampler = gr.Dropdown(value="Euler a", show_label=True, label="Sampling Method", choices=[
135
- "Euler",
136
- "Euler a",
137
- "LMS",
138
- "Heun",
139
- "DPM2",
140
- "DPM2 a",
141
- "DPM++ 2S a",
142
- "DPM++ 2M",
143
- "DPM++ SDE",
144
- "DPM fast",
145
- "DPM adaptive",
146
- "LMS Karras",
147
- "DPM2 Karras",
148
- "DPM2 a Karras",
149
- "DPM++ 2S a Karras",
150
- "DPM++ 2M Karras",
151
- "DPM++ SDE Karras",
152
- "DDIM",
153
- "PLMS",
154
- ])
 
 
 
 
 
 
 
 
 
155
 
156
- with gr.Column(scale=1):
157
- steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=30, value=25, step=1)
158
-
159
- with gr.Row():
160
- with gr.Column(scale=1):
161
- width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
162
- height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
163
-
164
- with gr.Column(scale=1):
165
- batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
166
- batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
167
-
168
- cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
169
- seed = gr.Number(label="Seed", value=-1)
170
-
171
-
172
- with gr.Column(scale=2):
173
- image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
174
-
175
- text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
176
 
177
- with gr.Tab("PNG Info"):
178
- def plaintext_to_html(text, classname=None):
179
- content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
180
-
181
- return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"
182
-
183
-
184
- def get_exif_data(image):
185
- items = image.info
186
-
187
- info = ''
188
- for key, text in items.items():
189
- info += f"""
190
- <div>
191
- <p><b>{plaintext_to_html(str(key))}</b></p>
192
- <p>{plaintext_to_html(str(text))}</p>
193
- </div>
194
- """.strip()+"\n"
195
-
196
- if len(info) == 0:
197
- message = "Nothing found in the image."
198
- info = f"<div><p>{message}<p></div>"
199
-
200
- return info
201
-
202
- with gr.Row():
203
- with gr.Column():
204
- image_input = gr.Image(type="pil")
205
-
206
- with gr.Column():
207
- exif_output = gr.HTML(label="EXIF Data")
208
-
209
- image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
210
-
 
 
 
 
 
 
 
 
 
 
 
211
 
212
  demo.queue(concurrency_count=32)
213
  demo.launch()
 
80
 
81
  return img_str.decode('utf-8') # Convert bytes to string
82
 
83
+ def remove_id_and_ext(text):
84
+ text = re.sub(r'\[.*\]$', '', text)
85
+ extension = text[-12:].strip()
86
+ if extension == "safetensors":
87
+ text = text[:-13]
88
+ elif extension == "ckpt":
89
+ text = text[:-4]
90
+ return text
91
+
92
+ def get_data(text):
93
+ results = {}
94
+ patterns = {
95
+ 'prompt': r'(.*)',
96
+ 'negative_prompt': r'Negative prompt: (.*)',
97
+ 'steps': r'Steps: (\d+),',
98
+ 'seed': r'Seed: (\d+),',
99
+ 'sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)',
100
+ 'model': r'Model:\s*([^\s,]+)',
101
+ 'cfg_scale': r'CFG scale:\s*([\d\.]+)',
102
+ 'size': r'Size:\s*([0-9]+x[0-9]+)'
103
+ }
104
+ for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']:
105
+ match = re.search(patterns[key], text)
106
+ if match:
107
+ results[key] = match.group(1)
108
+ else:
109
+ results[key] = None
110
+ if results['size'] is not None:
111
+ w, h = results['size'].split("x")
112
+ results['w'] = w
113
+ results['h'] = h
114
+ else:
115
+ results['w'] = None
116
+ results['h'] = None
117
+ return results
118
+
119
+ def send_to_txt2img(image):
120
+
121
+ result = {tabs: gr.Tabs.update(selected="t2i")}
122
+
123
+ try:
124
+ text = image.info['parameters']
125
+ data = get_data(text)
126
+ result[prompt] = gr.update(value=data['prompt'])
127
+ result[negative_prompt] = gr.update(value=data['negative_prompt']) if data['negative_prompt'] is not None else gr.update()
128
+ result[steps] = gr.update(value=int(data['steps'])) if data['steps'] is not None else gr.update()
129
+ result[seed] = gr.update(value=int(data['seed'])) if data['seed'] is not None else gr.update()
130
+ result[cfg_scale] = gr.update(value=float(data['cfg_scale'])) if data['cfg_scale'] is not None else gr.update()
131
+ result[width] = gr.update(value=int(data['w'])) if data['w'] is not None else gr.update()
132
+ result[height] = gr.update(value=int(data['h'])) if data['h'] is not None else gr.update()
133
+ result[sampler] = gr.update(value=data['sampler']) if data['sampler'] is not None else gr.update()
134
+ if model in model_names:
135
+ result[model] = gr.update(value=model_names[model])
136
+ else:
137
+ result[model] = gr.update()
138
+ return result
139
+
140
+ except Exception as e:
141
+ print(e)
142
+ result[prompt] = gr.update()
143
+ result[negative_prompt] = gr.update()
144
+ result[steps] = gr.update()
145
+ result[seed] = gr.update()
146
+ result[cfg_scale] = gr.update()
147
+ result[width] = gr.update()
148
+ result[height] = gr.update()
149
+ result[sampler] = gr.update()
150
+ result[model] = gr.update()
151
+
152
+ return result
153
+
154
 
155
 
156
  prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
157
+ model_list = prodia_client.list_models()
158
+ model_names = {}
159
+
160
+ for model_name in model_list:
161
+ name_without_ext = remove_id_and_ext(model_name)
162
+ model_names[name_without_ext] = model_name
163
 
164
  def flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
165
  result = prodia_client.generate({
 
187
 
188
  with gr.Blocks(css=css) as demo:
189
 
190
+
191
  with gr.Row():
192
  with gr.Column(scale=6):
193
  model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
 
195
  with gr.Column(scale=1):
196
  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 gen times check out our [API Docs](https://docs.prodia.com/reference/getting-started-guide)")
197
 
198
+ with gr.Tabs() as tabs:
199
+ with gr.Tab("txt2img", id='t2i'):
200
+ with gr.Row():
201
+ with gr.Column(scale=6, min_width=600):
202
+ prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
203
+ negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
204
+ with gr.Column():
205
+ text_button = gr.Button("Generate", variant='primary', elem_id="generate")
206
+
207
+ with gr.Row():
208
+ with gr.Column(scale=3):
209
+ with gr.Tab("Generation"):
210
+ with gr.Row():
211
+ with gr.Column(scale=1):
212
+ sampler = gr.Dropdown(value="Euler a", show_label=True, label="Sampling Method", choices=[
213
+ "Euler",
214
+ "Euler a",
215
+ "LMS",
216
+ "Heun",
217
+ "DPM2",
218
+ "DPM2 a",
219
+ "DPM++ 2S a",
220
+ "DPM++ 2M",
221
+ "DPM++ SDE",
222
+ "DPM fast",
223
+ "DPM adaptive",
224
+ "LMS Karras",
225
+ "DPM2 Karras",
226
+ "DPM2 a Karras",
227
+ "DPM++ 2S a Karras",
228
+ "DPM++ 2M Karras",
229
+ "DPM++ SDE Karras",
230
+ "DDIM",
231
+ "PLMS",
232
+ ])
233
+
234
+ with gr.Column(scale=1):
235
+ steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=30, value=25, step=1)
236
+
237
+ with gr.Row():
238
+ with gr.Column(scale=1):
239
+ width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
240
+ height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
241
 
242
+ with gr.Column(scale=1):
243
+ batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
244
+ batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
 
246
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
247
+ seed = gr.Number(label="Seed", value=-1)
248
+
249
+
250
+ with gr.Column(scale=2):
251
+ image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
252
+
253
+ text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)
254
+
255
+ with gr.Tab("PNG Info"):
256
+ def plaintext_to_html(text, classname=None):
257
+ content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
258
+
259
+ return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"
260
+
261
+
262
+ def get_exif_data(image):
263
+ items = image.info
264
+
265
+ info = ''
266
+ for key, text in items.items():
267
+ info += f"""
268
+ <div>
269
+ <p><b>{plaintext_to_html(str(key))}</b></p>
270
+ <p>{plaintext_to_html(str(text))}</p>
271
+ </div>
272
+ """.strip()+"\n"
273
+
274
+ if len(info) == 0:
275
+ message = "Nothing found in the image."
276
+ info = f"<div><p>{message}<p></div>"
277
+
278
+ return info
279
+
280
+ with gr.Row():
281
+ with gr.Column():
282
+ image_input = gr.Image(type="pil")
283
+
284
+ with gr.Column():
285
+ exif_output = gr.HTML(label="EXIF Data")
286
+ send_to_txt2img_btn = gr.Button("Send to txt2img")
287
+
288
+ image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
289
+ send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt, steps, seed,
290
+ model, sampler, width, height, cfg_scale])
291
 
292
  demo.queue(concurrency_count=32)
293
  demo.launch()