rastof9 commited on
Commit
721f91b
·
verified ·
1 Parent(s): 5bbcbee

Update generate.py

Browse files
Files changed (1) hide show
  1. generate.py +9 -13
generate.py CHANGED
@@ -1,12 +1,9 @@
1
  # generate.py
2
- # --- VERSION 11 (with Monkey-Patch) ---
3
 
4
- print("--- RUNNING GENERATE.PY VERSION 11 (with Monkey-Patch) ---")
5
 
6
  # --- MONKEY-PATCH FOR OLD TORCHVISION ---
7
- # This block fixes the "ModuleNotFoundError" by tricking the system.
8
- # It tells any library looking for the new `functional_tensor` module
9
- # to use the old `functional` module instead.
10
  try:
11
  import sys
12
  import torchvision.transforms.functional as F
@@ -75,9 +72,12 @@ class GenerationService:
75
 
76
  # --- Upscaler Model ---
77
  logger.info("Loading Real-ESRGAN upscaler model...")
78
- model_path = os.path.join('weights', 'realesrgan-x4plus.pth')
 
 
 
79
  if not os.path.exists(model_path):
80
- model_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
81
  load_file_from_url(url=model_url, model_dir=os.path.join('weights'), progress=True)
82
 
83
  self.upsampler = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
@@ -104,13 +104,12 @@ class GenerationService:
104
  output_img = output.squeeze().permute(1, 2, 0).cpu().numpy()
105
  output_img = (output_img * 255.0).round().astype('uint8')
106
 
107
- # Save the upscaled image back to the same path
108
  cv2.imwrite(image_path, output_img)
109
  logger.info(f"Successfully upscaled image: {image_path}")
110
  return image_path
111
  except Exception as e:
112
  logger.error(f"Failed to upscale image {image_path}: {e}")
113
- return image_path # Return original path on failure
114
 
115
 
116
  def generate_magic_image(self, face_images: list, gender: str, prompt: str, plan: str = 'free') -> str | None:
@@ -156,14 +155,11 @@ class GenerationService:
156
  local_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")
157
  image.save(local_path)
158
 
159
- # --- FEATURE TIER LOGIC ---
160
  if plan == 'free':
161
  utils.add_watermark(local_path, "@MagicFaceBot")
162
  else:
163
- # Upscale for paid users
164
  self._upscale_image(local_path)
165
 
166
- # --- Upload to Supabase Storage ---
167
  storage_path = f"public/{os.path.basename(local_path)}"
168
  with open(local_path, 'rb') as f:
169
  supabase.storage.from_(config.SUPABASE_BUCKET_NAME).upload(
@@ -191,7 +187,7 @@ if __name__ == '__main__':
191
  face_images=["test_face.jpg"],
192
  gender="Female",
193
  prompt="A beautiful portrait of a princess in a magical forest, fantasy art",
194
- plan='paid' # <-- Test the 'paid' plan to see the upscaling
195
  )
196
  if result_url:
197
  print(f"\n✅ Test successful! Image URL: {result_url}")
 
1
  # generate.py
2
+ # --- VERSION 12 (Final Fix) ---
3
 
4
+ print("--- RUNNING GENERATE.PY VERSION 12 (Final Fix) ---")
5
 
6
  # --- MONKEY-PATCH FOR OLD TORCHVISION ---
 
 
 
7
  try:
8
  import sys
9
  import torchvision.transforms.functional as F
 
72
 
73
  # --- Upscaler Model ---
74
  logger.info("Loading Real-ESRGAN upscaler model...")
75
+ # FIX: The downloaded file has uppercase letters. Match the filename exactly.
76
+ model_name = 'RealESRGAN_x4plus.pth'
77
+ model_path = os.path.join('weights', model_name)
78
+
79
  if not os.path.exists(model_path):
80
+ model_url = f'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/{model_name}'
81
  load_file_from_url(url=model_url, model_dir=os.path.join('weights'), progress=True)
82
 
83
  self.upsampler = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
 
104
  output_img = output.squeeze().permute(1, 2, 0).cpu().numpy()
105
  output_img = (output_img * 255.0).round().astype('uint8')
106
 
 
107
  cv2.imwrite(image_path, output_img)
108
  logger.info(f"Successfully upscaled image: {image_path}")
109
  return image_path
110
  except Exception as e:
111
  logger.error(f"Failed to upscale image {image_path}: {e}")
112
+ return image_path
113
 
114
 
115
  def generate_magic_image(self, face_images: list, gender: str, prompt: str, plan: str = 'free') -> str | None:
 
155
  local_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")
156
  image.save(local_path)
157
 
 
158
  if plan == 'free':
159
  utils.add_watermark(local_path, "@MagicFaceBot")
160
  else:
 
161
  self._upscale_image(local_path)
162
 
 
163
  storage_path = f"public/{os.path.basename(local_path)}"
164
  with open(local_path, 'rb') as f:
165
  supabase.storage.from_(config.SUPABASE_BUCKET_NAME).upload(
 
187
  face_images=["test_face.jpg"],
188
  gender="Female",
189
  prompt="A beautiful portrait of a princess in a magical forest, fantasy art",
190
+ plan='paid'
191
  )
192
  if result_url:
193
  print(f"\n✅ Test successful! Image URL: {result_url}")