ajsbsd commited on
Commit
dbe4bd1
·
verified ·
1 Parent(s): c02b5d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -35,8 +35,11 @@ def clear_memory():
35
  torch.cuda.empty_cache()
36
  gc.collect()
37
 
38
- def add_metadata_and_save(image: Image.Image, filepath: str, prompt: str, negative_prompt: str, seed: int, steps: int, guidance: float, strength: Optional[float] = None):
39
  """Embed generation metadata into a PNG and save it."""
 
 
 
40
  meta = PngImagePlugin.PngInfo()
41
  meta.add_text("Prompt", prompt)
42
  meta.add_text("NegativePrompt", negative_prompt)
@@ -50,8 +53,8 @@ def add_metadata_and_save(image: Image.Image, filepath: str, prompt: str, negati
50
  meta.add_text("Seed", str(seed))
51
  meta.add_text("Date", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
52
 
53
- image.save(filepath, "PNG", pnginfo=meta)
54
- return filepath
55
 
56
  def load_models():
57
  """Load both text2img and img2img pipelines optimized for Spaces"""
@@ -199,10 +202,9 @@ def generate_txt2img(prompt, negative_prompt, num_steps, guidance_scale, width,
199
 
200
  generation_time = time.time() - start_time
201
 
202
- # Save with metadata
203
- temp_path = tempfile.mktemp(suffix=".png")
204
- add_metadata_and_save(
205
- result.images[0], temp_path, enhanced_prompt, negative_prompt or "",
206
  seed, num_steps, guidance_scale
207
  )
208
 
@@ -212,7 +214,7 @@ def generate_txt2img(prompt, negative_prompt, num_steps, guidance_scale, width,
212
  negative_prompt or "", seed, num_steps, guidance_scale
213
  )
214
 
215
- return result.images[0], status
216
 
217
  except Exception as e:
218
  return None, f"Generation failed: {str(e)}"
@@ -277,10 +279,9 @@ def generate_img2img(input_image, prompt, negative_prompt, num_steps, guidance_s
277
 
278
  generation_time = time.time() - start_time
279
 
280
- # Save with metadata
281
- temp_path = tempfile.mktemp(suffix=".png")
282
- add_metadata_and_save(
283
- result.images[0], temp_path, enhanced_prompt, negative_prompt or "",
284
  seed, num_steps, guidance_scale, strength
285
  )
286
 
@@ -290,7 +291,7 @@ def generate_img2img(input_image, prompt, negative_prompt, num_steps, guidance_s
290
  negative_prompt or "", seed, num_steps, guidance_scale, strength
291
  )
292
 
293
- return result.images[0], status
294
 
295
  except Exception as e:
296
  return None, f"Transformation failed: {str(e)}"
@@ -315,8 +316,8 @@ def set_example_prompt():
315
 
316
  # Simplified negative prompt for better performance
317
  DEFAULT_NEGATIVE = """
318
- (low quality:1.3), (worst quality:1.3), (bad quality:1.2), (nsfw:1.5), (easynegative:1.3) (bad_prompt:1.3) badhandv4 bad-hands-5 (negative_hand-neg) (bad-picture-chill-75v), (worst quality:1.3), (low quality:1.3), (bad quality:1.3), (a shadow on skin:1.3), (a shaded skin:1.3), (a dark skin:1.3), (blush:1.3), (signature, watermark, username, letter, copyright name,
319
- copyright, chinese text, artist name, name tag, company name, name tag, text, error:1.5), (bad anatomy:1.5), (low quality hand:1.5), (worst quality hand:1.5)
320
  """
321
 
322
  # Gradio interface optimized for Spaces
@@ -374,7 +375,7 @@ with gr.Blocks(
374
  txt2img_btn = gr.Button("🎨 Generate", variant="primary", size="lg")
375
 
376
  with gr.Column():
377
- txt2img_output = gr.Image(label="Generated Image", height=400)
378
  txt2img_status = gr.Textbox(label="Generation Info", interactive=False, lines=6)
379
 
380
  with gr.TabItem("🖼️ Image to Image"):
@@ -420,7 +421,7 @@ with gr.Blocks(
420
  img2img_btn = gr.Button("🖼️ Transform", variant="primary", size="lg")
421
 
422
  with gr.Column():
423
- img2img_output = gr.Image(label="Generated Image", height=400)
424
  img2img_status = gr.Textbox(label="Generation Info", interactive=False, lines=6)
425
 
426
  # Event handlers
 
35
  torch.cuda.empty_cache()
36
  gc.collect()
37
 
38
+ def add_metadata_and_save(image: Image.Image, prompt: str, negative_prompt: str, seed: int, steps: int, guidance: float, strength: Optional[float] = None):
39
  """Embed generation metadata into a PNG and save it."""
40
+ # Create temporary file with unique name
41
+ temp_path = tempfile.mktemp(suffix=".png")
42
+
43
  meta = PngImagePlugin.PngInfo()
44
  meta.add_text("Prompt", prompt)
45
  meta.add_text("NegativePrompt", negative_prompt)
 
53
  meta.add_text("Seed", str(seed))
54
  meta.add_text("Date", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
55
 
56
+ image.save(temp_path, "PNG", pnginfo=meta)
57
+ return temp_path
58
 
59
  def load_models():
60
  """Load both text2img and img2img pipelines optimized for Spaces"""
 
202
 
203
  generation_time = time.time() - start_time
204
 
205
+ # Save with metadata - returns file path
206
+ png_path = add_metadata_and_save(
207
+ result.images[0], enhanced_prompt, negative_prompt or "",
 
208
  seed, num_steps, guidance_scale
209
  )
210
 
 
214
  negative_prompt or "", seed, num_steps, guidance_scale
215
  )
216
 
217
+ return png_path, status
218
 
219
  except Exception as e:
220
  return None, f"Generation failed: {str(e)}"
 
279
 
280
  generation_time = time.time() - start_time
281
 
282
+ # Save with metadata - returns file path
283
+ png_path = add_metadata_and_save(
284
+ result.images[0], enhanced_prompt, negative_prompt or "",
 
285
  seed, num_steps, guidance_scale, strength
286
  )
287
 
 
291
  negative_prompt or "", seed, num_steps, guidance_scale, strength
292
  )
293
 
294
+ return png_path, status
295
 
296
  except Exception as e:
297
  return None, f"Transformation failed: {str(e)}"
 
316
 
317
  # Simplified negative prompt for better performance
318
  DEFAULT_NEGATIVE = """
319
+ (low quality:1.3), (worst quality:1.3), (bad quality:1.2), blurry, noisy, ugly, deformed,
320
+ (text, watermark:1.4), (extra limbs:1.3), (bad hands:1.3), (bad anatomy:1.2)
321
  """
322
 
323
  # Gradio interface optimized for Spaces
 
375
  txt2img_btn = gr.Button("🎨 Generate", variant="primary", size="lg")
376
 
377
  with gr.Column():
378
+ txt2img_output = gr.File(label="Generated PNG with Metadata", file_types=[".png"])
379
  txt2img_status = gr.Textbox(label="Generation Info", interactive=False, lines=6)
380
 
381
  with gr.TabItem("🖼️ Image to Image"):
 
421
  img2img_btn = gr.Button("🖼️ Transform", variant="primary", size="lg")
422
 
423
  with gr.Column():
424
+ img2img_output = gr.File(label="Generated PNG with Metadata", file_types=[".png"])
425
  img2img_status = gr.Textbox(label="Generation Info", interactive=False, lines=6)
426
 
427
  # Event handlers