Aatricks commited on
Commit
f5a443f
·
verified ·
1 Parent(s): dfc5db4

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import glob
 
2
  import gradio as gr
3
  import sys
4
  import os
5
  from PIL import Image
 
6
  import spaces
7
  sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
8
 
@@ -58,13 +60,18 @@ def generate_images(
58
  """Generate images using the LightDiffusion pipeline"""
59
  try:
60
  if img2img_enabled and img2img_image is not None:
61
- # Save uploaded image temporarily and pass path to pipeline
62
- img2img_image.save("temp_img2img.png")
63
- prompt = "temp_img2img.png"
 
 
 
 
 
64
 
65
  # Run pipeline and capture saved images
66
  with torch.inference_mode():
67
- images = pipeline(
68
  prompt=prompt,
69
  w=width,
70
  h=height,
@@ -80,11 +87,18 @@ def generate_images(
80
  prio_speed=prio_speed
81
  )
82
 
 
 
 
 
83
  return load_generated_images()
84
 
85
  except Exception as e:
86
  import traceback
87
  print(traceback.format_exc())
 
 
 
88
  return [Image.new('RGB', (512, 512), color='black')]
89
 
90
  # Create Gradio interface
 
1
  import glob
2
+ import cv2
3
  import gradio as gr
4
  import sys
5
  import os
6
  from PIL import Image
7
+ import numpy as np
8
  import spaces
9
  sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
10
 
 
60
  """Generate images using the LightDiffusion pipeline"""
61
  try:
62
  if img2img_enabled and img2img_image is not None:
63
+ # Convert numpy array to PIL Image
64
+ if isinstance(img2img_image, np.ndarray):
65
+ # Convert BGR to RGB if needed
66
+ if img2img_image.shape[-1] == 3:
67
+ img2img_image = cv2.cvtColor(img2img_image, cv2.COLOR_BGR2RGB)
68
+ img_pil = Image.fromarray(img2img_image)
69
+ img_pil.save("temp_img2img.png")
70
+ prompt = "temp_img2img.png"
71
 
72
  # Run pipeline and capture saved images
73
  with torch.inference_mode():
74
+ pipeline(
75
  prompt=prompt,
76
  w=width,
77
  h=height,
 
87
  prio_speed=prio_speed
88
  )
89
 
90
+ # Clean up temporary file if it exists
91
+ if os.path.exists("temp_img2img.png"):
92
+ os.remove("temp_img2img.png")
93
+
94
  return load_generated_images()
95
 
96
  except Exception as e:
97
  import traceback
98
  print(traceback.format_exc())
99
+ # Clean up temporary file if it exists
100
+ if os.path.exists("temp_img2img.png"):
101
+ os.remove("temp_img2img.png")
102
  return [Image.new('RGB', (512, 512), color='black')]
103
 
104
  # Create Gradio interface