Menyu commited on
Commit
f0fe2ac
·
verified ·
1 Parent(s): e58692f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -1
app.py CHANGED
@@ -212,6 +212,25 @@ def get_embed_new(prompt, pipeline, compel, only_convert_string=False, compel_pr
212
 
213
  return merge_embeds([prompt_attention_to_invoke_prompt(i) for i in global_prompt_chanks], compel)
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  def add_comma_after_pattern_ti(text):
216
  pattern = re.compile(r'\b\w+_\d+\b')
217
  modified_text = pattern.sub(lambda x: x.group() + ',', text)
@@ -272,6 +291,8 @@ def infer(
272
  # 在 infer 函数中调用 get_embed_new
273
  if not use_negative_prompt:
274
  negative_prompt = ""
 
 
275
  prompt = get_embed_new(prompt, pipe, compel, only_convert_string=True)
276
  negative_prompt = get_embed_new(negative_prompt, pipe, compel, only_convert_string=True)
277
  conditioning, pooled = compel([prompt, negative_prompt]) # 必须同时处理来保证长度相等
@@ -289,7 +310,25 @@ def infer(
289
  generator=generator,
290
  use_resolution_binning=use_resolution_binning,
291
  ).images[0]
292
- return image, seed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
  examples = [
295
  "nahida (genshin impact)",
 
212
 
213
  return merge_embeds([prompt_attention_to_invoke_prompt(i) for i in global_prompt_chanks], compel)
214
 
215
+ # Add metadata to the image
216
+ def add_metadata_to_image(image, metadata):
217
+ metadata_str = json.dumps(metadata)
218
+
219
+ # Convert PIL Image to PNG with metadata
220
+ img_with_metadata = image.copy()
221
+
222
+ # Create a PngInfo object and add metadata
223
+ png_info = PngImagePlugin.PngInfo()
224
+ png_info.add_text("parameters", metadata_str)
225
+
226
+ # Save to a byte buffer with metadata
227
+ buffer = io.BytesIO()
228
+ img_with_metadata.save(buffer, format="PNG", pnginfo=png_info)
229
+
230
+ # Reopen from buffer to get the image with metadata
231
+ buffer.seek(0)
232
+ return Image.open(buffer)
233
+
234
  def add_comma_after_pattern_ti(text):
235
  pattern = re.compile(r'\b\w+_\d+\b')
236
  modified_text = pattern.sub(lambda x: x.group() + ',', text)
 
291
  # 在 infer 函数中调用 get_embed_new
292
  if not use_negative_prompt:
293
  negative_prompt = ""
294
+
295
+ original_prompt = prompt # Store original prompt for metadata
296
  prompt = get_embed_new(prompt, pipe, compel, only_convert_string=True)
297
  negative_prompt = get_embed_new(negative_prompt, pipe, compel, only_convert_string=True)
298
  conditioning, pooled = compel([prompt, negative_prompt]) # 必须同时处理来保证长度相等
 
310
  generator=generator,
311
  use_resolution_binning=use_resolution_binning,
312
  ).images[0]
313
+
314
+ # Create metadata dictionary
315
+ metadata = {
316
+ "prompt": original_prompt,
317
+ "processed_prompt": prompt,
318
+ "negative_prompt": negative_prompt,
319
+ "seed": seed,
320
+ "width": width,
321
+ "height": height,
322
+ "guidance_scale": guidance_scale,
323
+ "num_inference_steps": num_inference_steps,
324
+ "model": "MiaoMiaoPixel_V1.0",
325
+ "use_resolution_binning": use_resolution_binning,
326
+ "PreUrl": "https://huggingface.co/spaces/Menyu/MiaoPixel"
327
+ }
328
+ # Add metadata to the image
329
+ image_with_metadata = add_metadata_to_image(image, metadata)
330
+
331
+ return image_with_metadata, seed
332
 
333
  examples = [
334
  "nahida (genshin impact)",