gokaygokay commited on
Commit
b9f8924
1 Parent(s): 461d979

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -75,11 +75,23 @@ florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base',
75
  enhancer_medium = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance", device=device)
76
  enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
77
 
78
- # Initialize ESRGAN models
79
- realesrgan_x2 = RealESRGAN(device, scale=2)
80
- realesrgan_x2.load_weights('models/upscalers/RealESRGAN_x2.pth', download=False)
81
- realesrgan_x4 = RealESRGAN(device, scale=4)
82
- realesrgan_x4.load_weights('models/upscalers/RealESRGAN_x4.pth', download=False)
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # Florence caption function
85
  def florence_caption(image):
@@ -116,18 +128,12 @@ def enhance_prompt(input_prompt, model_choice):
116
  return enhanced_text
117
 
118
  def upscale_image(image, scale):
119
- # Convert PIL Image to numpy array
120
- img_np = np.array(image)
121
-
122
  if scale == 2:
123
- upscaled_np = realesrgan_x2.predict(img_np)
124
  elif scale == 4:
125
- upscaled_np = realesrgan_x4.predict(img_np)
126
  else:
127
  return image
128
-
129
- # Convert numpy array back to PIL Image
130
- return Image.fromarray(upscaled_np)
131
 
132
  @spaces.GPU(duration=120)
133
  def generate_image(additional_positive_prompt, additional_negative_prompt, height, width, num_inference_steps,
 
75
  enhancer_medium = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance", device=device)
76
  enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
77
 
78
+ class LazyRealESRGAN:
79
+ def __init__(self, device, scale):
80
+ self.device = device
81
+ self.scale = scale
82
+ self.model = None
83
+
84
+ def load_model(self):
85
+ if self.model is None:
86
+ self.model = RealESRGAN(self.device, scale=self.scale)
87
+ self.model.load_weights(f'models/upscalers/RealESRGAN_x{self.scale}.pth', download=False)
88
+
89
+ def predict(self, img):
90
+ self.load_model()
91
+ return self.model.predict(img)
92
+
93
+ lazy_realesrgan_x2 = LazyRealESRGAN(device, scale=2)
94
+ lazy_realesrgan_x4 = LazyRealESRGAN(device, scale=4)
95
 
96
  # Florence caption function
97
  def florence_caption(image):
 
128
  return enhanced_text
129
 
130
  def upscale_image(image, scale):
 
 
 
131
  if scale == 2:
132
+ return lazy_realesrgan_x2.predict(image)
133
  elif scale == 4:
134
+ return lazy_realesrgan_x4.predict(image)
135
  else:
136
  return image
 
 
 
137
 
138
  @spaces.GPU(duration=120)
139
  def generate_image(additional_positive_prompt, additional_negative_prompt, height, width, num_inference_steps,