akhaliq HF Staff commited on
Commit
514d67f
·
verified ·
1 Parent(s): de0c3b6

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +61 -7
app.py CHANGED
@@ -1,9 +1,9 @@
1
  """
2
- GLM-Image to Image Editing App
3
- A Gradio 6 application for image-to-image editing using the GLM-Image model.
4
 
5
  This app allows users to upload an image and provide a prompt to transform
6
- the image using the GLM-Image diffusion model.
7
  """
8
 
9
  import gradio as gr
@@ -13,6 +13,9 @@ from PIL import Image
13
  import os
14
  from datetime import datetime
15
 
 
 
 
16
  # Initialize the model (lazy loading for better startup performance)
17
  pipe = None
18
 
@@ -41,6 +44,34 @@ def get_image_dimensions(image: Image.Image) -> tuple:
41
  """Get the dimensions of an uploaded PIL image."""
42
  return image.size[1], image.size[0] # height, width
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def process_image(
45
  image: Image.Image,
46
  prompt: str,
@@ -53,6 +84,7 @@ def process_image(
53
  ) -> tuple:
54
  """
55
  Process the image through the GLM-Image pipeline.
 
56
 
57
  Args:
58
  image: Input PIL Image
@@ -152,7 +184,7 @@ custom_theme = gr.themes.Soft(
152
  )
153
 
154
  # Build the Gradio 6 application
155
- with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
156
 
157
  # Header with branding
158
  gr.Markdown(
@@ -167,6 +199,16 @@ with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
167
  elem_classes=["header-markdown"]
168
  )
169
 
 
 
 
 
 
 
 
 
 
 
170
  # Main content in a row
171
  with gr.Row(equal_height=True):
172
  # Left column - Input controls
@@ -276,7 +318,7 @@ with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
276
  # Status message
277
  status = gr.Textbox(
278
  label="Status",
279
- value="Ready to generate!",
280
  interactive=False,
281
  show_label=True
282
  )
@@ -339,7 +381,7 @@ with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
339
  api_visibility="private"
340
  )
341
 
342
- # Generate button handler
343
  generate_btn.click(
344
  fn=process_image,
345
  inputs=[
@@ -374,7 +416,7 @@ with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
374
  input_image: None,
375
  prompt: "",
376
  output_image: None,
377
- status: "Ready to generate!",
378
  download_btn: gr.DownloadButton(interactive=False)
379
  }
380
 
@@ -413,6 +455,17 @@ demo.launch(
413
  color: #ffd700 !important;
414
  text-decoration: underline;
415
  }
 
 
 
 
 
 
 
 
 
 
 
416
  #input-image, #output-image {
417
  border: 2px dashed var(--neutral-300);
418
  border-radius: var(--radius-lg);
@@ -424,6 +477,7 @@ demo.launch(
424
  footer_links=[
425
  {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
426
  {"label": "GLM-Image Model", "url": "https://huggingface.co/zai-org/GLM-Image"},
 
427
  {"label": "Diffusers Library", "url": "https://github.com/huggingface/diffusers"}
428
  ],
429
  server_name="0.0.0.0",
 
1
  """
2
+ GLM-Image to Image Editing App (ZeroGPU Version)
3
+ A Gradio 6 application for image-to-image editing using the GLM-Image model with ZeroGPU support.
4
 
5
  This app allows users to upload an image and provide a prompt to transform
6
+ the image using the GLM-Image diffusion model with dynamic GPU allocation.
7
  """
8
 
9
  import gradio as gr
 
13
  import os
14
  from datetime import datetime
15
 
16
+ # Import ZeroGPU
17
+ import spaces
18
+
19
  # Initialize the model (lazy loading for better startup performance)
20
  pipe = None
21
 
 
44
  """Get the dimensions of an uploaded PIL image."""
45
  return image.size[1], image.size[0] # height, width
46
 
47
+ def estimate_duration(num_inference_steps: int, height: int, width: int) -> int:
48
+ """
49
+ Estimate the duration needed for the GPU task based on complexity.
50
+
51
+ Args:
52
+ num_inference_steps: Number of diffusion steps
53
+ height: Image height
54
+ width: Image width
55
+
56
+ Returns:
57
+ Estimated duration in seconds
58
+ """
59
+ # Base time per step (adjust based on testing)
60
+ base_time_per_step = 3.5 # seconds
61
+
62
+ # Complexity factor based on image size (larger images take more time)
63
+ size_factor = (height * width) / (1024 * 1024) # relative to 1024x1024
64
+
65
+ # Estimate total time
66
+ estimated_time = num_inference_steps * base_time_per_step * size_factor
67
+
68
+ # Add buffer for image processing overhead
69
+ total_duration = int(estimated_time) + 30 # +30 seconds buffer
70
+
71
+ # Ensure minimum duration and cap at reasonable max
72
+ return max(60, min(total_duration, 180)) # Between 60s and 180s
73
+
74
+ @spaces.GPU(duration=estimate_duration)
75
  def process_image(
76
  image: Image.Image,
77
  prompt: str,
 
84
  ) -> tuple:
85
  """
86
  Process the image through the GLM-Image pipeline.
87
+ Decorated with @spaces.GPU for ZeroGPU dynamic allocation.
88
 
89
  Args:
90
  image: Input PIL Image
 
184
  )
185
 
186
  # Build the Gradio 6 application
187
+ with gr.Blocks(fill_height=True) as demo:
188
 
189
  # Header with branding
190
  gr.Markdown(
 
199
  elem_classes=["header-markdown"]
200
  )
201
 
202
+ # GPU Status indicator
203
+ gr.Markdown(
204
+ """
205
+ <div class="gpu-status">
206
+ 🚀 <strong>ZeroGPU Enabled</strong> - Dynamic GPU allocation for optimal performance
207
+ </div>
208
+ """,
209
+ elem_classes=["gpu-status-markdown"]
210
+ )
211
+
212
  # Main content in a row
213
  with gr.Row(equal_height=True):
214
  # Left column - Input controls
 
318
  # Status message
319
  status = gr.Textbox(
320
  label="Status",
321
+ value="Ready to generate! GPU will be allocated automatically.",
322
  interactive=False,
323
  show_label=True
324
  )
 
381
  api_visibility="private"
382
  )
383
 
384
+ # Generate button handler - uses ZeroGPU via @spaces.GPU decorator
385
  generate_btn.click(
386
  fn=process_image,
387
  inputs=[
 
416
  input_image: None,
417
  prompt: "",
418
  output_image: None,
419
+ status: "Ready to generate! GPU will be allocated automatically.",
420
  download_btn: gr.DownloadButton(interactive=False)
421
  }
422
 
 
455
  color: #ffd700 !important;
456
  text-decoration: underline;
457
  }
458
+ .gpu-status-markdown {
459
+ background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
460
+ padding: 0.75rem;
461
+ border-radius: 0.5rem;
462
+ margin-bottom: 1rem;
463
+ text-align: center;
464
+ color: white;
465
+ }
466
+ .gpu-status-markdown strong {
467
+ color: #fff;
468
+ }
469
  #input-image, #output-image {
470
  border: 2px dashed var(--neutral-300);
471
  border-radius: var(--radius-lg);
 
477
  footer_links=[
478
  {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
479
  {"label": "GLM-Image Model", "url": "https://huggingface.co/zai-org/GLM-Image"},
480
+ {"label": "ZeroGPU", "url": "https://huggingface.co/spaces/zero-gpu-explorers/README"},
481
  {"label": "Diffusers Library", "url": "https://github.com/huggingface/diffusers"}
482
  ],
483
  server_name="0.0.0.0",