akhaliq HF Staff commited on
Commit
873a1e2
·
verified ·
1 Parent(s): b5f13cd

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -15,6 +15,11 @@ from diffusers.pipelines.glm_image import GlmImagePipeline
15
  from PIL import Image
16
  import time
17
  import random
 
 
 
 
 
18
 
19
  # Load the GLM-Image model directly with bfloat16 precision
20
  print("Loading GLM-Image model... This may take a few minutes.")
@@ -122,7 +127,8 @@ def process_image(
122
  progress: Gradio progress tracker (handled automatically by Gradio 6)
123
 
124
  Returns:
125
- Tuple of (output_image, status_message)
 
126
  """
127
  try:
128
  if image is None:
@@ -159,15 +165,20 @@ def process_image(
159
 
160
  output_image = result.images[0]
161
 
 
 
 
 
 
162
  if progress:
163
  progress(1.0, desc="Complete!")
164
 
165
  status = f"Successfully generated! ({height}x{width}, {num_inference_steps} steps)"
166
- return output_image, status
167
 
168
  except Exception as e:
169
  error_msg = f"Error: {str(e)}"
170
- return None, error_msg
171
 
172
  def update_dimensions_from_image(image: Image.Image) -> tuple:
173
  """Update height and width based on uploaded image dimensions."""
@@ -404,14 +415,24 @@ with gr.Blocks(fill_height=True) as demo:
404
  guidance_scale,
405
  seed
406
  ],
407
- outputs=[output_image, status]
408
  )
409
 
410
- def enable_download(img):
411
- if img is not None:
 
 
 
 
 
 
 
 
 
 
412
  return {
413
  download_btn: gr.DownloadButton(
414
- value=img,
415
  interactive=True,
416
  variant="secondary"
417
  )
@@ -424,13 +445,6 @@ with gr.Blocks(fill_height=True) as demo:
424
  )
425
  }
426
 
427
- output_image.change(
428
- fn=enable_download,
429
- inputs=output_image,
430
- outputs=[download_btn],
431
- api_visibility="private"
432
- )
433
-
434
  def clear_all():
435
  """Clear all inputs and outputs."""
436
  return {
 
15
  from PIL import Image
16
  import time
17
  import random
18
+ import os
19
+ import tempfile
20
+
21
+ # Create a temp directory for saving images
22
+ TEMP_DIR = tempfile.mkdtemp(prefix="glm_image_")
23
 
24
  # Load the GLM-Image model directly with bfloat16 precision
25
  print("Loading GLM-Image model... This may take a few minutes.")
 
127
  progress: Gradio progress tracker (handled automatically by Gradio 6)
128
 
129
  Returns:
130
+ Tuple of (output_image, status_message, file_path)
131
+ file_path: Path to the saved image file for download (or None if failed)
132
  """
133
  try:
134
  if image is None:
 
165
 
166
  output_image = result.images[0]
167
 
168
+ # Save image to temp file for download button
169
+ timestamp = int(time.time() * 1000)
170
+ temp_path = os.path.join(TEMP_DIR, f"glm_output_{timestamp}.png")
171
+ output_image.save(temp_path, format="PNG")
172
+
173
  if progress:
174
  progress(1.0, desc="Complete!")
175
 
176
  status = f"Successfully generated! ({height}x{width}, {num_inference_steps} steps)"
177
+ return output_image, status, temp_path
178
 
179
  except Exception as e:
180
  error_msg = f"Error: {str(e)}"
181
+ return None, error_msg, None
182
 
183
  def update_dimensions_from_image(image: Image.Image) -> tuple:
184
  """Update height and width based on uploaded image dimensions."""
 
415
  guidance_scale,
416
  seed
417
  ],
418
+ outputs=[output_image, status, download_btn]
419
  )
420
 
421
+ def enable_download(img, file_path):
422
+ """
423
+ Enable the download button with the file path.
424
+
425
+ Args:
426
+ img: The PIL Image (unused, kept for compatibility)
427
+ file_path: Path to the saved image file
428
+
429
+ Returns:
430
+ Update dict for the download button
431
+ """
432
+ if file_path is not None and os.path.exists(file_path):
433
  return {
434
  download_btn: gr.DownloadButton(
435
+ value=file_path, # <- Use file path, not Image object
436
  interactive=True,
437
  variant="secondary"
438
  )
 
445
  )
446
  }
447
 
 
 
 
 
 
 
 
448
  def clear_all():
449
  """Clear all inputs and outputs."""
450
  return {