Spaces:
Build error
Build error
Update generate.py
Browse files- generate.py +12 -9
generate.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# generate.py
|
| 2 |
-
# --- VERSION
|
| 3 |
|
| 4 |
-
print("--- RUNNING GENERATE.PY VERSION
|
| 5 |
|
| 6 |
import torch
|
| 7 |
import cv2
|
|
@@ -17,6 +17,7 @@ from insightface.utils import face_align
|
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from storage3.utils import StorageException
|
| 19 |
import config
|
|
|
|
| 20 |
from database import supabase
|
| 21 |
|
| 22 |
# --- Setup Logging ---
|
|
@@ -61,7 +62,7 @@ class GenerationService:
|
|
| 61 |
raise RuntimeError(f"Could not initialize GenerationService: {e}") from e
|
| 62 |
|
| 63 |
def generate_magic_image(self, face_images: list, gender: str, prompt: str, plan: str = 'free') -> str | None:
|
| 64 |
-
logger.info("Starting image generation process
|
| 65 |
|
| 66 |
full_prompt = f"{prompt}, 4k, high-resolution, photorealistic, masterpiece, single person, solo portrait, centered composition"
|
| 67 |
negative_prompt = "multiple people, group photo, crowd, two faces, three faces, multiple faces, collage, ugly, deformed, blurry, low quality"
|
|
@@ -88,13 +89,8 @@ class GenerationService:
|
|
| 88 |
|
| 89 |
logger.info("Calling the generation pipeline...")
|
| 90 |
try:
|
| 91 |
-
# --- FINAL FIX: Create negative embeddings ---
|
| 92 |
-
# The IP-Adapter expects both positive and negative image embeddings.
|
| 93 |
-
# We create a zeroed-out tensor for the negative part.
|
| 94 |
positive_embedding = average_embedding.unsqueeze(0)
|
| 95 |
negative_embedding = torch.zeros_like(positive_embedding)
|
| 96 |
-
|
| 97 |
-
# Combine them for the pipeline
|
| 98 |
final_embedding = torch.cat([negative_embedding, positive_embedding], dim=0)
|
| 99 |
|
| 100 |
output = self.pipe(
|
|
@@ -117,6 +113,11 @@ class GenerationService:
|
|
| 117 |
local_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")
|
| 118 |
image.save(local_path)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
storage_path = f"public/{os.path.basename(local_path)}"
|
| 121 |
logger.info(f"Uploading {local_path} to Supabase bucket '{config.SUPABASE_BUCKET_NAME}' at path '{storage_path}'")
|
| 122 |
|
|
@@ -150,10 +151,12 @@ if __name__ == '__main__':
|
|
| 150 |
result_url = service.generate_magic_image(
|
| 151 |
face_images=["test_face.jpg"],
|
| 152 |
gender="Female",
|
| 153 |
-
prompt="A beautiful portrait of a princess in a magical forest, fantasy art"
|
|
|
|
| 154 |
)
|
| 155 |
if result_url:
|
| 156 |
print(f"\n✅ Test successful! Image URL: {result_url}")
|
|
|
|
| 157 |
else:
|
| 158 |
print(f"\n❌ Test failed. Please check the new traceback logs for details.")
|
| 159 |
else:
|
|
|
|
| 1 |
# generate.py
|
| 2 |
+
# --- VERSION 9 (with Watermarking) ---
|
| 3 |
|
| 4 |
+
print("--- RUNNING GENERATE.PY VERSION 9 (with Watermarking) ---")
|
| 5 |
|
| 6 |
import torch
|
| 7 |
import cv2
|
|
|
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from storage3.utils import StorageException
|
| 19 |
import config
|
| 20 |
+
import utils # <-- IMPORT THE NEW UTILS FILE
|
| 21 |
from database import supabase
|
| 22 |
|
| 23 |
# --- Setup Logging ---
|
|
|
|
| 62 |
raise RuntimeError(f"Could not initialize GenerationService: {e}") from e
|
| 63 |
|
| 64 |
def generate_magic_image(self, face_images: list, gender: str, prompt: str, plan: str = 'free') -> str | None:
|
| 65 |
+
logger.info(f"Starting image generation process for a user on the '{plan}' plan.")
|
| 66 |
|
| 67 |
full_prompt = f"{prompt}, 4k, high-resolution, photorealistic, masterpiece, single person, solo portrait, centered composition"
|
| 68 |
negative_prompt = "multiple people, group photo, crowd, two faces, three faces, multiple faces, collage, ugly, deformed, blurry, low quality"
|
|
|
|
| 89 |
|
| 90 |
logger.info("Calling the generation pipeline...")
|
| 91 |
try:
|
|
|
|
|
|
|
|
|
|
| 92 |
positive_embedding = average_embedding.unsqueeze(0)
|
| 93 |
negative_embedding = torch.zeros_like(positive_embedding)
|
|
|
|
|
|
|
| 94 |
final_embedding = torch.cat([negative_embedding, positive_embedding], dim=0)
|
| 95 |
|
| 96 |
output = self.pipe(
|
|
|
|
| 113 |
local_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")
|
| 114 |
image.save(local_path)
|
| 115 |
|
| 116 |
+
# --- APPLY WATERMARK FOR FREE USERS ---
|
| 117 |
+
if plan == 'free':
|
| 118 |
+
utils.add_watermark(local_path, "@MagicFaceBot")
|
| 119 |
+
|
| 120 |
+
# --- Upload to Supabase Storage ---
|
| 121 |
storage_path = f"public/{os.path.basename(local_path)}"
|
| 122 |
logger.info(f"Uploading {local_path} to Supabase bucket '{config.SUPABASE_BUCKET_NAME}' at path '{storage_path}'")
|
| 123 |
|
|
|
|
| 151 |
result_url = service.generate_magic_image(
|
| 152 |
face_images=["test_face.jpg"],
|
| 153 |
gender="Female",
|
| 154 |
+
prompt="A beautiful portrait of a princess in a magical forest, fantasy art",
|
| 155 |
+
plan='free' # <-- Test the 'free' plan to see the watermark
|
| 156 |
)
|
| 157 |
if result_url:
|
| 158 |
print(f"\n✅ Test successful! Image URL: {result_url}")
|
| 159 |
+
print("Check the image at the URL to see if it has a watermark.")
|
| 160 |
else:
|
| 161 |
print(f"\n❌ Test failed. Please check the new traceback logs for details.")
|
| 162 |
else:
|